-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3207-MaximumPointsAfterEnemyBattles.go
More file actions
80 lines (68 loc) · 4.05 KB
/
3207-MaximumPointsAfterEnemyBattles.go
File metadata and controls
80 lines (68 loc) · 4.05 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
package main
// 3207. Maximum Points After Enemy Battles
// You are given an integer array enemyEnergies denoting the energy values of various enemies.
// You are also given an integer currentEnergy denoting the amount of energy you have initially.
// You start with 0 points, and all the enemies are unmarked initially.
// You can perform either of the following operations zero or multiple times to gain points:
// 1. Choose an unmarked enemy, i, such that currentEnergy >= enemyEnergies[i]. By choosing this option:
// 1.1 You gain 1 point.
// 1.2 Your energy is reduced by the enemy's energy, i.e. currentEnergy = currentEnergy - enemyEnergies[i].
// 2. If you have at least 1 point, you can choose an unmarked enemy, i. By choosing this option:
// 2.1 Your energy increases by the enemy's energy, i.e. currentEnergy = currentEnergy + enemyEnergies[i].
// 2.2 The enemy i is marked.
// Return an integer denoting the maximum points you can get in the end by optimally performing operations.
// Example 1:
// Input: enemyEnergies = [3,2,2], currentEnergy = 2
// Output: 3
// Explanation:
// The following operations can be performed to get 3 points, which is the maximum:
// First operation on enemy 1: points increases by 1, and currentEnergy decreases by 2. So, points = 1, and currentEnergy = 0.
// Second operation on enemy 0: currentEnergy increases by 3, and enemy 0 is marked. So, points = 1, currentEnergy = 3, and marked enemies = [0].
// First operation on enemy 2: points increases by 1, and currentEnergy decreases by 2. So, points = 2, currentEnergy = 1, and marked enemies = [0].
// Second operation on enemy 2: currentEnergy increases by 2, and enemy 2 is marked. So, points = 2, currentEnergy = 3, and marked enemies = [0, 2].
// First operation on enemy 1: points increases by 1, and currentEnergy decreases by 2. So, points = 3, currentEnergy = 1, and marked enemies = [0, 2].
// Example 2:
// Input: enemyEnergies = [2], currentEnergy = 10
// Output: 5
// Explanation:
// Performing the first operation 5 times on enemy 0 results in the maximum number of points.
// Constraints:
// 1 <= enemyEnergies.length <= 10^5
// 1 <= enemyEnergies[i] <= 10^9
// 0 <= currentEnergy <= 10^9
import "fmt"
func maximumPoints(enemyEnergies []int, currentEnergy int) int64 {
totalEnergy, weakest := currentEnergy + enemyEnergies[0], enemyEnergies[0]
for i := 1; i < len(enemyEnergies); i++ {
totalEnergy += enemyEnergies[i]
if enemyEnergies[i] < weakest { // 找到最弱的
weakest = enemyEnergies[i]
}
}
if currentEnergy < weakest {
return 0
}
totalEnergy -= weakest
return int64(totalEnergy / weakest)
}
func main() {
// Example 1:
// Input: enemyEnergies = [3,2,2], currentEnergy = 2
// Output: 3
// Explanation:
// The following operations can be performed to get 3 points, which is the maximum:
// First operation on enemy 1: points increases by 1, and currentEnergy decreases by 2. So, points = 1, and currentEnergy = 0.
// Second operation on enemy 0: currentEnergy increases by 3, and enemy 0 is marked. So, points = 1, currentEnergy = 3, and marked enemies = [0].
// First operation on enemy 2: points increases by 1, and currentEnergy decreases by 2. So, points = 2, currentEnergy = 1, and marked enemies = [0].
// Second operation on enemy 2: currentEnergy increases by 2, and enemy 2 is marked. So, points = 2, currentEnergy = 3, and marked enemies = [0, 2].
// First operation on enemy 1: points increases by 1, and currentEnergy decreases by 2. So, points = 3, currentEnergy = 1, and marked enemies = [0, 2].
fmt.Println(maximumPoints([]int{3,2,2}, 2)) // 3
// Example 2:
// Input: enemyEnergies = [2], currentEnergy = 10
// Output: 5
// Explanation:
// Performing the first operation 5 times on enemy 0 results in the maximum number of points.
fmt.Println(maximumPoints([]int{2}, 10)) // 5
fmt.Println(maximumPoints([]int{1,2,3,4,5,6,7,8,9}, 2)) // 46
fmt.Println(maximumPoints([]int{9,8,7,6,5,4,3,2,1}, 2)) // 46
}