Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e13c2c2

Browse files
feat: add biweekly contest 135 and weekly contest 407 (doocs#3297)
1 parent 55513d2 commit e13c2c2

File tree

36 files changed

+2123
-8
lines changed

36 files changed

+2123
-8
lines changed

‎solution/1100-1199/1186.Maximum Subarray Sum with One Deletion/README.md‎

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ tags:
6767

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

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

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

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

7676
<!-- tabs:start -->
7777

@@ -195,6 +195,33 @@ function maximumSum(arr: number[]): number {
195195
}
196196
```
197197

198+
#### Rust
199+
200+
```rust
201+
impl Solution {
202+
pub fn maximum_sum(arr: Vec<i32>) -> i32 {
203+
let n = arr.len();
204+
let mut left = vec![0; n];
205+
let mut right = vec![0; n];
206+
let mut s = 0;
207+
for i in 0..n {
208+
s = (s.max(0)) + arr[i];
209+
left[i] = s;
210+
}
211+
s = 0;
212+
for i in (0..n).rev() {
213+
s = (s.max(0)) + arr[i];
214+
right[i] = s;
215+
}
216+
let mut ans = *left.iter().max().unwrap();
217+
for i in 1..n - 1 {
218+
ans = ans.max(left[i - 1] + right[i + 1]);
219+
}
220+
ans
221+
}
222+
}
223+
```
224+
198225
<!-- tabs:end -->
199226

200227
<!-- solution:end -->

‎solution/1100-1199/1186.Maximum Subarray Sum with One Deletion/README_EN.md‎

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ tags:
6363

6464
### Solution 1: Preprocessing + Enumeration
6565

66-
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.
66+
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.
6767

68-
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.
68+
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.
6969

70-
The time complexity is $O(n),ドル and the space complexity is $O(n)$. Here, $n$ is the length of the array $arr$.
70+
The time complexity is $O(n),ドル and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{arr}$.
7171

7272
<!-- tabs:start -->
7373

@@ -191,6 +191,33 @@ function maximumSum(arr: number[]): number {
191191
}
192192
```
193193

194+
#### Rust
195+
196+
```rust
197+
impl Solution {
198+
pub fn maximum_sum(arr: Vec<i32>) -> i32 {
199+
let n = arr.len();
200+
let mut left = vec![0; n];
201+
let mut right = vec![0; n];
202+
let mut s = 0;
203+
for i in 0..n {
204+
s = (s.max(0)) + arr[i];
205+
left[i] = s;
206+
}
207+
s = 0;
208+
for i in (0..n).rev() {
209+
s = (s.max(0)) + arr[i];
210+
right[i] = s;
211+
}
212+
let mut ans = *left.iter().max().unwrap();
213+
for i in 1..n - 1 {
214+
ans = ans.max(left[i - 1] + right[i + 1]);
215+
}
216+
ans
217+
}
218+
}
219+
```
220+
194221
<!-- tabs:end -->
195222

196223
<!-- solution:end -->
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
impl Solution {
2+
pub fn maximum_sum(arr: Vec<i32>) -> i32 {
3+
let n = arr.len();
4+
let mut left = vec![0; n];
5+
let mut right = vec![0; n];
6+
let mut s = 0;
7+
for i in 0..n {
8+
s = (s.max(0)) + arr[i];
9+
left[i] = s;
10+
}
11+
s = 0;
12+
for i in (0..n).rev() {
13+
s = (s.max(0)) + arr[i];
14+
right[i] = s;
15+
}
16+
let mut ans = *left.iter().max().unwrap();
17+
for i in 1..n - 1 {
18+
ans = ans.max(left[i - 1] + right[i + 1]);
19+
}
20+
ans
21+
}
22+
}
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
---
2+
comments: true
3+
difficulty: 简单
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3222.Find%20the%20Winning%20Player%20in%20Coin%20Game/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3222. 求出硬币游戏的赢家](https://leetcode.cn/problems/find-the-winning-player-in-coin-game)
10+
11+
[English Version](/solution/3200-3299/3222.Find%20the%20Winning%20Player%20in%20Coin%20Game/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>给你两个 <strong>正</strong>&nbsp;整数&nbsp;<code>x</code>&nbsp;&nbsp;<code>y</code>&nbsp;,分别表示价值为 75 和 10 的硬币的数目。</p>
18+
19+
<p>Alice 和 Bob 正在玩一个游戏。每一轮中,Alice&nbsp;先进行操作,Bob 后操作。每次操作中,玩家需要拿出价值 <b>总和</b>&nbsp;为 115 的硬币。如果一名玩家无法执行此操作,那么这名玩家 <strong>输掉</strong>&nbsp;游戏。</p>
20+
21+
<p>两名玩家都采取 <strong>最优</strong>&nbsp;策略,请你返回游戏的赢家。</p>
22+
23+
<p>&nbsp;</p>
24+
25+
<p><strong class="example">示例 1:</strong></p>
26+
27+
<div class="example-block">
28+
<p><span class="example-io"><b>输入:</b>x = 2, y = 7</span></p>
29+
30+
<p><span class="example-io"><b>输出:</b>"Alice"</span></p>
31+
32+
<p><strong>解释:</strong></p>
33+
34+
<p>游戏一次操作后结束:</p>
35+
36+
<ul>
37+
<li>Alice 拿走 1 枚价值为 75 的硬币和 4 枚价值为 10 的硬币。</li>
38+
</ul>
39+
</div>
40+
41+
<p><strong class="example">示例 2:</strong></p>
42+
43+
<div class="example-block">
44+
<p><span class="example-io"><b>输入:</b>x = 4, y = 11</span></p>
45+
46+
<p><span class="example-io"><b>输出:</b>"Bob"</span></p>
47+
48+
<p><strong>解释:</strong></p>
49+
50+
<p>游戏 2 次操作后结束:</p>
51+
52+
<ul>
53+
<li>Alice 拿走&nbsp;1 枚价值为 75 的硬币和 4 枚价值为 10 的硬币。</li>
54+
<li>Bob 拿走&nbsp;1 枚价值为 75 的硬币和 4 枚价值为 10 的硬币。</li>
55+
</ul>
56+
</div>
57+
58+
<p>&nbsp;</p>
59+
60+
<p><strong>提示:</strong></p>
61+
62+
<ul>
63+
<li><code>1 &lt;= x, y &lt;= 100</code></li>
64+
</ul>
65+
66+
<!-- description:end -->
67+
68+
## 解法
69+
70+
<!-- solution:start -->
71+
72+
### 方法一:数学
73+
74+
由于每一轮的操作,会消耗 2ドル$ 枚价值为 75ドル$ 的硬币和 8ドル$ 枚价值为 10ドル$ 的硬币,因此,我们可以计算得到操作的轮数 $k = \min(x / 2, y / 8),ドル然后更新 $x$ 和 $y$ 的值,此时 $x$ 和 $y$ 就是经过 $k$ 轮操作后剩余的硬币数目。
75+
76+
如果 $x > 0$ 且 $y \geq 4,ドル那么 Alice 还可以继续操作,此时 Bob 就输了,返回 "Alice";否则,返回 "Bob"。
77+
78+
时间复杂度 $O(1),ドル空间复杂度 $O(1)$。
79+
80+
<!-- tabs:start -->
81+
82+
#### Python3
83+
84+
```python
85+
class Solution:
86+
def losingPlayer(self, x: int, y: int) -> str:
87+
k = min(x // 2, y // 8)
88+
x -= k * 2
89+
y -= k * 8
90+
return "Alice" if x and y >= 4 else "Bob"
91+
```
92+
93+
#### Java
94+
95+
```java
96+
class Solution {
97+
public String losingPlayer(int x, int y) {
98+
int k = Math.min(x / 2, y / 8);
99+
x -= k * 2;
100+
y -= k * 8;
101+
return x > 0 && y >= 4 ? "Alice" : "Bob";
102+
}
103+
}
104+
```
105+
106+
#### C++
107+
108+
```cpp
109+
class Solution {
110+
public:
111+
string losingPlayer(int x, int y) {
112+
int k = min(x / 2, y / 8);
113+
x -= k * 2;
114+
y -= k * 8;
115+
return x && y >= 4 ? "Alice" : "Bob";
116+
}
117+
};
118+
```
119+
120+
#### Go
121+
122+
```go
123+
func losingPlayer(x int, y int) string {
124+
k := min(x/2, y/8)
125+
x -= 2 * k
126+
y -= 8 * k
127+
if x > 0 && y >= 4 {
128+
return "Alice"
129+
}
130+
return "Bob"
131+
}
132+
```
133+
134+
#### TypeScript
135+
136+
```ts
137+
function losingPlayer(x: number, y: number): string {
138+
const k = Math.min((x / 2) | 0, (y / 8) | 0);
139+
x -= k * 2;
140+
y -= k * 8;
141+
return x && y >= 4 ? 'Alice' : 'Bob';
142+
}
143+
```
144+
145+
<!-- tabs:end -->
146+
147+
<!-- solution:end -->
148+
149+
<!-- problem:end -->

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /