Show me an efficient algorithm!
I’m writing a program for an e-commerce campaign site. The deadline is the end of the holidays. Although I’m half way through writing the program, the code is now looking quite long, and I get the feeling that it's not very efficient and I cannot make the deadline. To smart hacker out there! Please help me!
You will choose two products to buy from a EC site. Find out the maximum total price of combination of two products. The total price of the two products need to be equal to or less than the specified target price.
→Problem details
The half-written program by the new lady programmer Ms. Noda:
Item_a_b = 4500 // total price of a and b
Item_a_c = 500 // total price of a and c
Item_a_d = 2300 // total price of a and d
Item_b_a = 1240 // total price of b and a
Item_b_c = 5020 // total price of b and c
(Snip)
if Item_a_b == campaign_price
print “Price of A and B is the target price!”
if Item_a_b == campaign_price -10
print “Price of A and B is closest to target price by 10 USD !”
if Item_a_c == campaign_price
(Snip)
The new lady programmer, Ms. Noda, has been assigned to your team. Improve her code she's written already. Write more efficient code to show your skills as a programmer and for a chance to be promoted.
Based on the details given in the "Details of your task" section at the bottom of this page, implement an efficient program using your favorite language ( Java, PHP, Ruby, Python2, Python3, Perl, C, C++, C#, JavaScript, Objective-C, Scala, Go, Swift, Kotlin ). Your submitted code will be run through multiple tests cases, and the number of correct answers and your code’s execution time will be recorded to assign you a score.
Write the code | ||
|
Verify its execution | |
|
Submit the code | |
|
You will receive your score! | |
|
Best answers will be published (requiring registration for paiza) |
Let's try! Let's compete for the best performance!
Language |
Best time |
Worst time |
Submissions |
---|---|---|---|
Java |
0.06 sec |
5.95 sec |
2989 |
PHP |
0.01 sec |
9.80 sec |
3258 |
Ruby |
0.01 sec |
9.47 sec |
2213 |
Python2 |
0.08 sec |
9.70 sec |
2519 |
Perl |
0.01 sec |
9.42 sec |
1937 |
C |
0.01 sec |
2.99 sec |
3466 |
C++ |
0.01 sec |
2.78 sec |
3367 |
C# |
0.01 sec |
6.00 sec |
2470 |
(2014/01/09 update)
* This is a execution time for test case 1(the same test case for all languages). The execution time may varies a bit on each execution.
Let's try! Let's compete for the best performance!
Language |
Best time |
Worst time |
Submissions |
---|---|---|---|
Java |
0.06 sec |
5.94 sec |
2989 |
PHP |
0.01 sec |
9.05 sec |
3258 |
Ruby |
0.01 sec |
8.14 sec |
2213 |
Python2 |
0.08 sec |
9.99 sec |
2519 |
Perl |
0.01 sec |
7.98 sec |
1937 |
C |
0.01 sec |
2.99 sec |
3466 |
C++ |
0.01 sec |
2.96 sec |
3367 |
C# |
0.01 sec |
6.00 sec |
2470 |
(2014/01/09 update)
* This is a execution time for test case 2(the same test case for all languages). The execution time may varies a bit on each execution.
Let's try! Let's compete for the best performance!
Language |
Best time |
Worst time |
Exam num |
---|---|---|---|
Java |
0.06 sec |
5.94 sec |
2989 |
PHP |
0.01 sec |
9.93 sec |
3258 |
Ruby |
0.01 sec |
9.48 sec |
2213 |
Python2 |
0.08 sec |
9.93 sec |
2519 |
Perl |
0.01 sec |
9.96 sec |
1937 |
C |
0.01 sec |
2.99 sec |
3466 |
C++ |
0.01 sec |
2.96 sec |
3367 |
C# |
0.01 sec |
6.00 sec |
2470 |
(2014/01/09 update)
※ This is a execution time for test case 2(
For C and C++ languages, N is 2.5 times and D is 4 times larger than those for languages
)
The execution time may varies a bit on each execution.
You are a programmer for an e-commerce site. This site sells many products including cheap items for as low as 10 USD to expensive items for as high as 100,000 USD. Your company, the operator of this e-commerce site, has decided to run a campaign titled "Combine Them and Get Them for Free” to attract new customers. In this campaign, if you purchase a combination of products with a total amount that is closest to a specified target amount, then you can get them for free. The details are as follows:
*The "//" symbols above denote comments in the code.
*The "//" symbols above denote comments in the code.
Please enter and submit the code that solves the problem above using the text box below.
You can use the following languages: Java, PHP, Ruby, Python2, Python3, Perl, C, C++, C#, JavaScript, Objective-C, Scala, Go, Swift, Kotlin.
To find out about methods for reading values from standard input, please refer to the sample codes shown in the pages below:
Select language
Once you submit your code, we will run multiple test cases and we will assign you a score based on the code’s execution time and any bugs encountered during the tests.