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 a242d2f

Browse files
feat: add solutions to lc problems: No.2960~2963 (#2083)
* No.2960.Count Tested Devices After Test Operations * No.2961.Double Modular Exponentiation * No.2962.Count Subarrays Where Max Element Appears at Least K Times * No.2963.Count the Number of Good Partitions
1 parent 0b44537 commit a242d2f

File tree

29 files changed

+1155
-28
lines changed

29 files changed

+1155
-28
lines changed

‎solution/2900-2999/2960.Count Tested Devices After Test Operations/README.md‎

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,13 @@
1212

1313
<ul>
1414
<li>如果 <code>batteryPercentages[i]</code> <strong>大于</strong> <code>0</code>:
15-
1615
<ul>
1716
<li><strong>增加</strong> 已测试设备的计数。</li>
1817
<li>将下标在 <code>[i + 1, n - 1]</code> 的所有设备的电池百分比减少 <code>1</code>,确保它们的电池百分比<strong> 不会低于</strong> <code>0</code> ,即 <code>batteryPercentages[j] = max(0, batteryPercentages[j] - 1)</code>。</li>
1918
<li>移动到下一个设备。</li>
2019
</ul>
2120
</li>
2221
<li>否则,移动到下一个设备而不执行任何测试。</li>
23-
2422
</ul>
2523

2624
<p>返回一个整数,表示按顺序执行测试操作后 <strong>已测试设备</strong> 的数量。</p>
@@ -66,34 +64,94 @@
6664

6765
<!-- 这里可写通用的实现逻辑 -->
6866

67+
**方法一:模拟**
68+
69+
假设我们当前已测试的设备数量为 $ans,ドル如果当测试新的一台设备 $i$ 时,它的剩余电量为 $\max(0, batteryPercentages[i] - ans),ドル如果剩余电量大于 0ドル,ドル则说明这台设备可以进行测试,此时我们需要将 $ans$ 增加 1ドル$。
70+
71+
最后返回 $ans$ 即可。
72+
73+
时间复杂度 $O(n),ドル其中 $n$ 为数组长度。空间复杂度 $O(1)$。
74+
6975
<!-- tabs:start -->
7076

7177
### **Python3**
7278

7379
<!-- 这里可写当前语言的特殊实现逻辑 -->
7480

7581
```python
76-
82+
class Solution:
83+
def countTestedDevices(self, batteryPercentages: List[int]) -> int:
84+
ans = 0
85+
for x in batteryPercentages:
86+
x -= ans
87+
ans += x > 0
88+
return ans
7789
```
7890

7991
### **Java**
8092

8193
<!-- 这里可写当前语言的特殊实现逻辑 -->
8294

8395
```java
84-
96+
class Solution {
97+
public int countTestedDevices(int[] batteryPercentages) {
98+
int ans = 0;
99+
for (int x : batteryPercentages) {
100+
x -= ans;
101+
if (x > 0) {
102+
++ans;
103+
}
104+
}
105+
return ans;
106+
}
107+
}
85108
```
86109

87110
### **C++**
88111

89112
```cpp
90-
113+
class Solution {
114+
public:
115+
int countTestedDevices(vector<int>& batteryPercentages) {
116+
int ans = 0;
117+
for (int x : batteryPercentages) {
118+
x -= ans;
119+
if (x > 0) {
120+
++ans;
121+
}
122+
}
123+
return ans;
124+
}
125+
};
91126
```
92127
93128
### **Go**
94129
95130
```go
131+
func countTestedDevices(batteryPercentages []int) (ans int) {
132+
for _, x := range batteryPercentages {
133+
x -= ans
134+
if x > 0 {
135+
ans++
136+
}
137+
}
138+
return
139+
}
140+
```
96141

142+
### **TypeScript**
143+
144+
```ts
145+
function countTestedDevices(batteryPercentages: number[]): number {
146+
let ans = 0;
147+
for (let x of batteryPercentages) {
148+
x -= ans;
149+
if (x > 0) {
150+
++ans;
151+
}
152+
}
153+
return ans;
154+
}
97155
```
98156

99157
### **...**

‎solution/2900-2999/2960.Count Tested Devices After Test Operations/README_EN.md‎

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,13 @@
1010

1111
<ul>
1212
<li>If <code>batteryPercentages[i]</code> is <strong>greater</strong> than <code>0</code>:
13-
1413
<ul>
1514
<li><strong>Increment</strong> the count of tested devices.</li>
1615
<li><strong>Decrease</strong> the battery percentage of all devices with indices <code>j</code> in the range <code>[i + 1, n - 1]</code> by <code>1</code>, ensuring their battery percentage <strong>never goes below</strong> <code>0</code>, i.e, <code>batteryPercentages[j] = max(0, batteryPercentages[j] - 1)</code>.</li>
1716
<li>Move to the next device.</li>
1817
</ul>
1918
</li>
2019
<li>Otherwise, move to the next device without performing any test.</li>
21-
2220
</ul>
2321

2422
<p>Return <em>an integer denoting the number of devices that will be tested after performing the test operations in order.</em></p>
@@ -60,30 +58,90 @@ So, the answer is 2.
6058

6159
## Solutions
6260

61+
**Solution 1: Simulation**
62+
63+
Assume that the current number of devices we have tested is $ans$. When testing a new device $i,ドル its remaining battery is $\max(0, batteryPercentages[i] - ans)$. If the remaining battery is greater than 0ドル,ドル it means this device can be tested, and we need to increase $ans$ by 1ドル$.
64+
65+
Finally, return $ans$.
66+
67+
The time complexity is $O(n),ドル where $n$ is the length of the array. The space complexity is $O(1)$.
68+
6369
<!-- tabs:start -->
6470

6571
### **Python3**
6672

6773
```python
68-
74+
class Solution:
75+
def countTestedDevices(self, batteryPercentages: List[int]) -> int:
76+
ans = 0
77+
for x in batteryPercentages:
78+
x -= ans
79+
ans += x > 0
80+
return ans
6981
```
7082

7183
### **Java**
7284

7385
```java
74-
86+
class Solution {
87+
public int countTestedDevices(int[] batteryPercentages) {
88+
int ans = 0;
89+
for (int x : batteryPercentages) {
90+
x -= ans;
91+
if (x > 0) {
92+
++ans;
93+
}
94+
}
95+
return ans;
96+
}
97+
}
7598
```
7699

77100
### **C++**
78101

79102
```cpp
80-
103+
class Solution {
104+
public:
105+
int countTestedDevices(vector<int>& batteryPercentages) {
106+
int ans = 0;
107+
for (int x : batteryPercentages) {
108+
x -= ans;
109+
if (x > 0) {
110+
++ans;
111+
}
112+
}
113+
return ans;
114+
}
115+
};
81116
```
82117
83118
### **Go**
84119
85120
```go
121+
func countTestedDevices(batteryPercentages []int) (ans int) {
122+
for _, x := range batteryPercentages {
123+
x -= ans
124+
if x > 0 {
125+
ans++
126+
}
127+
}
128+
return
129+
}
130+
```
86131

132+
### **TypeScript**
133+
134+
```ts
135+
function countTestedDevices(batteryPercentages: number[]): number {
136+
let ans = 0;
137+
for (let x of batteryPercentages) {
138+
x -= ans;
139+
if (x > 0) {
140+
++ans;
141+
}
142+
}
143+
return ans;
144+
}
87145
```
88146

89147
### **...**
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
int countTestedDevices(vector<int>& batteryPercentages) {
4+
int ans = 0;
5+
for (int x : batteryPercentages) {
6+
x -= ans;
7+
if (x > 0) {
8+
++ans;
9+
}
10+
}
11+
return ans;
12+
}
13+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
func countTestedDevices(batteryPercentages []int) (ans int) {
2+
for _, x := range batteryPercentages {
3+
x -= ans
4+
if x > 0 {
5+
ans++
6+
}
7+
}
8+
return
9+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public int countTestedDevices(int[] batteryPercentages) {
3+
int ans = 0;
4+
for (int x : batteryPercentages) {
5+
x -= ans;
6+
if (x > 0) {
7+
++ans;
8+
}
9+
}
10+
return ans;
11+
}
12+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def countTestedDevices(self, batteryPercentages: List[int]) -> int:
3+
ans = 0
4+
for x in batteryPercentages:
5+
x -= ans
6+
ans += x > 0
7+
return ans
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function countTestedDevices(batteryPercentages: number[]): number {
2+
let ans = 0;
3+
for (let x of batteryPercentages) {
4+
x -= ans;
5+
if (x > 0) {
6+
++ans;
7+
}
8+
}
9+
return ans;
10+
}

0 commit comments

Comments
(0)

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