Skip to content

Commit 7a4eeb8

Browse files
authored
Added tasks 1914, 1915, 1916, 1920, 1921.
1 parent 2341799 commit 7a4eeb8

File tree

15 files changed

+577
-0
lines changed

15 files changed

+577
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package g1901_2000.s1914_cyclically_rotating_a_grid;
2+
3+
// #Medium #Array #Matrix #Simulation #2022_05_14_Time_2_ms_(91.30%)_Space_43_MB_(86.96%)
4+
5+
public class Solution {
6+
public int[][] rotateGrid(int[][] grid, int k) {
7+
rotateInternal(grid, 0, grid[0].length - 1, 0, grid.length - 1, k);
8+
return grid;
9+
}
10+
11+
private void rotateInternal(int[][] grid, int left, int right, int up, int bottom, int k) {
12+
if (left > right || up > bottom) {
13+
return;
14+
}
15+
int loopLen = (right - left + 1) * 2 + (bottom - up + 1) * 2 - 4;
16+
int realK = k % loopLen;
17+
if (realK != 0) {
18+
rotateLayer(grid, left, right, up, bottom, realK);
19+
}
20+
rotateInternal(grid, left + 1, right - 1, up + 1, bottom - 1, k);
21+
}
22+
23+
private void rotateLayer(int[][] grid, int left, int right, int up, int bottom, int k) {
24+
int[] startPoint = new int[] {up, left};
25+
int loopLen = (right - left + 1) * 2 + (bottom - up + 1) * 2 - 4;
26+
int[] arr = new int[loopLen];
27+
int idx = 0;
28+
int[] currPoint = startPoint;
29+
int[] startPointAfterRotation = null;
30+
31+
while (idx < arr.length) {
32+
arr[idx] = grid[currPoint[0]][currPoint[1]];
33+
idx++;
34+
currPoint = getNextPosCC(left, right, up, bottom, currPoint);
35+
if (idx == k) {
36+
startPointAfterRotation = currPoint;
37+
}
38+
}
39+
40+
idx = 0;
41+
currPoint = startPointAfterRotation;
42+
if (currPoint != null) {
43+
while (idx < arr.length) {
44+
grid[currPoint[0]][currPoint[1]] = arr[idx];
45+
idx++;
46+
currPoint = getNextPosCC(left, right, up, bottom, currPoint);
47+
}
48+
}
49+
}
50+
51+
private int[] getNextPosCC(int left, int right, int up, int bottom, int[] curr) {
52+
int x = curr[0];
53+
int y = curr[1];
54+
if (x == up && y > left) {
55+
return new int[] {x, y - 1};
56+
} else if (y == left && x < bottom) {
57+
return new int[] {x + 1, y};
58+
} else if (x == bottom && y < right) {
59+
return new int[] {x, y + 1};
60+
} else {
61+
return new int[] {x - 1, y};
62+
}
63+
}
64+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
1914\. Cyclically Rotating a Grid
2+
3+
Medium
4+
5+
You are given an `m x n` integer matrix `grid`, where `m` and `n` are both **even** integers, and an integer `k`.
6+
7+
The matrix is composed of several layers, which is shown in the below image, where each color is its own layer:
8+
9+
![](https://assets.leetcode.com/uploads/2021/06/10/ringofgrid.png)
10+
11+
A cyclic rotation of the matrix is done by cyclically rotating **each layer** in the matrix. To cyclically rotate a layer once, each element in the layer will take the place of the adjacent element in the **counter-clockwise** direction. An example rotation is shown below:
12+
13+
![](https://assets.leetcode.com/uploads/2021/06/22/explanation_grid.jpg)
14+
15+
Return _the matrix after applying_ `k` _cyclic rotations to it_.
16+
17+
**Example 1:**
18+
19+
![](https://assets.leetcode.com/uploads/2021/06/19/rod2.png)
20+
21+
**Input:** grid = [[40,10],[30,20]], k = 1
22+
23+
**Output:** [[10,20],[40,30]]
24+
25+
**Explanation:** The figures above represent the grid at every state.
26+
27+
**Example 2:**
28+
29+
**![](https://assets.leetcode.com/uploads/2021/06/10/ringofgrid5.png)** **![](https://assets.leetcode.com/uploads/2021/06/10/ringofgrid6.png)** **![](https://assets.leetcode.com/uploads/2021/06/10/ringofgrid7.png)**
30+
31+
**Input:** grid = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]], k = 2
32+
33+
**Output:** [[3,4,8,12],[2,11,10,16],[1,7,6,15],[5,9,13,14]]
34+
35+
**Explanation:** The figures above represent the grid at every state.
36+
37+
**Constraints:**
38+
39+
* `m == grid.length`
40+
* `n == grid[i].length`
41+
* `2 <= m, n <= 50`
42+
* Both `m` and `n` are **even** integers.
43+
* `1 <= grid[i][j] <= 5000`
44+
* <code>1 <= k <= 10<sup>9</sup></code>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g1901_2000.s1915_number_of_wonderful_substrings;
2+
3+
// #Medium #String #Hash_Table #Bit_Manipulation #Prefix_Sum
4+
// #2022_05_14_Time_31_ms_(82.46%)_Space_54.6_MB_(38.60%)
5+
6+
public class Solution {
7+
public long wonderfulSubstrings(String word) {
8+
int[] count = new int[1024];
9+
long res = 0;
10+
int cur = 0;
11+
count[0] = 1;
12+
for (int i = 0; i < word.length(); i++) {
13+
cur ^= 1 << (word.charAt(i) - 'a');
14+
res += count[cur];
15+
for (int j = 0; j < 10; j++) {
16+
res += count[cur ^ (1 << j)];
17+
}
18+
++count[cur];
19+
}
20+
return res;
21+
}
22+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
1915\. Number of Wonderful Substrings
2+
3+
Medium
4+
5+
A **wonderful** string is a string where **at most one** letter appears an **odd** number of times.
6+
7+
* For example, `"ccjjc"` and `"abab"` are wonderful, but `"ab"` is not.
8+
9+
Given a string `word` that consists of the first ten lowercase English letters (`'a'` through `'j'`), return _the **number of wonderful non-empty substrings** in_ `word`_. If the same substring appears multiple times in_ `word`_, then count **each occurrence** separately._
10+
11+
A **substring** is a contiguous sequence of characters in a string.
12+
13+
**Example 1:**
14+
15+
**Input:** word = "aba"
16+
17+
**Output:** 4
18+
19+
**Explanation:** The four wonderful substrings are underlined below:
20+
21+
- "**a**ba" -> "a"
22+
23+
- "a**b**a" -> "b"
24+
25+
- "ab**a**" -> "a"
26+
27+
- "**aba**" -> "aba"
28+
29+
**Example 2:**
30+
31+
**Input:** word = "aabb"
32+
33+
**Output:** 9
34+
35+
**Explanation:** The nine wonderful substrings are underlined below:
36+
37+
- "**a**abb" -> "a"
38+
39+
- "**aa**bb" -> "aa"
40+
41+
- "**aab**b" -> "aab"
42+
43+
- "**aabb**" -> "aabb"
44+
45+
- "a**a**bb" -> "a"
46+
47+
- "a**abb**" -> "abb"
48+
49+
- "aa**b**b" -> "b"
50+
51+
- "aa**bb**" -> "bb"
52+
53+
- "aab**b**" -> "b"
54+
55+
**Example 3:**
56+
57+
**Input:** word = "he"
58+
59+
**Output:** 2
60+
61+
**Explanation:** The two wonderful substrings are underlined below:
62+
63+
- "**h**e" -> "h"
64+
65+
- "h**e**" -> "e"
66+
67+
**Constraints:**
68+
69+
* <code>1 <= word.length <= 10<sup>5</sup></code>
70+
* `word` consists of lowercase English letters from `'a'` to `'j'`.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package g1901_2000.s1916_count_ways_to_build_rooms_in_an_ant_colony;
2+
3+
// #Hard #Dynamic_Programming #Math #Tree #Graph #Topological_Sort #Combinatorics
4+
// #2022_05_14_Time_1527_ms_(34.38%)_Space_106_MB_(87.50%)
5+
6+
import java.math.BigInteger;
7+
import java.util.ArrayList;
8+
import java.util.Arrays;
9+
import java.util.List;
10+
11+
public class Solution {
12+
private static final int MOD = 1000000007;
13+
private List<Integer>[] graph;
14+
private long[] fact;
15+
16+
public int waysToBuildRooms(int[] a) {
17+
int n = a.length;
18+
graph = new ArrayList[n];
19+
Arrays.setAll(graph, e -> new ArrayList<>());
20+
21+
fact = new long[a.length + 10];
22+
fact[0] = fact[1] = 1;
23+
for (int i = 2; i < fact.length; i++) {
24+
fact[i] = fact[i - 1] * i;
25+
fact[i] %= MOD;
26+
}
27+
28+
for (int i = 1; i < a.length; i++) {
29+
int pre = a[i];
30+
graph[pre].add(i);
31+
}
32+
33+
long[] res = dfs(0);
34+
return (int) (res[1] % MOD);
35+
}
36+
37+
private long[] dfs(int root) {
38+
long[] res = new long[] {1, 0};
39+
int cnt = 0;
40+
List<long[]> list = new ArrayList<>();
41+
for (int next : graph[root]) {
42+
long[] v = dfs(next);
43+
cnt += v[0];
44+
list.add(v);
45+
}
46+
47+
res[0] += cnt;
48+
long com = 1;
49+
for (long[] p : list) {
50+
long choose = c(cnt, (int) (p[0]));
51+
cnt -= p[0];
52+
com = com * choose;
53+
com %= MOD;
54+
com = com * p[1];
55+
com %= MOD;
56+
}
57+
58+
res[1] = com;
59+
return res;
60+
}
61+
62+
private long c(int i, int j) {
63+
long mod = 1000000007;
64+
long a = fact[i];
65+
long b = ((fact[i - j] % mod) * (fact[j] % mod)) % mod;
66+
BigInteger value = BigInteger.valueOf(b);
67+
long binverse = value.modInverse(BigInteger.valueOf(mod)).longValue();
68+
return ((a) * (binverse % mod)) % mod;
69+
}
70+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
1916\. Count Ways to Build Rooms in an Ant Colony
2+
3+
Hard
4+
5+
You are an ant tasked with adding `n` new rooms numbered `0` to `n-1` to your colony. You are given the expansion plan as a **0-indexed** integer array of length `n`, `prevRoom`, where `prevRoom[i]` indicates that you must build room `prevRoom[i]` before building room `i`, and these two rooms must be connected **directly**. Room `0` is already built, so `prevRoom[0] = -1`. The expansion plan is given such that once all the rooms are built, every room will be reachable from room `0`.
6+
7+
You can only build **one room** at a time, and you can travel freely between rooms you have **already built** only if they are **connected**. You can choose to build **any room** as long as its **previous room** is already built.
8+
9+
Return _the **number of different orders** you can build all the rooms in_. Since the answer may be large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2021/06/19/d1.JPG)
14+
15+
**Input:** prevRoom = [-1,0,1]
16+
17+
**Output:** 1
18+
19+
**Explanation:** There is only one way to build the additional rooms: 0 → 1 → 2
20+
21+
**Example 2:**
22+
23+
**![](https://assets.leetcode.com/uploads/2021/06/19/d2.JPG)**
24+
25+
**Input:** prevRoom = [-1,0,0,1,2]
26+
27+
**Output:** 6
28+
29+
**Explanation:** The 6 ways are:
30+
31+
0 → 1 → 3 → 2 → 4
32+
33+
0 → 2 → 4 → 1 → 3
34+
35+
0 → 1 → 2 → 3 → 4
36+
37+
0 → 1 → 2 → 4 → 3
38+
39+
0 → 2 → 1 → 3 → 4
40+
41+
0 → 2 → 1 → 4 → 3
42+
43+
**Constraints:**
44+
45+
* `n == prevRoom.length`
46+
* <code>2 <= n <= 10<sup>5</sup></code>
47+
* `prevRoom[0] == -1`
48+
* `0 <= prevRoom[i] < n` for all `1 <= i < n`
49+
* Every room is reachable from room `0` once all the rooms are built.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package g1901_2000.s1920_build_array_from_permutation;
2+
3+
// #Easy #Array #Simulation #2022_05_14_Time_1_ms_(94.23%)_Space_54.1_MB_(13.18%)
4+
5+
public class Solution {
6+
public int[] buildArray(int[] nums) {
7+
int[] ans = new int[nums.length];
8+
for (int i = 0; i < nums.length; i++) {
9+
ans[i] = nums[nums[i]];
10+
}
11+
return ans;
12+
}
13+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
1920\. Build Array from Permutation
2+
3+
Easy
4+
5+
Given a **zero-based permutation** `nums` (**0-indexed**), build an array `ans` of the **same length** where `ans[i] = nums[nums[i]]` for each `0 <= i < nums.length` and return it.
6+
7+
A **zero-based permutation** `nums` is an array of **distinct** integers from `0` to `nums.length - 1` (**inclusive**).
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [0,2,1,5,3,4]
12+
13+
**Output:** [0,1,2,4,5,3]
14+
15+
**Explanation:** The array ans is built as follows:
16+
17+
ans = [nums[nums[0]], nums[nums[1]], nums[nums[2]], nums[nums[3]], nums[nums[4]], nums[nums[5]]]
18+
= [nums[0], nums[2], nums[1], nums[5], nums[3], nums[4]]
19+
= [0,1,2,4,5,3]
20+
21+
**Example 2:**
22+
23+
**Input:** nums = [5,0,1,2,3,4]
24+
25+
**Output:** [4,5,0,1,2,3]
26+
27+
**Explanation:** The array ans is built as follows:
28+
29+
ans = [nums[nums[0]], nums[nums[1]], nums[nums[2]], nums[nums[3]], nums[nums[4]], nums[nums[5]]]
30+
= [nums[5], nums[0], nums[1], nums[2], nums[3], nums[4]]
31+
= [4,5,0,1,2,3]
32+
33+
**Constraints:**
34+
35+
* `1 <= nums.length <= 1000`
36+
* `0 <= nums[i] < nums.length`
37+
* The elements in `nums` are **distinct**.
38+
39+
**Follow-up:** Can you solve it without using an extra space (i.e., `O(1)` memory)?

0 commit comments

Comments
 (0)