Skip to content

Commit dd843e0

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

File tree

6 files changed

+167
-178
lines changed

6 files changed

+167
-178
lines changed

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

Lines changed: 80 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,20 @@
6565

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

68-
**方法一:贪心 + 优先队列(大根堆)**
68+
**方法一:贪心 + 模拟**
6969

70-
每次贪心地拿出最大的两堆,直到至少有两堆石子为空。
70+
每次贪心地从最大的两堆石子中取石头,直到至少有两堆石子为空。
7171

72-
时间复杂度 $O(m\times \log n)$。其中 $m$, $n$ 分别为石子总数以及石子堆数。
72+
时间复杂度 $O(n)$,其中 $n$ 为石子总数。
73+
74+
**方法二:贪心 + 数学**
75+
76+
我们不妨设 $a \le b \le c$,那么:
77+
78+
- 当 $a + b \le c$ 时,我们可以先从 $a$, $c$ 两堆中取石头,得到分数 $a$;再从 $b$, $c$ 两堆中取石头,得到分数 $b$,总分数为 $a + b$;
79+
- 当 $a + b \gt c$ 时,这时我们每次会从 $c$ 以及 $a$ 和 $b$ 中较大的那一堆中取石头,最终将 $c$ 取空。此时 $a$ 和 $b$ 的大小差最多为 $1$。我们再从 $a$, $b$ 两堆中取石头,直到不能取为止,总分数为 $\left \lfloor \frac{a + b + c}{2} \right \rfloor$。
80+
81+
时间复杂度 $O(1)$。
7382

7483
<!-- tabs:start -->
7584

@@ -80,102 +89,116 @@
8089
```python
8190
class Solution:
8291
def maximumScore(self, a: int, b: int, c: int) -> int:
83-
h = [-a, -b, -c]
84-
heapify(h)
92+
s = sorted([a, b, c])
8593
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)
94+
while s[1]:
9295
ans += 1
96+
s[1] -= 1
97+
s[2] -= 1
98+
s.sort()
9399
return ans
94100
```
95101

102+
```python
103+
class Solution:
104+
def maximumScore(self, a: int, b: int, c: int) -> int:
105+
a, b, c = sorted([a, b, c])
106+
if a + b < c:
107+
return a + b
108+
return (a + b + c) >> 1
109+
```
110+
96111
### **Java**
97112

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

100115
```java
101116
class Solution {
102117
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);
118+
int[] s = new int[] {a, b, c};
119+
Arrays.sort(s);
107120
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);
121+
while (s[1] > 0) {
115122
++ans;
123+
s[1]--;
124+
s[2]--;
125+
Arrays.sort(s);
116126
}
117127
return ans;
118128
}
119129
}
120130
```
121131

132+
```java
133+
class Solution {
134+
public int maximumScore(int a, int b, int c) {
135+
int[] s = new int[] {a, b, c};
136+
Arrays.sort(s);
137+
if (s[0] + s[1] < s[2]) {
138+
return s[0] + s[1];
139+
}
140+
return (a + b + c) >> 1;
141+
}
142+
}
143+
```
144+
122145
### **C++**
123146

124147
```cpp
125148
class Solution {
126149
public:
127150
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);
151+
vector<int> s = {a, b, c};
152+
sort(s.begin(), s.end());
132153
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);
154+
while (s[1]) {
141155
++ans;
156+
s[1]--;
157+
s[2]--;
158+
sort(s.begin(), s.end());
142159
}
143160
return ans;
144161
}
145162
};
146163
```
147164
165+
```cpp
166+
class Solution {
167+
public:
168+
int maximumScore(int a, int b, int c) {
169+
vector<int> s = {a, b, c};
170+
sort(s.begin(), s.end());
171+
if (s[0] + s[1] < s[2]) return s[0] + s[1];
172+
return (a + b + c) >> 1;
173+
}
174+
};
175+
```
176+
148177
### **Go**
149178

