Skip to content

Commit

Permalink
feat: add java solution to lc problem: No.3048 (doocs#2387)
Browse files Browse the repository at this point in the history
No.3048.Earliest Second to Mark Indices I


Co-authored-by: Libin YANG <[email protected]>
  • Loading branch information
pursani9 and yanglbme authored Mar 8, 2024
1 parent 6b5b433 commit 891dc6e
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,51 @@
```

```java

class Solution {
public int earliestSecondToMarkIndices(int[] nums, int[] changeIndices) {
int l = 0;
int r = changeIndices.length + 1;
while (l < r) {
final int m = (l + r) / 2;
if (canMark(nums, changeIndices, m)) {
r = m;
} else {
l = m + 1;
}
}
return l <= changeIndices.length ? l : -1;
}

private boolean canMark(int[] nums, int[] changeIndices, int second) {
int numMarked = 0;
int decrement = 0;
// indexToLastSecond[i] := the last second to mark the index i
int[] indexToLastSecond = new int[nums.length];
Arrays.fill(indexToLastSecond, -1);

for (int i = 0; i < second; ++i) {
indexToLastSecond[changeIndices[i] - 1] = i;
}

for (int i = 0; i < second; ++i) {
// Convert to 0-indexed.
final int index = changeIndices[i] - 1;
if (i == indexToLastSecond[index]) {
// Reach the last occurrence of the number.
// So, the current second will be used to mark the index.
if (nums[index] > decrement) {
// The decrement is less than the number to be marked.
return false;
}
decrement -= nums[index];
++numMarked;
} else {
++decrement;
}
}
return numMarked == nums.length;
}
}
```

```cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,51 @@ Hence, the answer is -1.
```

```java

class Solution {
public int earliestSecondToMarkIndices(int[] nums, int[] changeIndices) {
int l = 0;
int r = changeIndices.length + 1;
while (l < r) {
final int m = (l + r) / 2;
if (canMark(nums, changeIndices, m)) {
r = m;
} else {
l = m + 1;
}
}
return l <= changeIndices.length ? l : -1;
}

private boolean canMark(int[] nums, int[] changeIndices, int second) {
int numMarked = 0;
int decrement = 0;
// indexToLastSecond[i] := the last second to mark the index i
int[] indexToLastSecond = new int[nums.length];
Arrays.fill(indexToLastSecond, -1);

for (int i = 0; i < second; ++i) {
indexToLastSecond[changeIndices[i] - 1] = i;
}

for (int i = 0; i < second; ++i) {
// Convert to 0-indexed.
final int index = changeIndices[i] - 1;
if (i == indexToLastSecond[index]) {
// Reach the last occurrence of the number.
// So, the current second will be used to mark the index.
if (nums[index] > decrement) {
// The decrement is less than the number to be marked.
return false;
}
decrement -= nums[index];
++numMarked;
} else {
++decrement;
}
}
return numMarked == nums.length;
}
}
```

```cpp
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class Solution {
public int earliestSecondToMarkIndices(int[] nums, int[] changeIndices) {
int l = 0;
int r = changeIndices.length + 1;
while (l < r) {
final int m = (l + r) / 2;
if (canMark(nums, changeIndices, m)) {
r = m;
} else {
l = m + 1;
}
}
return l <= changeIndices.length ? l : -1;
}

private boolean canMark(int[] nums, int[] changeIndices, int second) {
int numMarked = 0;
int decrement = 0;
// indexToLastSecond[i] := the last second to mark the index i
int[] indexToLastSecond = new int[nums.length];
Arrays.fill(indexToLastSecond, -1);

for (int i = 0; i < second; ++i) {
indexToLastSecond[changeIndices[i] - 1] = i;
}

for (int i = 0; i < second; ++i) {
// Convert to 0-indexed.
final int index = changeIndices[i] - 1;
if (i == indexToLastSecond[index]) {
// Reach the last occurrence of the number.
// So, the current second will be used to mark the index.
if (nums[index] > decrement) {
// The decrement is less than the number to be marked.
return false;
}
decrement -= nums[index];
++numMarked;
} else {
++decrement;
}
}
return numMarked == nums.length;
}
}

0 comments on commit 891dc6e

Please sign in to comment.