Skip to content

feat: add weekly contest 408 #3332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 167 additions & 0 deletions solution/3200-3299/3232.Find if Digit Game Can Be Won/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
---
comments: true
difficulty: 简单
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3232.Find%20if%20Digit%20Game%20Can%20Be%20Won/README.md
---

<!-- problem:start -->

# [3232. 判断是否可以赢得数字游戏](https://leetcode.cn/problems/find-if-digit-game-can-be-won)

[English Version](/solution/3200-3299/3232.Find%20if%20Digit%20Game%20Can%20Be%20Won/README_EN.md)

## 题目描述

<!-- description:start -->

<p>给你一个 <strong>正整数 </strong>数组 <code>nums</code>。</p>

<p>小红和小明正在玩游戏。在游戏中,小红可以从 <code>nums</code> 中选择所有个位数 <strong>或</strong> 所有两位数,剩余的数字归小明所有。如果小红所选数字之和 <strong>严格大于 </strong>小明的数字之和,则小红获胜。</p>

<p>如果小红能赢得这场游戏,返回 <code>true</code>;否则,返回 <code>false</code>。</p>

<p>&nbsp;</p>

<p><strong class="example">示例 1:</strong></p>

<div class="example-block">
<p><strong>输入:</strong><span class="example-io">nums = [1,2,3,4,10]</span></p>

<p><strong>输出:</strong><span class="example-io">false</span></p>

<p><strong>解释:</strong></p>

<p>小红不管选个位数还是两位数都无法赢得比赛。</p>
</div>

<p><strong class="example">示例 2:</strong></p>

<div class="example-block">
<p><strong>输入:</strong><span class="example-io">nums = [1,2,3,4,5,14]</span></p>

<p><strong>输出:</strong><span class="example-io">true</span></p>

<p><strong>解释:</strong></p>

<p>小红选择个位数可以赢得比赛,所选数字之和为 15。</p>
</div>

<p><strong class="example">示例 3:</strong></p>

<div class="example-block">
<p><strong>输入:</strong><span class="example-io">nums = [5,5,5,25]</span></p>

<p><strong>输出:</strong><span class="example-io">true</span></p>

<p><strong>解释:</strong></p>

<p>小红选择两位数可以赢得比赛,所选数字之和为 25。</p>
</div>

<p>&nbsp;</p>

<p><strong>提示:</strong></p>

<ul>
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
<li><code>1 &lt;= nums[i] &lt;= 99</code></li>
</ul>

<!-- description:end -->

## 解法

<!-- solution:start -->

### 方法一:求和

根据题目描述,只要个位数之和不等于两位数之和,那么小红一定可以选择一个较大的和来获胜。

时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

#### Python3

```python
class Solution:
def canAliceWin(self, nums: List[int]) -> bool:
a = sum(x for x in nums if x < 10)
b = sum(x for x in nums if x > 9)
return a != b
```

#### Java

```java
class Solution {
public boolean canAliceWin(int[] nums) {
int a = 0, b = 0;
for (int x : nums) {
if (x < 10) {
a += x;
} else {
b += x;
}
}
return a != b;
}
}
```

#### C++

```cpp
class Solution {
public:
bool canAliceWin(vector<int>& nums) {
int a = 0, b = 0;
for (int x : nums) {
if (x < 10) {
a += x;
} else {
b += x;
}
}
return a != b;
}
};
```

#### Go

```go
func canAliceWin(nums []int) bool {
a, b := 0, 0
for _, x := range nums {
if x < 10 {
a += x
} else {
b += x
}
}
return a != b
}
```

#### TypeScript

