Created
April 2, 2014 02:10
-
-
Save rayjcwu/9926832 to your computer and use it in GitHub Desktop.
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
public class IncreasingSubarray { | |
public static void main(String[] argv) { | |
IncreasingSubarray is = new IncreasingSubarray(); | |
System.out.println(is.solution(new int[]{2, 2, 2, 2, 1, 2, -1, 2, 1, 3})); | |
System.out.println(is.solution(new int[]{30, 20, 10})); | |
System.out.println(is.solution(new int[]{30})); | |
System.out.println(is.solution(new int[]{1, 2, 3, 4, 1, 2, 3, 4, 5})); | |
} | |
public int solution(int[] A) { | |
final int N = A.length; | |
int[] dp = new int[N]; // maxlen increasing subarray ending at i | |
dp[0] = 1; | |
int maxLen = 1; // at least 1 element | |
int maxLenIdx = 0; | |
for (int i = 1; i < N; ++i) { | |
dp[i] = (A[i] > A[i - 1]) ? dp[i - 1] + 1: 1; | |
if (dp[i] > maxLen) { | |
maxLen = dp[i]; | |
maxLenIdx = i - maxLen + 1; | |
} | |
} | |
return maxLenIdx; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment