From 2ada1ec72859d4b24bf149edfecc8cab07f96148 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Thu, 19 Dec 2024 16:12:40 +0200 Subject: [PATCH] Improved tasks 869, 870, 976, 1329, 1408 --- .../s0869_reordered_power_of_2/Solution.java | 35 +++----- .../s0870_advantage_shuffle/Solution.java | 85 ++++++++++--------- .../Solution.java | 2 +- .../Solution.java | 56 ++++++------ .../Solution.java | 29 ++++--- .../SolutionTest.java | 2 +- 6 files changed, 102 insertions(+), 107 deletions(-) diff --git a/src/main/java/g0801_0900/s0869_reordered_power_of_2/Solution.java b/src/main/java/g0801_0900/s0869_reordered_power_of_2/Solution.java index 75b21a949..72de67a77 100644 --- a/src/main/java/g0801_0900/s0869_reordered_power_of_2/Solution.java +++ b/src/main/java/g0801_0900/s0869_reordered_power_of_2/Solution.java @@ -1,38 +1,29 @@ package g0801_0900.s0869_reordered_power_of_2; // #Medium #Math #Sorting #Counting #Enumeration -// #2022_03_28_Time_9_ms_(25.97%)_Space_42.8_MB_(11.69%) +// #2024_12_19_Time_1_ms_(89.02%)_Space_40.9_MB_(44.51%) -import java.util.HashMap; -import java.util.Map; -import java.util.Objects; +import java.util.Arrays; public class Solution { public boolean reorderedPowerOf2(int n) { - int i = 0; - while (Math.pow(2, i) < (long) n * 10) { - if (isValid(String.valueOf((int) (Math.pow(2, i++))), String.valueOf(n))) { + int[] originalDigits = countDigits(n); + int num = 1; + for (int i = 0; i < 31; i++) { + if (Arrays.equals(originalDigits, countDigits(num))) { return true; } + num <<= 1; } return false; } - private boolean isValid(String a, String b) { - Map m = new HashMap<>(); - Map mTwo = new HashMap<>(); - for (char c : a.toCharArray()) { - m.put(c, m.containsKey(c) ? m.get(c) + 1 : 1); + private int[] countDigits(int num) { + int[] digitCount = new int[10]; + while (num > 0) { + digitCount[num % 10]++; + num /= 10; } - for (char c : b.toCharArray()) { - mTwo.put(c, mTwo.containsKey(c) ? mTwo.get(c) + 1 : 1); - } - for (Map.Entry entry : mTwo.entrySet()) { - if (!m.containsKey(entry.getKey()) - || !Objects.equals(entry.getValue(), m.get(entry.getKey()))) { - return false; - } - } - return a.charAt(0) != '0' && m.size() == mTwo.size(); + return digitCount; } } diff --git a/src/main/java/g0801_0900/s0870_advantage_shuffle/Solution.java b/src/main/java/g0801_0900/s0870_advantage_shuffle/Solution.java index a49dca644..b22e29edd 100644 --- a/src/main/java/g0801_0900/s0870_advantage_shuffle/Solution.java +++ b/src/main/java/g0801_0900/s0870_advantage_shuffle/Solution.java @@ -1,59 +1,60 @@ package g0801_0900.s0870_advantage_shuffle; -// #Medium #Array #Sorting #Greedy #2022_03_28_Time_188_ms_(28.01%)_Space_116.9_MB_(5.12%) +// #Medium #Array #Sorting #Greedy #2024_12_19_Time_42_ms_(99.16%)_Space_56.1_MB_(94.94%) -import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; -import java.util.Deque; -import java.util.HashMap; import java.util.List; -import java.util.PriorityQueue; -@SuppressWarnings("java:S5413") public class Solution { public int[] advantageCount(int[] nums1, int[] nums2) { - PriorityQueue pque = new PriorityQueue<>(); - for (int e : nums1) { - pque.add(e); - } - int l = nums1.length; - HashMap> map = new HashMap<>(); - int[] n = new int[l]; - System.arraycopy(nums2, 0, n, 0, l); - Arrays.sort(n); - Deque sta = new ArrayDeque<>(); - for (int i = 0; i < l && !pque.isEmpty(); i++) { - List p = map.getOrDefault(n[i], new ArrayList<>()); - int x = pque.poll(); - if (x > n[i]) { - p.add(x); - map.put(n[i], p); + Arrays.sort(nums1); + int[] result = new int[nums1.length]; + int low = 0; + boolean[] chosen = new boolean[nums1.length]; + for (int i = 0; i < nums2.length; i++) { + int pos = binSearch(nums1, nums2[i], low, chosen); + if (pos != -1 && pos < nums1.length) { + result[i] = nums1[pos]; + chosen[pos] = true; } else { - while (x <= n[i] && !pque.isEmpty()) { - sta.push(x); - x = pque.poll(); - } - if (x > n[i]) { - p.add(x); - map.put(n[i], p); - } else { - sta.push(x); - } + result[i] = -1; } } - for (int i = 0; i < nums2.length; i++) { - List p = map.getOrDefault(nums2[i], new ArrayList<>()); - int t; - if (!p.isEmpty()) { - t = p.get(0); - p.remove(0); - map.put(nums2[i], p); + List pos = new ArrayList<>(); + int i = 0; + for (boolean ch : chosen) { + if (!ch) { + pos.add(i); + } + i++; + } + int index = 0; + for (i = 0; i < result.length; i++) { + if (result[i] == -1) { + result[i] = nums1[pos.get(index)]; + index++; + } + } + return result; + } + + private int binSearch(int[] nums, int target, int low, boolean[] chosen) { + int high = nums.length - 1; + while (high >= low) { + int mid = high - (high - low) / 2; + if (nums[mid] > target && (mid == 0 || nums[mid - 1] <= target)) { + while (mid < nums.length && chosen[mid]) { + mid++; + } + return mid; + } + if (nums[mid] > target) { + high = mid - 1; } else { - t = sta.pop(); + low = mid + 1; } - nums1[i] = t; } - return nums1; + return -1; } } diff --git a/src/main/java/g0901_1000/s0976_largest_perimeter_triangle/Solution.java b/src/main/java/g0901_1000/s0976_largest_perimeter_triangle/Solution.java index 1a0b2692f..263f7523f 100644 --- a/src/main/java/g0901_1000/s0976_largest_perimeter_triangle/Solution.java +++ b/src/main/java/g0901_1000/s0976_largest_perimeter_triangle/Solution.java @@ -1,7 +1,7 @@ package g0901_1000.s0976_largest_perimeter_triangle; // #Easy #Array #Math #Sorting #Greedy #Programming_Skills_I_Day_3_Conditional_Statements -// #2022_03_31_Time_12_ms_(26.01%)_Space_53.8_MB_(69.91%) +// #2024_12_19_Time_7_ms_(99.33%)_Space_45.5_MB_(8.45%) import java.util.Arrays; diff --git a/src/main/java/g1301_1400/s1329_sort_the_matrix_diagonally/Solution.java b/src/main/java/g1301_1400/s1329_sort_the_matrix_diagonally/Solution.java index 1a2cd7205..bb0045f13 100644 --- a/src/main/java/g1301_1400/s1329_sort_the_matrix_diagonally/Solution.java +++ b/src/main/java/g1301_1400/s1329_sort_the_matrix_diagonally/Solution.java @@ -1,41 +1,35 @@ package g1301_1400.s1329_sort_the_matrix_diagonally; -// #Medium #Array #Sorting #Matrix #2022_03_19_Time_15_ms_(26.03%)_Space_47.7_MB_(56.76%) - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; +// #Medium #Array #Sorting #Matrix #2024_12_19_Time_0_ms_(100.00%)_Space_44.7_MB_(81.35%) public class Solution { - public int[][] diagonalSort(int[][] mat) { - int m = mat.length; - int n = mat[0].length; - int[][] sorted = new int[m][n]; - for (int i = m - 1; i >= 0; i--) { - int iCopy = i; - List list = new ArrayList<>(); - for (int j = 0; j < n && iCopy < m; j++, iCopy++) { - list.add(mat[iCopy][j]); - } - Collections.sort(list); - iCopy = i; - for (int j = 0; j < n && iCopy < m; j++, iCopy++) { - sorted[iCopy][j] = list.get(j); + private int[] count = new int[101]; + private int m; + private int n; + + public void search(int[][] mat, int i, int j) { + for (int ti = i, tj = j; ti < m && tj < n; ti++, tj++) { + count[mat[ti][tj]]++; + } + int c = 0; + for (int ti = i, tj = j; ti < m && tj < n; ti++, tj++) { + while (count[c] == 0) { + c++; } + mat[ti][tj] = c; + count[c]--; } + } - for (int j = n - 1; j > 0; j--) { - int jCopy = j; - List list = new ArrayList<>(); - for (int i = 0; i < m && jCopy < n; i++, jCopy++) { - list.add(mat[i][jCopy]); - } - Collections.sort(list); - jCopy = j; - for (int i = 0; i < m && jCopy < n; i++, jCopy++) { - sorted[i][jCopy] = list.get(i); - } + public int[][] diagonalSort(int[][] mat) { + m = mat.length; + n = mat[0].length; + for (int i = 0; i < m; i++) { + search(mat, i, 0); + } + for (int i = 1; i < n; i++) { + search(mat, 0, i); } - return sorted; + return mat; } } diff --git a/src/main/java/g1401_1500/s1408_string_matching_in_an_array/Solution.java b/src/main/java/g1401_1500/s1408_string_matching_in_an_array/Solution.java index 70be26d25..8d2c2c3ee 100644 --- a/src/main/java/g1401_1500/s1408_string_matching_in_an_array/Solution.java +++ b/src/main/java/g1401_1500/s1408_string_matching_in_an_array/Solution.java @@ -1,22 +1,31 @@ package g1401_1500.s1408_string_matching_in_an_array; -// #Easy #String #String_Matching #2022_03_26_Time_8_ms_(24.88%)_Space_43.3_MB_(13.46%) +// #Easy #String #String_Matching #2024_12_19_Time_1_ms_(100.00%)_Space_42.7_MB_(5.57%) import java.util.ArrayList; -import java.util.HashSet; import java.util.List; -import java.util.Set; public class Solution { public List stringMatching(String[] words) { - Set set = new HashSet<>(); - for (String word : words) { - for (String s : words) { - if (!word.equals(s) && word.length() < s.length() && s.contains(word)) { - set.add(word); - } + List matchedStrings = new ArrayList<>(); + for (int i = 0; i < words.length; i++) { + boolean containsSubstring = checkContains(words, i); + if (containsSubstring) { + matchedStrings.add(words[i]); } } - return new ArrayList<>(set); + return matchedStrings; + } + + private boolean checkContains(String[] words, int index) { + for (int j = 0; j < words.length; j++) { + if (index == j) { + continue; + } + if (words[j].contains(words[index])) { + return true; + } + } + return false; } } diff --git a/src/test/java/g1401_1500/s1408_string_matching_in_an_array/SolutionTest.java b/src/test/java/g1401_1500/s1408_string_matching_in_an_array/SolutionTest.java index 4cdcd2c88..d70df5fed 100644 --- a/src/test/java/g1401_1500/s1408_string_matching_in_an_array/SolutionTest.java +++ b/src/test/java/g1401_1500/s1408_string_matching_in_an_array/SolutionTest.java @@ -19,7 +19,7 @@ void stringMatching() { void stringMatching2() { assertThat( new Solution().stringMatching(new String[] {"leetcode", "et", "code"}), - equalTo(Arrays.asList("code", "et"))); + equalTo(Arrays.asList("et", "code"))); } @Test