From f77d182cdf3334041c4c1d748adfebcff3092990 Mon Sep 17 00:00:00 2001 From: yennanliu Date: Wed, 6 Mar 2024 19:10:52 +0800 Subject: [PATCH] update 128 java, progress --- data/progress.txt | 2 +- data/to_review.txt | 18 ++++---- .../Sort/LongestConsecutiveSequence.java | 46 ++++++++++++++++++- 3 files changed, 55 insertions(+), 11 deletions(-) diff --git a/data/progress.txt b/data/progress.txt index bf0616e9..8d9fe9e1 100644 --- a/data/progress.txt +++ b/data/progress.txt @@ -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 diff --git a/data/to_review.txt b/data/to_review.txt index fb4c58aa..8063a1ec 100644 --- a/data/to_review.txt +++ b/data/to_review.txt @@ -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)'] diff --git a/leetcode_java/src/main/java/LeetCodeJava/Sort/LongestConsecutiveSequence.java b/leetcode_java/src/main/java/LeetCodeJava/Sort/LongestConsecutiveSequence.java index 745b3880..976aedb5 100644 --- a/leetcode_java/src/main/java/LeetCodeJava/Sort/LongestConsecutiveSequence.java +++ b/leetcode_java/src/main/java/LeetCodeJava/Sort/LongestConsecutiveSequence.java @@ -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 set = new HashSet<>(); + List 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;