Skip to content

Commit 50c4b42

Browse files
authored
Added tasks 2785-2789
1 parent 7957fe4 commit 50c4b42

File tree

15 files changed

+437
-0
lines changed

15 files changed

+437
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package g2701_2800.s2785_sort_vowels_in_a_string
2+
3+
// #Medium #String #Sorting #2023_08_08_Time_233_ms_(100.00%)_Space_38.6_MB_(100.00%)
4+
5+
class Solution {
6+
fun sortVowels(s: String): String {
7+
val vowelCount = IntArray(11)
8+
val countIndexMap = IntArray(128)
9+
val result = s.toCharArray()
10+
val charMap = "AEIOUaeiou".toCharArray()
11+
run {
12+
var i = 0
13+
while (i < charMap.size) {
14+
countIndexMap[charMap[i].code] = ++i
15+
}
16+
}
17+
for (c in result) vowelCount[countIndexMap[c.code]]++
18+
var j = 1
19+
var i = 0
20+
while (j < vowelCount.size) {
21+
if (vowelCount[j] > 0) while (i < result.size) {
22+
if (countIndexMap[result[i++].code] == 0) continue
23+
vowelCount[j]--
24+
result[i - 1] = charMap[j - 1]
25+
break
26+
} else j++
27+
}
28+
return String(result)
29+
}
30+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2785\. Sort Vowels in a String
2+
3+
Medium
4+
5+
Given a **0-indexed** string `s`, **permute** `s` to get a new string `t` such that:
6+
7+
* All consonants remain in their original places. More formally, if there is an index `i` with `0 <= i < s.length` such that `s[i]` is a consonant, then `t[i] = s[i]`.
8+
* The vowels must be sorted in the **nondecreasing** order of their **ASCII** values. More formally, for pairs of indices `i`, `j` with `0 <= i < j < s.length` such that `s[i]` and `s[j]` are vowels, then `t[i]` must not have a higher ASCII value than `t[j]`.
9+
10+
Return _the resulting string_.
11+
12+
The vowels are `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.
13+
14+
**Example 1:**
15+
16+
**Input:** s = "lEetcOde"
17+
18+
**Output:** "lEOtcede"
19+
20+
**Explanation:** 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places.
21+
22+
**Example 2:**
23+
24+
**Input:** s = "lYmpH"
25+
26+
**Output:** "lYmpH"
27+
28+
**Explanation:** There are no vowels in s (all characters in s are consonants), so we return "lYmpH".
29+
30+
**Constraints:**
31+
32+
* <code>1 <= s.length <= 10<sup>5</sup></code>
33+
* `s` consists only of letters of the English alphabet in **uppercase and lowercase**.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g2701_2800.s2786_visit_array_positions_to_maximize_score
2+
3+
// #Medium #Array #Dynamic_Programming #2023_08_08_Time_625_ms_(84.00%)_Space_68.5_MB_(52.00%)
4+
5+
import kotlin.math.max
6+
7+
class Solution {
8+
fun maxScore(nums: IntArray, x: Int): Long {
9+
val dp = longArrayOf(-x.toLong(), -x.toLong())
10+
dp[nums[0] and 1] = nums[0].toLong()
11+
for (i in 1 until nums.size) {
12+
val toggle = dp[nums[i] and 1 xor 1] - x
13+
val nottoggle = dp[nums[i] and 1]
14+
dp[nums[i] and 1] = (max(toggle.toDouble(), nottoggle.toDouble()) + nums[i]).toLong()
15+
}
16+
return max(dp[0].toDouble(), dp[1].toDouble()).toLong()
17+
}
18+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2786\. Visit Array Positions to Maximize Score
2+
3+
Medium
4+
5+
You are given a **0-indexed** integer array `nums` and a positive integer `x`.
6+
7+
You are **initially** at position `0` in the array and you can visit other positions according to the following rules:
8+
9+
* If you are currently in position `i`, then you can move to **any** position `j` such that `i < j`.
10+
* For each position `i` that you visit, you get a score of `nums[i]`.
11+
* If you move from a position `i` to a position `j` and the **parities** of `nums[i]` and `nums[j]` differ, then you lose a score of `x`.
12+
13+
Return _the **maximum** total score you can get_.
14+
15+
**Note** that initially you have `nums[0]` points.
16+
17+
**Example 1:**
18+
19+
**Input:** nums = [2,3,6,1,9,2], x = 5
20+
21+
**Output:** 13
22+
23+
**Explanation:**
24+
25+
We can visit the following positions in the array: 0 -> 2 -> 3 -> 4.
26+
27+
The corresponding values are 2, 6, 1 and 9. Since the integers 6 and 1 have different parities, the move 2 -> 3 will make you lose a score of x = 5.
28+
29+
The total score will be: 2 + 6 + 1 + 9 - 5 = 13.
30+
31+
**Example 2:**
32+
33+
**Input:** nums = [2,4,6,8], x = 3
34+
35+
**Output:** 20
36+
37+
**Explanation:**
38+
39+
All the integers in the array have the same parities, so we can visit all of them without losing any score.
40+
41+
The total score is: 2 + 4 + 6 + 8 = 20.
42+
43+
**Constraints:**
44+
45+
* <code>2 <= nums.length <= 10<sup>5</sup></code>
46+
* <code>1 <= nums[i], x <= 10<sup>6</sup></code>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g2701_2800.s2787_ways_to_express_an_integer_as_sum_of_powers
2+
3+
// #Medium #Dynamic_Programming #2023_08_08_Time_152_ms_(100.00%)_Space_35.8_MB_(90.91%)
4+
5+
class Solution {
6+
fun numberOfWays(n: Int, x: Int): Int {
7+
val dp = IntArray(301)
8+
val mod = 1000000007
9+
var v: Int
10+
dp[0] = 1
11+
var a = 1
12+
while ((Math.pow(a.toDouble(), x.toDouble())).also { v = it.toInt() } <= n) {
13+
for (i in n downTo v) {
14+
dp[i] = (dp[i] + dp[i - v]) % mod
15+
}
16+
a++
17+
}
18+
return dp[n]
19+
}
20+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2787\. Ways to Express an Integer as Sum of Powers
2+
3+
Medium
4+
5+
Given two **positive** integers `n` and `x`.
6+
7+
Return _the number of ways_ `n` _can be expressed as the sum of the_ <code>x<sup>th</sup></code> _power of **unique** positive integers, in other words, the number of sets of unique integers_ <code>[n<sub>1</sub>, n<sub>2</sub>, ..., n<sub>k</sub>]</code> _where_ <code>n = n<sub>1</sub><sup>x</sup> + n<sub>2</sub><sup>x</sup> + ... + n<sub>k</sub><sup>x</sup></code>_._
8+
9+
Since the result can be very large, return it modulo <code>10<sup>9</sup> + 7</code>.
10+
11+
For example, if `n = 160` and `x = 3`, one way to express `n` is <code>n = 2<sup>3</sup> + 3<sup>3</sup> + 5<sup>3</sup></code>.
12+
13+
**Example 1:**
14+
15+
**Input:** n = 10, x = 2
16+
17+
**Output:** 1
18+
19+
**Explanation:**
20+
21+
We can express n as the following: n = 3<sup>2</sup> + 1<sup>2</sup> = 10.
22+
23+
It can be shown that it is the only way to express 10 as the sum of the 2<sup>nd</sup> power of unique integers.
24+
25+
**Example 2:**
26+
27+
**Input:** n = 4, x = 1
28+
29+
**Output:** 2
30+
31+
**Explanation:**
32+
33+
We can express n in the following ways:
34+
35+
- n = 4<sup>1</sup> = 4.
36+
37+
- n = 3<sup>1</sup> + 1<sup>1</sup> = 4.
38+
39+
**Constraints:**
40+
41+
* `1 <= n <= 300`
42+
* `1 <= x <= 5`
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g2701_2800.s2788_split_strings_by_separator
2+
3+
// #Easy #Array #String #2023_08_08_Time_314_ms_(85.45%)_Space_38_MB_(98.18%)
4+
5+
class Solution {
6+
fun splitWordsBySeparator(words: List<String>, separator: Char): List<String> {
7+
val list: MutableList<String> = ArrayList()
8+
for (str in words) {
9+
var si = 0
10+
for (i in str.indices) {
11+
if (str[i] == separator) {
12+
if (i > si) {
13+
list.add(str.substring(si, i))
14+
}
15+
si = i + 1
16+
}
17+
}
18+
if (si != str.length) {
19+
list.add(str.substring(si, str.length))
20+
}
21+
}
22+
return list
23+
}
24+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
2788\. Split Strings by Separator
2+
3+
Easy
4+
5+
Given an array of strings `words` and a character `separator`, **split** each string in `words` by `separator`.
6+
7+
Return _an array of strings containing the new strings formed after the splits, **excluding empty strings**._
8+
9+
**Notes**
10+
11+
* `separator` is used to determine where the split should occur, but it is not included as part of the resulting strings.
12+
* A split may result in more than two strings.
13+
* The resulting strings must maintain the same order as they were initially given.
14+
15+
**Example 1:**
16+
17+
**Input:** words = ["one.two.three","four.five","six"], separator = "."
18+
19+
**Output:** ["one","two","three","four","five","six"]
20+
21+
**Explanation:**
22+
23+
In this example we split as follows: "one.two.three" splits into
24+
25+
"one", "two", "three" "four.five" splits into
26+
27+
"four", "five" "six" splits into "six"
28+
29+
Hence, the resulting array is ["one","two","three","four","five","six"].
30+
31+
**Example 2:**
32+
33+
**Input:** words = ["$easy$","$problem$"], separator = "$"
34+
35+
**Output:** ["easy","problem"]
36+
37+
**Explanation:**
38+
39+
In this example we split as follows:
40+
41+
"$easy$" splits into "easy" (excluding empty strings)
42+
43+
"$problem$" splits into "problem" (excluding empty strings)
44+
45+
Hence, the resulting array is ["easy","problem"].
46+
47+
**Example 3:**
48+
49+
**Input:** words = ["|||"], separator = "|"
50+
51+
**Output:** []
52+
53+
**Explanation:**
54+
55+
In this example the resulting split of "|||" will contain only empty strings, so we return an empty array [].
56+
57+
**Constraints:**
58+
59+
* `1 <= words.length <= 100`
60+
* `1 <= words[i].length <= 20`
61+
* characters in `words[i]` are either lowercase English letters or characters from the string `".,|$#@"` (excluding the quotes)
62+
* `separator` is a character from the string `".,|$#@"` (excluding the quotes)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g2701_2800.s2789_largest_element_in_an_array_after_merge_operations
2+
3+
// #Medium #Array #Greedy #Prefix_Sum #2023_08_08_Time_683_ms_(73.68%)_Space_70.8_MB_(42.10%)
4+
5+
class Solution {
6+
fun maxArrayValue(nums: IntArray): Long {
7+
var ans = nums[nums.size - 1].toLong()
8+
for (i in nums.size - 1 downTo 1) {
9+
if (ans >= nums[i - 1]) {
10+
ans += nums[i - 1].toLong()
11+
} else {
12+
ans = nums[i - 1].toLong()
13+
}
14+
}
15+
return ans
16+
}
17+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2789\. Largest Element in an Array after Merge Operations
2+
3+
Medium
4+
5+
You are given a **0-indexed** array `nums` consisting of positive integers.
6+
7+
You can do the following operation on the array **any** number of times:
8+
9+
* Choose an integer `i` such that `0 <= i < nums.length - 1` and `nums[i] <= nums[i + 1]`. Replace the element `nums[i + 1]` with `nums[i] + nums[i + 1]` and delete the element `nums[i]` from the array.
10+
11+
Return _the value of the **largest** element that you can possibly obtain in the final array._
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [2,3,7,9,3]
16+
17+
**Output:** 21
18+
19+
**Explanation:**
20+
21+
We can apply the following operations on the array:
22+
23+
- Choose i = 0. The resulting array will be nums = [<ins>5</ins>,7,9,3].
24+
25+
- Choose i = 1. The resulting array will be nums = [5,<ins>16</ins>,3].
26+
27+
- Choose i = 0. The resulting array will be nums = [<ins>21</ins>,3].
28+
29+
The largest element in the final array is 21. It can be shown that we cannot obtain a larger element.
30+
31+
**Example 2:**
32+
33+
**Input:** nums = [5,3,3]
34+
35+
**Output:** 11
36+
37+
**Explanation:**
38+
39+
We can do the following operations on the array:
40+
41+
- Choose i = 1. The resulting array will be nums = [5,<ins>6</ins>].
42+
43+
- Choose i = 0. The resulting array will be nums = [<ins>11</ins>].
44+
45+
There is only one element in the final array, which is 11.
46+
47+
**Constraints:**
48+
49+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
50+
* <code>1 <= nums[i] <= 10<sup>6</sup></code>

0 commit comments

Comments
 (0)