```ts
function canAliceWin(nums: number[]): boolean {
let [a, b] = [0, 0];
for (const x of nums) {
if (x < 10) {
a += x;
} else {
b += x;
}
}
return a !== b;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
165 changes: 165 additions & 0 deletions solution/3200-3299/3232.Find if Digit Game Can Be Won/README_EN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
---
comments: true
difficulty: Easy
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3232.Find%20if%20Digit%20Game%20Can%20Be%20Won/README_EN.md
---

<!-- problem:start -->

# [3232. Find if Digit Game Can Be Won](https://leetcode.com/problems/find-if-digit-game-can-be-won)

[中文文档](/solution/3200-3299/3232.Find%20if%20Digit%20Game%20Can%20Be%20Won/README.md)

## Description

<!-- description:start -->

<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p>

<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>

<p>Return <code>true</code> if Alice can win this game, otherwise, return <code>false</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,10]</span></p>

<p><strong>Output:</strong> <span class="example-io">false</span></p>

<p><strong>Explanation:</strong></p>

<p>Alice cannot win by choosing either single-digit or double-digit numbers.</p>
</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,5,14]</span></p>

<p><strong>Output:</strong> <span class="example-io">true</span></p>

<p><strong>Explanation:</strong></p>

<p>Alice can win by choosing single-digit numbers which have a sum equal to 15.</p>
</div>

<p><strong class="example">Example 3:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [5,5,5,25]</span></p>

<p><strong>Output:</strong> <span class="example-io">true</span></p>

<p><strong>Explanation:</strong></p>

<p>Alice can win by choosing double-digit numbers which have a sum equal to 25.</p>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
<li><code>1 &lt;= nums[i] &lt;= 99</code></li>
</ul>

<!-- description:end -->

## Solutions

<!-- solution:start -->

### Solution 1: Summation

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.

The time complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$.

<!-- tabs:start -->

#### Python3

```python
class Solution:
def canAliceWin(self, nums: List[int]) -> bool:
a = sum(x for x in nums if x < 10)
b = sum(x for x in nums if x > 9)
return a != b
```

#### Java

```java
class Solution {
public boolean canAliceWin(int[] nums) {
int a = 0, b = 0;
for (int x : nums) {
if (x < 10) {
a += x;
} else {
b += x;
}
}
return a != b;
}
}
```

#### C++

```cpp
class Solution {
public:
bool canAliceWin(vector<int>& nums) {
int a = 0, b = 0;
for (int x : nums) {
if (x < 10) {
a += x;
} else {
b += x;
}
}
return a != b;
}
};
```

#### Go

```go
func canAliceWin(nums []int) bool {
a, b := 0, 0
for _, x := range nums {
if x < 10 {
a += x
} else {
b += x
}
}
return a != b
}
```

#### TypeScript

```ts
function canAliceWin(nums: number[]): boolean {
let [a, b] = [0, 0];
for (const x of nums) {
if (x < 10) {
a += x;
} else {
b += x;
}
}
return a !== b;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
14 changes: 14 additions & 0 deletions solution/3200-3299/3232.Find if Digit Game Can Be Won/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
public:
bool canAliceWin(vector<int>& nums) {
int a = 0, b = 0;
for (int x : nums) {
if (x < 10) {
a += x;
} else {
b += x;
}
}
return a != b;
}
};
11 changes: 11 additions & 0 deletions solution/3200-3299/3232.Find if Digit Game Can Be Won/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
func canAliceWin(nums []int) bool {
a, b := 0, 0
for _, x := range nums {
if x < 10 {
a += x
} else {
b += x
}
}
return a != b
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution {
public boolean canAliceWin(int[] nums) {
int a = 0, b = 0;
for (int x : nums) {
if (x < 10) {
a += x;
} else {
b += x;
}
}
return a != b;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Solution:
def canAliceWin(self, nums: List[int]) -> bool:
a = sum(x for x in nums if x < 10)
b = sum(x for x in nums if x > 9)
return a != b
11 changes: 11 additions & 0 deletions solution/3200-3299/3232.Find if Digit Game Can Be Won/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function canAliceWin(nums: number[]): boolean {
let [a, b] = [0, 0];
for (const x of nums) {
if (x < 10) {
a += x;
} else {
b += x;
}
}
return a !== b;
}
Loading
Loading