2960. Count Tested Devices After Test Operations Easy
1/**
2 * [2960] Count Tested Devices After Test Operations
3 *
4 * You are given a 0-indexed integer array batteryPercentages having length n, denoting the battery percentages of n 0-indexed devices.
5 * Your task is to test each device i in order from 0 to n - 1, by performing the following test operations:
6 *
7 * If batteryPercentages[i] is greater than 0:
8 *
9 * Increment the count of tested devices.
10 * Decrease the battery percentage of all devices with indices j in the range [i + 1, n - 1] by 1, ensuring their battery percentage never goes below 0, i.e, batteryPercentages[j] = max(0, batteryPercentages[j] - 1).
11 * Move to the next device.
12 *
13 *
14 * Otherwise, move to the next device without performing any test.
15 *
16 * Return an integer denoting the number of devices that will be tested after performing the test operations in order.
17 *
18 * <strong class="example">Example 1:
19 *
20 * Input: batteryPercentages = [1,1,2,1,3]
21 * Output: 3
22 * Explanation: Performing the test operations in order starting from device 0:
23 * At device 0, batteryPercentages[0] > 0, so there is now 1 tested device, and batteryPercentages becomes [1,0,1,0,2].
24 * At device 1, batteryPercentages[1] == 0, so we move to the next device without testing.
25 * At device 2, batteryPercentages[2] > 0, so there are now 2 tested devices, and batteryPercentages becomes [1,0,1,0,1].
26 * At device 3, batteryPercentages[3] == 0, so we move to the next device without testing.
27 * At device 4, batteryPercentages[4] > 0, so there are now 3 tested devices, and batteryPercentages stays the same.
28 * So, the answer is 3.
29 *
30 * <strong class="example">Example 2:
31 *
32 * Input: batteryPercentages = [0,1,2]
33 * Output: 2
34 * Explanation: Performing the test operations in order starting from device 0:
35 * At device 0, batteryPercentages[0] == 0, so we move to the next device without testing.
36 * At device 1, batteryPercentages[1] > 0, so there is now 1 tested device, and batteryPercentages becomes [0,1,1].
37 * At device 2, batteryPercentages[2] > 0, so there are now 2 tested devices, and batteryPercentages stays the same.
38 * So, the answer is 2.
39 *
40 *
41 * Constraints:
42 *
43 * 1 <= n == batteryPercentages.length <= 100
44 * 0 <= batteryPercentages[i] <= 100
45 *
46 */
47pub struct Solution {}
48
49// problem: https://leetcode.com/problems/count-tested-devices-after-test-operations/
50// discuss: https://leetcode.com/problems/count-tested-devices-after-test-operations/discuss/?currentPage=1&orderBy=most_votes&query=
51
52// submission codes start here
53
54impl Solution {
55 pub fn count_tested_devices(battery_percentages: Vec<i32>) -> i32 {
56 0
57 }
58}
59
60// submission codes end
61
62#[cfg(test)]
63mod tests {
64 use super::*;
65
66 #[test]
67 fn test_2960() {
68 }
69}
70
Back
© 2025 bowen.ge All Rights Reserved.