Skip to content

Commit 5abefe0

Browse files
authored
Added tasks 3330-3337
1 parent 62708bc commit 5abefe0

File tree

24 files changed

+1037
-0
lines changed

24 files changed

+1037
-0
lines changed
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 #String #2024_10_29_Time_142_ms_(88.24%)_Space_34.7_MB_(70.59%)
4+
5+
class Solution {
6+
fun possibleStringCount(word: String): Int {
7+
val n = word.length
8+
var count = 1
9+
var pre = word[0]
10+
var temp = 0
11+
for (i in 1 until n) {
12+
val ch = word[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+
}
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.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package g3301_3400.s3331_find_subtree_sizes_after_changes
2+
3+
// #Medium #Array #String #Hash_Table #Tree #Depth_First_Search
4+
// #2024_10_29_Time_139_ms_(95.24%)_Space_82.2_MB_(19.05%)
5+
6+
class Solution {
7+
private lateinit var finalAns: IntArray
8+
9+
fun findSubtreeSizes(parent: IntArray, s: String): IntArray {
10+
val n = parent.size
11+
val arr = s.toCharArray()
12+
val newParent = IntArray(n)
13+
finalAns = IntArray(n)
14+
val tree = HashMap<Int, ArrayList<Int>>()
15+
16+
for (i in 1 until n) {
17+
var parentNode = parent[i]
18+
newParent[i] = parentNode
19+
while (parentNode != -1) {
20+
if (arr[parentNode] == arr[i]) {
21+
newParent[i] = parentNode
22+
break
23+
}
24+
parentNode = parent[parentNode]
25+
}
26+
}
27+
28+
for (i in 1 until n) {
29+
if (!tree.containsKey(newParent[i])) {
30+
tree.put(newParent[i], ArrayList<Int>())
31+
}
32+
33+
tree[newParent[i]]!!.add(i)
34+
}
35+
36+
findNodes(0, tree)
37+
return finalAns
38+
}
39+
40+
private fun findNodes(parent: Int, tree: HashMap<Int, ArrayList<Int>>): Int {
41+
var count = 1
42+
if (tree.containsKey(parent)) {
43+
for (i in tree[parent]!!) {
44+
count += findNodes(i, tree)
45+
}
46+
}
47+
finalAns[parent] = count
48+
return count
49+
}
50+
}
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.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g3301_3400.s3332_maximum_points_tourist_can_earn
2+
3+
// #Medium #Array #Dynamic_Programming #Matrix
4+
// #2024_10_29_Time_216_ms_(100.00%)_Space_64_MB_(78.95%)
5+
6+
import kotlin.math.max
7+
8+
class Solution {
9+
fun maxScore(n: Int, k: Int, stayScores: Array<IntArray>, travelScores: Array<IntArray>): Int {
10+
// dp[day][city]
11+
val dp = Array<IntArray?>(k + 1) { IntArray(n) }
12+
var result = 0
13+
for (day in k - 1 downTo 0) {
14+
for (city in 0 until n) {
15+
val stayScore = stayScores[day][city] + dp[day + 1]!![city]
16+
var travelScore = 0
17+
for (nextCity in 0 until n) {
18+
val nextScore = travelScores[city][nextCity] + dp[day + 1]!![nextCity]
19+
travelScore = max(nextScore, travelScore)
20+
}
21+
dp[day]!![city] = max(stayScore, travelScore)
22+
result = max(dp[day]!![city], result)
23+
}
24+
}
25+
return result
26+
}
27+
}
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`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package g3301_3400.s3333_find_the_original_typed_string_ii
2+
3+
// #Hard #String #Dynamic_Programming #Prefix_Sum
4+
// #2024_10_29_Time_490_ms_(100.00%)_Space_52.2_MB_(33.33%)
5+
6+
class Solution {
7+
fun possibleStringCount(word: String, k: Int): Int {
8+
val list: MutableList<Int> = ArrayList<Int>()
9+
val n = word.length
10+
var i = 0
11+
while (i < n) {
12+
var j = i + 1
13+
while (j < n && word[j] == word[j - 1]) {
14+
j++
15+
}
16+
list.add(j - i)
17+
i = j
18+
}
19+
val m = list.size
20+
val power = LongArray(m)
21+
power[m - 1] = list[m - 1].toLong()
22+
i = m - 2
23+
while (i >= 0) {
24+
power[i] = (power[i + 1] * list[i]) % MOD
25+
i--
26+
}
27+
if (m >= k) {
28+
return power[0].toInt()
29+
}
30+
val dp = Array<LongArray?>(m) { LongArray(k - m + 1) }
31+
i = 0
32+
while (i < k - m + 1) {
33+
if (list[m - 1] + i + m > k) {
34+
dp[m - 1]!![i] = list[m - 1] - (k - m - i).toLong()
35+
}
36+
i++
37+
}
38+
i = m - 2
39+
while (i >= 0) {
40+
var sum: Long = dp[i + 1]!![k - m] * list[i] % MOD
41+
for (j in k - m downTo 0) {
42+
sum += dp[i + 1]!![j]
43+
if (j + list[i] > k - m) {
44+
sum = (sum - dp[i + 1]!![k - m] + MOD) % MOD
45+
} else {
46+
sum = (sum - dp[i + 1]!![j + list[i]] + MOD) % MOD
47+
}
48+
dp[i]!![j] = sum
49+
}
50+
i--
51+
}
52+
return dp[0]!![0].toInt()
53+
}
54+
55+
companion object {
56+
private const val MOD = 1e9.toLong() + 7
57+
}
58+
}
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)