diff --git a/src/main/java/g0901_1000/s0922_sort_array_by_parity_ii/Solution.java b/src/main/java/g0901_1000/s0922_sort_array_by_parity_ii/Solution.java new file mode 100644 index 000000000..a71b20c13 --- /dev/null +++ b/src/main/java/g0901_1000/s0922_sort_array_by_parity_ii/Solution.java @@ -0,0 +1,26 @@ +package g0901_1000.s0922_sort_array_by_parity_ii; + +// #Easy #Array #Sorting #Two_Pointers + +public class Solution { + public int[] sortArrayByParityII(int[] nums) { + int i = 0; + int j = 1; + while (i < nums.length - 1 && j < nums.length) { + if (nums[i] % 2 != 0 && nums[j] % 2 == 0) { + int tmp = nums[i]; + nums[i] = nums[j]; + nums[j] = tmp; + i += 2; + j += 2; + } + while (i < nums.length - 1 && nums[i] % 2 == 0) { + i += 2; + } + while (j < nums.length && nums[j] % 2 != 0) { + j += 2; + } + } + return nums; + } +} diff --git a/src/main/java/g0901_1000/s0922_sort_array_by_parity_ii/readme.md b/src/main/java/g0901_1000/s0922_sort_array_by_parity_ii/readme.md new file mode 100644 index 000000000..d8291c7f7 --- /dev/null +++ b/src/main/java/g0901_1000/s0922_sort_array_by_parity_ii/readme.md @@ -0,0 +1,32 @@ +922\. Sort Array By Parity II + +Easy + +Given an array of integers `nums`, half of the integers in `nums` are **odd**, and the other half are **even**. + +Sort the array so that whenever `nums[i]` is odd, `i` is **odd**, and whenever `nums[i]` is even, `i` is **even**. + +Return _any answer array that satisfies this condition_. + +**Example 1:** + +**Input:** nums = [4,2,5,7] + +**Output:** [4,5,2,7] + +**Explanation:** [4,7,2,5], [2,5,4,7], [2,7,4,5] would also have been accepted. + +**Example 2:** + +**Input:** nums = [2,3] + +**Output:** [2,3] + +**Constraints:** + +* 2 <= nums.length <= 2 * 104 +* `nums.length` is even. +* Half of the integers in `nums` are even. +* `0 <= nums[i] <= 1000` + +**Follow Up:** Could you solve it in-place? \ No newline at end of file diff --git a/src/main/java/g0901_1000/s0923_3sum_with_multiplicity/Solution.java b/src/main/java/g0901_1000/s0923_3sum_with_multiplicity/Solution.java new file mode 100644 index 000000000..cddc62cd1 --- /dev/null +++ b/src/main/java/g0901_1000/s0923_3sum_with_multiplicity/Solution.java @@ -0,0 +1,33 @@ +package g0901_1000.s0923_3sum_with_multiplicity; + +// #Medium #Array #Hash_Table #Sorting #Two_Pointers #Counting + +public class Solution { + private static final int MOD = (int) 1e9 + 7; + private static final int MAX = 100; + + public int threeSumMulti(int[] arr, int target) { + int answer = 0; + int[] countRight = new int[MAX + 1]; + for (int num : arr) { + ++countRight[num]; + } + int[] countLeft = new int[MAX + 1]; + for (int j = 0; j < arr.length - 1; j++) { + --countRight[arr[j]]; + int remains = target - arr[j]; + if (remains <= 2 * MAX) { + for (int v = 0; v <= Math.min(remains, MAX); v++) { + if (remains - v <= MAX) { + int count = countRight[v] * countLeft[remains - v]; + if (count> 0) { + answer = (answer + count) % MOD; + } + } + } + } + ++countLeft[arr[j]]; + } + return answer; + } +} diff --git a/src/main/java/g0901_1000/s0923_3sum_with_multiplicity/readme.md b/src/main/java/g0901_1000/s0923_3sum_with_multiplicity/readme.md new file mode 100644 index 000000000..d43cf7ee0 --- /dev/null +++ b/src/main/java/g0901_1000/s0923_3sum_with_multiplicity/readme.md @@ -0,0 +1,41 @@ +923\. 3Sum With Multiplicity + +Medium + +Given an integer array `arr`, and an integer `target`, return the number of tuples `i, j, k` such that `i < j < k` and `arr[i] + arr[j] + arr[k] == target`. + +As the answer can be very large, return it **modulo** 109 + 7. + +**Example 1:** + +**Input:** arr = [1,1,2,2,3,3,4,4,5,5], target = 8 + +**Output:** 20 + +**Explanation:** + + Enumerating by the values (arr[i], arr[j], arr[k]): + (1, 2, 5) occurs 8 times; + (1, 3, 4) occurs 8 times; + (2, 2, 4) occurs 2 times; + (2, 3, 3) occurs 2 times. + +**Example 2:** + +**Input:** arr = [1,1,2,2,2,2], target = 5 + +**Output:** 12 + +**Explanation:** + +arr[i] = 1, arr[j] = arr[k] = 2 occurs 12 times: + +We choose one 1 from [1,1] in 2 ways, + +and two 2s from [2,2,2,2] in 6 ways. + +**Constraints:** + +* `3 <= arr.length <= 3000` +* `0 <= arr[i] <= 100` +* `0 <= target <= 300` \ No newline at end of file diff --git a/src/main/java/g0901_1000/s0924_minimize_malware_spread/Solution.java b/src/main/java/g0901_1000/s0924_minimize_malware_spread/Solution.java new file mode 100644 index 000000000..82bdbf711 --- /dev/null +++ b/src/main/java/g0901_1000/s0924_minimize_malware_spread/Solution.java @@ -0,0 +1,70 @@ +package g0901_1000.s0924_minimize_malware_spread; + +// #Hard #Array #Depth_First_Search #Breadth_First_Search #Matrix #Union_Find + +import java.util.HashMap; +import java.util.HashSet; + +public class Solution { + private int[][] par; + + public int minMalwareSpread(int[][] graph, int[] initial) { + int n = graph.length; + par = new int[n][2]; + for (int i = 0; i < par.length; i++) { + par[i][0] = i; + par[i][1] = 1; + } + for (int i = 0; i < n; i++) { + for (int j = i + 1; j < n; j++) { + if (graph[i][j] == 1) { + int li = find(i); + int lj = find(j); + if (li != lj) { + par[li][0] = lj; + par[lj][1] += par[li][1]; + } + } + } + } + HashMap> map = new HashMap(); + for (int val : initial) { + int lv = find(val); + if (!map.containsKey(lv)) { + map.put(lv, new HashSet()); + } + map.get(lv).add(val); + } + int ans = -1; + int max = Integer.MIN_VALUE; + + for (int val : initial) { + int lv = find(val); + if (map.get(lv).size() == 1) { + if (par[lv][1]> max) { + max = par[lv][1]; + ans = val; + } else if (par[lv][1] == max && val < ans) { + ans = val; + } + } else { + if (max < 0) { + max = 0; + ans = val; + } else if (max == 0 && val < ans) { + ans = val; + } + } + } + + return ans; + } + + private int find(int x) { + if (par[x][0] == x) { + return x; + } + + return find(par[x][0]); + } +} diff --git a/src/main/java/g0901_1000/s0924_minimize_malware_spread/readme.md b/src/main/java/g0901_1000/s0924_minimize_malware_spread/readme.md new file mode 100644 index 000000000..a5f74d2ea --- /dev/null +++ b/src/main/java/g0901_1000/s0924_minimize_malware_spread/readme.md @@ -0,0 +1,43 @@ +924\. Minimize Malware Spread + +Hard + +You are given a network of `n` nodes represented as an `n x n` adjacency matrix `graph`, where the ith node is directly connected to the jth node if `graph[i][j] == 1`. + +Some nodes `initial` are initially infected by malware. Whenever two nodes are directly connected, and at least one of those two nodes is infected by malware, both nodes will be infected by malware. This spread of malware will continue until no more nodes can be infected in this manner. + +Suppose `M(initial)` is the final number of nodes infected with malware in the entire network after the spread of malware stops. We will remove **exactly one node** from `initial`. + +Return the node that, if removed, would minimize `M(initial)`. If multiple nodes could be removed to minimize `M(initial)`, return such a node with **the smallest index**. + +Note that if a node was removed from the `initial` list of infected nodes, it might still be infected later due to the malware spread. + +**Example 1:** + +**Input:** graph = [[1,1,0],[1,1,0],[0,0,1]], initial = [0,1] + +**Output:** 0 + +**Example 2:** + +**Input:** graph = [[1,0,0],[0,1,0],[0,0,1]], initial = [0,2] + +**Output:** 0 + +**Example 3:** + +**Input:** graph = [[1,1,1],[1,1,1],[1,1,1]], initial = [1,2] + +**Output:** 1 + +**Constraints:** + +* `n == graph.length` +* `n == graph[i].length` +* `2 <= n <= 300` +* `graph[i][j]` is `0` or `1`. +* `graph[i][j] == graph[j][i]` +* `graph[i][i] == 1` +* `1 <= initial.length <= n` +* `0 <= initial[i] <= n - 1` +* All the integers in `initial` are **unique**. \ No newline at end of file diff --git a/src/main/java/g0901_1000/s0925_long_pressed_name/Solution.java b/src/main/java/g0901_1000/s0925_long_pressed_name/Solution.java new file mode 100644 index 000000000..472412501 --- /dev/null +++ b/src/main/java/g0901_1000/s0925_long_pressed_name/Solution.java @@ -0,0 +1,42 @@ +package g0901_1000.s0925_long_pressed_name; + +// #Easy #String #Two_Pointers + +public class Solution { + public boolean isLongPressedName(String name, String typed) { + int i = 0; + int j = 0; + char prev = '$'; + if (typed.length() < name.length()) { + return false; + } + + while (i < name.length() && j < typed.length()) { + while (j < typed.length() && typed.charAt(j) != name.charAt(i)) { + if (typed.charAt(j) != prev) { + return false; + } + if (j == typed.length() - 1) { + return false; + } + j++; + } + + prev = name.charAt(i); + i++; + j++; + } + + if (i < name.length()) { + return false; + } + + for (; j < typed.length(); j++) { + if (typed.charAt(j) != prev) { + return false; + } + } + + return true; + } +} diff --git a/src/main/java/g0901_1000/s0925_long_pressed_name/readme.md b/src/main/java/g0901_1000/s0925_long_pressed_name/readme.md new file mode 100644 index 000000000..b6900eefb --- /dev/null +++ b/src/main/java/g0901_1000/s0925_long_pressed_name/readme.md @@ -0,0 +1,28 @@ +925\. Long Pressed Name + +Easy + +Your friend is typing his `name` into a keyboard. Sometimes, when typing a character `c`, the key might get _long pressed_, and the character will be typed 1 or more times. + +You examine the `typed` characters of the keyboard. Return `True` if it is possible that it was your friends name, with some characters (possibly none) being long pressed. + +**Example 1:** + +**Input:** name = "alex", typed = "aaleex" + +**Output:** true + +**Explanation:** 'a' and 'e' in 'alex' were long pressed. + +**Example 2:** + +**Input:** name = "saeed", typed = "ssaaedd" + +**Output:** false + +**Explanation:** 'e' must have been pressed twice, but it was not in the typed output. + +**Constraints:** + +* `1 <= name.length, typed.length <= 1000` +* `name` and `typed` consist of only lowercase English letters. \ No newline at end of file diff --git a/src/main/java/g0901_1000/s0926_flip_string_to_monotone_increasing/Solution.java b/src/main/java/g0901_1000/s0926_flip_string_to_monotone_increasing/Solution.java new file mode 100644 index 000000000..00e58e82d --- /dev/null +++ b/src/main/java/g0901_1000/s0926_flip_string_to_monotone_increasing/Solution.java @@ -0,0 +1,27 @@ +package g0901_1000.s0926_flip_string_to_monotone_increasing; + +// #Medium #String #Dynamic_Programming + +class Solution { + public int minFlipsMonoIncr(String s) { + if (s == null || s.length() <= 1) { + return 0; + } + final int n = s.length(); + int countOnes = 0; + int countFlips = 0; + for (int i = 0; i < n; i++) { + if (s.charAt(i) == '0') { + if (countOnes == 0) { + continue; + } else { + countFlips++; + } + } else { + countOnes++; + } + countFlips = Math.min(countFlips, countOnes); + } + return countFlips; + } +} diff --git a/src/main/java/g0901_1000/s0926_flip_string_to_monotone_increasing/readme.md b/src/main/java/g0901_1000/s0926_flip_string_to_monotone_increasing/readme.md new file mode 100644 index 000000000..e1d18b25c --- /dev/null +++ b/src/main/java/g0901_1000/s0926_flip_string_to_monotone_increasing/readme.md @@ -0,0 +1,38 @@ +926\. Flip String to Monotone Increasing + +Medium + +A binary string is monotone increasing if it consists of some number of `0`'s (possibly none), followed by some number of `1`'s (also possibly none). + +You are given a binary string `s`. You can flip `s[i]` changing it from `0` to `1` or from `1` to `0`. + +Return _the minimum number of flips to make_ `s` _monotone increasing_. + +**Example 1:** + +**Input:** s = "00110" + +**Output:** 1 + +**Explanation:** We flip the last digit to get 00111. + +**Example 2:** + +**Input:** s = "010110" + +**Output:** 2 + +**Explanation:** We flip to get 011111, or alternatively 000111. + +**Example 3:** + +**Input:** s = "00011000" + +**Output:** 2 + +**Explanation:** We flip to get 00000000. + +**Constraints:** + +* 1 <= s.length <= 105 +* `s[i]` is either `'0'` or `'1'`. \ No newline at end of file diff --git a/src/test/java/g0901_1000/s0922_sort_array_by_parity_ii/SolutionTest.java b/src/test/java/g0901_1000/s0922_sort_array_by_parity_ii/SolutionTest.java new file mode 100644 index 000000000..e792ffb0c --- /dev/null +++ b/src/test/java/g0901_1000/s0922_sort_array_by_parity_ii/SolutionTest.java @@ -0,0 +1,20 @@ +package g0901_1000.s0922_sort_array_by_parity_ii; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void sortArrayByParityII() { + assertThat( + new Solution().sortArrayByParityII(new int[] {4, 2, 5, 7}), + equalTo(new int[] {4, 5, 2, 7})); + } + + @Test + void sortArrayByParityII2() { + assertThat(new Solution().sortArrayByParityII(new int[] {2, 3}), equalTo(new int[] {2, 3})); + } +} diff --git a/src/test/java/g0901_1000/s0923_3sum_with_multiplicity/SolutionTest.java b/src/test/java/g0901_1000/s0923_3sum_with_multiplicity/SolutionTest.java new file mode 100644 index 000000000..f160119d3 --- /dev/null +++ b/src/test/java/g0901_1000/s0923_3sum_with_multiplicity/SolutionTest.java @@ -0,0 +1,20 @@ +package g0901_1000.s0923_3sum_with_multiplicity; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void threeSumMulti() { + assertThat( + new Solution().threeSumMulti(new int[] {1, 1, 2, 2, 3, 3, 4, 4, 5, 5}, 8), + equalTo(20)); + } + + @Test + void threeSumMulti2() { + assertThat(new Solution().threeSumMulti(new int[] {1, 1, 2, 2, 2, 2}, 5), equalTo(12)); + } +} diff --git a/src/test/java/g0901_1000/s0924_minimize_malware_spread/SolutionTest.java b/src/test/java/g0901_1000/s0924_minimize_malware_spread/SolutionTest.java new file mode 100644 index 000000000..f63c6d895 --- /dev/null +++ b/src/test/java/g0901_1000/s0924_minimize_malware_spread/SolutionTest.java @@ -0,0 +1,35 @@ +package g0901_1000.s0924_minimize_malware_spread; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minMalwareSpread() { + assertThat( + new Solution() + .minMalwareSpread( + new int[][] {{1, 1, 0}, {1, 1, 0}, {0, 0, 1}}, new int[] {0, 1}), + equalTo(0)); + } + + @Test + void minMalwareSpread2() { + assertThat( + new Solution() + .minMalwareSpread( + new int[][] {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}, new int[] {0, 2}), + equalTo(0)); + } + + @Test + void minMalwareSpread3() { + assertThat( + new Solution() + .minMalwareSpread( + new int[][] {{1, 1, 1}, {1, 1, 1}, {1, 1, 1}}, new int[] {1, 2}), + equalTo(1)); + } +} diff --git a/src/test/java/g0901_1000/s0925_long_pressed_name/SolutionTest.java b/src/test/java/g0901_1000/s0925_long_pressed_name/SolutionTest.java new file mode 100644 index 000000000..438efb04c --- /dev/null +++ b/src/test/java/g0901_1000/s0925_long_pressed_name/SolutionTest.java @@ -0,0 +1,18 @@ +package g0901_1000.s0925_long_pressed_name; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void isLongPressedName() { + assertThat(new Solution().isLongPressedName("alex", "aaleex"), equalTo(true)); + } + + @Test + void isLongPressedName2() { + assertThat(new Solution().isLongPressedName("saeed", "ssaaedd"), equalTo(false)); + } +} diff --git a/src/test/java/g0901_1000/s0926_flip_string_to_monotone_increasing/SolutionTest.java b/src/test/java/g0901_1000/s0926_flip_string_to_monotone_increasing/SolutionTest.java new file mode 100644 index 000000000..14cdde6fb --- /dev/null +++ b/src/test/java/g0901_1000/s0926_flip_string_to_monotone_increasing/SolutionTest.java @@ -0,0 +1,23 @@ +package g0901_1000.s0926_flip_string_to_monotone_increasing; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minFlipsMonoIncr() { + assertThat(new Solution().minFlipsMonoIncr("00110"), equalTo(1)); + } + + @Test + void minFlipsMonoIncr2() { + assertThat(new Solution().minFlipsMonoIncr("010110"), equalTo(2)); + } + + @Test + void minFlipsMonoIncr3() { + assertThat(new Solution().minFlipsMonoIncr("00011000"), equalTo(2)); + } +}

AltStyle によって変換されたページ (->オリジナル) /