Last active
December 15, 2015 01:48
-
-
Save calebr/5182204 to your computer and use it in GitHub Desktop.
SRM 573 - D2L1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class SkiResortsEasy { | |
| public int minCost(int[] altitude) { | |
| int cost = 0; | |
| for (int i = 1; i < altitude.length; i++) { | |
| int diff = altitude[i] - altitude[i-1]; | |
| if (diff > 0) { | |
| cost += diff; | |
| altitude[i] = altitude[i-1]; | |
| } | |
| // Another way: prettier but with unnecessary assignments. | |
| // cost += max(0, altitude[i] - altitude[i-1]); | |
| // altitude[i] = min(altitude[i], altitude[i-1]); | |
| } | |
| return cost; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment