Skip to content

Commit d87a67b

Browse files
committed
feat: add solutions to lc problem: No.1753
No.1753 Maximum Score From Removing Stones
1 parent e2bfe8c commit d87a67b

File tree

6 files changed

+267
-2
lines changed

6 files changed

+267
-2
lines changed

solution/1700-1799/1753.Maximum Score From Removing Stones/README.md

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,117 @@
6565

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

68+
**方法一:贪心 + 优先队列(大根堆)**
69+
70+
每次贪心地拿出最大的两堆,直到至少有两堆石子为空。
71+
72+
时间复杂度 $O(m\times \log n)$。其中 $m$, $n$ 分别为石子总数以及石子堆数。
73+
6874
<!-- tabs:start -->
6975

7076
### **Python3**
7177

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

7480
```python
75-
81+
class Solution:
82+
def maximumScore(self, a: int, b: int, c: int) -> int:
83+
h = [-a, -b, -c]
84+
heapify(h)
85+
ans = 0
86+
while 1:
87+
a, b = heappop(h), heappop(h)
88+
if b == 0:
89+
break
90+
heappush(h, a + 1)
91+
heappush(h, b + 1)
92+
ans += 1
93+
return ans
7694
```
7795

7896
### **Java**
7997

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

82100
```java
101+
class Solution {
102+
public int maximumScore(int a, int b, int c) {
103+
PriorityQueue<Integer> pq = new PriorityQueue<>((x, y) -> y - x);
104+
pq.offer(a);
105+
pq.offer(b);
106+
pq.offer(c);
107+
int ans = 0;
108+
while (true) {
109+
int x = pq.poll(), y = pq.poll();
110+
if (y == 0) {
111+
break;
112+
}
113+
pq.offer(x - 1);
114+
pq.offer(y - 1);
115+
++ans;
116+
}
117+
return ans;
118+
}
119+
}
120+
```
121+
122+
### **C++**
123+
124+
```cpp
125+
class Solution {
126+
public:
127+
int maximumScore(int a, int b, int c) {
128+
priority_queue<int> pq;
129+
pq.push(a);
130+
pq.push(b);
131+
pq.push(c);
132+
int ans = 0;
133+
while (1) {
134+
a = pq.top(), pq.pop();
135+
b = pq.top(), pq.pop();
136+
if (b == 0) {
137+
break;
138+
}
139+
pq.push(a - 1);
140+
pq.push(b - 1);
141+
++ans;
142+
}
143+
return ans;
144+
}
145+
};
146+
```
83147
148+
### **Go**
149+
150+
```go
151+
func maximumScore(a int, b int, c int) int {
152+
q := hp{[]int{a, b, c}}
153+
ans := 0
154+
for {
155+
a = q.IntSlice[0]
156+
heap.Pop(&q)
157+
b = q.IntSlice[0]
158+
heap.Pop(&q)
159+
if b == 0 {
160+
break
161+
}
162+
heap.Push(&q, a-1)
163+
heap.Push(&q, b-1)
164+
ans++
165+
}
166+
return ans
167+
}
168+
169+
type hp struct{ sort.IntSlice }
170+
171+
func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) }
172+
func (h *hp) Pop() interface{} {
173+
a := h.IntSlice
174+
v := a[len(a)-1]
175+
h.IntSlice = a[:len(a)-1]
176+
return v
177+
}
178+
func (h *hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] }
84179
```
85180

86181
### **...**

solution/1700-1799/1753.Maximum Score From Removing Stones/README_EN.md

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,102 @@ After that, there are fewer than two non-empty piles, so the game ends.
6363
### **Python3**
6464

6565
```python
66-
66+
class Solution:
67+
def maximumScore(self, a: int, b: int, c: int) -> int:
68+
h = [-a, -b, -c]
69+
heapify(h)
70+
ans = 0
71+
while 1:
72+
a, b = heappop(h), heappop(h)
73+
if b == 0:
74+
break
75+
heappush(h, a + 1)
76+
heappush(h, b + 1)
77+
ans += 1
78+
return ans
6779
```
6880

6981
### **Java**
7082

