Skip to content

Commit 430b2f9

Browse files
authored
feat: add weekly contest 408 (#3332)
1 parent 8aa6828 commit 430b2f9

File tree

26 files changed

+1621
-1
lines changed

26 files changed

+1621
-1
lines changed
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
---
2+
comments: true
3+
difficulty: 简单
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3232.Find%20if%20Digit%20Game%20Can%20Be%20Won/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3232. 判断是否可以赢得数字游戏](https://leetcode.cn/problems/find-if-digit-game-can-be-won)
10+
11+
[English Version](/solution/3200-3299/3232.Find%20if%20Digit%20Game%20Can%20Be%20Won/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>给你一个 <strong>正整数 </strong>数组 <code>nums</code>。</p>
18+
19+
<p>小红和小明正在玩游戏。在游戏中,小红可以从 <code>nums</code> 中选择所有个位数 <strong>或</strong> 所有两位数,剩余的数字归小明所有。如果小红所选数字之和 <strong>严格大于 </strong>小明的数字之和,则小红获胜。</p>
20+
21+
<p>如果小红能赢得这场游戏,返回 <code>true</code>;否则,返回 <code>false</code>。</p>
22+
23+
<p>&nbsp;</p>
24+
25+
<p><strong class="example">示例 1:</strong></p>
26+
27+
<div class="example-block">
28+
<p><strong>输入:</strong><span class="example-io">nums = [1,2,3,4,10]</span></p>
29+
30+
<p><strong>输出:</strong><span class="example-io">false</span></p>
31+
32+
<p><strong>解释:</strong></p>
33+
34+
<p>小红不管选个位数还是两位数都无法赢得比赛。</p>
35+
</div>
36+
37+
<p><strong class="example">示例 2:</strong></p>
38+
39+
<div class="example-block">
40+
<p><strong>输入:</strong><span class="example-io">nums = [1,2,3,4,5,14]</span></p>
41+
42+
<p><strong>输出:</strong><span class="example-io">true</span></p>
43+
44+
<p><strong>解释:</strong></p>
45+
46+
<p>小红选择个位数可以赢得比赛,所选数字之和为 15。</p>
47+
</div>
48+
49+
<p><strong class="example">示例 3:</strong></p>
50+
51+
<div class="example-block">
52+
<p><strong>输入:</strong><span class="example-io">nums = [5,5,5,25]</span></p>
53+
54+
<p><strong>输出:</strong><span class="example-io">true</span></p>
55+
56+
<p><strong>解释:</strong></p>
57+
58+
<p>小红选择两位数可以赢得比赛,所选数字之和为 25。</p>
59+
</div>
60+
61+
<p>&nbsp;</p>
62+
63+
<p><strong>提示:</strong></p>
64+
65+
<ul>
66+
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
67+
<li><code>1 &lt;= nums[i] &lt;= 99</code></li>
68+
</ul>
69+
70+
<!-- description:end -->
71+
72+
## 解法
73+
74+
<!-- solution:start -->
75+
76+
### 方法一:求和
77+
78+
根据题目描述,只要个位数之和不等于两位数之和,那么小红一定可以选择一个较大的和来获胜。
79+
80+
时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。
81+
82+
<!-- tabs:start -->
83+
84+
#### Python3
85+
86+
```python
87+
class Solution:
88+
def canAliceWin(self, nums: List[int]) -> bool:
89+
a = sum(x for x in nums if x < 10)
90+
b = sum(x for x in nums if x > 9)
91+
return a != b
92+
```
93+
94+
#### Java
95+
96+
```java
97+
class Solution {
98+
public boolean canAliceWin(int[] nums) {
99+
int a = 0, b = 0;
100+
for (int x : nums) {
101+
if (x < 10) {
102+
a += x;
103+
} else {
104+
b += x;
105+
}
106+
}
107+
return a != b;
108+
}
109+
}
110+
```
111+
112+
#### C++
113+
114+
```cpp
115+
class Solution {
116+
public:
117+
bool canAliceWin(vector<int>& nums) {
118+
int a = 0, b = 0;
119+
for (int x : nums) {
120+
if (x < 10) {
121+
a += x;
122+
} else {
123+
b += x;
124+
}
125+
}
126+
return a != b;
127+
}
128+
};
129+
```
130+
131+
#### Go
132+
133+
```go
134+
func canAliceWin(nums []int) bool {
135+
a, b := 0, 0
136+
for _, x := range nums {
137+
if x < 10 {
138+
a += x
139+
} else {
140+
b += x
141+
}
142+
}
143+
return a != b
144+
}
145+
```
146+
147+
#### TypeScript
148+
149+
```ts
150+
function canAliceWin(nums: number[]): boolean {
151+
let [a, b] = [0, 0];
152+
for (const x of nums) {
153+
if (x < 10) {
154+
a += x;
155+
} else {
156+
b += x;
157+
}
158+
}
159+
return a !== b;
160+
}
161+
```
162+
163+
<!-- tabs:end -->
164+
165+
<!-- solution:end -->
166+
167+
<!-- problem:end -->
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
---
2+
comments: true
3+
difficulty: Easy
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3232.Find%20if%20Digit%20Game%20Can%20Be%20Won/README_EN.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3232. Find if Digit Game Can Be Won](https://leetcode.com/problems/find-if-digit-game-can-be-won)
10+
11+
[中文文档](/solution/3200-3299/3232.Find%20if%20Digit%20Game%20Can%20Be%20Won/README.md)
12+
13+
## Description
14+
15+
<!-- description:start -->
16+
17+
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p>
18+
19+
<p>Alice and Bob are playing a game. In the game, Alice can choose <strong>either</strong> all single-digit numbers or all double-digit numbers from <code>nums</code>, and the rest of the numbers are given to Bob. Alice wins if the sum of her numbers is <strong>strictly greater</strong> than the sum of Bob&#39;s numbers.</p>
20+
21+
<p>Return <code>true</code> if Alice can win this game, otherwise, return <code>false</code>.</p>
22+
23+
<p>&nbsp;</p>
24+
<p><strong class="example">Example 1:</strong></p>
25+
26+
<div class="example-block">
27+
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,10]</span></p>
28+
29+
<p><strong>Output:</strong> <span class="example-io">false</span></p>
30+
31+
<p><strong>Explanation:</strong></p>
32+
33+
<p>Alice cannot win by choosing either single-digit or double-digit numbers.</p>
34+
</div>
35+
36+
<p><strong class="example">Example 2:</strong></p>
37+
38+
<div class="example-block">
39+
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,14]</span></p>
40+
41+
<p><strong>Output:</strong> <span class="example-io">true</span></p>
42+
43+
<p><strong>Explanation:</strong></p>
44+
45+
<p>Alice can win by choosing single-digit numbers which have a sum equal to 15.</p>
46+
</div>
47+
48+
<p><strong class="example">Example 3:</strong></p>
49+
50+
<div class="example-block">
51+
<p><strong>Input:</strong> <span class="example-io">nums = [5,5,5,25]</span></p>
52+
53+
<p><strong>Output:</strong> <span class="example-io">true</span></p>
54+
55+
<p><strong>Explanation:</strong></p>
56+
57+
<p>Alice can win by choosing double-digit numbers which have a sum equal to 25.</p>
58+
</div>
59+
60+
<p>&nbsp;</p>
61+
<p><strong>Constraints:</strong></p>
62+
63+
<ul>
64+
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
65+
<li><code>1 &lt;= nums[i] &lt;= 99</code></li>
66+
</ul>
67+
68+
<!-- description:end -->
69+
70+
## Solutions
71+
72+
<!-- solution:start -->
73+
74+
### Solution 1: Summation
75+
76+
According to the problem description, as long as the sum of the units digits is not equal to the sum of the tens digits, Xiaohong can always choose a larger sum to win.
77+
78+
The time complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$.
79+
80+
<!-- tabs:start -->
81+
82+
#### Python3
83+
84+
```python
85+
class Solution:
86+
def canAliceWin(self, nums: List[int]) -> bool:
87+
a = sum(x for x in nums if x < 10)
88+
b = sum(x for x in nums if x > 9)
89+
return a != b
90+
```
91+
92+
#### Java
93+
94+
```java
95+
class Solution {
96+
public boolean canAliceWin(int[] nums) {
97+
int a = 0, b = 0;
98+
for (int x : nums) {
99+
if (x < 10) {
100+
a += x;
101+
} else {
102+
b += x;
103+
}
104+
}
105+
return a != b;
106+
}
107+
}
108+
```
109+
110+
#### C++
111+
112+
```cpp
113+
class Solution {
114+
public:
115+
bool canAliceWin(vector<int>& nums) {
116+
int a = 0, b = 0;
117+
for (int x : nums) {
118+
if (x < 10) {
119+
a += x;
120+
} else {
121+
b += x;
122+
}
123+
}
124+
return a != b;
125+
}
126+
};
127+
```
128+
129+
#### Go
130+
131+
```go
132+
func canAliceWin(nums []int) bool {
133+
a, b := 0, 0
134+
for _, x := range nums {
135+
if x < 10 {
136+
a += x
137+
} else {
138+
b += x
139+
}
140+
}
141+
return a != b
142+
}
143+
```
144+
145+
#### TypeScript
146+
147+
```ts
148+
function canAliceWin(nums: number[]): boolean {
149+
let [a, b] = [0, 0];
150+
for (const x of nums) {
151+
if (x < 10) {
152+
a += x;
153+
} else {
154+
b += x;
155+
}
156+
}
157+
return a !== b;
158+
}
159+
```
160+
161+
<!-- tabs:end -->
162+
163+
<!-- solution:end -->
164+
165+
<!-- problem:end -->
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
bool canAliceWin(vector<int>& nums) {
4+
int a = 0, b = 0;
5+
for (int x : nums) {
6+
if (x < 10) {
7+
a += x;
8+
} else {
9+
b += x;
10+
}
11+
}
12+
return a != b;
13+
}
14+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func canAliceWin(nums []int) bool {
2+
a, b := 0, 0
3+
for _, x := range nums {
4+
if x < 10 {
5+
a += x
6+
} else {
7+
b += x
8+
}
9+
}
10+
return a != b
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public boolean canAliceWin(int[] nums) {
3+
int a = 0, b = 0;
4+
for (int x : nums) {
5+
if (x < 10) {
6+
a += x;
7+
} else {
8+
b += x;
9+
}
10+
}
11+
return a != b;
12+
}
13+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution:
2+
def canAliceWin(self, nums: List[int]) -> bool:
3+
a = sum(x for x in nums if x < 10)
4+
b = sum(x for x in nums if x > 9)
5+
return a != b
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function canAliceWin(nums: number[]): boolean {
2+
let [a, b] = [0, 0];
3+
for (const x of nums) {
4+
if (x < 10) {
5+
a += x;
6+
} else {
7+
b += x;
8+
}
9+
}
10+
return a !== b;
11+
}

0 commit comments

Comments
 (0)