5 kyu
Simple Fun #314: Lucky Candies
26 of 77myjinxin2015
Loading description...
Algorithms
Dynamic Programming
Performance
View
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Spoiler
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}
-
-
Your rendered github-flavored markdown will appear here.
-
Label this discussion...
-
No Label
Keep the comment unlabeled if none of the below applies.
-
Issue
Use the issue label when reporting problems with the kata.
Be sure to explain the problem clearly and include the steps to reproduce. -
Suggestion
Use the suggestion label if you have feedback on how this kata can be improved.
-
Question
Use the question label if you have questions and/or need help solving the kata.
Don't forget to mention the language you're using, and mark as having spoiler if you include your solution.
-
No Label
- Cancel
Commenting is not allowed on this discussion
You cannot view this solution
There is no solution to show
Please sign in or sign up to leave a comment.
What are we looking for here?
the first
in an example we have an array of [50,40,30,20,10] that equal 150 in total, and k = 9 so I got in my result 144, 144 is a multiple of 9 but it's not working and they said the correct output should be 90 can someone explain it for me please, maybe I didn't understand the kata
Only sums of elements of the input array are allowed. You cannot write
144
as a sum of some elements of[50, 40, 30, 20, 10]
because any sum of such elements is a multiple of10
.90
is a correct answer because90 = 50 + 40
and it is not possible to get a larger sum divisible by9
using numbers from the input array.thanks, it's clear now
It's hard for a 5 kyu kata
Heh, glad you agree I'm still trying to figure out how to do it without millions loops
Hello everybody! Apparently I don't understand the task. I need to find the maximum multiple of "k" in the sum of an array? What is required in kata? it is correct?:
def lucky_candies(prizes, k): return sum(prizes) // k * k
I believe the task is, find the largest possible sum from the given list, where the sum is divisible by k. A modified version of the example could be [1,2,3,4,5,6] k=5. So the total sum is 21, and that's not divisible by 5, but 20 is, and 20 is possible by 2+3+4+5+6 from the list
This comment has been hidden.
Your solution is too slow for test cases with
len(prizes) > 25
because you iterate over all possible combinations of prizes. You need to find a different approach to solve this problem. This kata has a tagDynamic Programming
. I recommend to read about this technique. Even better, try to solve other (easier) kata with this tag and learn from existing solutions. Here are some similar kata:I am closing this issue because it is not a valid issue. Issues should be used when something is wrong with the kata itself. If you have a problem with your solution just post a comment without any label or use the
Question
label. Also, use markdown to format your code:My solution sometimes has 1 failed test, but after repetead attempts, it can passes all the tests. Maybe the tests could be improved?
Maybe, but unless you provide your code no one will be able to check check what's going on :)
People who've solved can see it with the 'View Solution' button.
I can't really see why those test cases fail. Often
k
is present inprizes
when you fail, but not always. Maybe it would be sufficient to simply increase the smaller test cases from 40 to 200 or so?I updated Python tests. Now your solution does not pass about 10% of tests. I am going to do some additional experiments. Then I will close the issue if you don't find any flaws in the current tests.
The new tests probably warrant a 'performance' tag, don't they?
Edit: or maybe not, I may have misunderstood what was changed.
I only increased the number of small test cases and added some restrictions on randomly generated inputs. But I will add a performance tag to make the performance requirements explicit (there is already the 'dynamic programming' tag which almost always implies performance requirements but it may be not immediately obvious).
Increased the prize values to
5000000
.