|
10 | 10 |
|
11 | 11 | <ul>
|
12 | 12 | <li>If <code>batteryPercentages[i]</code> is <strong>greater</strong> than <code>0</code>:
|
13 |
| - |
14 | 13 | <ul>
|
15 | 14 | <li><strong>Increment</strong> the count of tested devices.</li>
|
16 | 15 | <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>
|
17 | 16 | <li>Move to the next device.</li>
|
18 | 17 | </ul>
|
19 | 18 | </li>
|
20 | 19 | <li>Otherwise, move to the next device without performing any test.</li>
|
21 |
| - |
22 | 20 | </ul>
|
23 | 21 |
|
24 | 22 | <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.
|
60 | 58 |
|
61 | 59 | ## Solutions
|
62 | 60 |
|
| 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 | + |
63 | 69 | <!-- tabs:start -->
|
64 | 70 |
|
65 | 71 | ### **Python3**
|
66 | 72 |
|
67 | 73 | ```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 |
69 | 81 | ```
|
70 | 82 |
|
71 | 83 | ### **Java**
|
72 | 84 |
|
73 | 85 | ```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 | +} |
75 | 98 | ```
|
76 | 99 |
|
77 | 100 | ### **C++**
|
78 | 101 |
|
79 | 102 | ```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 | +}; |
81 | 116 | ```
|
82 | 117 |
|
83 | 118 | ### **Go**
|
84 | 119 |
|
85 | 120 | ```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 | +``` |
86 | 131 |
|
| 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 | +} |
87 | 145 | ```
|
88 | 146 |
|
89 | 147 | ### **...**
|
|
0 commit comments