File tree Expand file tree Collapse file tree 6 files changed +168
-2
lines changed
solution/2200-2299/2214.Minimum Health to Beat Game Expand file tree Collapse file tree 6 files changed +168
-2
lines changed Original file line number Diff line number Diff line change @@ -75,15 +75,73 @@ Note that you did not use your armor ability.
75
75
<!-- 这里可写当前语言的特殊实现逻辑 -->
76
76
77
77
``` python
78
-
78
+ class Solution :
79
+ def minimumHealth (self , damage : List[int ], armor : int ) -> int :
80
+ return sum (damage) - min (max (damage), armor) + 1
79
81
```
80
82
81
83
### ** Java**
82
84
83
85
<!-- 这里可写当前语言的特殊实现逻辑 -->
84
86
85
87
``` java
88
+ class Solution {
89
+ public long minimumHealth (int [] damage , int armor ) {
90
+ long s = 0 ;
91
+ int mx = damage[0 ];
92
+ for (int v : damage) {
93
+ s += v;
94
+ mx = Math . max(mx, v);
95
+ }
96
+ return s - Math . min(mx, armor) + 1 ;
97
+ }
98
+ }
99
+ ```
100
+
101
+ ### ** C++**
102
+
103
+ ``` cpp
104
+ class Solution {
105
+ public:
106
+ long long minimumHealth(vector<int >& damage, int armor) {
107
+ long long s = 0;
108
+ int mx = damage[ 0] ;
109
+ for (int& v : damage)
110
+ {
111
+ s += v;
112
+ mx = max(mx, v);
113
+ }
114
+ return s - min(mx, armor) + 1;
115
+ }
116
+ };
117
+ ```
86
118
119
+ ### **Go**
120
+
121
+ ```go
122
+ func minimumHealth(damage []int, armor int) int64 {
123
+ var s int64
124
+ var mx int
125
+ for _, v := range damage {
126
+ s += int64(v)
127
+ mx = max(mx, v)
128
+ }
129
+ return s - int64(min(mx, armor)) + 1
130
+ }
131
+
132
+ func max(a, b int) int {
133
+ if a > b {
134
+ return a
135
+ }
136
+ return b
137
+ }
138
+
139
+ func min(a, b int) int {
140
+ if a < b {
141
+ return a
142
+ }
143
+ return b
144
+ }
87
145
```
88
146
89
147
### ** TypeScript**
Original file line number Diff line number Diff line change @@ -69,13 +69,71 @@ Note that you did not use your armor ability.
69
69
### ** Python3**
70
70
71
71
``` python
72
-
72
+ class Solution :
73
+ def minimumHealth (self , damage : List[int ], armor : int ) -> int :
74
+ return sum (damage) - min (max (damage), armor) + 1
73
75
```
74
76
75
77
### ** Java**
76
78
77
79
``` java
80
+ class Solution {
81
+ public long minimumHealth (int [] damage , int armor ) {
82
+ long s = 0 ;
83
+ int mx = damage[0 ];
84
+ for (int v : damage) {
85
+ s += v;
86
+ mx = Math . max(mx, v);
87
+ }
88
+ return s - Math . min(mx, armor) + 1 ;
89
+ }
90
+ }
91
+ ```
92
+
93
+ ### ** C++**
94
+
95
+ ``` cpp
96
+ class Solution {
97
+ public:
98
+ long long minimumHealth(vector<int >& damage, int armor) {
99
+ long long s = 0;
100
+ int mx = damage[ 0] ;
101
+ for (int& v : damage)
102
+ {
103
+ s += v;
104
+ mx = max(mx, v);
105
+ }
106
+ return s - min(mx, armor) + 1;
107
+ }
108
+ };
109
+ ```
78
110
111
+ ### **Go**
112
+
113
+ ```go
114
+ func minimumHealth(damage []int, armor int) int64 {
115
+ var s int64
116
+ var mx int
117
+ for _, v := range damage {
118
+ s += int64(v)
119
+ mx = max(mx, v)
120
+ }
121
+ return s - int64(min(mx, armor)) + 1
122
+ }
123
+
124
+ func max(a, b int) int {
125
+ if a > b {
126
+ return a
127
+ }
128
+ return b
129
+ }
130
+
131
+ func min(a, b int) int {
132
+ if a < b {
133
+ return a
134
+ }
135
+ return b
136
+ }
79
137
```
80
138
81
139
### ** TypeScript**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ long long minimumHealth (vector<int >& damage, int armor) {
4
+ long long s = 0 ;
5
+ int mx = damage[0 ];
6
+ for (int & v : damage)
7
+ {
8
+ s += v;
9
+ mx = max (mx, v);
10
+ }
11
+ return s - min (mx, armor) + 1 ;
12
+ }
13
+ };
Original file line number Diff line number Diff line change
1
+ func minimumHealth (damage []int , armor int ) int64 {
2
+ var s int64
3
+ var mx int
4
+ for _ , v := range damage {
5
+ s += int64 (v )
6
+ mx = max (mx , v )
7
+ }
8
+ return s - int64 (min (mx , armor )) + 1
9
+ }
10
+
11
+ func max (a , b int ) int {
12
+ if a > b {
13
+ return a
14
+ }
15
+ return b
16
+ }
17
+
18
+ func min (a , b int ) int {
19
+ if a < b {
20
+ return a
21
+ }
22
+ return b
23
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public long minimumHealth (int [] damage , int armor ) {
3
+ long s = 0 ;
4
+ int mx = damage [0 ];
5
+ for (int v : damage ) {
6
+ s += v ;
7
+ mx = Math .max (mx , v );
8
+ }
9
+ return s - Math .min (mx , armor ) + 1 ;
10
+ }
11
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def minimumHealth (self , damage : List [int ], armor : int ) -> int :
3
+ return sum (damage ) - min (max (damage ), armor ) + 1
You can’t perform that action at this time.
0 commit comments