Skip to content

Added tasks 3222-3229 #1793

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Write your MySQL query statement below
# #Medium #2024_07_18_Time_272_ms_(100.00%)_Space_0B_(100.00%)
# #Medium #Database #2024_07_23_Time_248_ms_(85.85%)_Space_0B_(100.00%)
select transaction_date,
sum(case when amount%2<>0 then amount else 0 end) as odd_sum,
sum(case when amount%2=0 then amount else 0 end) as even_sum from transactions
group by transaction_date order by transaction_date;
group by transaction_date order by transaction_date asc;
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package g3201_3300.s3222_find_the_winning_player_in_coin_game;

// #Easy #Math #Simulation #Game_Theory #2024_07_23_Time_0_ms_(100.00%)_Space_41.6_MB_(67.81%)

public class Solution {
public String losingPlayer(int x, int y) {
boolean w = false;
while (x > 0 && y >= 4) {
x--;
y -= 4;
w = !w;
}
return w ? "Alice" : "Bob";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
3222\. Find the Winning Player in Coin Game

Easy

You are given two **positive** integers `x` and `y`, denoting the number of coins with values 75 and 10 _respectively_.

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.

Return the _name_ of the player who wins the game if both players play **optimally**.

**Example 1:**

**Input:** x = 2, y = 7

**Output:** "Alice"

**Explanation:**

The game ends in a single turn:

* Alice picks 1 coin with a value of 75 and 4 coins with a value of 10.

**Example 2:**

**Input:** x = 4, y = 11

**Output:** "Bob"

**Explanation:**

The game ends in 2 turns:

* Alice picks 1 coin with a value of 75 and 4 coins with a value of 10.
* Bob picks 1 coin with a value of 75 and 4 coins with a value of 10.

**Constraints:**

* `1 <= x, y <= 100`
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package g3201_3300.s3223_minimum_length_of_string_after_operations;

// #Medium #String #Hash_Table #Counting #2024_07_23_Time_9_ms_(94.23%)_Space_46.5_MB_(38.50%)

public class Solution {
public int minimumLength(String s) {
int[] freq = new int[26];
for (int i = 0; i < 26; i++) {
freq[i] = 0;
}
for (int i = 0; i < s.length(); i++) {
freq[s.charAt(i) - 'a']++;
}
int c = 0;
for (int i : freq) {
if (i != 0) {
if (i % 2 == 0) {
c += 2;
} else {
c += 1;
}
}
}
return c;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
3223\. Minimum Length of String After Operations

Medium

You are given a string `s`.

You can perform the following process on `s` **any** number of times:

* 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]`.
* Delete the **closest** character to the **left** of index `i` that is equal to `s[i]`.
* Delete the **closest** character to the **right** of index `i` that is equal to `s[i]`.

Return the **minimum** length of the final string `s` that you can achieve.

**Example 1:**

**Input:** s = "abaacbcbb"

**Output:** 5

**Explanation:**
We do the following operations:

* Choose index 2, then remove the characters at indices 0 and 3. The resulting string is `s = "bacbcbb"`.
* Choose index 3, then remove the characters at indices 0 and 5. The resulting string is `s = "acbcb"`.

**Example 2:**

**Input:** s = "aa"

**Output:** 2

**Explanation:**
We cannot perform any operations, so we return the length of the original string.

**Constraints:**

* <code>1 <= s.length <= 2 * 10<sup>5</sup></code>
* `s` consists only of lowercase English letters.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package g3201_3300.s3224_minimum_array_changes_to_make_differences_equal;

// #Medium #Array #Hash_Table #Prefix_Sum #2024_07_23_Time_4_ms_(100.00%)_Space_58.4_MB_(41.64%)

public class Solution {
public int minChanges(int[] nums, int k) {
int[] cm = new int[k + 2];
for (int i = 0; i < nums.length / 2; i++) {
int a = Math.min(nums[i], nums[nums.length - 1 - i]);
int b = Math.max(nums[i], nums[nums.length - 1 - i]);
int d = b - a;
if (d > 0) {
cm[0]++;
cm[d]--;
cm[d + 1]++;
int max = Math.max(a, k - b) + d;
cm[max + 1]++;
} else {
cm[1]++;
int max = Math.max(a, k - a);
cm[max + 1]++;
}
}
int sum = cm[0];
int res = cm[0];
for (int i = 1; i <= k; i++) {
sum += cm[i];
res = Math.min(res, sum);
}
return res;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
3224\. Minimum Array Changes to Make Differences Equal

Medium

You are given an integer array `nums` of size `n` where `n` is **even**, and an integer `k`.

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`.

You need to perform some changes (possibly none) such that the final array satisfies the following condition:

* There exists an integer `X` such that `abs(a[i] - a[n - i - 1]) = X` for all `(0 <= i < n)`.

Return the **minimum** number of changes required to satisfy the above condition.

**Example 1:**

**Input:** nums = [1,0,1,2,4,3], k = 4

**Output:** 2

**Explanation:**
We can perform the following changes:

* Replace `nums[1]` by 2. The resulting array is <code>nums = [1,<ins>**2**</ins>,1,2,4,3]</code>.
* Replace `nums[3]` by 3. The resulting array is <code>nums = [1,2,1,<ins>**3**</ins>,4,3]</code>.

The integer `X` will be 2.

**Example 2:**

**Input:** nums = [0,1,2,3,3,6,5,4], k = 6

**Output:** 2

**Explanation:**
We can perform the following operations:

* Replace `nums[3]` by 0. The resulting array is <code>nums = [0,1,2,<ins>**0**</ins>,3,6,5,4]</code>.
* Replace `nums[4]` by 4. The resulting array is <code>nums = [0,1,2,0,**<ins>4</ins>**,6,5,4]</code>.

The integer `X` will be 4.

**Constraints:**

* <code>2 <= n == nums.length <= 10<sup>5</sup></code>
* `n` is even.
* <code>0 <= nums[i] <= k <= 10<sup>5</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package g3201_3300.s3225_maximum_score_from_grid_operations;

// #Hard #Array #Dynamic_Programming #Matrix #Prefix_Sum
// #2024_07_23_Time_21_ms_(100.00%)_Space_45.1_MB_(96.92%)

public class Solution {
public long maximumScore(int[][] grid) {
final int n = grid.length;
long[] dp1 = new long[n];
long[] dp2 = new long[n + 1];
long[] dp3 = new long[n + 1];
long[] dp12 = new long[n];
long[] dp22 = new long[n + 1];
long[] dp32 = new long[n + 1];
long res = 0;
for (int i = 0; i < n; ++i) {
long sum = 0;
long pre = 0;
for (int[] ints : grid) {
sum += ints[i];
}
for (int j = n - 1; j >= 0; --j) {
long s2 = sum;
dp12[j] = s2 + dp3[n];
for (int k = 0; k <= j; ++k) {
s2 -= grid[k][i];
long v = Math.max(dp1[k] + s2, dp3[j] + s2);
v = Math.max(v, pre + s2);
dp12[j] = Math.max(dp12[j], v);
if (k == j) {
dp22[j] = dp32[j] = v;
res = Math.max(res, v);
}
}
if (i > 0) {
pre = Math.max(pre + grid[j][i], dp2[j] + grid[j][i]);
}
sum -= grid[j][i];
}
dp22[n] = dp32[n] = pre;
res = Math.max(res, pre);
for (int j = 1; j <= n; ++j) {
dp32[j] = Math.max(dp32[j], dp32[j - 1]);
}
long[] tem = dp1;
dp1 = dp12;
dp12 = tem;
tem = dp2;
dp2 = dp22;
dp22 = tem;
tem = dp3;
dp3 = dp32;
dp32 = tem;
}
return res;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
3225\. Maximum Score From Grid Operations

Hard

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.

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.

Return the **maximum** score that can be achieved after some number of operations.

**Example 1:**

**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]]

**Output:** 11

**Explanation:**

![](https://assets.leetcode.com/uploads/2024/05/11/one.png)

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.

**Example 2:**

**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]]

**Output:** 94

**Explanation:**

![](https://assets.leetcode.com/uploads/2024/05/11/two-1.png)

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.

**Constraints:**

* `1 <= n == grid.length <= 100`
* `n == grid[i].length`
* <code>0 <= grid[i][j] <= 10<sup>9</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package g3201_3300.s3226_number_of_bit_changes_to_make_two_integers_equal;

// #Easy #Bit_Manipulation #2024_07_23_Time_0_ms_(100.00%)_Space_40.5_MB_(97.19%)

public class Solution {
public int minChanges(int n, int k) {
if ((n | k) != n) {
return -1;
}
int cnt = 0;
while (n > 0 || k > 0) {
int bitN = n & 1;
int bitK = k & 1;
if (bitN == 1 && bitK == 0) {
cnt++;
}
n >>= 1;
k >>= 1;
}
return cnt;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
3226\. Number of Bit Changes to Make Two Integers Equal

Easy

You are given two positive integers `n` and `k`.

You can choose **any** bit in the **binary representation** of `n` that is equal to 1 and change it to 0.

Return the _number of changes_ needed to make `n` equal to `k`. If it is impossible, return -1.

**Example 1:**

**Input:** n = 13, k = 4

**Output:** 2

**Explanation:**
Initially, the binary representations of `n` and `k` are <code>n = (1101)<sub>2</sub></code> and <code>k = (0100)<sub>2</sub></code>.
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>.

**Example 2:**

**Input:** n = 21, k = 21

**Output:** 0

**Explanation:**
`n` and `k` are already equal, so no changes are needed.

**Example 3:**

**Input:** n = 14, k = 13

**Output:** \-1

**Explanation:**
It is not possible to make `n` equal to `k`.

**Constraints:**

* <code>1 <= n, k <= 10<sup>6</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package g3201_3300.s3227_vowels_game_in_a_string;

// #Medium #String #Math #Game_Theory #Brainteaser
// #2024_07_23_Time_4_ms_(96.15%)_Space_45.3_MB_(96.39%)

public class Solution {
public boolean doesAliceWin(String s) {
for (int i = 0; i < s.length(); i++) {
char curr = s.charAt(i);
if (curr == 'a' || curr == 'e' || curr == 'i' || curr == 'o' || curr == 'u') {
return true;
}
}
return false;
}
}
Loading
Loading