-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2383-MinimumHoursOfTrainingToWinACompetition.go
More file actions
108 lines (94 loc) · 5.17 KB
/
2383-MinimumHoursOfTrainingToWinACompetition.go
File metadata and controls
108 lines (94 loc) · 5.17 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
// 2383. Minimum Hours of Training to Win a Competition
// You are entering a competition, and are given two positive integers initialEnergy and initialExperience denoting your initial energy and initial experience respectively.
// You are also given two 0-indexed integer arrays energy and experience, both of length n.
// You will face n opponents in order. The energy and experience of the ith opponent is denoted by energy[i] and experience[i] respectively.
// When you face an opponent, you need to have both strictly greater experience and energy to defeat them and move to the next opponent if available.
// Defeating the ith opponent increases your experience by experience[i], but decreases your energy by energy[i].
// Before starting the competition, you can train for some number of hours.
// After each hour of training, you can either choose to increase your initial experience by one, or increase your initial energy by one.
// Return the minimum number of training hours required to defeat all n opponents.
// Example 1:
// Input: initialEnergy = 5, initialExperience = 3, energy = [1,4,3,2], experience = [2,6,3,1]
// Output: 8
// Explanation: You can increase your energy to 11 after 6 hours of training, and your experience to 5 after 2 hours of training.
// You face the opponents in the following order:
// - You have more energy and experience than the 0th opponent so you win.
// Your energy becomes 11 - 1 = 10, and your experience becomes 5 + 2 = 7.
// - You have more energy and experience than the 1st opponent so you win.
// Your energy becomes 10 - 4 = 6, and your experience becomes 7 + 6 = 13.
// - You have more energy and experience than the 2nd opponent so you win.
// Your energy becomes 6 - 3 = 3, and your experience becomes 13 + 3 = 16.
// - You have more energy and experience than the 3rd opponent so you win.
// Your energy becomes 3 - 2 = 1, and your experience becomes 16 + 1 = 17.
// You did a total of 6 + 2 = 8 hours of training before the competition, so we return 8.
// It can be proven that no smaller answer exists.
// Example 2:
// Input: initialEnergy = 2, initialExperience = 4, energy = [1], experience = [3]
// Output: 0
// Explanation: You do not need any additional energy or experience to win the competition, so we return 0.
// Constraints:
// n == energy.length == experience.length
// 1 <= n <= 100
// 1 <= initialEnergy, initialExperience, energy[i], experience[i] <= 100
import "fmt"
func minNumberOfHours(initialEnergy int, initialExperience int, energy []int, experience []int) int {
energyRequired := 0
for _, v := range energy {
energyRequired += v
}
energyToGain := energyRequired - initialEnergy + 1
if energyToGain < 0 {
energyToGain = 0
}
experienceToGain, curExperience := 0, initialExperience
for _, v := range experience {
deltaToWin := v - curExperience + 1
if deltaToWin > experienceToGain {
experienceToGain = deltaToWin
}
curExperience += v
}
return energyToGain + experienceToGain
}
func minNumberOfHours1(initialEnergy int, initialExperience int, energy []int, experience []int) int {
hours := 0
for i, v := range energy {
if initialEnergy <= v {
hours += v + 1 - initialEnergy
initialEnergy = v + 1
}
if initialExperience <= experience[i] {
hours += experience[i] + 1 - initialExperience
initialExperience = experience[i] + 1
}
initialEnergy -= energy[i]
initialExperience += experience[i]
}
return hours
}
func main() {
// Example 1:
// Input: initialEnergy = 5, initialExperience = 3, energy = [1,4,3,2], experience = [2,6,3,1]
// Output: 8
// Explanation: You can increase your energy to 11 after 6 hours of training, and your experience to 5 after 2 hours of training.
// You face the opponents in the following order:
// - You have more energy and experience than the 0th opponent so you win.
// Your energy becomes 11 - 1 = 10, and your experience becomes 5 + 2 = 7.
// - You have more energy and experience than the 1st opponent so you win.
// Your energy becomes 10 - 4 = 6, and your experience becomes 7 + 6 = 13.
// - You have more energy and experience than the 2nd opponent so you win.
// Your energy becomes 6 - 3 = 3, and your experience becomes 13 + 3 = 16.
// - You have more energy and experience than the 3rd opponent so you win.
// Your energy becomes 3 - 2 = 1, and your experience becomes 16 + 1 = 17.
// You did a total of 6 + 2 = 8 hours of training before the competition, so we return 8.
// It can be proven that no smaller answer exists.
fmt.Println(minNumberOfHours(5, 3, []int{1,4,3,2}, []int{2,6,3,1})) // 8
// Example 2:
// Input: initialEnergy = 2, initialExperience = 4, energy = [1], experience = [3]
// Output: 0
// Explanation: You do not need any additional energy or experience to win the competition, so we return 0.
fmt.Println(minNumberOfHours(2, 4, []int{1}, []int{3})) // 0
fmt.Println(minNumberOfHours1(5, 3, []int{1,4,3,2}, []int{2,6,3,1})) // 8
fmt.Println(minNumberOfHours1(2, 4, []int{1}, []int{3})) // 0
}