Created
January 13, 2018 07:21
-
-
Save gauravbansal74/d3a6a1cddb4b3550df27cceeb80747cb to your computer and use it in GitHub Desktop.
max_profit
This file contains 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
import java.util.*; | |
// you can write to stdout for debugging purposes, e.g. | |
// System.out.println("this is a debug message"); | |
class Solution { | |
public int solution(int[] A) { | |
// write your code in Java SE 8 | |
int length = A.length; | |
int min = 0; | |
int diff = 0; | |
int temp = 0; | |
int max_ending = 0; | |
int max_slice = 0; | |
for(int i =1; i < length; i++){ | |
diff = A[i] - A[i-1]; | |
temp = max_ending + diff; | |
max_ending = temp < min ? min:temp; | |
max_slice = max_slice < max_ending ? max_ending:max_slice; | |
} | |
return max_slice; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment