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

Commit 891dc6e

Browse files
pursani9yanglbme
andauthored
feat: add java solution to lc problem: No.3048 (#2387)
No.3048.Earliest Second to Mark Indices I Co-authored-by: Libin YANG <contact@yanglibin.info>
1 parent 6b5b433 commit 891dc6e

File tree

3 files changed

+135
-2
lines changed

3 files changed

+135
-2
lines changed

‎solution/3000-3099/3048.Earliest Second to Mark Indices I/README.md‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,51 @@
9191
```
9292

9393
```java
94-
94+
class Solution {
95+
public int earliestSecondToMarkIndices(int[] nums, int[] changeIndices) {
96+
int l = 0;
97+
int r = changeIndices.length + 1;
98+
while (l < r) {
99+
final int m = (l + r) / 2;
100+
if (canMark(nums, changeIndices, m)) {
101+
r = m;
102+
} else {
103+
l = m + 1;
104+
}
105+
}
106+
return l <= changeIndices.length ? l : -1;
107+
}
108+
109+
private boolean canMark(int[] nums, int[] changeIndices, int second) {
110+
int numMarked = 0;
111+
int decrement = 0;
112+
// indexToLastSecond[i] := the last second to mark the index i
113+
int[] indexToLastSecond = new int[nums.length];
114+
Arrays.fill(indexToLastSecond, -1);
115+
116+
for (int i = 0; i < second; ++i) {
117+
indexToLastSecond[changeIndices[i] - 1] = i;
118+
}
119+
120+
for (int i = 0; i < second; ++i) {
121+
// Convert to 0-indexed.
122+
final int index = changeIndices[i] - 1;
123+
if (i == indexToLastSecond[index]) {
124+
// Reach the last occurrence of the number.
125+
// So, the current second will be used to mark the index.
126+
if (nums[index] > decrement) {
127+
// The decrement is less than the number to be marked.
128+
return false;
129+
}
130+
decrement -= nums[index];
131+
++numMarked;
132+
} else {
133+
++decrement;
134+
}
135+
}
136+
return numMarked == nums.length;
137+
}
138+
}
95139
```
96140

97141
```cpp

‎solution/3000-3099/3048.Earliest Second to Mark Indices I/README_EN.md‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,51 @@ Hence, the answer is -1.
8787
```
8888

8989
```java
90-
90+
class Solution {
91+
public int earliestSecondToMarkIndices(int[] nums, int[] changeIndices) {
92+
int l = 0;
93+
int r = changeIndices.length + 1;
94+
while (l < r) {
95+
final int m = (l + r) / 2;
96+
if (canMark(nums, changeIndices, m)) {
97+
r = m;
98+
} else {
99+
l = m + 1;
100+
}
101+
}
102+
return l <= changeIndices.length ? l : -1;
103+
}
104+
105+
private boolean canMark(int[] nums, int[] changeIndices, int second) {
106+
int numMarked = 0;
107+
int decrement = 0;
108+
// indexToLastSecond[i] := the last second to mark the index i
109+
int[] indexToLastSecond = new int[nums.length];
110+
Arrays.fill(indexToLastSecond, -1);
111+
112+
for (int i = 0; i < second; ++i) {
113+
indexToLastSecond[changeIndices[i] - 1] = i;
114+
}
115+
116+
for (int i = 0; i < second; ++i) {
117+
// Convert to 0-indexed.
118+
final int index = changeIndices[i] - 1;
119+
if (i == indexToLastSecond[index]) {
120+
// Reach the last occurrence of the number.
121+
// So, the current second will be used to mark the index.
122+
if (nums[index] > decrement) {
123+
// The decrement is less than the number to be marked.
124+
return false;
125+
}
126+
decrement -= nums[index];
127+
++numMarked;
128+
} else {
129+
++decrement;
130+
}
131+
}
132+
return numMarked == nums.length;
133+
}
134+
}
91135
```
92136

93137
```cpp
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class Solution {
2+
public int earliestSecondToMarkIndices(int[] nums, int[] changeIndices) {
3+
int l = 0;
4+
int r = changeIndices.length + 1;
5+
while (l < r) {
6+
final int m = (l + r) / 2;
7+
if (canMark(nums, changeIndices, m)) {
8+
r = m;
9+
} else {
10+
l = m + 1;
11+
}
12+
}
13+
return l <= changeIndices.length ? l : -1;
14+
}
15+
16+
private boolean canMark(int[] nums, int[] changeIndices, int second) {
17+
int numMarked = 0;
18+
int decrement = 0;
19+
// indexToLastSecond[i] := the last second to mark the index i
20+
int[] indexToLastSecond = new int[nums.length];
21+
Arrays.fill(indexToLastSecond, -1);
22+
23+
for (int i = 0; i < second; ++i) {
24+
indexToLastSecond[changeIndices[i] - 1] = i;
25+
}
26+
27+
for (int i = 0; i < second; ++i) {
28+
// Convert to 0-indexed.
29+
final int index = changeIndices[i] - 1;
30+
if (i == indexToLastSecond[index]) {
31+
// Reach the last occurrence of the number.
32+
// So, the current second will be used to mark the index.
33+
if (nums[index] > decrement) {
34+
// The decrement is less than the number to be marked.
35+
return false;
36+
}
37+
decrement -= nums[index];
38+
++numMarked;
39+
} else {
40+
++decrement;
41+
}
42+
}
43+
return numMarked == nums.length;
44+
}
45+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /