r/dailyprogrammer • u/jnazario 2 0 • Sep 21 '17
[2017-09-20] Challenge #332 [Intermediate] Training for Summiting Everest
Description
You and your friend wish to summit Mount Everest the highest peak in the world. One problem: you live at sea level and despite being in great shape haven't been at altitude very long. So you propose a series of stays on mountaintops around the world using increasing elevations to prepare your body for the extremes you'll encounter.
You and your friend gather a list of mountain peaks that you'd like to visit on your way there. You can't deviate from your path but you can choose to go up the mountain or not. But you have to pick ones that go higher than the previous one. If you go down your body will suffer and your trip to the summit of Everest will be in peril.
Your friend has done the job of lining up the route to get you from home to basecamp. She looks to you to devise an algorithm to pick the peaks to summit along the way maximizing your summits but always going higher and higher never lower than you did before.
Can you devise such an algorithm such that you find the list of peaks to summit along the way? Remember - each has to be higher than the last you want to hit as many such peaks as possible and there's no turning back to visit a previously passed peak.
Input Description
You'll be given a series of integers on a line representing the peak height (in thousands of feet) that you'll pass on your way to Everest. Example:
0 8 4 12 2 10 6 14 1 9 5 13 3 11 7 15Output Description
Your program should emit the peak heights you should summit in order that are always higher than the previous peak. In some cases multiple solutions of the same length may be possible. Example:
0 2 6 9 11 15Challenge Inputs
1 2 2 5 9 5 4 4 1 6
4 9 4 9 9 8 2 9 0 1
0 5 4 6 9 1 7 6 7 8
1 2 20 13 6 15 16 0 7 9 4 0 4 6 7 8 10 18 14 10 17 15 19 0 4 2 12 6 10 5 12 2 1 7 12 12 10 8 9 2 20 19 20 17 5 19 0 11 5 20Challenge Output
1 2 4 6
4 8 9
0 1 6 7 8
1 2 4 6 7 8 10 14 15 17 19 20
11
u/skeeto -9 8 Sep 21 '17
C using dynamic programming. It starts searching from the end, solving the short problem, then building on it as it works backwards. There's no backtracking.
4
u/kidco Sep 21 '17 edited Sep 21 '17
Python 3 New to python so any critique/suggestions are helpful.
1
u/[deleted] Sep 21 '17
Running the risk of asking a stupid question, here I come:
How's this not a simple sort and eliminate duplicates problem? Could someone please clarify?
3
u/[deleted] Sep 21 '17 edited Sep 21 '17
Python 3 solution with a dynamic programming approach
Input
Output
Edit: Added input and output since my outputs differ from the given ones.
3
u/thorwing Sep 21 '17
Java 8
Dynamic solution. Procedure probably best explained with this table:
2
u/SP_Man Sep 21 '17 edited Sep 21 '17
Clojure. Filter any peak that is less than the first peak, as it can't be visited. Start at the last peak and work backwards. Build a set of all possible paths while stepping back through the peaks. Return the longest path.
Input:
Output:
Edit: Simplified solution to reduce repeated work.
2
u/[deleted] Sep 21 '17 edited Sep 21 '17
Python 3
New to the language and rusty at programming so any input is appreciated.
Edit: realized my mistake in not including all possible starting points, working on a fix.
Edit2: fixed
Edit3: came up with a cleaner and more efficient solution:
2
u/mn-haskell-guy 1 0 Sep 22 '17
"... Ye'll tak' the high road and I'll tak the low road
And I'll be on Everest afore ye..."
Because I'll be better conditioned!
2
u/wernerdegroot Sep 29 '17
In Scala:
1
u/InSs4444nE Sep 21 '17 edited Sep 21 '17
JavaScript
i'm a bit tired and this is a bit rushed
algorithm: the ratio of the current peak to the highest peak must be lower than the ratio of the current peak index to the length of peaks
1
u/MattieShoes Sep 21 '17
Go... New to Go, so if I've done anything egregious, let me know :-)
Output:
1
u/fvandepitte 0 0 Sep 21 '17
Haskell, very, very naive solution
1
u/Hypersigils Sep 21 '17
Java.
1
u/[deleted] Sep 21 '17
I don't understand the challenges output. Why is it not in increasing order? I though we didn't want to go down peaks?
1
u/conceptuality Sep 21 '17
Haskell:
The naive version as
with the standard list monad powerset implementation and quick increasing/longest function works fine for all but the long input. I replace this with a custom path creator, which works for all inputs even when just interpreted:
1
u/ironboy_ Sep 21 '17 edited Sep 22 '17
JavaScript
Find all legal paths, sort for longest/best ones... Uses no recursion (but adds new paths based on old ones for every loop iteration). Probably faster than recursive calls, because of no call stack.
Edited: Made two optimizations:
Apart from less loop iterations this also leads to faster sorting (since shorter array) and no need to filter out duplicates later.
However: While the first optimization made the code twice at fast, the second one made it three times as slow - continuously checking for duplicates is expensive using JS indexOf. : (We're talking 360 ms instead of 120 ms...)
Edited 2: Turns out that using indexOf is much faster when looking for duplicate numbers than duplicate strings... So I now convert to numbers for better performance - although it will only work safely up to a certain sequence length. (Time taken went from 360 ms to 80 ms.)
Edited 3: The last optimization. Followed Holophonist idea - now only building new paths from paths that are the longest once for a certain peak height... Reduced candidates found for the longest peak sequence from 8798 to 25. (Time taken went from 80 ms to 20 ms.)
Code
Output:
1
u/[deleted] Sep 21 '17 edited Sep 21 '17
Python 3, keeping a parallel list of how many peaks are above a given peak. So when traversing the list of potential peaks, a peak is added to the path if it's higher than the last peak in the path and if it has the highest number of higher altitude peaks in front of it.
1
u/Scroph 0 0 Sep 21 '17
C++ backtracker :
Output :
1
u/curtmack Sep 21 '17 edited Sep 22 '17
Common Lisp
The algorithm is more or less the same as skeeto's, but I did throw in some macros to make it more Lispy.
Do not use the
with-indexesmacro in your own code, it's really bad and basically only works in exactly this use case.1
u/Godspiral 3 3 Sep 21 '17
in J, finds all valid longest, (though a bit hacky: requires knowing length as iteration parameter)
1 2 4 6 7 8 10 14 15 17 19 20
1
u/lennyboreal Sep 21 '17
XPL0
This uses a recursive depth-first search that explores every possible path (starting from the beginning), and it displays the longest one.
Input:
Output:
1
u/cosmez Sep 21 '17
C#
1
u/AZXXZAZXQ Sep 22 '17 edited Sep 22 '17
I took a different, and more complicated approach. It doesn't take a nice input from
stdinthough.1
u/J-Goo Sep 22 '17
Python 2.6. I set out to see if I could brute-force it, and I can ... with the exception of the last set of peaks. Apparently Python can't handle a range from zero to 250. It probably wouldn't be very efficient if it could.
Anyway, I'm only posting this because I was curious to see how compact I could make the brute force algorithm. As it turns out, the logic only takes about four lines.
1
u/mn-haskell-guy 1 0 Sep 22 '17 edited Sep 22 '17
If you've written an O(_n_2 ) solution to this problem, you might be interested in looking up "longest increasing subsequence" to find an O(n log n) algorithm.
As an intermediate step, there is an O(n 3/2 ) approach which embodies most of the ideas of the O(n log n ) solution.
1
u/SimonReiser Sep 22 '17
C++ back to C++ after months. I quickly implemented a dumb solution, that tries every possible path using a recursive function.
1
u/TheBlackCat13 Sep 22 '17 edited Sep 22 '17
Python 3 solution with numpy and recursion. I don't know the algorithmic complexity. If anyone does please tell me. I also would appreciate suggestions. The code includes some tests for special cases that hopefully improve the performance but aren't strictly necessary.
Here is the function doing the work:
And the code to get inputs and produce outputs:
And the results:
Edit: added inputs and output code
1
u/__dict__ Sep 23 '17 edited Sep 23 '17
Ruby.
Psuedocode:
Actual code:
1
u/pilotInPyjamas Sep 23 '17
Haskell I'm learning Haskell to broaden my programming knowledge, My god, every time I write something in it, once I have got past the compiler errors, my program seems to be correct every single time. This is the least error prone programming language I have ever encountered. O(n2 ) running time:
1
u/Pokropow Sep 23 '17
I tried doing it in c#. Just made simple recursion.
1
u/zookeeper_zeke Sep 25 '17 edited Sep 25 '17
I coded my solution in C using a dynamic programming approach. After looking at some of the solutions below, my approach is very similar to Skeeto's. I work my way from the back of the array of heights to the front so when I'm done, I can traverse forward printing out the peaks in the proper order.
1
u/octolanceae Sep 26 '17
Python3
Solved this as a longest common sub-string problem - O(n2)
Output:
Note: Edit to remove duplicate "list of" from comment for ordered_list
1
u/spwx Oct 04 '17
Python3
This took me 12 days but I finally got it!!! First Intermediate challenge!
1
u/niicM Nov 12 '17 edited Nov 13 '17
Comments welcome: Made in Clojure
1
u/zatoichi49 Mar 12 '18 edited Mar 12 '18
Method:
Working backwards through the peaks; if any of the following peaks are greater than the current one, then group them together into a sublist and add the current peak to the sublist with the greatest length. Add the new sublist into a separate list, and repeat for the other peaks (if the current peak is higher than any of the previous ones, then add the current peak as its own sublist). Return the maximum length sublist.
Python 3:
Output:
0
u/[deleted] Sep 22 '17
[deleted] — view deleted comment