Skip to content

Commit 37ae413

Browse files
committed
feat: add solutions to lc problem: No.2491
No.2491.Divide Players Into Teams of Equal Skill
1 parent 0a581bd commit 37ae413

File tree

7 files changed

+243
-49
lines changed

7 files changed

+243
-49
lines changed

solution/1600-1699/1689.Partitioning Into Minimum Number Of Deci-Binary Numbers/README.md

+15-15
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,12 @@
4545

4646
<!-- 这里可写通用的实现逻辑 -->
4747

48+
**方法一:脑筋急转弯**
49+
4850
题目等价于找字符串中的最大数。
4951

52+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串长度。
53+
5054
<!-- tabs:start -->
5155

5256
### **Python3**
@@ -66,11 +70,11 @@ class Solution:
6670
```java
6771
class Solution {
6872
public int minPartitions(String n) {
69-
int res = 0;
70-
for (char c : n.toCharArray()) {
71-
res = Math.max(res, c - '0');
73+
int ans = 0;
74+
for (int i = 0; i < n.length(); ++i) {
75+
ans = Math.max(ans, n.charAt(i) - '0');
7276
}
73-
return res;
77+
return ans;
7478
}
7579
}
7680
```
@@ -81,27 +85,23 @@ class Solution {
8185
class Solution {
8286
public:
8387
int minPartitions(string n) {
84-
int res = 0;
85-
for (auto& c : n) {
86-
res = max(res, c - '0');
87-
}
88-
return res;
88+
int ans = 0;
89+
for (char& c : n) ans = max(ans, c - '0');
90+
return ans;
8991
}
9092
};
9193
```
9294
9395
### **Go**
9496
9597
```go
96-
func minPartitions(n string) int {
97-
res := 0
98+
func minPartitions(n string) (ans int) {
9899
for _, c := range n {
99-
t := int(c - '0')
100-
if t > res {
101-
res = t
100+
if t := int(c - '0'); ans < t {
101+
ans = t
102102
}
103103
}
104-
return res
104+
return
105105
}
106106
```
107107

solution/1600-1699/1689.Partitioning Into Minimum Number Of Deci-Binary Numbers/README_EN.md

+11-15
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ class Solution:
5757
```java
5858
class Solution {
5959
public int minPartitions(String n) {
60-
int res = 0;
61-
for (char c : n.toCharArray()) {
62-
res = Math.max(res, c - '0');
60+
int ans = 0;
61+
for (int i = 0; i < n.length(); ++i) {
62+
ans = Math.max(ans, n.charAt(i) - '0');
6363
}
64-
return res;
64+
return ans;
6565
}
6666
}
6767
```
@@ -72,27 +72,23 @@ class Solution {
7272
class Solution {
7373
public:
7474
int minPartitions(string n) {
75-
int res = 0;
76-
for (auto& c : n) {
77-
res = max(res, c - '0');
78-
}
79-
return res;
75+
int ans = 0;
76+
for (char& c : n) ans = max(ans, c - '0');
77+
return ans;
8078
}
8179
};
8280
```
8381
8482
### **Go**
8583
8684
```go
87-
func minPartitions(n string) int {
88-
res := 0
85+
func minPartitions(n string) (ans int) {
8986
for _, c := range n {
90-
t := int(c - '0')
91-
if t > res {
92-
res = t
87+
if t := int(c - '0'); ans < t {
88+
ans = t
9389
}
9490
}
95-
return res
91+
return
9692
}
9793
```
9894

Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
class Solution {
22
public:
33
int minPartitions(string n) {
4-
int res = 0;
5-
for (auto& c : n) {
6-
res = max(res, c - '0');
7-
}
8-
return res;
4+
int ans = 0;
5+
for (char& c : n) ans = max(ans, c - '0');
6+
return ans;
97
}
108
};
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
func minPartitions(n string) int {
2-
res := 0
1+
func minPartitions(n string) (ans int) {
32
for _, c := range n {
4-
t := int(c - '0')
5-
if t > res {
6-
res = t
3+
if t := int(c - '0'); ans < t {
4+
ans = t
75
}
86
}
9-
return res
7+
return
108
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
class Solution {
22
public int minPartitions(String n) {
3-
int res = 0;
4-
for (char c : n.toCharArray()) {
5-
res = Math.max(res, c - '0');
3+
int ans = 0;
4+
for (int i = 0; i < n.length(); ++i) {
5+
ans = Math.max(ans, n.charAt(i) - '0');
66
}
7-
return res;
7+
return ans;
88
}
99
}

solution/2400-2499/2491.Divide Players Into Teams of Equal Skill/README.md

+108-4
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,16 @@
6161

6262
要使得所有 $2$ 人团队的技能点相等,最小值一定需要跟最大值匹配。因此,我们将数组 `skill` 排序,然后用双指针 $i$ 和 $j$ 分别指向数组的首位,两两匹配,判断其和是否均为同一个数。
6363

64-
若不是,说明技能点无法相等,直接返回 $-1$,否则,将化学反应累加到答案中
64+
若不是,说明技能点无法相等,直接返回 $-1$,否则,将化学反应累加到答案中
6565

6666
遍历结束,返回答案即可。
6767

6868
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 是数组 `skill` 的长度。
6969

70+
**方法二:计数**
71+
72+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 `skill` 的长度。
73+
7074
<!-- tabs:start -->
7175

7276
### **Python3**
@@ -88,6 +92,26 @@ class Solution:
8892
return ans
8993
```
9094

95+
```python
96+
class Solution:
97+
def dividePlayers(self, skill: List[int]) -> int:
98+
s = sum(skill)
99+
m = len(skill) >> 1
100+
if s % m:
101+
return -1
102+
t = s // m
103+
d = defaultdict(int)
104+
ans = 0
105+
for v in skill:
106+
if d[t - v]:
107+
ans += v * (t - v)
108+
m -= 1
109+
d[t - v] -= 1
110+
else:
111+
d[v] += 1
112+
return -1 if m else ans
113+
```
114+
91115
### **Java**
92116

93117
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -110,6 +134,31 @@ class Solution {
110134
}
111135
```
112136

137+
```java
138+
class Solution {
139+
public long dividePlayers(int[] skill) {
140+
int s = Arrays.stream(skill).sum();
141+
int m = skill.length >> 1;
142+
if (s % m != 0) {
143+
return -1;
144+
}
145+
int t = s / m;
146+
int[] d = new int[1010];
147+
long ans = 0;
148+
for (int v : skill) {
149+
if (d[t - v] > 0) {
150+
ans += (long) v * (t - v);
151+
--d[t - v];
152+
--m;
153+
} else {
154+
++d[v];
155+
}
156+
}
157+
return m == 0 ? ans : -1;
158+
}
159+
}
160+
```
161+
113162
### **C++**
114163

115164
```cpp
@@ -129,6 +178,30 @@ public:
129178
};
130179
```
131180
181+
```cpp
182+
class Solution {
183+
public:
184+
long long dividePlayers(vector<int>& skill) {
185+
int s = accumulate(skill.begin(), skill.end(), 0);
186+
int m = skill.size() / 2;
187+
if (s % m) return -1;
188+
int t = s / m;
189+
int d[1010] = {0};
190+
long long ans = 0;
191+
for (int& v : skill) {
192+
if (d[t - v]) {
193+
ans += 1ll * v * (t - v);
194+
--d[t - v];
195+
--m;
196+
} else {
197+
++d[v];
198+
}
199+
}
200+
return m == 0 ? ans : -1;
201+
}
202+
};
203+
```
204+
132205
### **Go**
133206

134207
```go
@@ -146,16 +219,47 @@ func dividePlayers(skill []int) (ans int64) {
146219
}
147220
```
148221

222+
```go
223+
func dividePlayers(skill []int) int64 {
224+
s := 0
225+
for _, v := range skill {
226+
s += v
227+
}
228+
m := len(skill) >> 1
229+
if s%m != 0 {
230+
return -1
231+
}
232+
t := s / m
233+
d := [1010]int{}
234+
ans := 0
235+
for _, v := range skill {
236+
if d[t-v] > 0 {
237+
ans += v * (t - v)
238+
d[t-v]--
239+
m--
240+
} else {
241+
d[v]++
242+
}
243+
}
244+
if m == 0 {
245+
return int64(ans)
246+
}
247+
return -1
248+
}
249+
```
250+
149251
### **JavaScript**
150252

151253
```js
152-
var dividePlayers = function(skill) {
153-
const n = skill.length, m = n / 2;
254+
var dividePlayers = function (skill) {
255+
const n = skill.length,
256+
m = n / 2;
154257
skill.sort((a, b) => a - b);
155258
const sum = skill[0] + skill[n - 1];
156259
let ans = 0;
157260
for (let i = 0; i < m; i++) {
158-
const x = skill[i], y = skill[n - 1 - i];
261+
const x = skill[i],
262+
y = skill[n - 1 - i];
159263
if (x + y != sum) return -1;
160264
ans += x * y;
161265
}

0 commit comments

Comments
 (0)