Skip to content

Commit 6562eb1

Browse files
authored
Merge pull request #419 from LetMeFly666/3038
添加问题“3038.相同分数的最大操作数目I”的代码和题解
2 parents dbb3f6e + 95e3c27 commit 6562eb1

7 files changed

+231
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-06-07 20:19:32
4+
* @LastEditors: LetMeFly
5+
* @LastEditTime: 2024-06-07 20:21:19
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
int maxOperations(vector<int>& nums) {
14+
int ans = 1;
15+
int val = nums[0] + nums[1];
16+
for (int i = 2; i + 1< nums.size(); i += 2) {
17+
if (nums[i] + nums[i + 1] != val) {
18+
break;
19+
}
20+
ans++;
21+
}
22+
return ans;
23+
}
24+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-06-07 20:24:47
4+
* @LastEditors: LetMeFly
5+
* @LastEditTime: 2024-06-07 20:25:52
6+
*/
7+
package main
8+
9+
func maxOperations(nums []int) int {
10+
ans := 1
11+
val := nums[0] + nums[1]
12+
for i := 2; i < len(nums)-1; i += 2 {
13+
if nums[i]+nums[i+1] != val {
14+
break
15+
}
16+
ans++
17+
}
18+
return ans
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-06-07 20:23:10
4+
* @LastEditors: LetMeFly
5+
* @LastEditTime: 2024-06-07 20:24:19
6+
*/
7+
class Solution {
8+
public int maxOperations(int[] nums) {
9+
int ans = 1;
10+
int val = nums[0] + nums[1];
11+
for (int i = 2; i < nums.length - 1; i += 2) {
12+
if (nums[i] + nums[i + 1] != val) {
13+
break;
14+
}
15+
ans++;
16+
}
17+
return ans;
18+
}
19+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2024-06-07 20:21:40
4+
LastEditors: LetMeFly
5+
LastEditTime: 2024-06-07 20:22:25
6+
'''
7+
from typing import List
8+
9+
class Solution:
10+
def maxOperations(self, nums: List[int]) -> int:
11+
ans = 1
12+
val = nums[0] + nums[1]
13+
for i in range(2, len(nums) - 1, 2):
14+
if nums[i] + nums[i + 1] != val:
15+
break
16+
ans += 1
17+
return ans

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,7 @@
623623
|2960.统计已测试设备|简单|<a href="https://leetcode.cn/problems/count-tested-devices-after-test-operations/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/05/10/LeetCode%202960.%E7%BB%9F%E8%AE%A1%E5%B7%B2%E6%B5%8B%E8%AF%95%E8%AE%BE%E5%A4%87/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/138672383" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-tested-devices-after-test-operations/solutions/2772739/letmefly-2960tong-ji-yi-ce-shi-she-bei-k-59qt/" target="_blank">LeetCode题解</a>|
624624
|2965.找出缺失和重复的数字|简单|<a href="https://leetcode.cn/problems/find-missing-and-repeated-values/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/05/31/LeetCode%202965.%E6%89%BE%E5%87%BA%E7%BC%BA%E5%A4%B1%E5%92%8C%E9%87%8D%E5%A4%8D%E7%9A%84%E6%95%B0%E5%AD%97/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/139357662" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-missing-and-repeated-values/solutions/2796819/letmefly-2965zhao-chu-que-shi-he-zhong-f-p2rn/" target="_blank">LeetCode题解</a>|
625625
|2982.找出出现至少三次的最长特殊子字符串II|中等|<a href="https://leetcode.cn/problems/find-longest-special-substring-that-occurs-thrice-ii/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/05/30/LeetCode%202982.%E6%89%BE%E5%87%BA%E5%87%BA%E7%8E%B0%E8%87%B3%E5%B0%91%E4%B8%89%E6%AC%A1%E7%9A%84%E6%9C%80%E9%95%BF%E7%89%B9%E6%AE%8A%E5%AD%90%E5%AD%97%E7%AC%A6%E4%B8%B2II/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/139334864" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-longest-special-substring-that-occurs-thrice-ii/solutions/2795996/letmefly-2982zhao-chu-chu-xian-zhi-shao-9ubg1/" target="_blank">LeetCode题解</a>|
626+
|3038.相同分数的最大操作数目I|简单|<a href="https://leetcode.cn/problems/maximum-number-of-operations-with-the-same-score-i/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/06/07/LeetCode%203038.%E7%9B%B8%E5%90%8C%E5%88%86%E6%95%B0%E7%9A%84%E6%9C%80%E5%A4%A7%E6%93%8D%E4%BD%9C%E6%95%B0%E7%9B%AEI/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/139535702" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-number-of-operations-with-the-same-score-i/solutions/2804245/letmefly-3038xiang-tong-fen-shu-de-zui-d-dkj7/" target="_blank">LeetCode题解</a>|
626627
|3067.在带权树网络中统计可连接服务器对数目|中等|<a href="https://leetcode.cn/problems/count-pairs-of-connectable-servers-in-a-weighted-tree-network/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/06/04/LeetCode%203067.%E5%9C%A8%E5%B8%A6%E6%9D%83%E6%A0%91%E7%BD%91%E7%BB%9C%E4%B8%AD%E7%BB%9F%E8%AE%A1%E5%8F%AF%E8%BF%9E%E6%8E%A5%E6%9C%8D%E5%8A%A1%E5%99%A8%E5%AF%B9%E6%95%B0%E7%9B%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/139456087" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-pairs-of-connectable-servers-in-a-weighted-tree-network/solutions/2801125/letmefly-3067zai-dai-quan-shu-wang-luo-z-zf2j/" target="_blank">LeetCode题解</a>|
627628
|剑指Offer0047.礼物的最大价值|简单|<a href="https://leetcode.cn/problems/li-wu-de-zui-da-jie-zhi-lcof/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/03/08/LeetCode%20%E5%89%91%E6%8C%87%20Offer%2047.%20%E7%A4%BC%E7%89%A9%E7%9A%84%E6%9C%80%E5%A4%A7%E4%BB%B7%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/129408765" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/li-wu-de-zui-da-jie-zhi-lcof/solutions/2155672/letmefly-jian-zhi-offer-47li-wu-de-zui-d-rekb/" target="_blank">LeetCode题解</a>|
628629
|剑指OfferII0041.滑动窗口的平均值|简单|<a href="https://leetcode.cn/problems/qIsx9U/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/07/16/LeetCode%20%E5%89%91%E6%8C%87%20Offer%20II%200041.%20%E6%BB%91%E5%8A%A8%E7%AA%97%E5%8F%A3%E7%9A%84%E5%B9%B3%E5%9D%87%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/125819216" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/qIsx9U/solution/by-tisfy-30mq/" target="_blank">LeetCode题解</a>|
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
---
2+
title: 3038.相同分数的最大操作数目 I
3+
date: 2024-06-07 20:49:43
4+
tags: [题解, LeetCode, 简单, 数组, 模拟]
5+
---
6+
7+
# 【LetMeFly】3038.相同分数的最大操作数目 I
8+
9+
力扣题目链接:[https://leetcode.cn/problems/maximum-number-of-operations-with-the-same-score-i/](https://leetcode.cn/problems/maximum-number-of-operations-with-the-same-score-i/)
10+
11+
<p>给你一个整数数组&nbsp;<code>nums</code>&nbsp;,如果&nbsp;<code>nums</code>&nbsp;<strong>至少</strong>&nbsp;包含&nbsp;<code>2</code>&nbsp;个元素,你可以执行以下操作:</p>
12+
13+
<ul>
14+
<li>选择 <code>nums</code>&nbsp;中的前两个元素并将它们删除。</li>
15+
</ul>
16+
17+
<p>一次操作的 <strong>分数</strong>&nbsp;是被删除元素的和。</p>
18+
19+
<p>在确保<strong>&nbsp;所有操作分数相同</strong>&nbsp;的前提下,请你求出 <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+
<pre>
28+
<b>输入:</b>nums = [3,2,1,4,5]
29+
<b>输出:</b>2
30+
<b>解释:</b>我们执行以下操作:
31+
- 删除前两个元素,分数为 3 + 2 = 5 ,nums = [1,4,5] 。
32+
- 删除前两个元素,分数为 1 + 4 = 5 ,nums = [5] 。
33+
由于只剩下 1 个元素,我们无法继续进行任何操作。</pre>
34+
35+
<p><strong class="example">示例 2:</strong></p>
36+
37+
<pre>
38+
<b>输入:</b>nums = [3,2,6,1,4]
39+
<b>输出:</b>1
40+
<b>解释:</b>我们执行以下操作:
41+
- 删除前两个元素,分数为 3 + 2 = 5 ,nums = [6,1,4] 。
42+
由于下一次操作的分数与前一次不相等,我们无法继续进行任何操作。
43+
</pre>
44+
45+
<p>&nbsp;</p>
46+
47+
<p><strong>提示:</strong></p>
48+
49+
<ul>
50+
<li><code>2 &lt;= nums.length &lt;= 100</code></li>
51+
<li><code>1 &lt;= nums[i] &lt;= 1000</code></li>
52+
</ul>
53+
54+
55+
56+
## 解题方法:遍历模拟
57+
58+
首先记录$nums[0] + nums[1]的值$(记为$val$),接着从下标$2$开始遍历数组(遍历时$i$每次+2),如果相邻两个元素之和为$val$,则答案加一且遍历继续;否则遍历结束。
59+
60+
+ 时间复杂度$O(len(nums))$
61+
+ 空间复杂度$O(1)$
62+
63+
### AC代码
64+
65+
#### C++
66+
67+
```cpp
68+
class Solution {
69+
public:
70+
int maxOperations(vector<int>& nums) {
71+
int ans = 1;
72+
int val = nums[0] + nums[1];
73+
for (int i = 2; i + 1< nums.size(); i += 2) {
74+
if (nums[i] + nums[i + 1] != val) {
75+
break;
76+
}
77+
ans++;
78+
}
79+
return ans;
80+
}
81+
};
82+
```
83+
84+
#### Go
85+
86+
```go
87+
// package main
88+
89+
func maxOperations(nums []int) int {
90+
ans := 1
91+
val := nums[0] + nums[1]
92+
for i := 2; i < len(nums)-1; i += 2 {
93+
if nums[i]+nums[i+1] != val {
94+
break
95+
}
96+
ans++
97+
}
98+
return ans
99+
}
100+
```
101+
102+
#### Java
103+
104+
```java
105+
class Solution {
106+
public int maxOperations(int[] nums) {
107+
int ans = 1;
108+
int val = nums[0] + nums[1];
109+
for (int i = 2; i < nums.length - 1; i += 2) {
110+
if (nums[i] + nums[i + 1] != val) {
111+
break;
112+
}
113+
ans++;
114+
}
115+
return ans;
116+
}
117+
}
118+
```
119+
120+
#### Python
121+
122+
```python
123+
# from typing import List
124+
125+
class Solution:
126+
def maxOperations(self, nums: List[int]) -> int:
127+
ans = 1
128+
val = nums[0] + nums[1]
129+
for i in range(2, len(nums) - 1, 2):
130+
if nums[i] + nums[i + 1] != val:
131+
break
132+
ans += 1
133+
return ans
134+
```
135+
136+
## End
137+
138+
这是在486/1920(px)宽度屏幕下写的题解。这是为什么呢?
139+
140+
```base64
141+
5LuK5aSp5byA5aeL56uv5Y2I5pS+5YGH77yM5LqO5piv5bCx5pyJ5LqG5piO5pel6KaB5a6M5oiQ55qE56CU56m2546w54q25ZKM5LiL5LiL5ZGo5LqM6KaB5a6M5oiQ55qE5oqA5pyv6Lev57q/44CC44GE44GE44KT44GY44KD44Gq44GE44GLRG9nZSjotoXlpKfniYgpCg==
142+
```
143+
144+
还是白色背景的VsCode和Word比较像。
145+
146+
> 同步发文于CSDN和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2024/06/07/LeetCode%203038.%E7%9B%B8%E5%90%8C%E5%88%86%E6%95%B0%E7%9A%84%E6%9C%80%E5%A4%A7%E6%93%8D%E4%BD%9C%E6%95%B0%E7%9B%AEI/)哦~
147+
>
148+
> Tisfy:[https://letmefly.blog.csdn.net/article/details/139535702](https://letmefly.blog.csdn.net/article/details/139535702)

Solutions/Other-Japanese-LearningNotes.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,8 @@ xx型
358358
|東京(とうきょう)|东京|
359359
|大阪(おおさか)|大阪|
360360
|京都(きょうと)|京都|
361+
|遠く(とおく)|远方|
362+
|近く(ちかく)|附近|
361363
|||
362364
|||
363365
|えい||
@@ -560,6 +562,7 @@ xx型
560562
|スポーツはあまり好きではありません。<br/>不怎么喜欢运动。|
561563
|マリアに電話をかけます。<br/>给玛丽亚打电话。|
562564
|31時間はたらきました。<br/>工作了31个小时。|
565+
|何か描きましょう。<br/>画点什么吧。|
563566

564567

565568
<span style="background-color:#ffbd2a; color: white">TODO:</span> 实词用<font color="#28bea0">-</font>色助词用<font color="#ce82ff">-</font>色。

0 commit comments

Comments
 (0)