Skip to content

Commit 7af3439

Browse files
committed
feat: add solutions to lc/lcp problems
* Add solutions to lcp problem: No.40 * Add solution to lc problem: No.1921.Eliminate Maximum Number of Monsters
1 parent a24687a commit 7af3439

File tree

17 files changed

+529
-37
lines changed

17 files changed

+529
-37
lines changed

lcp/LCP 40. 心算挑战/README.md

+128-1
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,149 @@
3232

3333
<!-- 这里可写通用的实现逻辑 -->
3434

35+
**方法一:排序 + 贪心**
36+
37+
排序先取最大的 $cnt$ 个数,如果和为偶数则直接返回答案。
38+
39+
否则,找一个已取的最小奇数换成剩余未取的最大偶数,或者找一个已取的最小偶数换成剩下未取的最大奇数,取两者中较大的。
40+
41+
时间复杂度 $O(nlogn)$。
42+
3543
<!-- tabs:start -->
3644

3745
### **Python3**
3846

3947
<!-- 这里可写当前语言的特殊实现逻辑 -->
4048

4149
```python
42-
50+
class Solution:
51+
def maxmiumScore(self, cards: List[int], cnt: int) -> int:
52+
cards.sort(reverse=True)
53+
t = cards[:cnt]
54+
ans = sum(t)
55+
if ans % 2 == 0:
56+
return ans
57+
a = min([v for v in t if v & 1], default=inf)
58+
b = min([v for v in t if v % 2 == 0], default=inf)
59+
c = max([v for v in cards[cnt:] if v % 2 == 0], default=-inf)
60+
d = max([v for v in cards[cnt:] if v & 1], default=-inf)
61+
return max(ans - a + c, ans - b + d, 0)
4362
```
4463

4564
### **Java**
4665

4766
<!-- 这里可写当前语言的特殊实现逻辑 -->
4867

4968
```java
69+
class Solution {
70+
public int maxmiumScore(int[] cards, int cnt) {
71+
Arrays.sort(cards);
72+
int ans = 0;
73+
int n = cards.length;
74+
for (int i = 0; i < cnt; ++i) {
75+
ans += cards[n - i - 1];
76+
}
77+
if (ans % 2 == 0) {
78+
return ans;
79+
}
80+
int inf = 0x3f3f3f3f;
81+
int a = inf, b = inf;
82+
for (int i = 0; i < cnt; ++i) {
83+
int v = cards[n - i - 1];
84+
if (v % 2 == 1) {
85+
a = Math.min(a, v);
86+
} else {
87+
b = Math.min(b, v);
88+
}
89+
}
90+
int c = -inf, d = -inf;
91+
for (int i = cnt; i < n; ++i) {
92+
int v = cards[n - i - 1];
93+
if (v % 2 == 0) {
94+
c = Math.max(c, v);
95+
} else {
96+
d = Math.max(d, v);
97+
}
98+
}
99+
return Math.max(0, Math.max(ans - a + c, ans - b + d));
100+
}
101+
}
102+
```
103+
104+
### **C++**
105+
106+
```cpp
107+
class Solution {
108+
public:
109+
int maxmiumScore(vector<int>& cards, int cnt) {
110+
sort(cards.begin(), cards.end());
111+
reverse(cards.begin(), cards.end());
112+
int ans = 0, n = cards.size();
113+
for (int i = 0; i < cnt; ++i) ans += cards[i];
114+
if (ans % 2 == 0) return ans;
115+
int inf = 0x3f3f3f3f;
116+
int a = inf, b = inf, c = -inf, d = -inf;
117+
for (int i = 0; i < cnt; ++i)
118+
{
119+
int v = cards[i];
120+
if (v % 2 == 1) a = min(a, v);
121+
else b = min(b, v);
122+
}
123+
for (int i = cnt; i < n; ++i)
124+
{
125+
int v = cards[i];
126+
if (v % 2 == 0) c = max(c, v);
127+
else d = max(d, v);
128+
}
129+
return max(0, max(ans - a + c, ans - b + d));
130+
}
131+
};
132+
```
50133
134+
### **Go**
135+
136+
```go
137+
func maxmiumScore(cards []int, cnt int) int {
138+
ans := 0
139+
sort.Slice(cards, func(i, j int) bool { return cards[i] > cards[j] })
140+
for _, v := range cards[:cnt] {
141+
ans += v
142+
}
143+
if ans%2 == 0 {
144+
return ans
145+
}
146+
inf := 0x3f3f3f3f
147+
a, b, c, d := inf, inf, -inf, -inf
148+
for _, v := range cards[:cnt] {
149+
if v%2 == 1 {
150+
a = min(a, v)
151+
} else {
152+
b = min(b, v)
153+
}
154+
}
155+
for _, v := range cards[cnt:] {
156+
if v%2 == 0 {
157+
c = max(c, v)
158+
} else {
159+
d = max(d, v)
160+
}
161+
}
162+
return max(0, max(ans-a+c, ans-b+d))
163+
}
164+
165+
func max(a, b int) int {
166+
if a > b {
167+
return a
168+
}
169+
return b
170+
}
171+
172+
func min(a, b int) int {
173+
if a < b {
174+
return a
175+
}
176+
return b
177+
}
51178
```
52179

53180
### **...**

lcp/LCP 40. 心算挑战/Solution.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
int maxmiumScore(vector<int>& cards, int cnt) {
4+
sort(cards.begin(), cards.end());
5+
reverse(cards.begin(), cards.end());
6+
int ans = 0, n = cards.size();
7+
for (int i = 0; i < cnt; ++i) ans += cards[i];
8+
if (ans % 2 == 0) return ans;
9+
int inf = 0x3f3f3f3f;
10+
int a = inf, b = inf, c = -inf, d = -inf;
11+
for (int i = 0; i < cnt; ++i)
12+
{
13+
int v = cards[i];
14+
if (v % 2 == 1) a = min(a, v);
15+
else b = min(b, v);
16+
}
17+
for (int i = cnt; i < n; ++i)
18+
{
19+
int v = cards[i];
20+
if (v % 2 == 0) c = max(c, v);
21+
else d = max(d, v);
22+
}
23+
return max(0, max(ans - a + c, ans - b + d));
24+
}
25+
};

lcp/LCP 40. 心算挑战/Solution.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
func maxmiumScore(cards []int, cnt int) int {
2+
ans := 0
3+
sort.Slice(cards, func(i, j int) bool { return cards[i] > cards[j] })
4+
for _, v := range cards[:cnt] {
5+
ans += v
6+
}
7+
if ans%2 == 0 {
8+
return ans
9+
}
10+
inf := 0x3f3f3f3f
11+
a, b, c, d := inf, inf, -inf, -inf
12+
for _, v := range cards[:cnt] {
13+
if v%2 == 1 {
14+
a = min(a, v)
15+
} else {
16+
b = min(b, v)
17+
}
18+
}
19+
for _, v := range cards[cnt:] {
20+
if v%2 == 0 {
21+
c = max(c, v)
22+
} else {
23+
d = max(d, v)
24+
}
25+
}
26+
return max(0, max(ans-a+c, ans-b+d))
27+
}
28+
29+
func max(a, b int) int {
30+
if a > b {
31+
return a
32+
}
33+
return b
34+
}
35+
36+
func min(a, b int) int {
37+
if a < b {
38+
return a
39+
}
40+
return b
41+
}
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
public int maxmiumScore(int[] cards, int cnt) {
3+
Arrays.sort(cards);
4+
int ans = 0;
5+
int n = cards.length;
6+
for (int i = 0; i < cnt; ++i) {
7+
ans += cards[n - i - 1];
8+
}
9+
if (ans % 2 == 0) {
10+
return ans;
11+
}
12+
int inf = 0x3f3f3f3f;
13+
int a = inf, b = inf;
14+
for (int i = 0; i < cnt; ++i) {
15+
int v = cards[n - i - 1];
16+
if (v % 2 == 1) {
17+
a = Math.min(a, v);
18+
} else {
19+
b = Math.min(b, v);
20+
}
21+
}
22+
int c = -inf, d = -inf;
23+
for (int i = cnt; i < n; ++i) {
24+
int v = cards[n - i - 1];
25+
if (v % 2 == 0) {
26+
c = Math.max(c, v);
27+
} else {
28+
d = Math.max(d, v);
29+
}
30+
}
31+
return Math.max(0, Math.max(ans - a + c, ans - b + d));
32+
}
33+
}

