Skip to content

Commit

Permalink
update 128 java, progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Mar 6, 2024
1 parent 32fa688 commit f77d182
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 11 deletions.
2 changes: 1 addition & 1 deletion data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
20240306: 121,124(again),125
20240306: 121,124(again),125,128
20240305: 100,102,104,105(again)
20240304: 73,76,79(again),91,25
20240303: 55(again),56,62,70
Expand Down
18 changes: 9 additions & 9 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
2024-04-30 -> ['121,124(again),125']
2024-04-30 -> ['121,124(again),125,128']
2024-04-29 -> ['100,102,104,105(again)']
2024-04-28 -> ['73,76,79(again),91,25']
2024-04-27 -> ['55(again),56,62,70']
2024-04-24 -> ['39,48(again),49,53,54']
2024-04-23 -> ['20,21,23,33(again)']
2024-04-22 -> ['1,3,5,4,19']
2024-04-09 -> ['121,124(again),125']
2024-04-09 -> ['121,124(again),125,128']
2024-04-08 -> ['100,102,104,105(again)']
2024-04-07 -> ['73,76,79(again),91,25']
2024-04-06 -> ['55(again),56,62,70']
2024-04-03 -> ['39,48(again),49,53,54']
2024-04-02 -> ['20,21,23,33(again)']
2024-04-01 -> ['1,3,5,4,19']
2024-03-27 -> ['121,124(again),125']
2024-03-27 -> ['121,124(again),125,128']
2024-03-26 -> ['100,102,104,105(again)']
2024-03-25 -> ['73,76,79(again),91,25']
2024-03-24 -> ['55(again),56,62,70']
2024-03-21 -> ['39,48(again),49,53,54']
2024-03-20 -> ['20,21,23,33(again)']
2024-03-19 -> ['121,124(again),125', '1,3,5,4,19']
2024-03-19 -> ['121,124(again),125,128', '1,3,5,4,19']
2024-03-18 -> ['100,102,104,105(again)']
2024-03-17 -> ['73,76,79(again),91,25']
2024-03-16 -> ['55(again),56,62,70']
2024-03-14 -> ['121,124(again),125']
2024-03-14 -> ['121,124(again),125,128']
2024-03-13 -> ['100,102,104,105(again)', '39,48(again),49,53,54']
2024-03-12 -> ['73,76,79(again),91,25', '20,21,23,33(again)']
2024-03-11 -> ['121,124(again),125', '55(again),56,62,70', '1,3,5,4,19']
2024-03-11 -> ['121,124(again),125,128', '55(again),56,62,70', '1,3,5,4,19']
2024-03-10 -> ['100,102,104,105(again)']
2024-03-09 -> ['121,124(again),125', '73,76,79(again),91,25']
2024-03-08 -> ['121,124(again),125', '100,102,104,105(again)', '55(again),56,62,70', '39,48(again),49,53,54']
2024-03-07 -> ['121,124(again),125', '100,102,104,105(again)', '73,76,79(again),91,25', '20,21,23,33(again)']
2024-03-09 -> ['121,124(again),125,128', '73,76,79(again),91,25']
2024-03-08 -> ['121,124(again),125,128', '100,102,104,105(again)', '55(again),56,62,70', '39,48(again),49,53,54']
2024-03-07 -> ['121,124(again),125,128', '100,102,104,105(again)', '73,76,79(again),91,25', '20,21,23,33(again)']
2024-03-06 -> ['100,102,104,105(again)', '73,76,79(again),91,25', '55(again),56,62,70', '1,3,5,4,19']
2024-03-05 -> ['73,76,79(again),91,25', '55(again),56,62,70', '39,48(again),49,53,54']
2024-03-04 -> ['55(again),56,62,70', '20,21,23,33(again)']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,57 @@
public class LongestConsecutiveSequence {

// V0
// IDEA : SET + SORT + for loop
// step 1) set : only collect unique element
// step 2) sort, so element is in ascending ordering
// step 3) loop over final list, and maintain a max len of sequence
public int longestConsecutive(int[] nums) {

if (nums.length == 0 || nums.equals(null)){
return 0;
}

// get set
Set<Integer> set = new HashSet<>();
List<Integer> uniqueNums = new ArrayList<>();

for (int i : nums){
if (!set.contains(i)){
set.add(i);
//cnt += 1;
uniqueNums.add(i);
}
}
// sort
uniqueNums.sort((a, b) -> Integer.compare(a, b));
int ans = 0;
int tmp = 0;

// NOTE !!! start from idx = 1
for (int i = 1; i < uniqueNums.size(); i++){
// compare with cur, and last element
// check cur = last + 1 (e.g. consecutive)
if (uniqueNums.get(i) == uniqueNums.get(i-1) + 1){
tmp += 1;
ans = Math.max(ans, tmp);
}
else{
tmp = 0;
}
}

// NOTE : ans + 1, since we start from idx = 1
return ans+1;
}

// V0'
// IDEA : SET + SORT + SHIFT ARRAY
// Step 1) we get unique array as new one, since we get possible longest array from it
// Step 2) we sort unique array from above
// Step 3) we shift to with 1 index from above unique array as shift array
// Step 4) we loop over unique array, shift array and calculate max length when diff = 1
// Step 5) return amx length from above
public int longestConsecutive(int[] nums) {
public int longestConsecutive_1(int[] nums) {

if (nums.length == 0 || nums.equals(null)){
return 0;
Expand Down

0 comments on commit f77d182

Please sign in to comment.