Skip to content

Added tasks 3330-3337 #1840

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package g3301_3400.s3330_find_the_original_typed_string_i;

// #Easy #String #2024_10_29_Time_1_ms_(96.13%)_Space_42_MB_(72.46%)

public class Solution {
public int possibleStringCount(String word) {
int n = word.length();
int count = 1;
char pre = word.charAt(0);
int temp = 0;
for (int i = 1; i < n; i++) {
char ch = word.charAt(i);
if (ch == pre) {
temp++;
} else {
if (temp >= 1) {
count += temp;
}
temp = 0;
pre = ch;
}
}
if (temp >= 1) {
count += temp;
}
return count;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
3330\. Find the Original Typed String I

Easy

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.

Although Alice tried to focus on her typing, she is aware that she may still have done this **at most** _once_.

You are given a string `word`, which represents the **final** output displayed on Alice's screen.

Return the total number of _possible_ original strings that Alice _might_ have intended to type.

**Example 1:**

**Input:** word = "abbcccc"

**Output:** 5

**Explanation:**

The possible strings are: `"abbcccc"`, `"abbccc"`, `"abbcc"`, `"abbc"`, and `"abcccc"`.

**Example 2:**

**Input:** word = "abcd"

**Output:** 1

**Explanation:**

The only possible string is `"abcd"`.

**Example 3:**

**Input:** word = "aaaa"

**Output:** 4

**Constraints:**

* `1 <= word.length <= 100`
* `word` consists only of lowercase English letters.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package g3301_3400.s3331_find_subtree_sizes_after_changes;

// #Medium #Array #String #Hash_Table #Tree #Depth_First_Search
// #2024_10_29_Time_166_ms_(52.73%)_Space_86.3_MB_(8.86%)

import java.util.ArrayList;
import java.util.HashMap;

public class Solution {
private int[] finalAns;

public int[] findSubtreeSizes(int[] parent, String s) {
int n = parent.length;
char[] arr = s.toCharArray();
int[] newParent = new int[n];
finalAns = new int[n];
HashMap<Integer, ArrayList<Integer>> tree = new HashMap<>();

for (int i = 1; i < n; i++) {
int parentNode = parent[i];
newParent[i] = parentNode;
while (parentNode != -1) {
if (arr[parentNode] == arr[i]) {
newParent[i] = parentNode;
break;
}
parentNode = parent[parentNode];
}
}

for (int i = 1; i < n; i++) {
if (!tree.containsKey(newParent[i])) {
tree.put(newParent[i], new ArrayList<>());
}

tree.get(newParent[i]).add(i);
}

findNodes(0, tree);
return finalAns;
}

private int findNodes(int parent, HashMap<Integer, ArrayList<Integer>> tree) {
int count = 1;
if (tree.containsKey(parent)) {
for (int i : tree.get(parent)) {
count += findNodes(i, tree);
}
}
finalAns[parent] = count;
return count;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
3331\. Find Subtree Sizes After Changes

Medium

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`.

You are also given a string `s` of length `n`, where `s[i]` is the character assigned to node `i`.

We make the following changes on the tree **one** time **simultaneously** for all nodes `x` from `1` to `n - 1`:

* Find the **closest** node `y` to node `x` such that `y` is an ancestor of `x`, and `s[x] == s[y]`.
* If node `y` does not exist, do nothing.
* 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.

Return an array `answer` of size `n` where `answer[i]` is the **size** of the subtree rooted at node `i` in the **final** tree.

A **subtree** of `treeName` is a tree consisting of a node in `treeName` and all of its descendants.

**Example 1:**

**Input:** parent = [-1,0,0,1,1,1], s = "abaabc"

**Output:** [6,3,1,1,1,1]

**Explanation:**

![](https://assets.leetcode.com/uploads/2024/08/15/graphex1drawio.png)

The parent of node 3 will change from node 1 to node 0.

**Example 2:**

**Input:** parent = [-1,0,4,0,1], s = "abbba"

**Output:** [5,2,1,1,1]

**Explanation:**

![](https://assets.leetcode.com/uploads/2024/08/20/exgraph2drawio.png)

The following changes will happen at the same time:

* The parent of node 4 will change from node 1 to node 0.
* The parent of node 2 will change from node 4 to node 1.

**Constraints:**

* `n == parent.length == s.length`
* <code>1 <= n <= 10<sup>5</sup></code>
* `0 <= parent[i] <= n - 1` for all `i >= 1`.
* `parent[0] == -1`
* `parent` represents a valid tree.
* `s` consists only of lowercase English letters.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package g3301_3400.s3332_maximum_points_tourist_can_earn;

// #Medium #Array #Dynamic_Programming #Matrix #2024_10_29_Time_53_ms_(100.00%)_Space_55_MB_(78.55%)

public class Solution {
public int maxScore(int n, int k, int[][] stayScores, int[][] travelScores) {
// dp[day][city]
int[][] dp = new int[k + 1][n];
int result = 0;
for (int day = k - 1; day >= 0; day--) {
for (int city = 0; city < n; city++) {
int stayScore = stayScores[day][city] + dp[day + 1][city];
int travelScore = 0;
for (int nextCity = 0; nextCity < n; nextCity++) {
int nextScore = travelScores[city][nextCity] + dp[day + 1][nextCity];
travelScore = Math.max(nextScore, travelScore);
}
dp[day][city] = Math.max(stayScore, travelScore);
result = Math.max(dp[day][city], result);
}
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
3332\. Maximum Points Tourist Can Earn

Medium

You are given two integers, `n` and `k`, along with two 2D integer arrays, `stayScore` and `travelScore`.

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.

Each day, the tourist has two choices:

* **Stay in the current city**: If the tourist stays in their current city `curr` during day `i`, they will earn `stayScore[i][curr]` points.
* **Move to another city**: If the tourist moves from their current city `curr` to city `dest`, they will earn `travelScore[curr][dest]` points.

Return the **maximum** possible points the tourist can earn.

**Example 1:**

**Input:** n = 2, k = 1, stayScore = [[2,3]], travelScore = [[0,2],[1,0]]

**Output:** 3

**Explanation:**

The tourist earns the maximum number of points by starting in city 1 and staying in that city.

**Example 2:**

**Input:** n = 3, k = 2, stayScore = [[3,4,2],[2,1,2]], travelScore = [[0,2,1],[2,0,4],[3,2,0]]

**Output:** 8

**Explanation:**

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.

**Constraints:**

* `1 <= n <= 200`
* `1 <= k <= 200`
* `n == travelScore.length == travelScore[i].length == stayScore[i].length`
* `k == stayScore.length`
* `1 <= stayScore[i][j] <= 100`
* `0 <= travelScore[i][j] <= 100`
* `travelScore[i][i] == 0`
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package g3301_3400.s3333_find_the_original_typed_string_ii;

// #Hard #String #Dynamic_Programming #Prefix_Sum
// #2024_10_29_Time_89_ms_(90.20%)_Space_55.6_MB_(40.38%)

import java.util.ArrayList;
import java.util.List;

public class Solution {
private static final long MOD = (long) 1e9 + 7;

public int possibleStringCount(String word, int k) {
List<Integer> list = new ArrayList<>();
int n = word.length();
int i = 0;
while (i < n) {
int j = i + 1;
while (j < n && word.charAt(j) == word.charAt(j - 1)) {
j++;
}
list.add(j - i);
i = j;
}
int m = list.size();
long[] power = new long[m];
power[m - 1] = list.get(m - 1);
for (i = m - 2; i >= 0; i--) {
power[i] = (power[i + 1] * list.get(i)) % MOD;
}
if (m >= k) {
return (int) power[0];
}
long[][] dp = new long[m][k - m + 1];
for (i = 0; i < k - m + 1; i++) {
if (list.get(m - 1) + i + m > k) {
dp[m - 1][i] = list.get(m - 1) - (long) (k - m - i);
}
}
for (i = m - 2; i >= 0; i--) {
long sum = (dp[i + 1][k - m] * list.get(i)) % MOD;
for (int j = k - m; j >= 0; j--) {
sum += dp[i + 1][j];
if (j + list.get(i) > k - m) {
sum = (sum - dp[i + 1][k - m] + MOD) % MOD;
} else {
sum = (sum - dp[i + 1][j + list.get(i)] + MOD) % MOD;
}
dp[i][j] = sum;
}
}
return (int) dp[0][0];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
3333\. Find the Original Typed String II

Hard

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.

You are given a string `word`, which represents the **final** output displayed on Alice's screen. You are also given a **positive** integer `k`.

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`.

Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.

**Example 1:**

**Input:** word = "aabbccdd", k = 7

**Output:** 5

**Explanation:**

The possible strings are: `"aabbccdd"`, `"aabbccd"`, `"aabbcdd"`, `"aabccdd"`, and `"abbccdd"`.

**Example 2:**

**Input:** word = "aabbccdd", k = 8

**Output:** 1

**Explanation:**

The only possible string is `"aabbccdd"`.

**Example 3:**

**Input:** word = "aaabbb", k = 3

**Output:** 8

**Constraints:**

* <code>1 <= word.length <= 5 * 10<sup>5</sup></code>
* `word` consists only of lowercase English letters.
* `1 <= k <= 2000`
Loading
Loading