-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2960-CountTestedDevicesAfterTestOperations.go
More file actions
89 lines (79 loc) · 4.52 KB
/
2960-CountTestedDevicesAfterTestOperations.go
File metadata and controls
89 lines (79 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
// 2960. Count Tested Devices After Test Operations
// You are given a 0-indexed integer array batteryPercentages having length n, denoting the battery percentages of n 0-indexed devices.
// Your task is to test each device i in order from 0 to n - 1, by performing the following test operations:
// If batteryPercentages[i] is greater than 0:
// Increment the count of tested devices.
// 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).
// Move to the next device.
// Otherwise, move to the next device without performing any test.
// Return an integer denoting the number of devices that will be tested after performing the test operations in order.
// Example 1:
// Input: batteryPercentages = [1,1,2,1,3]
// Output: 3
// Explanation: Performing the test operations in order starting from device 0:
// At device 0, batteryPercentages[0] > 0, so there is now 1 tested device, and batteryPercentages becomes [1,0,1,0,2].
// At device 1, batteryPercentages[1] == 0, so we move to the next device without testing.
// At device 2, batteryPercentages[2] > 0, so there are now 2 tested devices, and batteryPercentages becomes [1,0,1,0,1].
// At device 3, batteryPercentages[3] == 0, so we move to the next device without testing.
// At device 4, batteryPercentages[4] > 0, so there are now 3 tested devices, and batteryPercentages stays the same.
// So, the answer is 3.
// Example 2:
// Input: batteryPercentages = [0,1,2]
// Output: 2
// Explanation: Performing the test operations in order starting from device 0:
// At device 0, batteryPercentages[0] == 0, so we move to the next device without testing.
// At device 1, batteryPercentages[1] > 0, so there is now 1 tested device, and batteryPercentages becomes [0,1,1].
// At device 2, batteryPercentages[2] > 0, so there are now 2 tested devices, and batteryPercentages stays the same.
// So, the answer is 2.
// Constraints:
// 1 <= n == batteryPercentages.length <= 100
// 0 <= batteryPercentages[i] <= 100
import "fmt"
func countTestedDevices(batteryPercentages []int) int {
res := 0
for _, v := range batteryPercentages {
if v > res {
res++
}
}
return res
}
func countTestedDevices1(batteryPercentages []int) int {
res := 0
// 如果 batteryPercentages[i] 大于 0:
// 增加 已测试设备的计数。
// 将下标在 [i + 1, n - 1] 的所有设备的电池百分比减少 1,确保它们的电池百分比 不会低于 0 ,即 batteryPercentages[j] = max(0, batteryPercentages[j] - 1)。
// 移动到下一个设备。
// 否则,移动到下一个设备而不执行任何测试。
for _,v := range batteryPercentages {
if v - res > 0 { // 电量不足直接跳过
res++ // 每测试一设置设备 下标在 [i + 1, n - 1] 的所有设备的电池百分比减少 1
}
}
return res
}
func main() {
// Example 1:
// Input: batteryPercentages = [1,1,2,1,3]
// Output: 3
// Explanation: Performing the test operations in order starting from device 0:
// At device 0, batteryPercentages[0] > 0, so there is now 1 tested device, and batteryPercentages becomes [1,0,1,0,2].
// At device 1, batteryPercentages[1] == 0, so we move to the next device without testing.
// At device 2, batteryPercentages[2] > 0, so there are now 2 tested devices, and batteryPercentages becomes [1,0,1,0,1].
// At device 3, batteryPercentages[3] == 0, so we move to the next device without testing.
// At device 4, batteryPercentages[4] > 0, so there are now 3 tested devices, and batteryPercentages stays the same.
// So, the answer is 3.
fmt.Println(countTestedDevices([]int{1,1,2,1,3})) // 3
// Example 2:
// Input: batteryPercentages = [0,1,2]
// Output: 2
// Explanation: Performing the test operations in order starting from device 0:
// At device 0, batteryPercentages[0] == 0, so we move to the next device without testing.
// At device 1, batteryPercentages[1] > 0, so there is now 1 tested device, and batteryPercentages becomes [0,1,1].
// At device 2, batteryPercentages[2] > 0, so there are now 2 tested devices, and batteryPercentages stays the same.
// So, the answer is 2.
fmt.Println(countTestedDevices([]int{0,1,2})) // 2
fmt.Println(countTestedDevices1([]int{1,1,2,1,3})) // 3
fmt.Println(countTestedDevices1([]int{0,1,2})) // 2
}