Skip to content
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
@@ -0,0 +1,44 @@
package g3901_4000.s3920_maximize_fixed_points_after_deletions;

// #Hard #Array #Sorting #Binary_Search #Senior_Staff #Weekly_Contest_500
// #2026_09_16_Time_19_ms_(100.00%)_Space_124.05_MB_(97.30%)

import java.util.Arrays;

public class Solution {
public int maxFixedPoints(int[] nums) {
int n = nums.length;
long[] arr = new long[n];
int index = 0;
for (int i = 0; i < n; i++) {
if (nums[i] <= i) {
arr[index++] = (long) i - nums[i] << 32 | nums[i];
}
}
if (index == 0) {
return 0;
}
Arrays.sort(arr, 0, index);
int max = 0;
int[] lis = new int[index];
lis[0] = (int) arr[0];
for (int i = 1; i < index; i++) {
int val = (int) arr[i];
lis[val > lis[max] ? ++max : binarySearch(lis, val, max)] = val;
}
return max + 1;
}

private int binarySearch(int[] arr, int target, int right) {
int left = 0;
while (left < right) {
int mid = left + right >>> 1;
if (arr[mid] >= target) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
3920\. Maximize Fixed Points After Deletions

Hard

You are given an integer array `nums`.

A position `i` is called a **fixed point** if `nums[i] == i`.

You are allowed to delete **any** number of elements (including zero) from the array. After each deletion, the remaining elements **shift left**, and indices are reassigned starting from 0.

Return an integer denoting the **maximum** number of fixed points that can be achieved after performing any number of deletions.

**Example 1:**

**Input:** nums = [0,2,1]

**Output:** 2

**Explanation:**

* Delete `nums[1] = 2`. The array becomes `[0, 1]`.
* Now, `nums[0] = 0` and `nums[1] = 1`, so both indices are fixed points.
* Thus, the answer is 2.

**Example 2:**

**Input:** nums = [3,1,2]

**Output:** 2

**Explanation:**

* Do not delete any elements. The array remains `[3, 1, 2]`.
* Here, `nums[1] = 1` and `nums[2] = 2`, so these indices are fixed points.
* Thus, the answer is 2.

**Example 3:**

**Input:** nums = [1,0,1,2]

**Output:** 3

**Explanation:**

* Delete `nums[0] = 1`. The array becomes `[0, 1, 2]`.
* Now, `nums[0] = 0`, `nums[1] = 1`, and `nums[2] = 2`, so all indices are fixed points.
* Thus, the answer is 3.

**Constraints:**

* <code>1 <= nums.length <= 10<sup>5</sup></code>
* <code>0 <= nums[i] <= 10<sup>5</sup></code>
26 changes: 26 additions & 0 deletions src/main/java/g3901_4000/s3921_score_validator/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package g3901_4000.s3921_score_validator;

// #Easy #Array #String #Simulation #Mid_Level #Biweekly_Contest_182
// #2026_09_16_Time_2_ms_(91.74%)_Space_47.44_MB_(30.84%)

public class Solution {
public int[] scoreValidator(String[] events) {
int counter = 0;
int score = 0;
for (String i : events) {
if (counter == 10) {
break;
}
if (i.equals("WD")) {
score += 1;
} else if (i.equals("NB")) {
score += 1;
} else if (i.equals("W")) {
counter += 1;
} else {
score += Integer.parseInt(i);
}
}
return new int[] {score, counter};
}
}
73 changes: 73 additions & 0 deletions src/main/java/g3901_4000/s3921_score_validator/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
3921\. Score Validator

Easy

You are given a string array `events`.

Initially, `score = 0` and `counter = 0`. Each element in `events` is one of the following:

* `"0"`, `"1"`, `"2"`, `"3"`, `"4"`, `"6"`: Add that value to the total score.
* `"W"`: Increase the counter by 1. No score is added.
* `"WD"`: Add 1 to the total score.
* `"NB"`: Add 1 to the total score.

Process the array from left to right. Stop processing when either:

* All elements in `events` have been processed, or
* The counter becomes 10.

Return an integer array `[score, counter]`, where:

* `score` is the final total score.
* `counter` is the final counter value.

**Example 1:**

**Input:** events = ["1","4","W","6","WD"]

**Output:** [12,1]

**Explanation:**

| Event | Score | Counter |
|---|---:|---:|
| `"1"` | 1 | 0 |
| `"4"` | 5 | 0 |
| `"W"` | 5 | 1 |
| `"6"` | 11 | 1 |
| `"WD"` | 12 | 1 |

Final result: `[12, 1]`.

**Example 2:**

**Input:** events = ["WD","NB","0","4","4"]

**Output:** [10,0]

**Explanation:**

| Event | Score | Counter |
|---|---:|---:|
| `"WD"` | 1 | 0 |
| `"NB"` | 2 | 0 |
| `"0"` | 2 | 0 |
| `"4"` | 6 | 0 |
| `"4"` | 10 | 0 |

Final result: `[10, 0]`.

**Example 3:**

**Input:** events = ["W","W","W","W","W","W","W","W","W","W","W"]

**Output:** [0,10]

**Explanation:**

After 10 occurrences of `"W"`, the counter reaches 10, so processing stops. The remaining events are ignored.

**Constraints:**

* `1 <= events.length <= 1000`
* `events[i]` is one of `"0"`, `"1"`, `"2"`, `"3"`, `"4"`, `"6"`, `"W"`, `"WD"`, or `"NB"`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package g3901_4000.s3920_maximize_fixed_points_after_deletions;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void maxFixedPoints() {
assertThat(new Solution().maxFixedPoints(new int[] {0, 2, 1}), equalTo(2));
}

@Test
void maxFixedPoints2() {
assertThat(new Solution().maxFixedPoints(new int[] {3, 1, 2}), equalTo(2));
}

@Test
void maxFixedPoints3() {
assertThat(new Solution().maxFixedPoints(new int[] {1, 0, 1, 2}), equalTo(3));
}

@Test
void maxFixedPoints4() {
assertThat(new Solution().maxFixedPoints(new int[] {}), equalTo(0));
}

@Test
void maxFixedPoints5() {
assertThat(new Solution().maxFixedPoints(new int[] {1}), equalTo(0));
}

@Test
void maxFixedPoints6() {
assertThat(new Solution().maxFixedPoints(new int[] {0}), equalTo(1));
}

@Test
void maxFixedPoints7() {
assertThat(new Solution().maxFixedPoints(new int[] {5, 5, 5}), equalTo(0));
}

@Test
void maxFixedPoints8() {
assertThat(new Solution().maxFixedPoints(new int[] {0, 0, 0, 0}), equalTo(1));
}

@Test
void maxFixedPoints9() {
assertThat(new Solution().maxFixedPoints(new int[] {0, 1, 2, 3, 4}), equalTo(5));
}

@Test
void maxFixedPoints10() {
assertThat(new Solution().maxFixedPoints(new int[] {0, 0, 1, 2, 3}), equalTo(4));
}

@Test
void maxFixedPoints11() {
assertThat(new Solution().maxFixedPoints(new int[] {0, 2, 2, 3}), equalTo(3));
}

@Test
void maxFixedPoints12() {
assertThat(new Solution().maxFixedPoints(new int[] {0, 3, 1, 2}), equalTo(3));
}

@Test
void maxFixedPoints13() {
assertThat(new Solution().maxFixedPoints(new int[] {0, 3, 2, 1}), equalTo(2));
}

@Test
void maxFixedPoints14() {
assertThat(new Solution().maxFixedPoints(new int[] {0, 1, 0, 1, 2}), equalTo(3));
}

@Test
void maxFixedPoints15() {
assertThat(new Solution().maxFixedPoints(new int[] {0, 2, 1, 0, 3}), equalTo(3));
}

@Test
void maxFixedPoints16() {
assertThat(new Solution().maxFixedPoints(new int[] {0, 4, 1, 2, 3}), equalTo(4));
}

@Test
void maxFixedPoints17() {
assertThat(new Solution().maxFixedPoints(new int[] {0, 5, 4, 3, 2, 1}), equalTo(2));
}

@Test
void maxFixedPoints18() {
assertThat(new Solution().maxFixedPoints(new int[] {0, 2, 3, 1, 4}), equalTo(2));
}

@Test
void maxFixedPoints19() {
assertThat(new Solution().maxFixedPoints(new int[] {0, 1, 3, 2, 4}), equalTo(3));
}

@Test
void maxFixedPoints20() {
assertThat(new Solution().maxFixedPoints(new int[] {0, 2, 1, 4, 3, 5}), equalTo(3));
}
}
33 changes: 33 additions & 0 deletions src/test/java/g3901_4000/s3921_score_validator/SolutionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package g3901_4000.s3921_score_validator;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void scoreValidator() {
assertThat(
new Solution().scoreValidator(new String[] {"1", "4", "W", "6", "WD"}),
equalTo(new int[] {12, 1}));
}

@Test
void scoreValidator2() {
assertThat(
new Solution().scoreValidator(new String[] {"WD", "NB", "0", "4", "4"}),
equalTo(new int[] {10, 0}));
}

@Test
void scoreValidator3() {
assertThat(
new Solution()
.scoreValidator(
new String[] {
"W", "W", "W", "W", "W", "W", "W", "W", "W", "W", "W"
}),
equalTo(new int[] {0, 10}));
}
}