LeetCode OJ 334 Increasing Triplet Subsequence

Question

[LeetCode 334] Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array.

Submission

Using dynamic programming, find if there exist increasing subsequence with length greater than or equal to 3

Java Submission
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class Solution {
public boolean increasingTriplet(int[] nums) {
if (nums.length < 3) return false;

int[] length = new int[nums.length];

for (int i = 0; i < length.length; i++) {
length[i] = 1;
}

for (int i = 1; i < nums.length; i++) {
for (int j = 0; j < i; j++) {
if (nums[j] < nums[i] && length[j] + 1 > length[i]) {
length[i] = length[j] + 1;
}
if (length[i] >= 3) {
return true;
}
}
}

return false;
}
}