Given an array of integers, return the smallest set of indices of numbers such that they add up to a target number. You may not use the same element twice.
I was thinking a naive way to solve this would be to iterate through the remaining elements from the back. In the case of 1 2 3 4 5, would 5. So there's a path where you try 4+5, 3+5, 2+5, 1+5. And if it exceeds the target you ignore it. if it is lower, and there are still elements before that you iterate through those. I'm not really sure how I can express this with maps and perhaps making use of multi arity functions for recursionI just realised that I still need to make sure the solution is the smallest set possible .... Anyone has any thoughts or is Clojure not too great for such tasks? (edited)
It sounds like a combinatorics problem to me, at least for a brute force approach, so I'd reach for clojure.math.combinatorics and do something like this:
(it would be more efficient to calculate the zipmap just once in a let at the top, but I was just playing in a REPL to get to this point). (edited)
I was thinking I needed to implement something like this https://www.geeksforgeeks.org/print-all-possible-combinations-of-r-elements-in-a-given-array-of-size-n/ but in clojure
Print all possible combinations of r elements in a given array of size n - GeeksforGeeks
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
Was gonna attempt to foolhardily reference the python code and make a conversion (if i had time)
The combinatorics library is great for stuff like this. Took me a few minutes to get that exact code above, but it really was just a dozen lines in the REPL looking at the shape of things as I worked from that top line down.
There's actually a function in there called subset that already does the mapcat.
Given an array of integers, return the smallest set of indices of numbers such that they add up to a target number. You may not use the same element twice.
Examples: [1,2,6,3,17,82,23,234] -> 26 Solution [3,6] [1,2,6,3,17,82,23,234] -> 40 Solution [4,6] [1,2,6,3,17,82,23,234] -> 23 Solution [6]
So thus far only have the backbone of what I would want to do. So far have done sorting and filtering out the numbers that would be too high
I was thinking a naive way to solve this would be to iterate through the remaining elements from the back. In the case of 1 2 3 4 5, would 5. So there's a path where you try 4+5, 3+5, 2+5, 1+5. And if it exceeds the target you ignore it. if it is lower, and there are still elements before that you iterate through those. I'm not really sure how I can express this with maps and perhaps making use of multi arity functions for recursionI just realised that I still need to make sure the solution is the smallest set possible .... Anyone has any thoughts or is Clojure not too great for such tasks? (edited)
It sounds like a combinatorics problem to me, at least for a brute force approach, so I'd reach for clojure.math.combinatorics and do something like this:
(it would be more efficient to calculate the zipmap just once in a let at the top, but I was just playing in a REPL to get to this point). (edited)
I was thinking I needed to implement something like this https://www.geeksforgeeks.org/print-all-possible-combinations-of-r-elements-in-a-given-array-of-size-n/ but in clojure Print all possible combinations of r elements in a given array of size n - GeeksforGeeks A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
Was gonna attempt to foolhardily reference the python code and make a conversion (if i had time)
The combinatorics library is great for stuff like this. Took me a few minutes to get that exact code above, but it really was just a dozen lines in the REPL looking at the shape of things as I worked from that top line down.
There's actually a function in there called subset that already does the mapcat.
This http://creditsuisse.recsolu.com/external/events/O4d5CSpiL25wEbLEAwcPkQ
Another approach:
Credit mostly goes to this MATHLAB solution I adapted from: https://stackoverflow.com/a/35689122/172272