lcp/LCP 40. 心算挑战/Solution.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def maxmiumScore(self, cards: List[int], cnt: int) -> int:
3+
cards.sort(reverse=True)
4+
t = cards[:cnt]
5+
ans = sum(t)
6+
if ans % 2 == 0:
7+
return ans
8+
a = min([v for v in t if v & 1], default=inf)
9+
b = min([v for v in t if v % 2 == 0], default=inf)
10+
c = max([v for v in cards[cnt:] if v % 2 == 0], default=-inf)
11+
d = max([v for v in cards[cnt:] if v & 1], default=-inf)
12+
return max(ans - a + c, ans - b + d, 0)

solution/1900-1999/1921.Eliminate Maximum Number of Monsters/README.md

+39-9
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@
6666

6767
<!-- 这里可写通用的实现逻辑 -->
6868

69+
**方法一:排序 + 贪心**
70+
71+
用 $times$ 数组记录每个怪物最晚可被消灭的时间。对于第 $i$ 个怪物,最晚可被消灭的时间满足:
72+
73+
$$times[i] = \lfloor \frac{dist[i]-1}{speed[i]} \rfloor$$
74+
75+
我们对 $times$ 数组升序排列,然后遍历 $times$ 数组。对于第 $i$ 个怪物,如果 $times[i] \geq i$,说明第 $i$ 个怪物可以被消灭,否则说明第 $i$ 个怪物无法被消灭,直接返回 $i$ 即可。
76+
77+
若所有怪物都可以被消灭,则返回 $n$。
78+
79+
时间复杂度 $O(nlogn)$。
80+
6981
<!-- tabs:start -->
7082

7183
### **Python3**
@@ -75,13 +87,12 @@
7587
```python
7688
class Solution:
7789
def eliminateMaximum(self, dist: List[int], speed: List[int]) -> int:
78-
n = len(dist)
79-
times = [(dist[i] - 1) // speed[i] for i in range(n)]
90+
times = [(d - 1) // s for d, s in zip(dist, speed)]
8091
times.sort()
81-
for i in range(n):
82-
if times[i] < i:
92+
for i, t in enumerate(times):
93+
if t < i:
8394
return i
84-
return n
95+
return len(dist)
8596
```
8697

8798
### **Java**
@@ -158,19 +169,38 @@ public:
158169
func eliminateMaximum(dist []int, speed []int) int {
159170
n := len(dist)
160171
times := make([]int, n)
161-
for i := 0; i < n; i++ {
162-
times[i] = (dist[i] - 1) / speed[i]
172+
for i, d := range dist {
173+
times[i] = (d - 1) / speed[i]
163174
}
164175
sort.Ints(times)
165-
for i := 0; i < n; i++ {
166-
if times[i] < i {
176+
for i, t := range times {
177+
if t < i {
167178
return i
168179
}
169180
}
170181
return n
171182
}
172183
```
173184

185+
### **TypeScript**
186+
187+
```ts
188+
function eliminateMaximum(dist: number[], speed: number[]): number {
189+
const n = dist.length;
190+
const times = new Array(n).fill(0);
191+
for (let i = 0; i < n; ++i) {
192+
times[i] = Math.floor((dist[i] - 1) / speed[i]);
193+
}
194+
times.sort((a, b) => a - b);
195+
for (let i = 0; i < n; ++i) {
196+
if (times[i] < i) {
197+
return i;
198+
}
199+
}
200+
return n;
201+
}
202+
```
203+
174204
### **...**
175205

176206
```

0 commit comments

Comments
 (0)