Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Added tasks 922, 923, 924, 925, 926 #400

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
javadev merged 3 commits into javadev:main from ThanhNIT:tasks-922-923-924-925-926
Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package g0901_1000.s0922_sort_array_by_parity_ii;

// #Easy #Array #Sorting #Two_Pointers

public class Solution {
public int[] sortArrayByParityII(int[] nums) {
for (int i = 0, j = 1; i < nums.length - 1 && j < nums.length; ) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please replace for with while.

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;
}
}
32 changes: 32 additions & 0 deletions src/main/java/g0901_1000/s0922_sort_array_by_parity_ii/readme.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -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:**

* <code>2 <= nums.length <= 2 * 10<sup>4</sup></code>
* `nums.length` is even.
* Half of the integers in `nums` are even.
* `0 <= nums[i] <= 1000`

**Follow Up:** Could you solve it in-place?
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package g0901_1000.s0923_3sum_with_multiplicity;

public class Solution {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add comment with tags.

ThanhNIT reacted with thumbs up emoji
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;
}
}
41 changes: 41 additions & 0 deletions src/main/java/g0901_1000/s0923_3sum_with_multiplicity/readme.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -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** <code>10<sup>9</sup> + 7</code>.

**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`
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -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<Integer, HashSet<Integer>> 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]);
}
}
43 changes: 43 additions & 0 deletions src/main/java/g0901_1000/s0924_minimize_malware_spread/readme.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -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 <code>i<sup>th</sup></code> node is directly connected to the <code>j<sup>th</sup></code> 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**.
42 changes: 42 additions & 0 deletions src/main/java/g0901_1000/s0925_long_pressed_name/Solution.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -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;
}
}
28 changes: 28 additions & 0 deletions src/main/java/g0901_1000/s0925_long_pressed_name/readme.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -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.
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package g0901_1000.s0926_flip_string_to_monotone_increasing;

class Solution {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add comment with tags and public for class.

ThanhNIT reacted with thumbs up emoji
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;
}
}
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -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:**

* <code>1 <= s.length <= 10<sup>5</sup></code>
* `s[i]` is either `'0'` or `'1'`.
Loading

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