Skip to content

Commit

Permalink
update 334 java
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Oct 12, 2024
1 parent d6306db commit 796590c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,26 @@
public class IncreasingTripletSubsequence {

// V0
// TODO : implement
// public boolean increasingTriplet(int[] nums) {
//
// }
public boolean increasingTriplet(int[] nums) {

// NOTE !!! we init as max integer
Integer biggest_1 = Integer.MAX_VALUE;
Integer biggest_2 = Integer.MAX_VALUE;

for (int x : nums){
// NOTE !!! condition below
if (biggest_1 >= x){
biggest_1 = x;
// NOTE !!! condition below
}else if (biggest_2 >= x){
biggest_2 = x;
}else{
return true;
}
}

return false;
}

// V1
// IDEA : LOOP (fixed by gpt)
Expand Down
56 changes: 39 additions & 17 deletions leetcode_java/src/main/java/dev/workspace5.java
Original file line number Diff line number Diff line change
Expand Up @@ -1080,7 +1080,7 @@ public int pickIndex() {

// LC 334
// https://leetcode.com/problems/increasing-triplet-subsequence/
// 0745 - 0800
// 0720 - 0730
/**
*
* Given an integer array nums,
Expand All @@ -1091,29 +1091,51 @@ public int pickIndex() {
*/
public boolean increasingTriplet(int[] nums) {

if (nums.length < 3){
return false;
}
Integer biggest_1 = Integer.MAX_VALUE;
Integer biggest_2 = Integer.MAX_VALUE;
//Integer biggest_3 = Integer.MAX_VALUE;

// 2 pointers
for (int i = 0; i < nums.length-2; i++){
int cnt = 1;
int prev = nums[i];
for (int j = i+1; j < nums.length; j++){
if (cnt >= 3){
return true;
}
if (nums[j] > prev){
cnt += 1;
prev = nums[j];
}
j += 1;
for (int x : nums){
if (biggest_1 >= x){
biggest_1 = x;
}
// else if (biggest_2 >= x){
else if (x > biggest_1){
biggest_2 = x;
}else{
//biggest_3 = x;
return true;
}
}

return false;
}

// public boolean increasingTriplet(int[] nums) {
//
// if (nums.length < 3){
// return false;
// }
//
// // 2 pointers
// for (int i = 0; i < nums.length-2; i++){
// int cnt = 1;
// int prev = nums[i];
// for (int j = i+1; j < nums.length; j++){
// if (cnt >= 3){
// return true;
// }
// if (nums[j] > prev){
// cnt += 1;
// prev = nums[j];
// }
// j += 1;
// }
// }
//
// return false;
// }

// LC 353
// https://leetcode.ca/2016-11-17-353-Design-Snake-Game/
// 6.24 pm - 6.40 pm
Expand Down

0 comments on commit 796590c

Please sign in to comment.