Skip to content

feat: add biweekly contest 135 and weekly contest 407 #3297

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 2 commits into from
Jul 21, 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
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ tags:

### 方法一:预处理 + 枚举

我们可以先预处理出数组 $arr$ 以每个元素结尾和开头的最大子数组和,分别存入数组 $left$ 和 $right$ 中。
我们可以先预处理出数组 $\textit{arr}$ 以每个元素结尾和开头的最大子数组和,分别存入数组 $\textit{left}$ 和 $\textit{right}$ 中。

如果我们不删除任何元素,那么最大子数组和就是 $left[i]$ 或 $right[i]$ 中的最大值;如果我们删除一个元素,我们可以枚举 $[1..n-2]$ 中的每个位置 $i$,计算 $left[i-1] + right[i+1]$ 的值,取最大值即可。
如果我们不删除任何元素,那么最大子数组和就是 $\textit{left}[i]$ 或 $\textit{right}[i]$ 中的最大值;如果我们删除一个元素,我们可以枚举 $[1..n-2]$ 中的每个位置 $i$,计算 $\textit{left}[i-1] + \textit{right}[i+1]$ 的值,取最大值即可。

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

<!-- tabs:start -->

Expand Down Expand Up @@ -195,6 +195,33 @@ function maximumSum(arr: number[]): number {
}
```

#### Rust

```rust
impl Solution {
pub fn maximum_sum(arr: Vec<i32>) -> i32 {
let n = arr.len();
let mut left = vec![0; n];
let mut right = vec![0; n];
let mut s = 0;
for i in 0..n {
s = (s.max(0)) + arr[i];
left[i] = s;
}
s = 0;
for i in (0..n).rev() {
s = (s.max(0)) + arr[i];
right[i] = s;
}
let mut ans = *left.iter().max().unwrap();
for i in 1..n - 1 {
ans = ans.max(left[i - 1] + right[i + 1]);
}
ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ tags:

### Solution 1: Preprocessing + Enumeration

We can first preprocess the array $arr$ to find the maximum subarray sum ending at and starting from each element, and store them in the arrays $left$ and $right$ respectively.
We can preprocess the array $\textit{arr}$ to find the maximum subarray sum ending and starting with each element, storing them in arrays $\textit{left}$ and $\textit{right}$, respectively.

If we do not delete any element, then the maximum subarray sum is the maximum value in $left[i]$ or $right[i]$. If we delete one element, we can enumerate each position $i$ in $[1..n-2]$, calculate the value of $left[i-1] + right[i+1]$, and take the maximum value.
If we do not delete any element, then the maximum subarray sum is the maximum value in $\textit{left}[i]$ or $\textit{right}[i]$; if we delete an element, we can enumerate each position $i$ in $[1..n-2]$, calculate the value of $\textit{left}[i-1] + \textit{right}[i+1]$, and take the maximum value.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $arr$.
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{arr}$.

<!-- tabs:start -->

Expand Down Expand Up @@ -191,6 +191,33 @@ function maximumSum(arr: number[]): number {
}
```

#### Rust

```rust
impl Solution {
pub fn maximum_sum(arr: Vec<i32>) -> i32 {
let n = arr.len();
let mut left = vec![0; n];
let mut right = vec![0; n];
let mut s = 0;
for i in 0..n {
s = (s.max(0)) + arr[i];
left[i] = s;
}
s = 0;
for i in (0..n).rev() {
s = (s.max(0)) + arr[i];
right[i] = s;
}
let mut ans = *left.iter().max().unwrap();
for i in 1..n - 1 {
ans = ans.max(left[i - 1] + right[i + 1]);
}
ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
impl Solution {
pub fn maximum_sum(arr: Vec<i32>) -> i32 {
let n = arr.len();
let mut left = vec![0; n];
let mut right = vec![0; n];
let mut s = 0;
for i in 0..n {
s = (s.max(0)) + arr[i];
left[i] = s;
}
s = 0;
for i in (0..n).rev() {
s = (s.max(0)) + arr[i];
right[i] = s;
}
let mut ans = *left.iter().max().unwrap();
for i in 1..n - 1 {
ans = ans.max(left[i - 1] + right[i + 1]);
}
ans
}
}
149 changes: 149 additions & 0 deletions solution/3200-3299/3222.Find the Winning Player in Coin Game/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
---
comments: true
difficulty: 简单
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3222.Find%20the%20Winning%20Player%20in%20Coin%20Game/README.md
---

<!-- problem:start -->

# [3222. 求出硬币游戏的赢家](https://leetcode.cn/problems/find-the-winning-player-in-coin-game)

[English Version](/solution/3200-3299/3222.Find%20the%20Winning%20Player%20in%20Coin%20Game/README_EN.md)

## 题目描述

<!-- description:start -->

<p>给你两个 <strong>正</strong>&nbsp;整数&nbsp;<code>x</code>&nbsp;&nbsp;<code>y</code>&nbsp;,分别表示价值为 75 和 10 的硬币的数目。</p>

<p>Alice 和 Bob 正在玩一个游戏。每一轮中,Alice&nbsp;先进行操作,Bob 后操作。每次操作中,玩家需要拿出价值 <b>总和</b>&nbsp;为 115 的硬币。如果一名玩家无法执行此操作,那么这名玩家 <strong>输掉</strong>&nbsp;游戏。</p>

<p>两名玩家都采取 <strong>最优</strong>&nbsp;策略,请你返回游戏的赢家。</p>

<p>&nbsp;</p>

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

<div class="example-block">
<p><span class="example-io"><b>输入:</b>x = 2, y = 7</span></p>

<p><span class="example-io"><b>输出:</b>"Alice"</span></p>

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

<p>游戏一次操作后结束:</p>

<ul>
<li>Alice 拿走 1 枚价值为 75 的硬币和 4 枚价值为 10 的硬币。</li>
</ul>
</div>

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

<div class="example-block">
<p><span class="example-io"><b>输入:</b>x = 4, y = 11</span></p>

<p><span class="example-io"><b>输出:</b>"Bob"</span></p>

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

<p>游戏 2 次操作后结束:</p>

<ul>
<li>Alice 拿走&nbsp;1 枚价值为 75 的硬币和 4 枚价值为 10 的硬币。</li>
<li>Bob 拿走&nbsp;1 枚价值为 75 的硬币和 4 枚价值为 10 的硬币。</li>
</ul>
</div>

<p>&nbsp;</p>

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

<ul>
<li><code>1 &lt;= x, y &lt;= 100</code></li>
</ul>

<!-- description:end -->

## 解法

<!-- solution:start -->

### 方法一:数学

由于每一轮的操作,会消耗 $2$ 枚价值为 $75$ 的硬币和 $8$ 枚价值为 $10$ 的硬币,因此,我们可以计算得到操作的轮数 $k = \min(x / 2, y / 8)$,然后更新 $x$ 和 $y$ 的值,此时 $x$ 和 $y$ 就是经过 $k$ 轮操作后剩余的硬币数目。

如果 $x > 0$ 且 $y \geq 4$,那么 Alice 还可以继续操作,此时 Bob 就输了,返回 "Alice";否则,返回 "Bob"。

时间复杂度 $O(1)$,空间复杂度 $O(1)$。

<!-- tabs:start -->

#### Python3

```python
class Solution:
def losingPlayer(self, x: int, y: int) -> str:
k = min(x // 2, y // 8)
x -= k * 2
y -= k * 8
return "Alice" if x and y >= 4 else "Bob"
```

#### Java

```java
class Solution {
public String losingPlayer(int x, int y) {
int k = Math.min(x / 2, y / 8);
x -= k * 2;
y -= k * 8;
return x > 0 && y >= 4 ? "Alice" : "Bob";
}
}
```

#### C++

```cpp
class Solution {
public:
string losingPlayer(int x, int y) {
int k = min(x / 2, y / 8);
x -= k * 2;
y -= k * 8;
return x && y >= 4 ? "Alice" : "Bob";
}
};
```
#### Go
```go
func losingPlayer(x int, y int) string {
k := min(x/2, y/8)
x -= 2 * k
y -= 8 * k
if x > 0 && y >= 4 {
return "Alice"
}
return "Bob"
}
```

#### TypeScript

```ts
function losingPlayer(x: number, y: number): string {
const k = Math.min((x / 2) | 0, (y / 8) | 0);
x -= k * 2;
y -= k * 8;
return x && y >= 4 ? 'Alice' : 'Bob';
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Loading
Loading