Skip to content

Commit 2f462a7

Browse files
authored
Added tasks 1958, 1959, 1960, 1961, 1962.
1 parent b06d8c3 commit 2f462a7

File tree

15 files changed

+549
-0
lines changed

15 files changed

+549
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package g1901_2000.s1958_check_if_move_is_legal;
2+
3+
// #Medium #Array #Matrix #Enumeration #2022_05_20_Time_0_ms_(100.00%)_Space_43.9_MB_(11.63%)
4+
5+
public class Solution {
6+
7+
private static final int[][] DIRS =
8+
new int[][] {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};
9+
10+
public boolean checkMove(char[][] board, int rMove, int cMove, char color) {
11+
char opposite = (color == 'W' ? 'B' : (color == 'B' ? 'W' : ' '));
12+
if (opposite == ' ' || !find(board, rMove, cMove, '.')) {
13+
return false;
14+
}
15+
for (int[] dir : DIRS) {
16+
int rNext = rMove + dir[0];
17+
int cNext = cMove + dir[1];
18+
if (find(board, rNext, cNext, opposite)) {
19+
rNext += dir[0];
20+
cNext += dir[1];
21+
while (find(board, rNext, cNext, opposite)) {
22+
rNext += dir[0];
23+
cNext += dir[1];
24+
}
25+
if (find(board, rNext, cNext, color)) {
26+
return true;
27+
}
28+
}
29+
}
30+
return false;
31+
}
32+
33+
private boolean find(char[][] board, final int r, final int c, final char target) {
34+
return (r >= 0
35+
&& r < board.length
36+
&& c >= 0
37+
&& c < board[r].length
38+
&& board[r][c] == target);
39+
}
40+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
1958\. Check if Move is Legal
2+
3+
Medium
4+
5+
You are given a **0-indexed** `8 x 8` grid `board`, where `board[r][c]` represents the cell `(r, c)` on a game board. On the board, free cells are represented by `'.'`, white cells are represented by `'W'`, and black cells are represented by `'B'`.
6+
7+
Each move in this game consists of choosing a free cell and changing it to the color you are playing as (either white or black). However, a move is only **legal** if, after changing it, the cell becomes the **endpoint of a good line** (horizontal, vertical, or diagonal).
8+
9+
A **good line** is a line of **three or more cells (including the endpoints)** where the endpoints of the line are **one color**, and the remaining cells in the middle are the **opposite color** (no cells in the line are free). You can find examples for good lines in the figure below:
10+
11+
![](https://assets.leetcode.com/uploads/2021/07/22/goodlines5.png)
12+
13+
Given two integers `rMove` and `cMove` and a character `color` representing the color you are playing as (white or black), return `true` _if changing cell_ `(rMove, cMove)` _to color_ `color` _is a **legal** move, or_ `false` _if it is not legal_.
14+
15+
**Example 1:**
16+
17+
![](https://assets.leetcode.com/uploads/2021/07/10/grid11.png)
18+
19+
**Input:** board = [[".",".",".","B",".",".",".","."],[".",".",".","W",".",".",".","."],[".",".",".","W",".",".",".","."],[".",".",".","W",".",".",".","."],["W","B","B",".","W","W","W","B"],[".",".",".","B",".",".",".","."],[".",".",".","B",".",".",".","."],[".",".",".","W",".",".",".","."]], rMove = 4, cMove = 3, color = "B"
20+
21+
**Output:** true
22+
23+
**Explanation:** '.', 'W', and 'B' are represented by the colors blue, white, and black respectively, and cell (rMove, cMove) is marked with an 'X'. The two good lines with the chosen cell as an endpoint are annotated above with the red rectangles.
24+
25+
**Example 2:**
26+
27+
![](https://assets.leetcode.com/uploads/2021/07/10/grid2.png)
28+
29+
**Input:** board = [[".",".",".",".",".",".",".","."],[".","B",".",".","W",".",".","."],[".",".","W",".",".",".",".","."],[".",".",".","W","B",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".","B","W",".","."],[".",".",".",".",".",".","W","."],[".",".",".",".",".",".",".","B"]], rMove = 4, cMove = 4, color = "W"
30+
31+
**Output:** false
32+
33+
**Explanation:** While there are good lines with the chosen cell as a middle cell, there are no good lines with the chosen cell as an endpoint.
34+
35+
**Constraints:**
36+
37+
* `board.length == board[r].length == 8`
38+
* `0 <= rMove, cMove < 8`
39+
* `board[rMove][cMove] == '.'`
40+
* `color` is either `'B'` or `'W'`.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package g1901_2000.s1959_minimum_total_space_wasted_with_k_resizing_operations;
2+
3+
// #Medium #Array #Dynamic_Programming #2022_05_20_Time_42_ms_(95.45%)_Space_43_MB_(66.67%)
4+
5+
public class Solution {
6+
public int minSpaceWastedKResizing(int[] arr, int k) {
7+
int n = arr.length;
8+
k++;
9+
int[][] dp = new int[n][k + 1];
10+
11+
for (int j = 1; j <= k; j++) {
12+
for (int i = n - 1; i >= 0; i--) {
13+
int ele = n - i;
14+
if (j == ele) {
15+
dp[i][j] = 0;
16+
continue;
17+
}
18+
19+
if (j == 1) {
20+
int sum = 0;
21+
int maxEle = -1;
22+
for (int l = i; l < n; l++) {
23+
maxEle = Math.max(maxEle, arr[l]);
24+
sum += arr[l];
25+
}
26+
27+
dp[i][j] = (maxEle * (n - i)) - sum;
28+
continue;
29+
}
30+
int maxEle = -1;
31+
int sum = 0;
32+
int ans = Integer.MAX_VALUE;
33+
for (int cut = i; cut <= n - j; cut++) {
34+
maxEle = Math.max(maxEle, arr[cut]);
35+
sum += arr[cut];
36+
int recAns = dp[cut + 1][j - 1];
37+
int myAns = (maxEle * (cut - i + 1)) - (sum);
38+
ans = Math.min(ans, recAns + myAns);
39+
}
40+
41+
dp[i][j] = ans;
42+
}
43+
}
44+
45+
return dp[0][k];
46+
}
47+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
1959\. Minimum Total Space Wasted With K Resizing Operations
2+
3+
Medium
4+
5+
You are currently designing a dynamic array. You are given a **0-indexed** integer array `nums`, where `nums[i]` is the number of elements that will be in the array at time `i`. In addition, you are given an integer `k`, the **maximum** number of times you can **resize** the array (to **any** size).
6+
7+
The size of the array at time `t`, <code>size<sub>t</sub></code>, must be at least `nums[t]` because there needs to be enough space in the array to hold all the elements. The **space wasted** at time `t` is defined as <code>size<sub>t</sub> - nums[t]</code>, and the **total** space wasted is the **sum** of the space wasted across every time `t` where `0 <= t < nums.length`.
8+
9+
Return _the **minimum** **total space wasted** if you can resize the array at most_ `k` _times_.
10+
11+
**Note:** The array can have **any size** at the start and does **not** count towards the number of resizing operations.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [10,20], k = 0
16+
17+
**Output:** 10
18+
19+
**Explanation:** size = [20,20].
20+
21+
We can set the initial size to be 20.
22+
23+
The total wasted space is (20 - 10) + (20 - 20) = 10.
24+
25+
**Example 2:**
26+
27+
**Input:** nums = [10,20,30], k = 1
28+
29+
**Output:** 10
30+
31+
**Explanation:** size = [20,20,30].
32+
33+
We can set the initial size to be 20 and resize to 30 at time 2.
34+
35+
The total wasted space is (20 - 10) + (20 - 20) + (30 - 30) = 10.
36+
37+
**Example 3:**
38+
39+
**Input:** nums = [10,20,15,30,20], k = 2
40+
41+
**Output:** 15
42+
43+
**Explanation:** size = [10,20,20,30,30].
44+
45+
We can set the initial size to 10, resize to 20 at time 1, and resize to 30 at time 3.
46+
47+
The total wasted space is (10 - 10) + (20 - 20) + (20 - 15) + (30 - 30) + (30 - 20) = 15.
48+
49+
**Constraints:**
50+
51+
* `1 <= nums.length <= 200`
52+
* <code>1 <= nums[i] <= 10<sup>6</sup></code>
53+
* `0 <= k <= nums.length - 1`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package g1901_2000.s1960_maximum_product_of_the_length_of_two_palindromic_substrings;
2+
3+
// #Hard #String #Hash_Function #Rolling_Hash #2022_05_20_Time_32_ms_(75.00%)_Space_44.7_MB_(91.67%)
4+
5+
public class Solution {
6+
7+
public long maxProduct(String s) {
8+
int n = s.length();
9+
if (n == 2) {
10+
return 1;
11+
}
12+
int[] len = manaCherS(s);
13+
14+
long[] left = new long[n];
15+
16+
int max = 1;
17+
left[0] = max;
18+
for (int i = 1; i <= n - 1; i++) {
19+
if (len[(i - max - 1 + i) / 2] > max) {
20+
max += 2;
21+
}
22+
left[i] = max;
23+
}
24+
max = 1;
25+
long[] right = new long[n];
26+
right[n - 1] = max;
27+
28+
for (int i = n - 2; i >= 0; i--) {
29+
if (len[(i + max + 1 + i) / 2] > max) {
30+
max += 2;
31+
}
32+
right[i] = max;
33+
}
34+
35+
long res = 1;
36+
37+
for (int i = 1; i < n; i++) {
38+
res = Math.max(res, left[i - 1] * right[i]);
39+
}
40+
return res;
41+
}
42+
43+
private int[] manaCherS(String s) {
44+
45+
int len = s.length();
46+
int[] p = new int[len];
47+
int c = 0;
48+
int r = 0;
49+
50+
for (int i = 0; i < len; i++) {
51+
int mirror = (2 * c) - i;
52+
if (i < r) {
53+
p[i] = Math.min(r - i, p[mirror]);
54+
}
55+
56+
int a = i + (1 + p[i]);
57+
int b = i - (1 + p[i]);
58+
while (a < len && b >= 0 && s.charAt(a) == s.charAt(b)) {
59+
p[i]++;
60+
a++;
61+
b--;
62+
}
63+
64+
if (i + p[i] > r) {
65+
c = i;
66+
r = i + p[i];
67+
}
68+
}
69+
for (int i = 0; i < len; i++) {
70+
p[i] = 1 + 2 * p[i];
71+
}
72+
return p;
73+
}
74+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
1960\. Maximum Product of the Length of Two Palindromic Substrings
2+
3+
Hard
4+
5+
You are given a **0-indexed** string `s` and are tasked with finding two **non-intersecting palindromic** substrings of **odd** length such that the product of their lengths is maximized.
6+
7+
More formally, you want to choose four integers `i`, `j`, `k`, `l` such that `0 <= i <= j < k <= l < s.length` and both the substrings `s[i...j]` and `s[k...l]` are palindromes and have odd lengths. `s[i...j]` denotes a substring from index `i` to index `j` **inclusive**.
8+
9+
Return _the **maximum** possible product of the lengths of the two non-intersecting palindromic substrings._
10+
11+
A **palindrome** is a string that is the same forward and backward. A **substring** is a contiguous sequence of characters in a string.
12+
13+
**Example 1:**
14+
15+
**Input:** s = "ababbb"
16+
17+
**Output:** 9
18+
19+
**Explanation:** Substrings "aba" and "bbb" are palindromes with odd length. product = 3 \* 3 = 9.
20+
21+
**Example 2:**
22+
23+
**Input:** s = "zaaaxbbby"
24+
25+
**Output:** 9
26+
27+
**Explanation:** Substrings "aaa" and "bbb" are palindromes with odd length. product = 3 \* 3 = 9.
28+
29+
**Constraints:**
30+
31+
* <code>2 <= s.length <= 10<sup>5</sup></code>
32+
* `s` consists of lowercase English letters.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package g1901_2000.s1961_check_if_string_is_a_prefix_of_array;
2+
3+
// #Easy #Array #String #2022_05_20_Time_2_ms_(60.87%)_Space_43.5_MB_(27.83%)
4+
5+
public class Solution {
6+
public boolean isPrefixString(String s, String[] words) {
7+
StringBuilder sb = new StringBuilder();
8+
for (String word : words) {
9+
sb.append(word);
10+
if (sb.toString().equals(s)) {
11+
return true;
12+
}
13+
}
14+
return false;
15+
}
16+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
1961\. Check If String Is a Prefix of Array
2+
3+
Easy
4+
5+
Given a string `s` and an array of strings `words`, determine whether `s` is a **prefix string** of `words`.
6+
7+
A string `s` is a **prefix string** of `words` if `s` can be made by concatenating the first `k` strings in `words` for some **positive** `k` no larger than `words.length`.
8+
9+
Return `true` _if_ `s` _is a **prefix string** of_ `words`_, or_ `false` _otherwise_.
10+
11+
**Example 1:**
12+
13+
**Input:** s = "iloveleetcode", words = ["i","love","leetcode","apples"]
14+
15+
**Output:** true
16+
17+
**Explanation:** s can be made by concatenating "i", "love", and "leetcode" together.
18+
19+
**Example 2:**
20+
21+
**Input:** s = "iloveleetcode", words = ["apples","i","love","leetcode"]
22+
23+
**Output:** false
24+
25+
**Explanation:** It is impossible to make s using a prefix of arr.
26+
27+
**Constraints:**
28+
29+
* `1 <= words.length <= 100`
30+
* `1 <= words[i].length <= 20`
31+
* `1 <= s.length <= 1000`
32+
* `words[i]` and `s` consist of only lowercase English letters.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g1901_2000.s1962_remove_stones_to_minimize_the_total;
2+
3+
// #Medium #Array #Heap_Priority_Queue #2022_05_20_Time_761_ms_(48.67%)_Space_124.8_MB_(24.78%)
4+
5+
import java.util.Collections;
6+
import java.util.PriorityQueue;
7+
8+
public class Solution {
9+
public int minStoneSum(int[] piles, int k) {
10+
PriorityQueue<Integer> descendingQueue = new PriorityQueue<>(Collections.reverseOrder());
11+
int sum = 0;
12+
int newValue;
13+
int currentValue;
14+
int half;
15+
for (int stones : piles) {
16+
sum += stones;
17+
descendingQueue.offer(stones);
18+
}
19+
while (k > 0) {
20+
currentValue = descendingQueue.poll();
21+
half = currentValue / 2;
22+
newValue = currentValue - half;
23+
descendingQueue.offer(newValue);
24+
sum -= half;
25+
k--;
26+
}
27+
return sum;
28+
}
29+
}

0 commit comments

Comments
 (0)