150179
```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)
180+
func maximumScore(a int, b int, c int) (ans int) {
181+
s := []int{a, b, c}
182+
sort.Ints(s)
183+
for s[1] > 0 {
164184
ans++
185+
s[1]--
186+
s[2]--
187+
sort.Ints(s)
165188
}
166-
return ans
189+
return
167190
}
191+
```
168192

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
193+
```go
194+
func maximumScore(a int, b int, c int) int {
195+
s := []int{a, b, c}
196+
sort.Ints(s)
197+
if s[0]+s[1] < s[2] {
198+
return s[0] + s[1]
199+
}
200+
return (a + b + c) >> 1
177201
}
178-
func (h *hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] }
179202
```
180203

181204
### **...**

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

Lines changed: 68 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -65,100 +65,114 @@ After that, there are fewer than two non-empty piles, so the game ends.
6565
```python
6666
class Solution:
6767
def maximumScore(self, a: int, b: int, c: int) -> int:
68-
h = [-a, -b, -c]
69-
heapify(h)
68+
s = sorted([a, b, c])
7069
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)
70+
while s[1]:
7771
ans += 1
72+
s[1] -= 1
73+
s[2] -= 1
74+
s.sort()
7875
return ans
7976
```
8077

78+
```python
79+
class Solution:
80+
def maximumScore(self, a: int, b: int, c: int) -> int:
81+
a, b, c = sorted([a, b, c])
82+
if a + b < c:
83+
return a + b
84+
return (a + b + c) >> 1
85+
```
86+
8187
### **Java**
8288

8389
```java
8490
class Solution {
8591
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);
92+
int[] s = new int[] {a, b, c};
93+
Arrays.sort(s);
9094
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);
95+
while (s[1] > 0) {
9896
++ans;
97+
s[1]--;
98+
s[2]--;
99+
Arrays.sort(s);
99100
}
100101
return ans;
101102
}
102103
}
103104
```
104105

106+
```java
107+
class Solution {
108+
public int maximumScore(int a, int b, int c) {
109+
int[] s = new int[] {a, b, c};
110+
Arrays.sort(s);
111+
if (s[0] + s[1] < s[2]) {
112+
return s[0] + s[1];
113+
}
114+
return (a + b + c) >> 1;
115+
}
116+
}
117+
```
118+
105119
### **C++**
106120

107121
```cpp
108122
class Solution {
109123
public:
110124
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);
125+
vector<int> s = {a, b, c};
126+
sort(s.begin(), s.end());
115127
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);
128+
while (s[1]) {
124129
++ans;
130+
s[1]--;
131+
s[2]--;
132+
sort(s.begin(), s.end());
125133
}
126134
return ans;
127135
}
128136
};
129137
```
130138
139+
```cpp
140+
class Solution {
141+
public:
142+
int maximumScore(int a, int b, int c) {
143+
vector<int> s = {a, b, c};
144+
sort(s.begin(), s.end());
145+
if (s[0] + s[1] < s[2]) return s[0] + s[1];
146+
return (a + b + c) >> 1;
147+
}
148+
};
149+
```
150+
131151
### **Go**
132152

133153
```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)
154+
func maximumScore(a int, b int, c int) (ans int) {
155+
s := []int{a, b, c}
156+
sort.Ints(s)
157+
for s[1] > 0 {
147158
ans++
159+
s[1]--
160+
s[2]--
161+
sort.Ints(s)
148162
}
149-
return ans
163+
return
150164
}
165+
```
151166

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
167+
```go
168+
func maximumScore(a int, b int, c int) int {
169+
s := []int{a, b, c}
170+
sort.Ints(s)
171+
if s[0]+s[1] < s[2] {
172+
return s[0] + s[1]
173+
}
174+
return (a + b + c) >> 1
160175
}
161-
func (h *hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] }
162176
```
163177

164178
### **...**
Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,9 @@
11
class Solution {
22
public:
33
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;
4+
vector<int> s = {a, b, c};
5+
sort(s.begin(), s.end());
6+
if (s[0] + s[1] < s[2]) return s[0] + s[1];
7+
return (a + b + c) >> 1;
208
}
219
};

0 commit comments

Comments
 (0)