Skip to content

Commit 34adc7f

Browse files
committed
Added tasks 3300-3307
1 parent 8bee6c7 commit 34adc7f

File tree

24 files changed

+876
-0
lines changed

24 files changed

+876
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g3201_3300.s3300_minimum_element_after_replacement_with_digit_sum;
2+
3+
// #Easy #2024_09_30_Time_4_ms_(100.00%)_Space_43.3_MB_(100.00%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
public int minElement(int[] nums) {
9+
int n = nums.length;
10+
int[] arr = new int[n];
11+
12+
for (int i = 0; i < n; i++) {
13+
int sum = sumOfDigits(nums[i]);
14+
arr[i] = sum;
15+
}
16+
Arrays.sort(arr);
17+
return arr[0];
18+
}
19+
20+
private int sumOfDigits(int num) {
21+
int sum = 0;
22+
if (num <= 9) {
23+
return num;
24+
}
25+
while (num > 0) {
26+
sum += num % 10;
27+
num /= 10;
28+
}
29+
return sum;
30+
}
31+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
3300\. Minimum Element After Replacement With Digit Sum
2+
3+
Easy
4+
5+
You are given an integer array `nums`.
6+
7+
You replace each element in `nums` with the **sum** of its digits.
8+
9+
Return the **minimum** element in `nums` after all replacements.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [10,12,13,14]
14+
15+
**Output:** 1
16+
17+
**Explanation:**
18+
19+
`nums` becomes `[1, 3, 4, 5]` after all replacements, with minimum element 1.
20+
21+
**Example 2:**
22+
23+
**Input:** nums = [1,2,3,4]
24+
25+
**Output:** 1
26+
27+
**Explanation:**
28+
29+
`nums` becomes `[1, 2, 3, 4]` after all replacements, with minimum element 1.
30+
31+
**Example 3:**
32+
33+
**Input:** nums = [999,19,199]
34+
35+
**Output:** 10
36+
37+
**Explanation:**
38+
39+
`nums` becomes `[27, 10, 19]` after all replacements, with minimum element 10.
40+
41+
**Constraints:**
42+
43+
* `1 <= nums.length <= 100`
44+
* <code>1 <= nums[i] <= 10<sup>4</sup></code>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g3301_3400.s3301_maximize_the_total_height_of_unique_towers;
2+
3+
// #Medium #2024_09_30_Time_49_ms_(100.00%)_Space_57.8_MB_(100.00%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
public long maximumTotalSum(int[] maximumHeight) {
9+
Arrays.sort(maximumHeight);
10+
long result = maximumHeight[maximumHeight.length - 1];
11+
long previousHeight = maximumHeight[maximumHeight.length - 1];
12+
for (int i = maximumHeight.length - 2; i >= 0; i--) {
13+
if (previousHeight == 1) {
14+
return -1;
15+
}
16+
long height = maximumHeight[i];
17+
if (height >= previousHeight) {
18+
result = result + previousHeight - 1;
19+
previousHeight = previousHeight - 1;
20+
} else {
21+
result = result + height;
22+
previousHeight = height;
23+
}
24+
}
25+
return result;
26+
}
27+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
3301\. Maximize the Total Height of Unique Towers
2+
3+
Medium
4+
5+
You are given an array `maximumHeight`, where `maximumHeight[i]` denotes the **maximum** height the <code>i<sup>th</sup></code> tower can be assigned.
6+
7+
Your task is to assign a height to each tower so that:
8+
9+
1. The height of the <code>i<sup>th</sup></code> tower is a positive integer and does not exceed `maximumHeight[i]`.
10+
2. No two towers have the same height.
11+
12+
Return the **maximum** possible total sum of the tower heights. If it's not possible to assign heights, return `-1`.
13+
14+
**Example 1:**
15+
16+
**Input:** maximumHeight = [2,3,4,3]
17+
18+
**Output:** 10
19+
20+
**Explanation:**
21+
22+
We can assign heights in the following way: `[1, 2, 4, 3]`.
23+
24+
**Example 2:**
25+
26+
**Input:** maximumHeight = [15,10]
27+
28+
**Output:** 25
29+
30+
**Explanation:**
31+
32+
We can assign heights in the following way: `[15, 10]`.
33+
34+
**Example 3:**
35+
36+
**Input:** maximumHeight = [2,2,1]
37+
38+
**Output:** \-1
39+
40+
**Explanation:**
41+
42+
It's impossible to assign positive heights to each index so that no two towers have the same height.
43+
44+
**Constraints:**
45+
46+
* <code>1 <= maximumHeight.length <= 10<sup>5</sup></code>
47+
* <code>1 <= maximumHeight[i] <= 10<sup>9</sup></code>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package g3301_3400.s3302_find_the_lexicographically_smallest_valid_sequence;
2+
3+
// #Medium #2024_09_30_Time_33_ms_(100.00%)_Space_79.5_MB_(50.00%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
public int[] validSequence(String word1, String word2) {
9+
int n = word1.length();
10+
int m = word2.length();
11+
int[] revGreedyMatchInd = new int[m];
12+
Arrays.fill(revGreedyMatchInd, -1);
13+
{
14+
int i = n - 1;
15+
int j = m - 1;
16+
while (j >= 0 && i >= 0) {
17+
if (word1.charAt(i) == word2.charAt(j)) {
18+
revGreedyMatchInd[j--] = i;
19+
}
20+
i--;
21+
}
22+
}
23+
boolean canSkip = true;
24+
int j = 0;
25+
int i = 0;
26+
while (i < n && j < m && m - j <= n - i) {
27+
if (word1.charAt(i) == word2.charAt(j)) {
28+
revGreedyMatchInd[j++] = i;
29+
} else if (canSkip && (j == m - 1 || i < revGreedyMatchInd[j + 1])) {
30+
revGreedyMatchInd[j++] = i;
31+
canSkip = false;
32+
} else if (!canSkip && revGreedyMatchInd[j] == -1) {
33+
break;
34+
}
35+
i++;
36+
}
37+
return j == m ? revGreedyMatchInd : new int[0];
38+
}
39+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
3302\. Find the Lexicographically Smallest Valid Sequence
2+
3+
Medium
4+
5+
You are given two strings `word1` and `word2`.
6+
7+
A string `x` is called **almost equal** to `y` if you can change **at most** one character in `x` to make it _identical_ to `y`.
8+
9+
A sequence of indices `seq` is called **valid** if:
10+
11+
* The indices are sorted in **ascending** order.
12+
* _Concatenating_ the characters at these indices in `word1` in **the same** order results in a string that is **almost equal** to `word2`.
13+
14+
Return an array of size `word2.length` representing the lexicographically smallest **valid** sequence of indices. If no such sequence of indices exists, return an **empty** array.
15+
16+
**Note** that the answer must represent the _lexicographically smallest array_, **not** the corresponding string formed by those indices.
17+
18+
**Example 1:**
19+
20+
**Input:** word1 = "vbcca", word2 = "abc"
21+
22+
**Output:** [0,1,2]
23+
24+
**Explanation:**
25+
26+
The lexicographically smallest valid sequence of indices is `[0, 1, 2]`:
27+
28+
* Change `word1[0]` to `'a'`.
29+
* `word1[1]` is already `'b'`.
30+
* `word1[2]` is already `'c'`.
31+
32+
**Example 2:**
33+
34+
**Input:** word1 = "bacdc", word2 = "abc"
35+
36+
**Output:** [1,2,4]
37+
38+
**Explanation:**
39+
40+
The lexicographically smallest valid sequence of indices is `[1, 2, 4]`:
41+
42+
* `word1[1]` is already `'a'`.
43+
* Change `word1[2]` to `'b'`.
44+
* `word1[4]` is already `'c'`.
45+
46+
**Example 3:**
47+
48+
**Input:** word1 = "aaaaaa", word2 = "aaabc"
49+
50+
**Output:** []
51+
52+
**Explanation:**
53+
54+
There is no valid sequence of indices.
55+
56+
**Example 4:**
57+
58+
**Input:** word1 = "abc", word2 = "ab"
59+
60+
**Output:** [0,1]
61+
62+
**Constraints:**
63+
64+
* <code>1 <= word2.length < word1.length <= 3 * 10<sup>5</sup></code>
65+
* `word1` and `word2` consist only of lowercase English letters.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package g3301_3400.s3303_find_the_occurrence_of_first_almost_equal_substring;
2+
3+
// #Hard #2024_09_30_Time_1198_ms_(100.00%)_Space_97.4_MB_(100.00%)
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
public class Solution {
9+
public int minStartingIndex(String s, String p) {
10+
Map<Long, Integer> mp = new HashMap<>();
11+
long hash = 0;
12+
long base = 26;
13+
long d = 1;
14+
long hashP = 0;
15+
long mod = 10000000000283L;
16+
int n = s.length();
17+
int sz = p.length();
18+
// Rolling hash for string s
19+
for (int i = 0; i < n; i++) {
20+
hash = (hash * base + s.charAt(i)) % mod;
21+
if (i >= sz) {
22+
hash = (mod + hash - d * s.charAt(i - sz) % mod) % mod;
23+
} else {
24+
d = d * base % mod;
25+
}
26+
if (i >= sz - 1 && !mp.containsKey(hash)) {
27+
mp.put(hash, i - sz + 1);
28+
}
29+
}
30+
// Hash for the pattern p
31+
for (int i = 0; i < sz; i++) {
32+
hashP = (hashP * base + p.charAt(i)) % mod;
33+
}
34+
d = 1;
35+
int ans = Integer.MAX_VALUE;
36+
// Find the minimum index with almost equal string
37+
for (int i = sz - 1; i >= 0; i--) {
38+
long newhashP = (mod + hashP - d * p.charAt(i) % mod) % mod;
39+
for (char a = 'a'; a <= 'z'; a++) {
40+
long candidateHash = (newhashP + d * a % mod) % mod;
41+
if (mp.containsKey(candidateHash)) {
42+
ans = Math.min(ans, mp.get(candidateHash));
43+
}
44+
}
45+
d = d * base % mod;
46+
}
47+
return ans == Integer.MAX_VALUE ? -1 : ans;
48+
}
49+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
3303\. Find the Occurrence of First Almost Equal Substring
2+
3+
Hard
4+
5+
You are given two strings `s` and `pattern`.
6+
7+
A string `x` is called **almost equal** to `y` if you can change **at most** one character in `x` to make it _identical_ to `y`.
8+
9+
Return the **smallest** _starting index_ of a substring in `s` that is **almost equal** to `pattern`. If no such index exists, return `-1`.
10+
11+
A **substring** is a contiguous **non-empty** sequence of characters within a string.
12+
13+
**Example 1:**
14+
15+
**Input:** s = "abcdefg", pattern = "bcdffg"
16+
17+
**Output:** 1
18+
19+
**Explanation:**
20+
21+
The substring `s[1..6] == "bcdefg"` can be converted to `"bcdffg"` by changing `s[4]` to `"f"`.
22+
23+
**Example 2:**
24+
25+
**Input:** s = "ababbababa", pattern = "bacaba"
26+
27+
**Output:** 4
28+
29+
**Explanation:**
30+
31+
The substring `s[4..9] == "bababa"` can be converted to `"bacaba"` by changing `s[6]` to `"c"`.
32+
33+
**Example 3:**
34+
35+
**Input:** s = "abcd", pattern = "dba"
36+
37+
**Output:** \-1
38+
39+
**Example 4:**
40+
41+
**Input:** s = "dde", pattern = "d"
42+
43+
**Output:** 0
44+
45+
**Constraints:**
46+
47+
* <code>1 <= pattern.length < s.length <= 3 * 10<sup>5</sup></code>
48+
* `s` and `pattern` consist only of lowercase English letters.
49+
50+
**Follow-up:** Could you solve the problem if **at most** `k` **consecutive** characters can be changed?
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g3301_3400.s3304_find_the_k_th_character_in_string_game_i;
2+
3+
// #Easy #2024_09_30_Time_16_ms_(100.00%)_Space_44.5_MB_(100.00%)
4+
5+
public class Solution {
6+
public char kthCharacter(int k) {
7+
StringBuilder sb = new StringBuilder();
8+
char c = 'a';
9+
sb.append(c);
10+
String s = sb.toString();
11+
while (s.length() <= k) {
12+
s = sb.toString();
13+
char[] cq = s.toCharArray();
14+
for (char c1 : cq) {
15+
int ascii = (int) c1;
16+
c1 = (char) (ascii + 1);
17+
sb.append(c1);
18+
}
19+
}
20+
for (int i = 0; i < sb.toString().length(); i++) {
21+
if (i == k) {
22+
return sb.toString().charAt(i - 1);
23+
}
24+
}
25+
return '\0';
26+
}
27+
}

0 commit comments

Comments
 (0)