-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
912 additions
and
0 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
...main/java/g3201_3300/s3300_minimum_element_after_replacement_with_digit_sum/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package g3201_3300.s3300_minimum_element_after_replacement_with_digit_sum; | ||
|
||
// #Easy #Array #Math #2024_10_01_Time_1_ms_(100.00%)_Space_42.9_MB_(75.97%) | ||
|
||
public class Solution { | ||
public int minElement(int[] nums) { | ||
int min = Integer.MAX_VALUE; | ||
for (int x : nums) { | ||
min = Math.min(min, solve(x)); | ||
} | ||
return min; | ||
} | ||
|
||
private int solve(int x) { | ||
int sum = 0; | ||
while (x != 0) { | ||
sum += x % 10; | ||
x /= 10; | ||
} | ||
return sum; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...ava/g3201_3300/s3300_minimum_element_after_replacement_with_digit_sum/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
3300\. Minimum Element After Replacement With Digit Sum | ||
|
||
Easy | ||
|
||
You are given an integer array `nums`. | ||
|
||
You replace each element in `nums` with the **sum** of its digits. | ||
|
||
Return the **minimum** element in `nums` after all replacements. | ||
|
||
**Example 1:** | ||
|
||
**Input:** nums = [10,12,13,14] | ||
|
||
**Output:** 1 | ||
|
||
**Explanation:** | ||
|
||
`nums` becomes `[1, 3, 4, 5]` after all replacements, with minimum element 1. | ||
|
||
**Example 2:** | ||
|
||
**Input:** nums = [1,2,3,4] | ||
|
||
**Output:** 1 | ||
|
||
**Explanation:** | ||
|
||
`nums` becomes `[1, 2, 3, 4]` after all replacements, with minimum element 1. | ||
|
||
**Example 3:** | ||
|
||
**Input:** nums = [999,19,199] | ||
|
||
**Output:** 10 | ||
|
||
**Explanation:** | ||
|
||
`nums` becomes `[27, 10, 19]` after all replacements, with minimum element 10. | ||
|
||
**Constraints:** | ||
|
||
* `1 <= nums.length <= 100` | ||
* <code>1 <= nums[i] <= 10<sup>4</sup></code> |
27 changes: 27 additions & 0 deletions
27
src/main/java/g3301_3400/s3301_maximize_the_total_height_of_unique_towers/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package g3301_3400.s3301_maximize_the_total_height_of_unique_towers; | ||
|
||
// #Medium #Array #Sorting #Greedy #2024_10_01_Time_49_ms_(92.39%)_Space_57.9_MB_(70.01%) | ||
|
||
import java.util.Arrays; | ||
|
||
public class Solution { | ||
public long maximumTotalSum(int[] maximumHeight) { | ||
Arrays.sort(maximumHeight); | ||
long result = maximumHeight[maximumHeight.length - 1]; | ||
long previousHeight = maximumHeight[maximumHeight.length - 1]; | ||
for (int i = maximumHeight.length - 2; i >= 0; i--) { | ||
if (previousHeight == 1) { | ||
return -1; | ||
} | ||
long height = maximumHeight[i]; | ||
if (height >= previousHeight) { | ||
result = result + previousHeight - 1; | ||
previousHeight = previousHeight - 1; | ||
} else { | ||
result = result + height; | ||
previousHeight = height; | ||
} | ||
} | ||
return result; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...main/java/g3301_3400/s3301_maximize_the_total_height_of_unique_towers/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
3301\. Maximize the Total Height of Unique Towers | ||
|
||
Medium | ||
|
||
You are given an array `maximumHeight`, where `maximumHeight[i]` denotes the **maximum** height the <code>i<sup>th</sup></code> tower can be assigned. | ||
|
||
Your task is to assign a height to each tower so that: | ||
|
||
1. The height of the <code>i<sup>th</sup></code> tower is a positive integer and does not exceed `maximumHeight[i]`. | ||
2. No two towers have the same height. | ||
|
||
Return the **maximum** possible total sum of the tower heights. If it's not possible to assign heights, return `-1`. | ||
|
||
**Example 1:** | ||
|
||
**Input:** maximumHeight = [2,3,4,3] | ||
|
||
**Output:** 10 | ||
|
||
**Explanation:** | ||
|
||
We can assign heights in the following way: `[1, 2, 4, 3]`. | ||
|
||
**Example 2:** | ||
|
||
**Input:** maximumHeight = [15,10] | ||
|
||
**Output:** 25 | ||
|
||
**Explanation:** | ||
|
||
We can assign heights in the following way: `[15, 10]`. | ||
|
||
**Example 3:** | ||
|
||
**Input:** maximumHeight = [2,2,1] | ||
|
||
**Output:** \-1 | ||
|
||
**Explanation:** | ||
|
||
It's impossible to assign positive heights to each index so that no two towers have the same height. | ||
|
||
**Constraints:** | ||
|
||
* <code>1 <= maximumHeight.length <= 10<sup>5</sup></code> | ||
* <code>1 <= maximumHeight[i] <= 10<sup>9</sup></code> |
49 changes: 49 additions & 0 deletions
49
...in/java/g3301_3400/s3302_find_the_lexicographically_smallest_valid_sequence/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package g3301_3400.s3302_find_the_lexicographically_smallest_valid_sequence; | ||
|
||
// #Medium #String #Dynamic_Programming #Greedy #Two_Pointers | ||
// #2024_10_01_Time_21_ms_(97.32%)_Space_74.3_MB_(74.55%) | ||
|
||
public class Solution { | ||
public int[] validSequence(String word1, String word2) { | ||
char[] c1 = word1.toCharArray(); | ||
char[] c2 = word2.toCharArray(); | ||
int[] dp = new int[c1.length + 1]; | ||
int j = c2.length - 1; | ||
for (int i = c1.length - 1; i >= 0; i--) { | ||
if (j >= 0 && c1[i] == c2[j]) { | ||
dp[i] = dp[i + 1] + 1; | ||
j--; | ||
} else { | ||
dp[i] = dp[i + 1]; | ||
} | ||
} | ||
int[] ans = new int[c2.length]; | ||
int i = 0; | ||
j = 0; | ||
while (i < c1.length && j < c2.length) { | ||
if (c1[i] == c2[j]) { | ||
ans[j] = i; | ||
j++; | ||
} else { | ||
if (dp[i + 1] >= c2.length - 1 - j) { | ||
ans[j] = i; | ||
j++; | ||
i++; | ||
break; | ||
} | ||
} | ||
i++; | ||
} | ||
if (j < c2.length && i == c1.length) { | ||
return new int[0]; | ||
} | ||
while (j < c2.length && i < c1.length) { | ||
if (c2[j] == c1[i]) { | ||
ans[j] = i; | ||
j++; | ||
} | ||
i++; | ||
} | ||
return ans; | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
...a/g3301_3400/s3302_find_the_lexicographically_smallest_valid_sequence/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
3302\. Find the Lexicographically Smallest Valid Sequence | ||
|
||
Medium | ||
|
||
You are given two strings `word1` and `word2`. | ||
|
||
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`. | ||
|
||
A sequence of indices `seq` is called **valid** if: | ||
|
||
* The indices are sorted in **ascending** order. | ||
* _Concatenating_ the characters at these indices in `word1` in **the same** order results in a string that is **almost equal** to `word2`. | ||
|
||
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. | ||
|
||
**Note** that the answer must represent the _lexicographically smallest array_, **not** the corresponding string formed by those indices. | ||
|
||
**Example 1:** | ||
|
||
**Input:** word1 = "vbcca", word2 = "abc" | ||
|
||
**Output:** [0,1,2] | ||
|
||
**Explanation:** | ||
|
||
The lexicographically smallest valid sequence of indices is `[0, 1, 2]`: | ||
|
||
* Change `word1[0]` to `'a'`. | ||
* `word1[1]` is already `'b'`. | ||
* `word1[2]` is already `'c'`. | ||
|
||
**Example 2:** | ||
|
||
**Input:** word1 = "bacdc", word2 = "abc" | ||
|
||
**Output:** [1,2,4] | ||
|
||
**Explanation:** | ||
|
||
The lexicographically smallest valid sequence of indices is `[1, 2, 4]`: | ||
|
||
* `word1[1]` is already `'a'`. | ||
* Change `word1[2]` to `'b'`. | ||
* `word1[4]` is already `'c'`. | ||
|
||
**Example 3:** | ||
|
||
**Input:** word1 = "aaaaaa", word2 = "aaabc" | ||
|
||
**Output:** [] | ||
|
||
**Explanation:** | ||
|
||
There is no valid sequence of indices. | ||
|
||
**Example 4:** | ||
|
||
**Input:** word1 = "abc", word2 = "ab" | ||
|
||
**Output:** [0,1] | ||
|
||
**Constraints:** | ||
|
||
* <code>1 <= word2.length < word1.length <= 3 * 10<sup>5</sup></code> | ||
* `word1` and `word2` consist only of lowercase English letters. |
58 changes: 58 additions & 0 deletions
58
...n/java/g3301_3400/s3303_find_the_occurrence_of_first_almost_equal_substring/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package g3301_3400.s3303_find_the_occurrence_of_first_almost_equal_substring; | ||
|
||
// #Hard #String #String_Matching #2024_10_01_Time_39_ms_(100.00%)_Space_46.1_MB_(100.00%) | ||
|
||
public class Solution { | ||
public int minStartingIndex(String s, String pattern) { | ||
int n = s.length(); | ||
int left = 0; | ||
int right = 0; | ||
int[] f1 = new int[26]; | ||
int[] f2 = new int[26]; | ||
for (char ch : pattern.toCharArray()) { | ||
f2[ch - 'a']++; | ||
} | ||
while (right < n) { | ||
char ch = s.charAt(right); | ||
f1[ch - 'a']++; | ||
if (right - left + 1 == pattern.length() + 1) { | ||
f1[s.charAt(left) - 'a']--; | ||
left += 1; | ||
} | ||
if (right - left + 1 == pattern.length() && check(f1, f2, left, s, pattern)) { | ||
return left; | ||
} | ||
right += 1; | ||
} | ||
return -1; | ||
} | ||
|
||
private boolean check(int[] f1, int[] f2, int left, String s, String pattern) { | ||
int cnt = 0; | ||
for (int i = 0; i < 26; i++) { | ||
if (f1[i] != f2[i]) { | ||
if ((Math.abs(f1[i] - f2[i]) > 1) || (Math.abs(f1[i] - f2[i]) != 1 && cnt == 2)) { | ||
return false; | ||
} | ||
cnt += 1; | ||
} | ||
} | ||
cnt = 0; | ||
int start = 0; | ||
int end = pattern.length() - 1; | ||
while (start <= end) { | ||
if (s.charAt(start + left) != pattern.charAt(start)) { | ||
cnt += 1; | ||
} | ||
if (start + left != left + end && s.charAt(left + end) != pattern.charAt(end)) { | ||
cnt += 1; | ||
} | ||
if (cnt >= 2) { | ||
return false; | ||
} | ||
start++; | ||
end--; | ||
} | ||
return true; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
.../g3301_3400/s3303_find_the_occurrence_of_first_almost_equal_substring/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
3303\. Find the Occurrence of First Almost Equal Substring | ||
|
||
Hard | ||
|
||
You are given two strings `s` and `pattern`. | ||
|
||
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`. | ||
|
||
Return the **smallest** _starting index_ of a substring in `s` that is **almost equal** to `pattern`. If no such index exists, return `-1`. | ||
|
||
A **substring** is a contiguous **non-empty** sequence of characters within a string. | ||
|
||
**Example 1:** | ||
|
||
**Input:** s = "abcdefg", pattern = "bcdffg" | ||
|
||
**Output:** 1 | ||
|
||
**Explanation:** | ||
|
||
The substring `s[1..6] == "bcdefg"` can be converted to `"bcdffg"` by changing `s[4]` to `"f"`. | ||
|
||
**Example 2:** | ||
|
||
**Input:** s = "ababbababa", pattern = "bacaba" | ||
|
||
**Output:** 4 | ||
|
||
**Explanation:** | ||
|
||
The substring `s[4..9] == "bababa"` can be converted to `"bacaba"` by changing `s[6]` to `"c"`. | ||
|
||
**Example 3:** | ||
|
||
**Input:** s = "abcd", pattern = "dba" | ||
|
||
**Output:** \-1 | ||
|
||
**Example 4:** | ||
|
||
**Input:** s = "dde", pattern = "d" | ||
|
||
**Output:** 0 | ||
|
||
**Constraints:** | ||
|
||
* <code>1 <= pattern.length < s.length <= 3 * 10<sup>5</sup></code> | ||
* `s` and `pattern` consist only of lowercase English letters. | ||
|
||
**Follow-up:** Could you solve the problem if **at most** `k` **consecutive** characters can be changed? |
Oops, something went wrong.