1
0
Fork 0
mirror of https://gitlab.com/mfocko/CodeWars.git synced 2024-11-22 00:23:47 +01:00

5kyu: add best travel

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2021-12-28 23:19:02 +01:00
parent e394821b7b
commit b4062b40c8
No known key found for this signature in database
GPG key ID: 332171FADF1DB90B
2 changed files with 28 additions and 0 deletions

View file

@ -0,0 +1,24 @@
def choose_best_sum(t, k, ls)
def choose_best_sum_rec(max_total_distance, n, distances)
if n == 0 && max_total_distance >= 0 then
return 0
elsif max_total_distance < 0 || distances.size < 1 then
return nil
end
found = Array.new(2)
travelled = distances.pop
including_last = choose_best_sum_rec(max_total_distance - travelled, n - 1, distances)
if including_last != nil then
found[0] = including_last + travelled
end
found[1] = choose_best_sum_rec(max_total_distance, n, distances)
distances.push(travelled)
return found.select { |x| x != nil }.max
end
choose_best_sum_rec(t, k, ls.sort)
end

View file

@ -113,6 +113,10 @@
- [Esolang Interpreters #2 - Custom Smallfuck Interpreter](https://www.codewars.com/kata/58678d29dbca9a68d80000d7) - [solution](5kyu/esolang_interpreters_ii)
### Ruby
- [Best travel](https://www.codewars.com/kata/55e7280b40e1c4a06d0000aa) - [solution](5kyu/best_travel)
## 6 kyu
### Rust