Skip to content

Commit e7ba218

Browse files
authored
Added tasks 1031, 1032, 1033, 1034, 1035.
1 parent 8744ecd commit e7ba218

File tree

15 files changed

+559
-0
lines changed

15 files changed

+559
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package g1001_1100.s1031_maximum_sum_of_two_non_overlapping_subarrays;
2+
3+
// #Medium #Array #Dynamic_Programming #Sliding_Window
4+
// #2022_02_27_Time_3_ms_(46.04%)_Space_43.2_MB_(12.23%)
5+
6+
public class Solution {
7+
public int maxSumTwoNoOverlap(int[] nums, int f, int s) {
8+
int sum = 0;
9+
int n = nums.length;
10+
int[] pref = new int[n];
11+
int[] suff = new int[n];
12+
for (int i = 0; i < n; i++) {
13+
sum += nums[i];
14+
if (i < f - 1) {
15+
continue;
16+
}
17+
18+
pref[i] = Math.max(i > 0 ? pref[i - 1] : 0, sum);
19+
20+
sum -= nums[i + 1 - f];
21+
}
22+
23+
sum = 0;
24+
for (int i = n - 1; i >= 0; i--) {
25+
sum += nums[i];
26+
if (i > n - f) {
27+
continue;
28+
}
29+
30+
suff[i] = Math.max(i < n - 1 ? suff[i + 1] : 0, sum);
31+
32+
sum -= nums[i + f - 1];
33+
}
34+
35+
sum = 0;
36+
37+
for (int i = 0; i < s - 1; i++) {
38+
sum += nums[i];
39+
}
40+
41+
int ans = Integer.MIN_VALUE;
42+
for (int i = s - 1; i < n; i++) {
43+
44+
sum += nums[i];
45+
46+
if (i >= s) {
47+
ans = Math.max(ans, pref[i - s] + sum);
48+
}
49+
50+
if (i < n - 1) {
51+
ans = Math.max(ans, suff[i + 1] + sum);
52+
}
53+
54+
sum -= nums[i + 1 - s];
55+
}
56+
57+
return ans;
58+
}
59+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
1031\. Maximum Sum of Two Non-Overlapping Subarrays
2+
3+
Medium
4+
5+
Given an integer array `nums` and two integers `firstLen` and `secondLen`, return _the maximum sum of elements in two non-overlapping **subarrays** with lengths_ `firstLen` _and_ `secondLen`.
6+
7+
The array with length `firstLen` could occur before or after the array with length `secondLen`, but they have to be non-overlapping.
8+
9+
A **subarray** is a **contiguous** part of an array.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [0,6,5,2,2,5,1,9,4], firstLen = 1, secondLen = 2
14+
15+
**Output:** 20
16+
17+
**Explanation:** One choice of subarrays is [9] with length 1, and [6,5] with length 2.
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [3,8,1,3,2,1,8,9,0], firstLen = 3, secondLen = 2
22+
23+
**Output:** 29
24+
25+
**Explanation:** One choice of subarrays is [3,8,1] with length 3, and [8,9] with length 2.
26+
27+
**Example 3:**
28+
29+
**Input:** nums = [2,1,5,6,0,9,5,0,3,8], firstLen = 4, secondLen = 3
30+
31+
**Output:** 31
32+
33+
**Explanation:** One choice of subarrays is [5,6,0,9] with length 4, and [3,8] with length 3.
34+
35+
**Constraints:**
36+
37+
* `1 <= firstLen, secondLen <= 1000`
38+
* `2 <= firstLen + secondLen <= 1000`
39+
* `firstLen + secondLen <= nums.length <= 1000`
40+
* `0 <= nums[i] <= 1000`
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package g1001_1100.s1032_stream_of_characters;
2+
3+
// #Hard #Array #String #Design #Trie #Data_Stream
4+
// #2022_02_27_Time_137_ms_(81.54%)_Space_148.2_MB_(53.37%)
5+
6+
public class StreamChecker {
7+
static class Node {
8+
Node[] child;
9+
boolean isEnd;
10+
11+
Node() {
12+
child = new Node[26];
13+
}
14+
}
15+
16+
private final StringBuilder sb;
17+
private final Node root;
18+
19+
public void insert(String s) {
20+
Node curr = root;
21+
for (int i = s.length() - 1; i >= 0; i--) {
22+
char c = s.charAt(i);
23+
if (curr.child[c - 'a'] == null) {
24+
curr.child[c - 'a'] = new Node();
25+
}
26+
curr = curr.child[c - 'a'];
27+
}
28+
curr.isEnd = true;
29+
}
30+
31+
public StreamChecker(String[] words) {
32+
root = new Node();
33+
sb = new StringBuilder();
34+
for (String s : words) {
35+
insert(s);
36+
}
37+
}
38+
39+
public boolean query(char letter) {
40+
sb.append(letter);
41+
Node curr = root;
42+
for (int i = sb.length() - 1; i >= 0; i--) {
43+
char c = sb.charAt(i);
44+
if (curr.child[c - 'a'] == null) {
45+
return false;
46+
}
47+
if (curr.child[c - 'a'].isEnd) {
48+
return true;
49+
}
50+
curr = curr.child[c - 'a'];
51+
}
52+
return false;
53+
}
54+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
1032\. Stream of Characters
2+
3+
Hard
4+
5+
Design an algorithm that accepts a stream of characters and checks if a suffix of these characters is a string of a given array of strings `words`.
6+
7+
For example, if `words = ["abc", "xyz"]` and the stream added the four characters (one by one) `'a'`, `'x'`, `'y'`, and `'z'`, your algorithm should detect that the suffix `"xyz"` of the characters `"axyz"` matches `"xyz"` from `words`.
8+
9+
Implement the `StreamChecker` class:
10+
11+
* `StreamChecker(String[] words)` Initializes the object with the strings array `words`.
12+
* `boolean query(char letter)` Accepts a new character from the stream and returns `true` if any non-empty suffix from the stream forms a word that is in `words`.
13+
14+
**Example 1:**
15+
16+
**Input** ["StreamChecker", "query", "query", "query", "query", "query", "query", "query", "query", "query", "query", "query", "query"] [[["cd", "f", "kl"]], ["a"], ["b"], ["c"], ["d"], ["e"], ["f"], ["g"], ["h"], ["i"], ["j"], ["k"], ["l"]]
17+
18+
**Output:** [null, false, false, false, true, false, true, false, false, false, false, false, true]
19+
20+
**Explanation:**
21+
22+
StreamChecker streamChecker = new StreamChecker(["cd", "f", "kl"]);
23+
streamChecker.query("a"); // return False
24+
streamChecker.query("b"); // return False
25+
streamChecker.query("c"); // return False
26+
streamChecker.query("d"); // return True, because 'cd' is in the wordlist
27+
streamChecker.query("e"); // return False
28+
streamChecker.query("f"); // return True, because 'f' is in the wordlist
29+
streamChecker.query("g"); // return False
30+
streamChecker.query("h"); // return False
31+
streamChecker.query("i"); // return False
32+
streamChecker.query("j"); // return False
33+
streamChecker.query("k"); // return False
34+
streamChecker.query("l"); // return True, because 'kl' is in the wordlist
35+
36+
**Constraints:**
37+
38+
* `1 <= words.length <= 2000`
39+
* `1 <= words[i].length <= 2000`
40+
* `words[i]` consists of lowercase English letters.
41+
* `letter` is a lowercase English letter.
42+
* At most <code>4 * 10<sup>4</sup></code> calls will be made to query.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package g1001_1100.s1033_moving_stones_until_consecutive;
2+
3+
// #Medium #Math #Brainteaser #2022_02_27_Time_1_ms_(86.36%)_Space_42.7_MB_(6.82%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
private int minMoves(int x, int y, int z) {
9+
if (x + 1 == y && y + 1 == z) {
10+
return 0;
11+
}
12+
if (y - x <= 2 || z - y <= 2) {
13+
return 1;
14+
}
15+
16+
return 2;
17+
}
18+
19+
private int maxMoves(int x, int z) {
20+
return z - x - 2;
21+
}
22+
23+
public int[] numMovesStones(int a, int b, int c) {
24+
int[] t = {a, b, c};
25+
Arrays.sort(t);
26+
27+
int min = minMoves(t[0], t[1], t[2]);
28+
int max = maxMoves(t[0], t[2]);
29+
30+
return new int[] {min, max};
31+
}
32+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
1033\. Moving Stones Until Consecutive
2+
3+
Medium
4+
5+
There are three stones in different positions on the X-axis. You are given three integers `a`, `b`, and `c`, the positions of the stones.
6+
7+
In one move, you pick up a stone at an endpoint (i.e., either the lowest or highest position stone), and move it to an unoccupied position between those endpoints. Formally, let's say the stones are currently at positions `x`, `y`, and `z` with `x < y < z`. You pick up the stone at either position `x` or position `z`, and move that stone to an integer position `k`, with `x < k < z` and `k != y`.
8+
9+
The game ends when you cannot make any more moves (i.e., the stones are in three consecutive positions).
10+
11+
Return _an integer array_ `answer` _of length_ `2` _where_:
12+
13+
* `answer[0]` _is the minimum number of moves you can play, and_
14+
* `answer[1]` _is the maximum number of moves you can play_.
15+
16+
**Example 1:**
17+
18+
**Input:** a = 1, b = 2, c = 5
19+
20+
**Output:** [1,2]
21+
22+
**Explanation:** Move the stone from 5 to 3, or move the stone from 5 to 4 to 3.
23+
24+
**Example 2:**
25+
26+
**Input:** a = 4, b = 3, c = 2
27+
28+
**Output:** [0,0]
29+
30+
**Explanation:** We cannot make any moves.
31+
32+
**Example 3:**
33+
34+
**Input:** a = 3, b = 5, c = 1
35+
36+
**Output:** [1,2]
37+
38+
**Explanation:** Move the stone from 1 to 4; or move the stone from 1 to 2 to 4.
39+
40+
**Constraints:**
41+
42+
* `1 <= a, b, c <= 100`
43+
* `a`, `b`, and `c` have different values.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package g1001_1100.s1034_coloring_a_border;
2+
3+
// #Medium #Array #Depth_First_Search #Breadth_First_Search #Matrix
4+
// #2022_02_27_Time_11_ms_(5.70%)_Space_55.1_MB_(9.60%)
5+
// #2022_02_27_Time_1_ms_(93.85%)_Space_55_MB_(9.60%)
6+
// #2022_02_27_Time_1_ms_(93.85%)_Space_55.3_MB_(9.60%)
7+
8+
public class Solution {
9+
public int[][] colorBorder(int[][] grid, int row, int col, int color) {
10+
getComp(grid, row, col, color, grid[row][col]);
11+
for (int i = 0; i < grid.length; i++) {
12+
for (int j = 0; j < grid[0].length; j++) {
13+
if (grid[i][j] < 0) {
14+
grid[i][j] = color;
15+
}
16+
}
17+
}
18+
return grid;
19+
}
20+
21+
private int getComp(int[][] grid, int r, int c, int color, int stColor) {
22+
if (r < 0
23+
|| c < 0
24+
|| r >= grid.length
25+
|| c >= grid[0].length
26+
|| Math.abs(grid[r][c]) != stColor) {
27+
return 0;
28+
}
29+
30+
if (grid[r][c] == -stColor) {
31+
return 1;
32+
}
33+
34+
grid[r][c] = -grid[r][c];
35+
36+
int count = 0;
37+
count += getComp(grid, r - 1, c, color, stColor);
38+
count += getComp(grid, r + 1, c, color, stColor);
39+
count += getComp(grid, r, c - 1, color, stColor);
40+
count += getComp(grid, r, c + 1, color, stColor);
41+
42+
if (count == 4) {
43+
grid[r][c] = -grid[r][c];
44+
}
45+
46+
return 1;
47+
}
48+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
1034\. Coloring A Border
2+
3+
Medium
4+
5+
You are given an `m x n` integer matrix `grid`, and three integers `row`, `col`, and `color`. Each value in the grid represents the color of the grid square at that location.
6+
7+
Two squares belong to the same **connected component** if they have the same color and are next to each other in any of the 4 directions.
8+
9+
The **border of a connected component** is all the squares in the connected component that are either **4-directionally** adjacent to a square not in the component, or on the boundary of the grid (the first or last row or column).
10+
11+
You should color the **border** of the **connected component** that contains the square `grid[row][col]` with `color`.
12+
13+
Return _the final grid_.
14+
15+
**Example 1:**
16+
17+
**Input:** grid = [[1,1],[1,2]], row = 0, col = 0, color = 3
18+
19+
**Output:** [[3,3],[3,2]]
20+
21+
**Example 2:**
22+
23+
**Input:** grid = [[1,2,2],[2,3,2]], row = 0, col = 1, color = 3
24+
25+
**Output:** [[1,3,3],[2,3,3]]
26+
27+
**Example 3:**
28+
29+
**Input:** grid = [[1,1,1],[1,1,1],[1,1,1]], row = 1, col = 1, color = 2
30+
31+
**Output:** [[2,2,2],[2,1,2],[2,2,2]]
32+
33+
**Constraints:**
34+
35+
* `m == grid.length`
36+
* `n == grid[i].length`
37+
* `1 <= m, n <= 50`
38+
* `1 <= grid[i][j], color <= 1000`
39+
* `0 <= row < m`
40+
* `0 <= col < n`
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g1001_1100.s1035_uncrossed_lines;
2+
3+
// #Medium #Array #Dynamic_Programming #2022_02_27_Time_5_ms_(85.32%)_Space_45.3_MB_(5.21%)
4+
5+
public class Solution {
6+
public int maxUncrossedLines(int[] nums1, int[] nums2) {
7+
int[] dp = new int[nums2.length + 1];
8+
for (int i = 1; i <= nums1.length; i++) {
9+
int[] dpRow = new int[nums2.length + 1];
10+
for (int j = 1; j <= nums2.length; j++) {
11+
if (nums1[i - 1] == nums2[j - 1]) {
12+
dpRow[j] = dp[j - 1] + 1;
13+
} else {
14+
dpRow[j] = Math.max(dp[j], dpRow[j - 1]);
15+
}
16+
}
17+
dp = dpRow;
18+
}
19+
20+
return dp[nums2.length];
21+
}
22+
}

0 commit comments

Comments
 (0)