7183
```java
84+
class Solution {
85+
public int maximumScore(int a, int b, int c) {
86+
PriorityQueue<Integer> pq = new PriorityQueue<>((x, y) -> y - x);
87+
pq.offer(a);
88+
pq.offer(b);
89+
pq.offer(c);
90+
int ans = 0;
91+
while (true) {
92+
int x = pq.poll(), y = pq.poll();
93+
if (y == 0) {
94+
break;
95+
}
96+
pq.offer(x - 1);
97+
pq.offer(y - 1);
98+
++ans;
99+
}
100+
return ans;
101+
}
102+
}
103+
```
104+
105+
### **C++**
106+
107+
```cpp
108+
class Solution {
109+
public:
110+
int maximumScore(int a, int b, int c) {
111+
priority_queue<int> pq;
112+
pq.push(a);
113+
pq.push(b);
114+
pq.push(c);
115+
int ans = 0;
116+
while (1) {
117+
a = pq.top(), pq.pop();
118+
b = pq.top(), pq.pop();
119+
if (b == 0) {
120+
break;
121+
}
122+
pq.push(a - 1);
123+
pq.push(b - 1);
124+
++ans;
125+
}
126+
return ans;
127+
}
128+
};
129+
```
72130
131+
### **Go**
132+
133+
```go
134+
func maximumScore(a int, b int, c int) int {
135+
q := hp{[]int{a, b, c}}
136+
ans := 0
137+
for {
138+
a = q.IntSlice[0]
139+
heap.Pop(&q)
140+
b = q.IntSlice[0]
141+
heap.Pop(&q)
142+
if b == 0 {
143+
break
144+
}
145+
heap.Push(&q, a-1)
146+
heap.Push(&q, b-1)
147+
ans++
148+
}
149+
return ans
150+
}
151+
152+
type hp struct{ sort.IntSlice }
153+
154+
func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) }
155+
func (h *hp) Pop() interface{} {
156+
a := h.IntSlice
157+
v := a[len(a)-1]
158+
h.IntSlice = a[:len(a)-1]
159+
return v
160+
}
161+
func (h *hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] }
73162
```
74163

75164
### **...**
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
int maximumScore(int a, int b, int c) {
4+
priority_queue<int> pq;
5+
pq.push(a);
6+
pq.push(b);
7+
pq.push(c);
8+
int ans = 0;
9+
while (1) {
10+
a = pq.top(), pq.pop();
11+
b = pq.top(), pq.pop();
12+
if (b == 0) {
13+
break;
14+
}
15+
pq.push(a - 1);
16+
pq.push(b - 1);
17+
++ans;
18+
}
19+
return ans;
20+
}
21+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
func maximumScore(a int, b int, c int) int {
2+
q := hp{[]int{a, b, c}}
3+
ans := 0
4+
for {
5+
a = q.IntSlice[0]
6+
heap.Pop(&q)
7+
b = q.IntSlice[0]
8+
heap.Pop(&q)
9+
if b == 0 {
10+
break
11+
}
12+
heap.Push(&q, a-1)
13+
heap.Push(&q, b-1)
14+
ans++
15+
}
16+
return ans
17+
}
18+
19+
type hp struct{ sort.IntSlice }
20+
21+
func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) }
22+
func (h *hp) Pop() interface{} {
23+
a := h.IntSlice
24+
v := a[len(a)-1]
25+
h.IntSlice = a[:len(a)-1]
26+
return v
27+
}
28+
func (h *hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] }
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int maximumScore(int a, int b, int c) {
3+
PriorityQueue<Integer> pq = new PriorityQueue<>((x, y) -> y - x);
4+
pq.offer(a);
5+
pq.offer(b);
6+
pq.offer(c);
7+
int ans = 0;
8+
while (true) {
9+
int x = pq.poll(), y = pq.poll();
10+
if (y == 0) {
11+
break;
12+
}
13+
pq.offer(x - 1);
14+
pq.offer(y - 1);
15+
++ans;
16+
}
17+
return ans;
18+
}
19+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def maximumScore(self, a: int, b: int, c: int) -> int:
3+
h = [-a, -b, -c]
4+
heapify(h)
5+
ans = 0
6+
while 1:
7+
a, b = heappop(h), heappop(h)
8+
if b == 0:
9+
break
10+
heappush(h, a + 1)
11+
heappush(h, b + 1)
12+
ans += 1
13+
return ans

0 commit comments

Comments
 (0)