Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd0d71a3

Browse files
authored
Added tasks 2785-2789
1 parentd896d26 commitd0d71a3

File tree

16 files changed

+455
-1
lines changed

16 files changed

+455
-1
lines changed

‎src/main/java/g2601_2700/s2699_modify_graph_edge_weights/Solution.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
importjava.util.List;
99
importjava.util.PriorityQueue;
1010

11-
@SuppressWarnings("java:S135")
11+
@SuppressWarnings({"unchecked","java:S135"})
1212
publicclassSolution {
1313
publicint[][]modifiedGraphEdges(
1414
intn,int[][]edges,intsource,intdestination,inttarget) {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
packageg2701_2800.s2785_sort_vowels_in_a_string;
2+
3+
// #Medium #String #Sorting #2023_09_15_Time_5_ms_(100.00%)_Space_44.6_MB_(96.53%)
4+
5+
publicclassSolution {
6+
publicStringsortVowels(Strings) {
7+
int[]vowelCount =newint[11];
8+
int[]countIndexMap =newint[128];
9+
char[]result =s.toCharArray();
10+
char[]charMap ="AEIOUaeiou".toCharArray();
11+
for (inti =0;i <charMap.length;i++) {
12+
countIndexMap[charMap[i]] =i +1;
13+
}
14+
for (charc :result) {
15+
vowelCount[countIndexMap[c]]++;
16+
}
17+
intj =1;
18+
inti =0;
19+
while (j <vowelCount.length) {
20+
if (vowelCount[j] >0) {
21+
while (i <result.length) {
22+
if (countIndexMap[result[i]] ==0) {
23+
i++;
24+
}else {
25+
vowelCount[j]--;
26+
result[i++] =charMap[j -1];
27+
break;
28+
}
29+
}
30+
}else {
31+
j++;
32+
}
33+
}
34+
returnnewString(result);
35+
}
36+
}
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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
packageg2701_2800.s2786_visit_array_positions_to_maximize_score;
2+
3+
// #Medium #Array #Dynamic_Programming #2023_09_15_Time_5_ms_(100.00%)_Space_54.5_MB_(99.95%)
4+
5+
publicclassSolution {
6+
publiclongmaxScore(int[]nums,intx) {
7+
long[]dp = {-x, -x};
8+
dp[nums[0] &1] =nums[0];
9+
for (inti =1;i <nums.length;i++) {
10+
longtoggle =dp[nums[i] &1 ^1] -x;
11+
longnottoggle =dp[nums[i] &1];
12+
dp[nums[i] &1] =Math.max(toggle,nottoggle) +nums[i];
13+
}
14+
returnMath.max(dp[0],dp[1]);
15+
}
16+
}
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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
packageg2701_2800.s2787_ways_to_express_an_integer_as_sum_of_powers;
2+
3+
// #Medium #Dynamic_Programming #2023_09_15_Time_12_ms_(98.96%)_Space_42.4_MB_(86.68%)
4+
5+
publicclassSolution {
6+
publicintnumberOfWays(intn,intx) {
7+
int[]dp =newint[301];
8+
intmod =1000000007;
9+
intv;
10+
dp[0] =1;
11+
inta =1;
12+
while (Math.pow(a,x) <=n) {
13+
v = (int)Math.pow(a,x);
14+
for (inti =n;i >=v;i--) {
15+
dp[i] = (dp[i] +dp[i -v]) %mod;
16+
}
17+
a++;
18+
}
19+
returndp[n];
20+
}
21+
}
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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
packageg2701_2800.s2788_split_strings_by_separator;
2+
3+
// #Easy #Array #String #2023_09_15_Time_3_ms_(99.98%)_Space_44.8_MB_(68.11%)
4+
5+
importjava.util.ArrayList;
6+
importjava.util.List;
7+
8+
publicclassSolution {
9+
publicList<String>splitWordsBySeparator(List<String>words,charseparator) {
10+
List<String>list =newArrayList<>();
11+
for (Stringstr :words) {
12+
intsi =0;
13+
for (inti =0;i <str.length();i++) {
14+
if (str.charAt(i) ==separator) {
15+
if (i >si) {
16+
list.add(str.substring(si,i));
17+
}
18+
si =i +1;
19+
}
20+
}
21+
if (si !=str.length()) {
22+
list.add(str.substring(si,str.length()));
23+
}
24+
}
25+
returnlist;
26+
}
27+
}
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+
packageg2701_2800.s2789_largest_element_in_an_array_after_merge_operations;
2+
3+
// #Medium #Array #Greedy #Prefix_Sum #2023_09_15_Time_1_ms_(100.00%)_Space_58_MB_(58.92%)
4+
5+
publicclassSolution {
6+
publiclongmaxArrayValue(int[]nums) {
7+
longans =nums[nums.length -1];
8+
for (inti =nums.length -1;i >0; --i) {
9+
if (ans >=nums[i -1]) {
10+
ans +=nums[i -1];
11+
}else {
12+
ans =nums[i -1];
13+
}
14+
}
15+
returnans;
16+
}
17+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp