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

Commit3962831

Browse files
authored
Added tasks 3222-3229
1 parent3d6c1f7 commit3962831

File tree

25 files changed

+720
-1
lines changed

25 files changed

+720
-1
lines changed

‎src/main/java/g3201_3300/s3220_odd_and_even_transactions/script.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Write your MySQL query statement below
2-
# #Medium #2024_07_18_Time_272_ms_(100.00%)_Space_0B_(100.00%)
2+
# #Medium #Database #2024_07_23_Time_248_ms_(85.85%)_Space_0B_(100.00%)
33
select transaction_date,
44
sum(case when amount%2<>0 then amount else0 end)as odd_sum,
55
sum(case when amount%2=0 then amount else0 end)as even_sumfrom transactions
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
packageg3201_3300.s3222_find_the_winning_player_in_coin_game;
2+
3+
// #Easy #Math #Simulation #Game_Theory #2024_07_23_Time_0_ms_(100.00%)_Space_41.6_MB_(67.81%)
4+
5+
publicclassSolution {
6+
publicStringlosingPlayer(intx,inty) {
7+
booleanw =false;
8+
while (x >0 &&y >=4) {
9+
x--;
10+
y -=4;
11+
w = !w;
12+
}
13+
returnw ?"Alice" :"Bob";
14+
}
15+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
3222\. Find the Winning Player in Coin Game
2+
3+
Easy
4+
5+
You are given two**positive** integers`x` and`y`, denoting the number of coins with values 75 and 10_respectively_.
6+
7+
Alice and Bob are playing a game. Each turn, starting with**Alice**, the player must pick up coins with a**total** value 115. If the player is unable to do so, they**lose** the game.
8+
9+
Return the_name_ of the player who wins the game if both players play**optimally**.
10+
11+
**Example 1:**
12+
13+
**Input:** x = 2, y = 7
14+
15+
**Output:** "Alice"
16+
17+
**Explanation:**
18+
19+
The game ends in a single turn:
20+
21+
* Alice picks 1 coin with a value of 75 and 4 coins with a value of 10.
22+
23+
**Example 2:**
24+
25+
**Input:** x = 4, y = 11
26+
27+
**Output:** "Bob"
28+
29+
**Explanation:**
30+
31+
The game ends in 2 turns:
32+
33+
* Alice picks 1 coin with a value of 75 and 4 coins with a value of 10.
34+
* Bob picks 1 coin with a value of 75 and 4 coins with a value of 10.
35+
36+
**Constraints:**
37+
38+
*`1 <= x, y <= 100`
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
packageg3201_3300.s3223_minimum_length_of_string_after_operations;
2+
3+
// #Medium #String #Hash_Table #Counting #2024_07_23_Time_9_ms_(94.23%)_Space_46.5_MB_(38.50%)
4+
5+
publicclassSolution {
6+
publicintminimumLength(Strings) {
7+
int[]freq =newint[26];
8+
for (inti =0;i <26;i++) {
9+
freq[i] =0;
10+
}
11+
for (inti =0;i <s.length();i++) {
12+
freq[s.charAt(i) -'a']++;
13+
}
14+
intc =0;
15+
for (inti :freq) {
16+
if (i !=0) {
17+
if (i %2 ==0) {
18+
c +=2;
19+
}else {
20+
c +=1;
21+
}
22+
}
23+
}
24+
returnc;
25+
}
26+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
3223\. Minimum Length of String After Operations
2+
3+
Medium
4+
5+
You are given a string`s`.
6+
7+
You can perform the following process on`s`**any** number of times:
8+
9+
* Choose an index`i` in the string such that there is**at least** one character to the left of index`i` that is equal to`s[i]`, and**at least** one character to the right that is also equal to`s[i]`.
10+
* Delete the**closest** character to the**left** of index`i` that is equal to`s[i]`.
11+
* Delete the**closest** character to the**right** of index`i` that is equal to`s[i]`.
12+
13+
Return the**minimum** length of the final string`s` that you can achieve.
14+
15+
**Example 1:**
16+
17+
**Input:** s = "abaacbcbb"
18+
19+
**Output:** 5
20+
21+
**Explanation:**
22+
We do the following operations:
23+
24+
* Choose index 2, then remove the characters at indices 0 and 3. The resulting string is`s = "bacbcbb"`.
25+
* Choose index 3, then remove the characters at indices 0 and 5. The resulting string is`s = "acbcb"`.
26+
27+
**Example 2:**
28+
29+
**Input:** s = "aa"
30+
31+
**Output:** 2
32+
33+
**Explanation:**
34+
We cannot perform any operations, so we return the length of the original string.
35+
36+
**Constraints:**
37+
38+
* <code>1 <= s.length <= 2 * 10<sup>5</sup></code>
39+
*`s` consists only of lowercase English letters.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
packageg3201_3300.s3224_minimum_array_changes_to_make_differences_equal;
2+
3+
// #Medium #Array #Hash_Table #Prefix_Sum #2024_07_23_Time_4_ms_(100.00%)_Space_58.4_MB_(41.64%)
4+
5+
publicclassSolution {
6+
publicintminChanges(int[]nums,intk) {
7+
int[]cm =newint[k +2];
8+
for (inti =0;i <nums.length /2;i++) {
9+
inta =Math.min(nums[i],nums[nums.length -1 -i]);
10+
intb =Math.max(nums[i],nums[nums.length -1 -i]);
11+
intd =b -a;
12+
if (d >0) {
13+
cm[0]++;
14+
cm[d]--;
15+
cm[d +1]++;
16+
intmax =Math.max(a,k -b) +d;
17+
cm[max +1]++;
18+
}else {
19+
cm[1]++;
20+
intmax =Math.max(a,k -a);
21+
cm[max +1]++;
22+
}
23+
}
24+
intsum =cm[0];
25+
intres =cm[0];
26+
for (inti =1;i <=k;i++) {
27+
sum +=cm[i];
28+
res =Math.min(res,sum);
29+
}
30+
returnres;
31+
}
32+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
3224\. Minimum Array Changes to Make Differences Equal
2+
3+
Medium
4+
5+
You are given an integer array`nums` of size`n` where`n` is**even**, and an integer`k`.
6+
7+
You can perform some changes on the array, where in one change you can replace**any** element in the array with**any** integer in the range from`0` to`k`.
8+
9+
You need to perform some changes (possibly none) such that the final array satisfies the following condition:
10+
11+
* There exists an integer`X` such that`abs(a[i] - a[n - i - 1]) = X` for all`(0 <= i < n)`.
12+
13+
Return the**minimum** number of changes required to satisfy the above condition.
14+
15+
**Example 1:**
16+
17+
**Input:** nums =[1,0,1,2,4,3], k = 4
18+
19+
**Output:** 2
20+
21+
**Explanation:**
22+
We can perform the following changes:
23+
24+
* Replace`nums[1]` by 2. The resulting array is <code>nums =[1,<ins>**2**</ins>,1,2,4,3]</code>.
25+
* Replace`nums[3]` by 3. The resulting array is <code>nums =[1,2,1,<ins>**3**</ins>,4,3]</code>.
26+
27+
The integer`X` will be 2.
28+
29+
**Example 2:**
30+
31+
**Input:** nums =[0,1,2,3,3,6,5,4], k = 6
32+
33+
**Output:** 2
34+
35+
**Explanation:**
36+
We can perform the following operations:
37+
38+
* Replace`nums[3]` by 0. The resulting array is <code>nums =[0,1,2,<ins>**0**</ins>,3,6,5,4]</code>.
39+
* Replace`nums[4]` by 4. The resulting array is <code>nums =[0,1,2,0,**<ins>4</ins>**,6,5,4]</code>.
40+
41+
The integer`X` will be 4.
42+
43+
**Constraints:**
44+
45+
* <code>2 <= n == nums.length <= 10<sup>5</sup></code>
46+
*`n` is even.
47+
* <code>0 <= nums[i] <= k <= 10<sup>5</sup></code>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
packageg3201_3300.s3225_maximum_score_from_grid_operations;
2+
3+
// #Hard #Array #Dynamic_Programming #Matrix #Prefix_Sum
4+
// #2024_07_23_Time_21_ms_(100.00%)_Space_45.1_MB_(96.92%)
5+
6+
publicclassSolution {
7+
publiclongmaximumScore(int[][]grid) {
8+
finalintn =grid.length;
9+
long[]dp1 =newlong[n];
10+
long[]dp2 =newlong[n +1];
11+
long[]dp3 =newlong[n +1];
12+
long[]dp12 =newlong[n];
13+
long[]dp22 =newlong[n +1];
14+
long[]dp32 =newlong[n +1];
15+
longres =0;
16+
for (inti =0;i <n; ++i) {
17+
longsum =0;
18+
longpre =0;
19+
for (int[]ints :grid) {
20+
sum +=ints[i];
21+
}
22+
for (intj =n -1;j >=0; --j) {
23+
longs2 =sum;
24+
dp12[j] =s2 +dp3[n];
25+
for (intk =0;k <=j; ++k) {
26+
s2 -=grid[k][i];
27+
longv =Math.max(dp1[k] +s2,dp3[j] +s2);
28+
v =Math.max(v,pre +s2);
29+
dp12[j] =Math.max(dp12[j],v);
30+
if (k ==j) {
31+
dp22[j] =dp32[j] =v;
32+
res =Math.max(res,v);
33+
}
34+
}
35+
if (i >0) {
36+
pre =Math.max(pre +grid[j][i],dp2[j] +grid[j][i]);
37+
}
38+
sum -=grid[j][i];
39+
}
40+
dp22[n] =dp32[n] =pre;
41+
res =Math.max(res,pre);
42+
for (intj =1;j <=n; ++j) {
43+
dp32[j] =Math.max(dp32[j],dp32[j -1]);
44+
}
45+
long[]tem =dp1;
46+
dp1 =dp12;
47+
dp12 =tem;
48+
tem =dp2;
49+
dp2 =dp22;
50+
dp22 =tem;
51+
tem =dp3;
52+
dp3 =dp32;
53+
dp32 =tem;
54+
}
55+
returnres;
56+
}
57+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
3225\. Maximum Score From Grid Operations
2+
3+
Hard
4+
5+
You are given a 2D matrix`grid` of size`n x n`. Initially, all cells of the grid are colored white. In one operation, you can select any cell of indices`(i, j)`, and color black all the cells of the <code>j<sup>th</sup></code> column starting from the top row down to the <code>i<sup>th</sup></code> row.
6+
7+
The grid score is the sum of all`grid[i][j]` such that cell`(i, j)` is white and it has a horizontally adjacent black cell.
8+
9+
Return the**maximum** score that can be achieved after some number of operations.
10+
11+
**Example 1:**
12+
13+
**Input:** grid =[[0,0,0,0,0],[0,0,3,0,0],[0,1,0,0,0],[5,0,0,3,0],[0,0,0,0,2]]
14+
15+
**Output:** 11
16+
17+
**Explanation:**
18+
19+
![](https://assets.leetcode.com/uploads/2024/05/11/one.png)
20+
21+
In the first operation, we color all cells in column 1 down to row 3, and in the second operation, we color all cells in column 4 down to the last row. The score of the resulting grid is`grid[3][0] + grid[1][2] + grid[3][3]` which is equal to 11.
22+
23+
**Example 2:**
24+
25+
**Input:** grid =[[10,9,0,0,15],[7,1,0,8,0],[5,20,0,11,0],[0,0,0,1,2],[8,12,1,10,3]]
26+
27+
**Output:** 94
28+
29+
**Explanation:**
30+
31+
![](https://assets.leetcode.com/uploads/2024/05/11/two-1.png)
32+
33+
We perform operations on 1, 2, and 3 down to rows 1, 4, and 0, respectively. The score of the resulting grid is`grid[0][0] + grid[1][0] + grid[2][1] + grid[4][1] + grid[1][3] + grid[2][3] + grid[3][3] + grid[4][3] + grid[0][4]` which is equal to 94.
34+
35+
**Constraints:**
36+
37+
*`1 <= n == grid.length <= 100`
38+
*`n == grid[i].length`
39+
* <code>0 <= grid[i][j] <= 10<sup>9</sup></code>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
packageg3201_3300.s3226_number_of_bit_changes_to_make_two_integers_equal;
2+
3+
// #Easy #Bit_Manipulation #2024_07_23_Time_0_ms_(100.00%)_Space_40.5_MB_(97.19%)
4+
5+
publicclassSolution {
6+
publicintminChanges(intn,intk) {
7+
if ((n |k) !=n) {
8+
return -1;
9+
}
10+
intcnt =0;
11+
while (n >0 ||k >0) {
12+
intbitN =n &1;
13+
intbitK =k &1;
14+
if (bitN ==1 &&bitK ==0) {
15+
cnt++;
16+
}
17+
n >>=1;
18+
k >>=1;
19+
}
20+
returncnt;
21+
}
22+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
3226\. Number of Bit Changes to Make Two Integers Equal
2+
3+
Easy
4+
5+
You are given two positive integers`n` and`k`.
6+
7+
You can choose**any** bit in the**binary representation** of`n` that is equal to 1 and change it to 0.
8+
9+
Return the_number of changes_ needed to make`n` equal to`k`. If it is impossible, return -1.
10+
11+
**Example 1:**
12+
13+
**Input:** n = 13, k = 4
14+
15+
**Output:** 2
16+
17+
**Explanation:**
18+
Initially, the binary representations of`n` and`k` are <code>n = (1101)<sub>2</sub></code> and <code>k = (0100)<sub>2</sub></code>.
19+
We can change the first and fourth bits of`n`. The resulting integer is <code>n = (<ins>**0**</ins>10<ins>**0**</ins>)<sub>2</sub> = k</code>.
20+
21+
**Example 2:**
22+
23+
**Input:** n = 21, k = 21
24+
25+
**Output:** 0
26+
27+
**Explanation:**
28+
`n` and`k` are already equal, so no changes are needed.
29+
30+
**Example 3:**
31+
32+
**Input:** n = 14, k = 13
33+
34+
**Output:**\-1
35+
36+
**Explanation:**
37+
It is not possible to make`n` equal to`k`.
38+
39+
**Constraints:**
40+
41+
* <code>1 <= n, k <= 10<sup>6</sup></code>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
packageg3201_3300.s3227_vowels_game_in_a_string;
2+
3+
// #Medium #String #Math #Game_Theory #Brainteaser
4+
// #2024_07_23_Time_4_ms_(96.15%)_Space_45.3_MB_(96.39%)
5+
6+
publicclassSolution {
7+
publicbooleandoesAliceWin(Strings) {
8+
for (inti =0;i <s.length();i++) {
9+
charcurr =s.charAt(i);
10+
if (curr =='a' ||curr =='e' ||curr =='i' ||curr =='o' ||curr =='u') {
11+
returntrue;
12+
}
13+
}
14+
returnfalse;
15+
}
16+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp