-
Notifications
You must be signed in to change notification settings - Fork 108
Added tasks 3806-3828 #2122
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
Added tasks 3806-3828 #2122
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e3a7579
Added tasks 3806-3828
ThanhNIT ef6073e
Refactor bestTower method for improved readability
javadev ee65db6
Update readme.md
ThanhNIT a88abdc
Update README by removing schema sections
javadev 3251624
Clean up whitespace in Solution.java
javadev cf5dce1
Improve DFS and edge management in Solution
javadev f314963
Refactor maximum capacity calculation logic
javadev 7c35a8f
Refactor Solution.java for lexicographically smallest string
javadev a676f70
Optimize BFS and distance checks in Solution.java
javadev 8c084b2
Remove unnecessary blank line in Solution.java
javadev d9cee8e
Refactor minPartitionScore and solveWithPenalty methods
javadev be249ce
Refactor reverseByType method to improve readability
javadev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
src/main/java/g3801_3900/s3806_maximum_bitwise_and_after_increment_operations/Solution.java
This file contains hidden or 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,40 @@ | ||
| package g3801_3900.s3806_maximum_bitwise_and_after_increment_operations; | ||
|
|
||
| // #Hard #Array #Sorting #Greedy #Bit_Manipulation #Senior_Staff #Weekly_Contest_484 | ||
| // #2026_06_09_Time_107_ms_(78.72%)_Space_51.33_MB_(44.68%) | ||
|
|
||
| import java.util.Arrays; | ||
|
|
||
| public class Solution { | ||
| public int maximumAND(int[] a, int b, int c) { | ||
| long e = 0; | ||
| int f = a.length; | ||
| long[] g = new long[f]; | ||
| for (int h = 30; h >= 0; --h) { | ||
| long i = e | (1L << h); | ||
| for (int j = 0; j < f; ++j) { | ||
| long k = a[j]; | ||
| long l = i & ~k; | ||
| if (l == 0) { | ||
| g[j] = 0; | ||
| } else { | ||
| int n = 63 - Long.numberOfLeadingZeros(l); | ||
| while (((k >> n) & 1) == 1) { | ||
| n++; | ||
| } | ||
| long o = (1L << n) - 1; | ||
| g[j] = ((k & ~o) | (1L << n) | (i & o)) - k; | ||
| } | ||
| } | ||
| Arrays.sort(g); | ||
| long p = 0; | ||
| for (int q = 0; q < c; ++q) { | ||
| p += g[q]; | ||
| } | ||
| if (p <= b) { | ||
| e = i; | ||
| } | ||
| } | ||
| return (int) e; | ||
| } | ||
| } |
55 changes: 55 additions & 0 deletions
55
.../java/g3801_3900/s3806_maximum_bitwise_and_after_increment_operations/readme.md
This file contains hidden or 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,55 @@ | ||
| 3806\. Maximum Bitwise AND After Increment Operations | ||
|
|
||
| Hard | ||
|
|
||
| You are given an integer array `nums` and two integers `k` and `m`. | ||
|
|
||
| You may perform **at most** `k` operations. In one operation, you may choose any index `i` and **increase** `nums[i]` by 1. | ||
|
|
||
| Return an integer denoting the **maximum** possible **bitwise AND** of any **subset** of size `m` after performing up to `k` operations optimally. | ||
|
|
||
| **Example 1:** | ||
|
|
||
| **Input:** nums = [3,1,2], k = 8, m = 2 | ||
|
|
||
| **Output:** 6 | ||
|
|
||
| **Explanation:** | ||
|
|
||
| * We need a subset of size `m = 2`. Choose indices `[0, 2]`. | ||
| * Increase `nums[0] = 3` to 6 using 3 operations, and increase `nums[2] = 2` to 6 using 4 operations. | ||
| * The total number of operations used is 7, which is not greater than `k = 8`. | ||
| * The two chosen values become `[6, 6]`, and their bitwise AND is `6`, which is the maximum possible. | ||
|
|
||
| **Example 2:** | ||
|
|
||
| **Input:** nums = [1,2,8,4], k = 7, m = 3 | ||
|
|
||
| **Output:** 4 | ||
|
|
||
| **Explanation:** | ||
|
|
||
| * We need a subset of size `m = 3`. Choose indices `[0, 1, 3]`. | ||
| * Increase `nums[0] = 1` to 4 using 3 operations, increase `nums[1] = 2` to 4 using 2 operations, and keep `nums[3] = 4`. | ||
| * The total number of operations used is 5, which is not greater than `k = 7`. | ||
| * The three chosen values become `[4, 4, 4]`, and their bitwise AND is 4, which is the maximum possible. | ||
|
|
||
| **Example 3:** | ||
|
|
||
| **Input:** nums = [1,1], k = 3, m = 2 | ||
|
|
||
| **Output:** 2 | ||
|
|
||
| **Explanation:** | ||
|
|
||
| * We need a subset of size `m = 2`. Choose indices `[0, 1]`. | ||
| * Increase both values from 1 to 2 using 1 operation each. | ||
| * The total number of operations used is 2, which is not greater than `k = 3`. | ||
| * The two chosen values become `[2, 2]`, and their bitwise AND is 2, which is the maximum possible. | ||
|
|
||
| **Constraints:** | ||
|
|
||
| * <code>1 <= n == nums.length <= 5 * 10<sup>4</sup></code> | ||
| * <code>1 <= nums[i] <= 10<sup>9</sup></code> | ||
| * <code>1 <= k <= 10<sup>9</sup></code> | ||
| * `1 <= m <= n` |
1 change: 1 addition & 0 deletions
1
src/main/java/g3801_3900/s3808_find_emotionally_consistent_users/readme.md
This file contains hidden or 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 @@ | ||
| 3808\. | ||
50 changes: 50 additions & 0 deletions
50
src/main/java/g3801_3900/s3808_find_emotionally_consistent_users/script.sql
This file contains hidden or 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 @@ | ||
| # #Medium #2026_06_09_Time_290_ms_(87.69%)_Space_0.0_MB_(100.00%) | ||
| # Write your MySQL query statement below | ||
| WITH user_selection AS | ||
| (SELECT | ||
| user_id, | ||
| COUNT(reaction) AS total_reaction_count | ||
| FROM | ||
| reactions | ||
| GROUP BY | ||
| user_id | ||
| HAVING | ||
| COUNT(DISTINCT content_id) >= 5 | ||
| ), | ||
| reaction_counts | ||
| AS | ||
| (SELECT | ||
| user_id, | ||
| reaction, | ||
| COUNT(*) AS reaction_count | ||
| FROM | ||
| reactions | ||
| group by | ||
| user_id, | ||
| reaction | ||
| ), | ||
| ranked_reactions AS ( | ||
| -- Step 2: Use a window function to find the max for each user | ||
| SELECT | ||
| user_id, | ||
| reaction, | ||
| reaction_count, | ||
| RANK() OVER(PARTITION BY user_id ORDER BY reaction_count DESC) as rnk | ||
| FROM reaction_counts | ||
| ) | ||
| SELECT | ||
| rc.user_id, | ||
| rc.reaction AS dominant_reaction, | ||
| ROUND(reaction_count / total_reaction_count, 2) AS reaction_ratio | ||
| FROM | ||
| ranked_reactions rc | ||
| INNER JOIN | ||
| user_selection us | ||
| ON | ||
| rc.user_id = us.user_id | ||
| WHERE | ||
| rc.rnk = 1 | ||
| AND ROUND(reaction_count / total_reaction_count, 2) >= 0.60 | ||
| ORDER BY | ||
| 3 DESC, | ||
| rc.user_id |
35 changes: 35 additions & 0 deletions
35
src/main/java/g3801_3900/s3809_best_reachable_tower/Solution.java
This file contains hidden or 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,35 @@ | ||
| package g3801_3900.s3809_best_reachable_tower; | ||
|
|
||
| // #Medium #Array #Senior #Biweekly_Contest_174 | ||
| // #2026_06_09_Time_3_ms_(70.30%)_Space_219.56_MB_(26.73%) | ||
|
|
||
| public class Solution { | ||
| public int[] bestTower(int[][] towers, int[] center, int radius) { | ||
| int bestX = -1; | ||
| int bestY = -1; | ||
| int bestQ = -1; | ||
|
|
||
| int cx = center[0]; | ||
| int cy = center[1]; | ||
|
|
||
| for (int[] t : towers) { | ||
| int x = t[0]; | ||
| int y = t[1]; | ||
| int q = t[2]; | ||
|
|
||
| long dx = Math.abs((long) x - cx); | ||
| long dy = Math.abs((long) y - cy); | ||
|
|
||
| if (dx + dy <= radius | ||
| && (q > bestQ | ||
| || (q == bestQ | ||
| && (bestX == -1 || x < bestX || (x == bestX && y < bestY))))) { | ||
| bestQ = q; | ||
| bestX = x; | ||
| bestY = y; | ||
| } | ||
| } | ||
|
|
||
| return bestQ == -1 ? new int[] {-1, -1} : new int[] {bestX, bestY}; | ||
| } | ||
| } |
69 changes: 69 additions & 0 deletions
69
src/main/java/g3801_3900/s3809_best_reachable_tower/readme.md
This file contains hidden or 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,69 @@ | ||
| 3809\. Best Reachable Tower | ||
|
|
||
| Medium | ||
|
|
||
| You are given a 2D integer array `towers`, where <code>towers[i] = [x<sub>i</sub>, y<sub>i</sub>, q<sub>i</sub>]</code> represents the coordinates <code>(x<sub>i</sub>, y<sub>i</sub>)</code> and quality factor <code>q<sub>i</sub></code> of the <code>i<sup>th</sup></code> tower. | ||
|
|
||
| You are also given an integer array `center = [cx, cy]` representing your location, and an integer `radius`. | ||
|
|
||
| A tower is **reachable** if its **Manhattan distance** from `center` is **less than or equal** to `radius`. | ||
|
|
||
| Among all reachable towers: | ||
|
|
||
| * Return the coordinates of the tower with the **maximum** quality factor. | ||
| * If there is a tie, return the tower with the **lexicographically smallest** coordinate. If no tower is reachable, return `[-1, -1]`. | ||
|
|
||
| The **Manhattan Distance** between two cells <code>(x<sub>i</sub>, y<sub>i</sub>)</code> and <code>(x<sub>j</sub>, y<sub>j</sub>)</code> is <code>|x<sub>i</sub> - x<sub>j</sub>| + |y<sub>i</sub> - y<sub>j</sub>|</code>. | ||
|
|
||
| A coordinate <code>[x<sub>i</sub>, y<sub>i</sub>]</code> is **lexicographically smaller** than <code>[x<sub>j</sub>, y<sub>j</sub>]</code> if <code>x<sub>i</sub> < x<sub>j</sub></code>, or <code>x<sub>i</sub> == x<sub>j</sub></code> and <code>y<sub>i</sub> < y<sub>j</sub></code>. | ||
|
|
||
| `|x|` denotes the **absolute** **value** of `x`. | ||
|
|
||
| **Example 1:** | ||
|
|
||
| **Input:** towers = [[1,2,5], [2,1,7], [3,1,9]], center = [1,1], radius = 2 | ||
|
|
||
| **Output:** [3,1] | ||
|
|
||
| **Explanation:** | ||
|
|
||
| * Tower `[1, 2, 5]`: Manhattan distance = `|1 - 1| + |2 - 1| = 1`, reachable. | ||
| * Tower `[2, 1, 7]`: Manhattan distance = `|2 - 1| + |1 - 1| = 1`, reachable. | ||
| * Tower `[3, 1, 9]`: Manhattan distance = `|3 - 1| + |1 - 1| = 2`, reachable. | ||
|
|
||
| All towers are reachable. The maximum quality factor is 9, which corresponds to tower `[3, 1]`. | ||
|
|
||
| **Example 2:** | ||
|
|
||
| **Input:** towers = [[1,3,4], [2,2,4], [4,4,7]], center = [0,0], radius = 5 | ||
|
|
||
| **Output:** [1,3] | ||
|
|
||
| **Explanation:** | ||
|
|
||
| * Tower `[1, 3, 4]`: Manhattan distance = `|1 - 0| + |3 - 0| = 4`, reachable. | ||
| * Tower `[2, 2, 4]`: Manhattan distance = `|2 - 0| + |2 - 0| = 4`, reachable. | ||
| * Tower `[4, 4, 7]`: Manhattan distance = `|4 - 0| + |4 - 0| = 8`, not reachable. | ||
|
|
||
| Among the reachable towers, the maximum quality factor is 4. Both `[1, 3]` and `[2, 2]` have the same quality, so the lexicographically smaller coordinate is `[1, 3]`. | ||
|
|
||
| **Example 3:** | ||
|
|
||
| **Input:** towers = [[5,6,8], [0,3,5]], center = [1,2], radius = 1 | ||
|
|
||
| **Output:** [-1,-1] | ||
|
|
||
| **Explanation:** | ||
|
|
||
| * Tower `[5, 6, 8]`: Manhattan distance = `|5 - 1| + |6 - 2| = 8`, not reachable. | ||
| * Tower `[0, 3, 5]`: Manhattan distance = `|0 - 1| + |3 - 2| = 2`, not reachable. | ||
|
|
||
| No tower is reachable within the given radius, so `[-1, -1]` is returned. | ||
|
|
||
| **Constraints:** | ||
|
|
||
| * <code>1 <= towers.length <= 10<sup>5</sup></code> | ||
| * <code>towers[i] = [x<sub>i</sub>, y<sub>i</sub>, q<sub>i</sub>]</code> | ||
| * `center = [cx, cy]` | ||
| * <code>0 <= x<sub>i</sub>, y<sub>i</sub>, q<sub>i</sub>, cx, cy <= 10<sup>5</sup></code> | ||
| * <code>0 <= radius <= 10<sup>5</sup></code> |
19 changes: 19 additions & 0 deletions
19
src/main/java/g3801_3900/s3810_minimum_operations_to_reach_target_array/Solution.java
This file contains hidden or 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,19 @@ | ||
| package g3801_3900.s3810_minimum_operations_to_reach_target_array; | ||
|
|
||
| // #Medium #Array #Hash_Table #Greedy #Senior #Biweekly_Contest_174 | ||
| // #2026_06_09_Time_24_ms_(89.58%)_Space_123.26_MB_(72.92%) | ||
|
|
||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
|
|
||
| public class Solution { | ||
| public int minOperations(int[] nums, int[] target) { | ||
| Set<Integer> virelantos = new HashSet<>(); | ||
| for (int i = 0; i < nums.length; i++) { | ||
| if (nums[i] != target[i]) { | ||
| virelantos.add(nums[i]); | ||
| } | ||
| } | ||
| return virelantos.size(); | ||
| } | ||
| } |
54 changes: 54 additions & 0 deletions
54
src/main/java/g3801_3900/s3810_minimum_operations_to_reach_target_array/readme.md
This file contains hidden or 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,54 @@ | ||
| 3810\. Minimum Operations to Reach Target Array | ||
|
|
||
| Medium | ||
|
|
||
| You are given two integer arrays `nums` and `target`, each of length `n`, where `nums[i]` is the current value at index `i` and `target[i]` is the desired value at index `i`. | ||
|
|
||
| You may perform the following operation any number of times (including zero): | ||
|
|
||
| * Choose an integer value `x` | ||
| * Find all **maximal contiguous segments** where `nums[i] == x` (a segment is **maximal** if it cannot be extended to the left or right while keeping all values equal to `x`) | ||
| * For each such segment `[l, r]`, update **simultaneously**: | ||
| * `nums[l] = target[l], nums[l + 1] = target[l + 1], ..., nums[r] = target[r]` | ||
|
|
||
| Return the **minimum** number of operations required to make `nums` equal to `target`. | ||
|
|
||
| **Example 1:** | ||
|
|
||
| **Input:** nums = [1,2,3], target = [2,1,3] | ||
|
|
||
| **Output:** 2 | ||
|
|
||
| **Explanation:** | ||
|
|
||
| * Choose `x = 1`: maximal segment `[0, 0]` updated -> nums becomes `[2, 2, 3]` | ||
| * Choose `x = 2`: maximal segment `[0, 1]` updated (`nums[0]` stays 2, `nums[1]` becomes 1) -> `nums` becomes `[2, 1, 3]` | ||
| * Thus, 2 operations are required to convert `nums` to `target`. | ||
|
|
||
| **Example 2:** | ||
|
|
||
| **Input:** nums = [4,1,4], target = [5,1,4] | ||
|
|
||
| **Output:** 1 | ||
|
|
||
| **Explanation:** | ||
|
|
||
| * Choose `x = 4`: maximal segments `[0, 0]` and `[2, 2]` updated (`nums[2]` stays 4) -> `nums` becomes `[5, 1, 4]` | ||
| * Thus, 1 operation is required to convert `nums` to `target`. | ||
|
|
||
| **Example 3:** | ||
|
|
||
| **Input:** nums = [7,3,7], target = [5,5,9] | ||
|
|
||
| **Output:** 2 | ||
|
|
||
| **Explanation:** | ||
|
|
||
| * Choose `x = 7`: maximal segments `[0, 0]` and `[2, 2]` updated -> `nums` becomes `[5, 3, 9]` | ||
| * Choose `x = 3`: maximal segment `[1, 1]` updated -> `nums` becomes `[5, 5, 9]` | ||
| * Thus, 2 operations are required to convert `nums` to `target`. | ||
|
|
||
| **Constraints:** | ||
|
|
||
| * <code>1 <= n == nums.length == target.length <= 10<sup>5</sup></code> | ||
| * <code>1 <= nums[i], target[i] <= 10<sup>5</sup></code> |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add description