Skip to content

Commit 1d8ac2f

Browse files
committed
Added tasks 3330-3337
1 parent 411c6cd commit 1d8ac2f

File tree

24 files changed

+978
-0
lines changed

24 files changed

+978
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g3301_3400.s3330_find_the_original_typed_string_i;
2+
3+
// #Easy #2024_10_27_Time_1_ms_(100.00%)_Space_42_MB_(100.00%)
4+
5+
public class Solution {
6+
public int possibleStringCount(String word) {
7+
int n = word.length();
8+
int count = 1;
9+
char pre = word.charAt(0);
10+
int temp = 0;
11+
for (int i = 1; i < n; i++) {
12+
char ch = word.charAt(i);
13+
if (ch == pre) {
14+
temp++;
15+
} else {
16+
if (temp >= 1) {
17+
count += temp;
18+
}
19+
temp = 0;
20+
pre = ch;
21+
}
22+
}
23+
if (temp >= 1) {
24+
count += temp;
25+
}
26+
return count;
27+
}
28+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
3330\. Find the Original Typed String I
2+
3+
Easy
4+
5+
Alice is attempting to type a specific string on her computer. However, she tends to be clumsy and **may** press a key for too long, resulting in a character being typed **multiple** times.
6+
7+
Although Alice tried to focus on her typing, she is aware that she may still have done this **at most** _once_.
8+
9+
You are given a string `word`, which represents the **final** output displayed on Alice's screen.
10+
11+
Return the total number of _possible_ original strings that Alice _might_ have intended to type.
12+
13+
**Example 1:**
14+
15+
**Input:** word = "abbcccc"
16+
17+
**Output:** 5
18+
19+
**Explanation:**
20+
21+
The possible strings are: `"abbcccc"`, `"abbccc"`, `"abbcc"`, `"abbc"`, and `"abcccc"`.
22+
23+
**Example 2:**
24+
25+
**Input:** word = "abcd"
26+
27+
**Output:** 1
28+
29+
**Explanation:**
30+
31+
The only possible string is `"abcd"`.
32+
33+
**Example 3:**
34+
35+
**Input:** word = "aaaa"
36+
37+
**Output:** 4
38+
39+
**Constraints:**
40+
41+
* `1 <= word.length <= 100`
42+
* `word` consists only of lowercase English letters.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package g3301_3400.s3331_find_subtree_sizes_after_changes;
2+
3+
// #Medium #2024_10_27_Time_129_ms_(100.00%)_Space_58.4_MB_(100.00%)
4+
5+
import java.util.ArrayList;
6+
import java.util.HashMap;
7+
8+
public class Solution {
9+
private int[] finalAns;
10+
11+
public int[] findSubtreeSizes(int[] parent, String s) {
12+
int n = parent.length;
13+
char[] arr = s.toCharArray();
14+
int[] newParent = new int[n];
15+
finalAns = new int[n];
16+
HashMap<Integer, ArrayList<Integer>> tree = new HashMap<>();
17+
18+
for (int i = 1; i < n; i++) {
19+
int parentNode = parent[i];
20+
newParent[i] = parentNode;
21+
while (parentNode != -1) {
22+
if (arr[parentNode] == arr[i]) {
23+
newParent[i] = parentNode;
24+
break;
25+
}
26+
parentNode = parent[parentNode];
27+
}
28+
}
29+
30+
for (int i = 1; i < n; i++) {
31+
if (!tree.containsKey(newParent[i])) {
32+
tree.put(newParent[i], new ArrayList<>());
33+
}
34+
35+
tree.get(newParent[i]).add(i);
36+
}
37+
38+
findNodes(0, tree);
39+
return finalAns;
40+
}
41+
42+
private int findNodes(int parent, HashMap<Integer, ArrayList<Integer>> tree) {
43+
int count = 1;
44+
if (tree.containsKey(parent)) {
45+
for (int i : tree.get(parent)) {
46+
count += findNodes(i, tree);
47+
}
48+
}
49+
finalAns[parent] = count;
50+
return count;
51+
}
52+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
3331\. Find Subtree Sizes After Changes
2+
3+
Medium
4+
5+
You are given a tree rooted at node 0 that consists of `n` nodes numbered from `0` to `n - 1`. The tree is represented by an array `parent` of size `n`, where `parent[i]` is the parent of node `i`. Since node 0 is the root, `parent[0] == -1`.
6+
7+
You are also given a string `s` of length `n`, where `s[i]` is the character assigned to node `i`.
8+
9+
We make the following changes on the tree **one** time **simultaneously** for all nodes `x` from `1` to `n - 1`:
10+
11+
* Find the **closest** node `y` to node `x` such that `y` is an ancestor of `x`, and `s[x] == s[y]`.
12+
* If node `y` does not exist, do nothing.
13+
* Otherwise, **remove** the edge between `x` and its current parent and make node `y` the new parent of `x` by adding an edge between them.
14+
15+
Return an array `answer` of size `n` where `answer[i]` is the **size** of the subtree rooted at node `i` in the **final** tree.
16+
17+
A **subtree** of `treeName` is a tree consisting of a node in `treeName` and all of its descendants.
18+
19+
**Example 1:**
20+
21+
**Input:** parent = [-1,0,0,1,1,1], s = "abaabc"
22+
23+
**Output:** [6,3,1,1,1,1]
24+
25+
**Explanation:**
26+
27+
![](https://assets.leetcode.com/uploads/2024/08/15/graphex1drawio.png)
28+
29+
The parent of node 3 will change from node 1 to node 0.
30+
31+
**Example 2:**
32+
33+
**Input:** parent = [-1,0,4,0,1], s = "abbba"
34+
35+
**Output:** [5,2,1,1,1]
36+
37+
**Explanation:**
38+
39+
![](https://assets.leetcode.com/uploads/2024/08/20/exgraph2drawio.png)
40+
41+
The following changes will happen at the same time:
42+
43+
* The parent of node 4 will change from node 1 to node 0.
44+
* The parent of node 2 will change from node 4 to node 1.
45+
46+
**Constraints:**
47+
48+
* `n == parent.length == s.length`
49+
* <code>1 <= n <= 10<sup>5</sup></code>
50+
* `0 <= parent[i] <= n - 1` for all `i >= 1`.
51+
* `parent[0] == -1`
52+
* `parent` represents a valid tree.
53+
* `s` consists only of lowercase English letters.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package g3301_3400.s3332_maximum_points_tourist_can_earn;
2+
3+
// #Medium #2024_10_27_Time_305_ms_(100.00%)_Space_55.1_MB_(100.00%)
4+
5+
public class Solution {
6+
int days;
7+
int cities;
8+
Integer[][] dp;
9+
10+
private int f(int day, int city, int[][] stayScore, int[][] travelScore) {
11+
if (day == days) {
12+
return 0;
13+
}
14+
if (dp[day][city] != null) {
15+
return dp[day][city];
16+
}
17+
int maxScore = 0;
18+
for (int desCity = 0; desCity < cities; desCity++) {
19+
int score;
20+
if (desCity == city) {
21+
score = stayScore[day][city];
22+
} else {
23+
score = travelScore[city][desCity];
24+
}
25+
maxScore = Math.max(maxScore, score + f(day + 1, desCity, stayScore, travelScore));
26+
}
27+
return dp[day][city] = maxScore;
28+
}
29+
30+
public int maxScore(int n, int k, int[][] stayScore, int[][] travelScore) {
31+
days = k;
32+
cities = n;
33+
int maxScore = 0;
34+
dp = new Integer[days + 1][cities + 1];
35+
for (int city = 0; city < cities; city++) {
36+
maxScore = Math.max(maxScore, f(0, city, stayScore, travelScore));
37+
}
38+
return maxScore;
39+
}
40+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
3332\. Maximum Points Tourist Can Earn
2+
3+
Medium
4+
5+
You are given two integers, `n` and `k`, along with two 2D integer arrays, `stayScore` and `travelScore`.
6+
7+
A tourist is visiting a country with `n` cities, where each city is **directly** connected to every other city. The tourist's journey consists of **exactly** `k` **0-indexed** days, and they can choose **any** city as their starting point.
8+
9+
Each day, the tourist has two choices:
10+
11+
* **Stay in the current city**: If the tourist stays in their current city `curr` during day `i`, they will earn `stayScore[i][curr]` points.
12+
* **Move to another city**: If the tourist moves from their current city `curr` to city `dest`, they will earn `travelScore[curr][dest]` points.
13+
14+
Return the **maximum** possible points the tourist can earn.
15+
16+
**Example 1:**
17+
18+
**Input:** n = 2, k = 1, stayScore = [[2,3]], travelScore = [[0,2],[1,0]]
19+
20+
**Output:** 3
21+
22+
**Explanation:**
23+
24+
The tourist earns the maximum number of points by starting in city 1 and staying in that city.
25+
26+
**Example 2:**
27+
28+
**Input:** n = 3, k = 2, stayScore = [[3,4,2],[2,1,2]], travelScore = [[0,2,1],[2,0,4],[3,2,0]]
29+
30+
**Output:** 8
31+
32+
**Explanation:**
33+
34+
The tourist earns the maximum number of points by starting in city 1, staying in that city on day 0, and traveling to city 2 on day 1.
35+
36+
**Constraints:**
37+
38+
* `1 <= n <= 200`
39+
* `1 <= k <= 200`
40+
* `n == travelScore.length == travelScore[i].length == stayScore[i].length`
41+
* `k == stayScore.length`
42+
* `1 <= stayScore[i][j] <= 100`
43+
* `0 <= travelScore[i][j] <= 100`
44+
* `travelScore[i][i] == 0`
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package g3301_3400.s3333_find_the_original_typed_string_ii;
2+
3+
// #Hard #2024_10_27_Time_89_ms_(50.00%)_Space_58.4_MB_(100.00%)
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class Solution {
9+
private static final long MOD = (long) 1e9 + 7;
10+
11+
public int possibleStringCount(String word, int k) {
12+
List<Integer> list = new ArrayList<>();
13+
int n = word.length();
14+
int i = 0;
15+
while (i < n) {
16+
int j = i + 1;
17+
while (j < n && word.charAt(j) == word.charAt(j - 1)) {
18+
j++;
19+
}
20+
list.add(j - i);
21+
i = j;
22+
}
23+
int m = list.size();
24+
long[] power = new long[m];
25+
power[m - 1] = list.get(m - 1);
26+
for (i = m - 2; i >= 0; i--) {
27+
power[i] = (power[i + 1] * list.get(i)) % MOD;
28+
}
29+
if (m >= k) {
30+
return (int) power[0];
31+
}
32+
long[][] dp = new long[m][k - m + 1];
33+
for (i = 0; i < k - m + 1; i++) {
34+
if (list.get(m - 1) + i + m > k) {
35+
dp[m - 1][i] = list.get(m - 1) - (k - m - i);
36+
}
37+
}
38+
for (i = m - 2; i >= 0; i--) {
39+
long sum = (dp[i + 1][k - m] * list.get(i)) % MOD;
40+
for (int j = k - m; j >= 0; j--) {
41+
sum += dp[i + 1][j];
42+
if (j + list.get(i) > k - m) {
43+
sum = (sum - dp[i + 1][k - m] + MOD) % MOD;
44+
} else {
45+
sum = (sum - dp[i + 1][j + list.get(i)] + MOD) % MOD;
46+
}
47+
dp[i][j] = sum;
48+
}
49+
}
50+
return (int) dp[0][0];
51+
}
52+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
3333\. Find the Original Typed String II
2+
3+
Hard
4+
5+
Alice is attempting to type a specific string on her computer. However, she tends to be clumsy and **may** press a key for too long, resulting in a character being typed **multiple** times.
6+
7+
You are given a string `word`, which represents the **final** output displayed on Alice's screen. You are also given a **positive** integer `k`.
8+
9+
Return the total number of _possible_ original strings that Alice _might_ have intended to type, if she was trying to type a string of size **at least** `k`.
10+
11+
Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
12+
13+
**Example 1:**
14+
15+
**Input:** word = "aabbccdd", k = 7
16+
17+
**Output:** 5
18+
19+
**Explanation:**
20+
21+
The possible strings are: `"aabbccdd"`, `"aabbccd"`, `"aabbcdd"`, `"aabccdd"`, and `"abbccdd"`.
22+
23+
**Example 2:**
24+
25+
**Input:** word = "aabbccdd", k = 8
26+
27+
**Output:** 1
28+
29+
**Explanation:**
30+
31+
The only possible string is `"aabbccdd"`.
32+
33+
**Example 3:**
34+
35+
**Input:** word = "aaabbb", k = 3
36+
37+
**Output:** 8
38+
39+
**Constraints:**
40+
41+
* <code>1 <= word.length <= 5 * 10<sup>5</sup></code>
42+
* `word` consists only of lowercase English letters.
43+
* `1 <= k <= 2000`

0 commit comments

Comments
 (0)