Skip to content

Improved tasks 869, 870, 976, 1329, 1408 #1886

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 13 additions & 22 deletions src/main/java/g0801_0900/s0869_reordered_power_of_2/Solution.java
Original file line number Diff line number Diff line change
@@ -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<Character, Integer> m = new HashMap<>();
Map<Character, Integer> 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<Character, Integer> 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;
}
}
85 changes: 43 additions & 42 deletions src/main/java/g0801_0900/s0870_advantage_shuffle/Solution.java
Original file line number Diff line number Diff line change
@@ -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<Integer> pque = new PriorityQueue<>();
for (int e : nums1) {
pque.add(e);
}
int l = nums1.length;
HashMap<Integer, List<Integer>> map = new HashMap<>();
int[] n = new int[l];
System.arraycopy(nums2, 0, n, 0, l);
Arrays.sort(n);
Deque<Integer> sta = new ArrayDeque<>();
for (int i = 0; i < l && !pque.isEmpty(); i++) {
List<Integer> 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<Integer> 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<Integer> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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<Integer> 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<Integer> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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<String> stringMatching(String[] words) {
Set<String> 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<String> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading