Skip to content

Commit 2ada1ec

Browse files
authored
Improved tasks 869, 870, 976, 1329, 1408
1 parent dac7b6f commit 2ada1ec

File tree

6 files changed

+102
-107
lines changed

6 files changed

+102
-107
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,29 @@
11
package g0801_0900.s0869_reordered_power_of_2;
22

33
// #Medium #Math #Sorting #Counting #Enumeration
4-
// #2022_03_28_Time_9_ms_(25.97%)_Space_42.8_MB_(11.69%)
4+
// #2024_12_19_Time_1_ms_(89.02%)_Space_40.9_MB_(44.51%)
55

6-
import java.util.HashMap;
7-
import java.util.Map;
8-
import java.util.Objects;
6+
import java.util.Arrays;
97

108
public class Solution {
119
public boolean reorderedPowerOf2(int n) {
12-
int i = 0;
13-
while (Math.pow(2, i) < (long) n * 10) {
14-
if (isValid(String.valueOf((int) (Math.pow(2, i++))), String.valueOf(n))) {
10+
int[] originalDigits = countDigits(n);
11+
int num = 1;
12+
for (int i = 0; i < 31; i++) {
13+
if (Arrays.equals(originalDigits, countDigits(num))) {
1514
return true;
1615
}
16+
num <<= 1;
1717
}
1818
return false;
1919
}
2020

21-
private boolean isValid(String a, String b) {
22-
Map<Character, Integer> m = new HashMap<>();
23-
Map<Character, Integer> mTwo = new HashMap<>();
24-
for (char c : a.toCharArray()) {
25-
m.put(c, m.containsKey(c) ? m.get(c) + 1 : 1);
21+
private int[] countDigits(int num) {
22+
int[] digitCount = new int[10];
23+
while (num > 0) {
24+
digitCount[num % 10]++;
25+
num /= 10;
2626
}
27-
for (char c : b.toCharArray()) {
28-
mTwo.put(c, mTwo.containsKey(c) ? mTwo.get(c) + 1 : 1);
29-
}
30-
for (Map.Entry<Character, Integer> entry : mTwo.entrySet()) {
31-
if (!m.containsKey(entry.getKey())
32-
|| !Objects.equals(entry.getValue(), m.get(entry.getKey()))) {
33-
return false;
34-
}
35-
}
36-
return a.charAt(0) != '0' && m.size() == mTwo.size();
27+
return digitCount;
3728
}
3829
}
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,60 @@
11
package g0801_0900.s0870_advantage_shuffle;
22

3-
// #Medium #Array #Sorting #Greedy #2022_03_28_Time_188_ms_(28.01%)_Space_116.9_MB_(5.12%)
3+
// #Medium #Array #Sorting #Greedy #2024_12_19_Time_42_ms_(99.16%)_Space_56.1_MB_(94.94%)
44

5-
import java.util.ArrayDeque;
65
import java.util.ArrayList;
76
import java.util.Arrays;
8-
import java.util.Deque;
9-
import java.util.HashMap;
107
import java.util.List;
11-
import java.util.PriorityQueue;
128

13-
@SuppressWarnings("java:S5413")
149
public class Solution {
1510
public int[] advantageCount(int[] nums1, int[] nums2) {
16-
PriorityQueue<Integer> pque = new PriorityQueue<>();
17-
for (int e : nums1) {
18-
pque.add(e);
19-
}
20-
int l = nums1.length;
21-
HashMap<Integer, List<Integer>> map = new HashMap<>();
22-
int[] n = new int[l];
23-
System.arraycopy(nums2, 0, n, 0, l);
24-
Arrays.sort(n);
25-
Deque<Integer> sta = new ArrayDeque<>();
26-
for (int i = 0; i < l && !pque.isEmpty(); i++) {
27-
List<Integer> p = map.getOrDefault(n[i], new ArrayList<>());
28-
int x = pque.poll();
29-
if (x > n[i]) {
30-
p.add(x);
31-
map.put(n[i], p);
11+
Arrays.sort(nums1);
12+
int[] result = new int[nums1.length];
13+
int low = 0;
14+
boolean[] chosen = new boolean[nums1.length];
15+
for (int i = 0; i < nums2.length; i++) {
16+
int pos = binSearch(nums1, nums2[i], low, chosen);
17+
if (pos != -1 && pos < nums1.length) {
18+
result[i] = nums1[pos];
19+
chosen[pos] = true;
3220
} else {
33-
while (x <= n[i] && !pque.isEmpty()) {
34-
sta.push(x);
35-
x = pque.poll();
36-
}
37-
if (x > n[i]) {
38-
p.add(x);
39-
map.put(n[i], p);
40-
} else {
41-
sta.push(x);
42-
}
21+
result[i] = -1;
4322
}
4423
}
45-
for (int i = 0; i < nums2.length; i++) {
46-
List<Integer> p = map.getOrDefault(nums2[i], new ArrayList<>());
47-
int t;
48-
if (!p.isEmpty()) {
49-
t = p.get(0);
50-
p.remove(0);
51-
map.put(nums2[i], p);
24+
List<Integer> pos = new ArrayList<>();
25+
int i = 0;
26+
for (boolean ch : chosen) {
27+
if (!ch) {
28+
pos.add(i);
29+
}
30+
i++;
31+
}
32+
int index = 0;
33+
for (i = 0; i < result.length; i++) {
34+
if (result[i] == -1) {
35+
result[i] = nums1[pos.get(index)];
36+
index++;
37+
}
38+
}
39+
return result;
40+
}
41+
42+
private int binSearch(int[] nums, int target, int low, boolean[] chosen) {
43+
int high = nums.length - 1;
44+
while (high >= low) {
45+
int mid = high - (high - low) / 2;
46+
if (nums[mid] > target && (mid == 0 || nums[mid - 1] <= target)) {
47+
while (mid < nums.length && chosen[mid]) {
48+
mid++;
49+
}
50+
return mid;
51+
}
52+
if (nums[mid] > target) {
53+
high = mid - 1;
5254
} else {
53-
t = sta.pop();
55+
low = mid + 1;
5456
}
55-
nums1[i] = t;
5657
}
57-
return nums1;
58+
return -1;
5859
}
5960
}

src/main/java/g0901_1000/s0976_largest_perimeter_triangle/Solution.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package g0901_1000.s0976_largest_perimeter_triangle;
22

33
// #Easy #Array #Math #Sorting #Greedy #Programming_Skills_I_Day_3_Conditional_Statements
4-
// #2022_03_31_Time_12_ms_(26.01%)_Space_53.8_MB_(69.91%)
4+
// #2024_12_19_Time_7_ms_(99.33%)_Space_45.5_MB_(8.45%)
55

66
import java.util.Arrays;
77

Original file line numberDiff line numberDiff line change
@@ -1,41 +1,35 @@
11
package g1301_1400.s1329_sort_the_matrix_diagonally;
22

3-
// #Medium #Array #Sorting #Matrix #2022_03_19_Time_15_ms_(26.03%)_Space_47.7_MB_(56.76%)
4-
5-
import java.util.ArrayList;
6-
import java.util.Collections;
7-
import java.util.List;
3+
// #Medium #Array #Sorting #Matrix #2024_12_19_Time_0_ms_(100.00%)_Space_44.7_MB_(81.35%)
84

95
public class Solution {
10-
public int[][] diagonalSort(int[][] mat) {
11-
int m = mat.length;
12-
int n = mat[0].length;
13-
int[][] sorted = new int[m][n];
14-
for (int i = m - 1; i >= 0; i--) {
15-
int iCopy = i;
16-
List<Integer> list = new ArrayList<>();
17-
for (int j = 0; j < n && iCopy < m; j++, iCopy++) {
18-
list.add(mat[iCopy][j]);
19-
}
20-
Collections.sort(list);
21-
iCopy = i;
22-
for (int j = 0; j < n && iCopy < m; j++, iCopy++) {
23-
sorted[iCopy][j] = list.get(j);
6+
private int[] count = new int[101];
7+
private int m;
8+
private int n;
9+
10+
public void search(int[][] mat, int i, int j) {
11+
for (int ti = i, tj = j; ti < m && tj < n; ti++, tj++) {
12+
count[mat[ti][tj]]++;
13+
}
14+
int c = 0;
15+
for (int ti = i, tj = j; ti < m && tj < n; ti++, tj++) {
16+
while (count[c] == 0) {
17+
c++;
2418
}
19+
mat[ti][tj] = c;
20+
count[c]--;
2521
}
22+
}
2623

27-
for (int j = n - 1; j > 0; j--) {
28-
int jCopy = j;
29-
List<Integer> list = new ArrayList<>();
30-
for (int i = 0; i < m && jCopy < n; i++, jCopy++) {
31-
list.add(mat[i][jCopy]);
32-
}
33-
Collections.sort(list);
34-
jCopy = j;
35-
for (int i = 0; i < m && jCopy < n; i++, jCopy++) {
36-
sorted[i][jCopy] = list.get(i);
37-
}
24+
public int[][] diagonalSort(int[][] mat) {
25+
m = mat.length;
26+
n = mat[0].length;
27+
for (int i = 0; i < m; i++) {
28+
search(mat, i, 0);
29+
}
30+
for (int i = 1; i < n; i++) {
31+
search(mat, 0, i);
3832
}
39-
return sorted;
33+
return mat;
4034
}
4135
}
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,31 @@
11
package g1401_1500.s1408_string_matching_in_an_array;
22

3-
// #Easy #String #String_Matching #2022_03_26_Time_8_ms_(24.88%)_Space_43.3_MB_(13.46%)
3+
// #Easy #String #String_Matching #2024_12_19_Time_1_ms_(100.00%)_Space_42.7_MB_(5.57%)
44

55
import java.util.ArrayList;
6-
import java.util.HashSet;
76
import java.util.List;
8-
import java.util.Set;
97

108
public class Solution {
119
public List<String> stringMatching(String[] words) {
12-
Set<String> set = new HashSet<>();
13-
for (String word : words) {
14-
for (String s : words) {
15-
if (!word.equals(s) && word.length() < s.length() && s.contains(word)) {
16-
set.add(word);
17-
}
10+
List<String> matchedStrings = new ArrayList<>();
11+
for (int i = 0; i < words.length; i++) {
12+
boolean containsSubstring = checkContains(words, i);
13+
if (containsSubstring) {
14+
matchedStrings.add(words[i]);
1815
}
1916
}
20-
return new ArrayList<>(set);
17+
return matchedStrings;
18+
}
19+
20+
private boolean checkContains(String[] words, int index) {
21+
for (int j = 0; j < words.length; j++) {
22+
if (index == j) {
23+
continue;
24+
}
25+
if (words[j].contains(words[index])) {
26+
return true;
27+
}
28+
}
29+
return false;
2130
}
2231
}

src/test/java/g1401_1500/s1408_string_matching_in_an_array/SolutionTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ void stringMatching() {
1919
void stringMatching2() {
2020
assertThat(
2121
new Solution().stringMatching(new String[] {"leetcode", "et", "code"}),
22-
equalTo(Arrays.asList("code", "et")));
22+
equalTo(Arrays.asList("et", "code")));
2323
}
2424

2525
@Test

0 commit comments

Comments
 (0)