Skip to content

Commit 57393fc

Browse files
committed
feat: add solutions to lc problems: No.2593,2594
* No.2593.Find Score of an Array After Marking All Elements * No.2594.Minimum Time to Repair Cars
1 parent df2216e commit 57393fc

File tree

8 files changed

+393
-2
lines changed

8 files changed

+393
-2
lines changed

solution/1600-1699/1625.Lexicographically Smallest String After Applying Operations/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ class Solution {
170170
cs[i] = (char) (((cs[i] - '0' + a) % 10) + '0');
171171
}
172172
String t1 = String.valueOf(cs);
173-
String t2 = s.substring(b) + s.substring(0, b);
173+
String t2 = s.substring(n - b) + s.substring(0, n - b);
174174
for (String t : List.of(t1, t2)) {
175175
if (vis.add(t)) {
176176
q.offer(t);

solution/1600-1699/1625.Lexicographically Smallest String After Applying Operations/README_EN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ class Solution {
141141
cs[i] = (char) (((cs[i] - '0' + a) % 10) + '0');
142142
}
143143
String t1 = String.valueOf(cs);
144-
String t2 = s.substring(b) + s.substring(0, b);
144+
String t2 = s.substring(n - b) + s.substring(0, n - b);
145145
for (String t : List.of(t1, t2)) {
146146
if (vis.add(t)) {
147147
q.offer(t);

solution/2500-2599/2593.Find Score of an Array After Marking All Elements/README.md

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@
6666

6767
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组的长度。
6868

69+
**方法二:排序**
70+
71+
我们可以创建一个下标数组 $idx$,其中 $idx[i]=i$,然后我们对数组 $idx$ 按照数组 $nums$ 中的元素值进行排序,如果元素值相同,则按照下标值进行排序。
72+
73+
接下来创建一个长度为 $n+2$ 的数组 $vis$,其中 $vis[i]=false$,表示数组中的元素是否被标记。
74+
75+
我们遍历下标数组 $idx$,对于数组中的每一个下标 $i$,如果 $vis[i + 1]$ 为 $false$,则表示 $i$ 位置的元素未被标记,我们将 $nums[i]$ 加入答案,然后标记 $i$ 位置的元素,以及 $i$ 位置的左右相邻元素,即 $i - 1$ 和 $i + 1$ 位置的元素。继续遍历下标数组 $idx$,直到遍历结束。
76+
77+
最后返回答案即可。
78+
79+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组的长度。
80+
6981
<!-- tabs:start -->
7082

7183
### **Python3**
@@ -92,6 +104,20 @@ class Solution:
92104
return ans
93105
```
94106

107+
```python
108+
class Solution:
109+
def findScore(self, nums: List[int]) -> int:
110+
n = len(nums)
111+
vis = [False] * (n + 2)
112+
idx = sorted(range(n), key=lambda i: (nums[i], i))
113+
ans = 0
114+
for i in idx:
115+
if not vis[i + 1]:
116+
ans += nums[i]
117+
vis[i] = vis[i + 2] = True
118+
return ans
119+
```
120+
95121
### **Java**
96122

97123
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -124,6 +150,29 @@ class Solution {
124150
}
125151
```
126152

153+
```java
154+
class Solution {
155+
public long findScore(int[] nums) {
156+
int n = nums.length;
157+
boolean[] vis = new boolean[n + 2];
158+
Integer[] idx = new Integer[n];
159+
for (int i = 0; i < n; ++i) {
160+
idx[i] = i;
161+
}
162+
Arrays.sort(idx, (i, j) -> nums[i] - nums[j]);
163+
long ans = 0;
164+
for (int i : idx) {
165+
if (!vis[i + 1]) {
166+
ans += nums[i];
167+
vis[i] = true;
168+
vis[i + 2] = true;
169+
}
170+
}
171+
return ans;
172+
}
173+
}
174+
```
175+
127176
### **C++**
128177

129178
```cpp
@@ -158,6 +207,29 @@ public:
158207
};
159208
```
160209
210+
```cpp
211+
class Solution {
212+
public:
213+
long long findScore(vector<int>& nums) {
214+
int n = nums.size();
215+
vector<int> idx(n);
216+
iota(idx.begin(), idx.end(), 0);
217+
sort(idx.begin(), idx.end(), [&](int i, int j) {
218+
return nums[i] < nums[j] || (nums[i] == nums[j] && i < j);
219+
});
220+
long long ans = 0;
221+
vector<bool> vis(n + 2);
222+
for (int i : idx) {
223+
if (!vis[i + 1]) {
224+
ans += nums[i];
225+
vis[i] = vis[i + 2] = true;
226+
}
227+
}
228+
return ans;
229+
}
230+
};
231+
```
232+
161233
### **Go**
162234

163235
```go
@@ -195,6 +267,88 @@ func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) }
195267
func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v }
196268
```
197269

270+
```go
271+
func findScore(nums []int) (ans int64) {
272+
n := len(nums)
273+
idx := make([]int, n)
274+
for i := range idx {
275+
idx[i] = i
276+
}
277+
sort.Slice(idx, func(i, j int) bool {
278+
i, j = idx[i], idx[j]
279+
return nums[i] < nums[j] || (nums[i] == nums[j] && i < j)
280+
})
281+
vis := make([]bool, n+2)
282+
for _, i := range idx {
283+
if !vis[i+1] {
284+
ans += int64(nums[i])
285+
vis[i], vis[i+2] = true, true
286+
}
287+
}
288+
return
289+
}
290+
```
291+
292+
### **TypeScript**
293+
294+
```ts
295+
interface pair {
296+
x: number;
297+
i: number;
298+
}
299+
300+
function findScore(nums: number[]): number {
301+
const q = new PriorityQueue({
302+
compare: (a: pair, b: pair) => (a.x === b.x ? a.i - b.i : a.x - b.x),
303+
});
304+
const n = nums.length;
305+
const vis: boolean[] = new Array(n).fill(false);
306+
for (let i = 0; i < n; ++i) {
307+
q.enqueue({ x: nums[i], i });
308+
}
309+
let ans = 0;
310+
while (!q.isEmpty()) {
311+
const { x, i } = q.dequeue()!;
312+
if (vis[i]) {
313+
continue;
314+
}
315+
ans += x;
316+
vis[i] = true;
317+
if (i - 1 >= 0) {
318+
vis[i - 1] = true;
319+
}
320+
if (i + 1 < n) {
321+
vis[i + 1] = true;
322+
}
323+
while (!q.isEmpty() && vis[q.front()!.i]) {
324+
q.dequeue();
325+
}
326+
}
327+
return ans;
328+
}
329+
```
330+
331+
```ts
332+
function findScore(nums: number[]): number {
333+
const n = nums.length;
334+
const idx: number[] = new Array(n);
335+
for (let i = 0; i < n; ++i) {
336+
idx[i] = i;
337+
}
338+
idx.sort((i, j) => (nums[i] == nums[j] ? i - j : nums[i] - nums[j]));
339+
const vis: boolean[] = new Array(n + 2).fill(false);
340+
let ans = 0;
341+
for (const i of idx) {
342+
if (!vis[i + 1]) {
343+
ans += nums[i];
344+
vis[i] = true;
345+
vis[i + 2] = true;
346+
}
347+
}
348+
return ans;
349+
}
350+
```
351+
198352
### **...**
199353

200354
```

solution/2500-2599/2593.Find Score of an Array After Marking All Elements/README_EN.md

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,20 @@ class Solution:
7676
return ans
7777
```
7878

79+
```python
80+
class Solution:
81+
def findScore(self, nums: List[int]) -> int:
82+
n = len(nums)
83+
vis = [False] * (n + 2)
84+
idx = sorted(range(n), key=lambda i: (nums[i], i))
85+
ans = 0
86+
for i in idx:
87+
if not vis[i + 1]:
88+
ans += nums[i]
89+
vis[i] = vis[i + 2] = True
90+
return ans
91+
```
92+
7993
### **Java**
8094

8195
```java
@@ -106,6 +120,29 @@ class Solution {
106120
}
107121
```
108122

123+
```java
124+
class Solution {
125+
public long findScore(int[] nums) {
126+
int n = nums.length;
127+
boolean[] vis = new boolean[n + 2];
128+
Integer[] idx = new Integer[n];
129+
for (int i = 0; i < n; ++i) {
130+
idx[i] = i;
131+
}
132+
Arrays.sort(idx, (i, j) -> nums[i] - nums[j]);
133+
long ans = 0;
134+
for (int i : idx) {
135+
if (!vis[i + 1]) {
136+
ans += nums[i];
137+
vis[i] = true;
138+
vis[i + 2] = true;
139+
}
140+
}
141+
return ans;
142+
}
143+
}
144+
```
145+
109146
### **C++**
110147

111148
```cpp
@@ -140,6 +177,29 @@ public:
140177
};
141178
```
142179
180+
```cpp
181+
class Solution {
182+
public:
183+
long long findScore(vector<int>& nums) {
184+
int n = nums.size();
185+
vector<int> idx(n);
186+
iota(idx.begin(), idx.end(), 0);
187+
sort(idx.begin(), idx.end(), [&](int i, int j) {
188+
return nums[i] < nums[j] || (nums[i] == nums[j] && i < j);
189+
});
190+
long long ans = 0;
191+
vector<bool> vis(n + 2);
192+
for (int i : idx) {
193+
if (!vis[i + 1]) {
194+
ans += nums[i];
195+
vis[i] = vis[i + 2] = true;
196+
}
197+
}
198+
return ans;
199+
}
200+
};
201+
```
202+
143203
### **Go**
144204

145205
```go
@@ -177,6 +237,88 @@ func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) }
177237
func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v }
178238
```
179239

240+
```go
241+
func findScore(nums []int) (ans int64) {
242+
n := len(nums)
243+
idx := make([]int, n)
244+
for i := range idx {
245+
idx[i] = i
246+
}
247+
sort.Slice(idx, func(i, j int) bool {
248+
i, j = idx[i], idx[j]
249+
return nums[i] < nums[j] || (nums[i] == nums[j] && i < j)
250+
})
251+
vis := make([]bool, n+2)
252+
for _, i := range idx {
253+
if !vis[i+1] {
254+
ans += int64(nums[i])
255+
vis[i], vis[i+2] = true, true
256+
}
257+
}
258+
return
259+
}
260+
```
261+
262+
### **TypeScript**
263+
264+
```ts
265+
interface pair {
266+
x: number;
267+
i: number;
268+
}
269+
270+
function findScore(nums: number[]): number {
271+
const q = new PriorityQueue({
272+
compare: (a: pair, b: pair) => (a.x === b.x ? a.i - b.i : a.x - b.x),
273+
});
274+
const n = nums.length;
275+
const vis: boolean[] = new Array(n).fill(false);
276+
for (let i = 0; i < n; ++i) {
277+
q.enqueue({ x: nums[i], i });
278+
}
279+
let ans = 0;
280+
while (!q.isEmpty()) {
281+
const { x, i } = q.dequeue()!;
282+
if (vis[i]) {
283+
continue;
284+
}
285+
ans += x;
286+
vis[i] = true;
287+
if (i - 1 >= 0) {
288+
vis[i - 1] = true;
289+
}
290+
if (i + 1 < n) {
291+
vis[i + 1] = true;
292+
}
293+
while (!q.isEmpty() && vis[q.front()!.i]) {
294+
q.dequeue();
295+
}
296+
}
297+
return ans;
298+
}
299+
```
300+
301+
```ts
302+
function findScore(nums: number[]): number {
303+
const n = nums.length;
304+
const idx: number[] = new Array(n);
305+
for (let i = 0; i < n; ++i) {
306+
idx[i] = i;
307+
}
308+
idx.sort((i, j) => (nums[i] == nums[j] ? i - j : nums[i] - nums[j]));
309+
const vis: boolean[] = new Array(n + 2).fill(false);
310+
let ans = 0;
311+
for (const i of idx) {
312+
if (!vis[i + 1]) {
313+
ans += nums[i];
314+
vis[i] = true;
315+
vis[i + 2] = true;
316+
}
317+
}
318+
return ans;
319+
}
320+
```
321+
180322
### **...**
181323

182324
```

0 commit comments

Comments
 (0)