File tree Expand file tree Collapse file tree 5 files changed +194
-12
lines changed
solution/2200-2299/2208.Minimum Operations to Halve Array Sum Expand file tree Collapse file tree 5 files changed +194
-12
lines changed Original file line number Diff line number Diff line change @@ -62,21 +62,73 @@ nums 的和减小了 31 - 14.5 = 16.5 ,减小的部分超过了初始数组和
62
62
<!-- 这里可写当前语言的特殊实现逻辑 -->
63
63
64
64
``` python
65
-
65
+ class Solution :
66
+ def halveArray (self , nums : List[int ]) -> int :
67
+ s = sum (nums) / 2
68
+ h = []
69
+ for v in nums:
70
+ heappush(h, - v)
71
+ ans = 0
72
+ while s > 0 :
73
+ t = - heappop(h) / 2
74
+ s -= t
75
+ heappush(h, - t)
76
+ ans += 1
77
+ return ans
66
78
```
67
79
68
80
### ** Java**
69
81
70
82
<!-- 这里可写当前语言的特殊实现逻辑 -->
71
83
72
84
``` java
73
-
85
+ class Solution {
86
+ public int halveArray (int [] nums ) {
87
+ long s = 0 ;
88
+ PriorityQueue<Double > q = new PriorityQueue<> (Collections . reverseOrder());
89
+ for (int v : nums) {
90
+ q. offer(v * 1.0 );
91
+ s += v;
92
+ }
93
+ double d = s / 2.0 ;
94
+ int ans = 0 ;
95
+ while (d > 0 ) {
96
+ double t = q. poll();
97
+ d -= t / 2.0 ;
98
+ q. offer(t / 2.0 );
99
+ ++ ans;
100
+ }
101
+ return ans;
102
+ }
103
+ }
74
104
```
75
105
76
- ### ** TypeScript**
77
-
78
- ``` ts
79
-
106
+ ### ** C++**
107
+
108
+ ``` cpp
109
+ class Solution {
110
+ public:
111
+ int halveArray(vector<int >& nums) {
112
+ priority_queue<double > q;
113
+ long long s = 0;
114
+ for (int& v : nums)
115
+ {
116
+ s += v;
117
+ q.push(v);
118
+ }
119
+ double d = s / 2.0;
120
+ int ans = 0;
121
+ while (d > 0)
122
+ {
123
+ double t = q.top() / 2;
124
+ q.pop();
125
+ d -= t;
126
+ q.push(t);
127
+ ++ans;
128
+ }
129
+ return ans;
130
+ }
131
+ };
80
132
```
81
133
82
134
### **Go**
@@ -103,6 +155,18 @@ type hp struct{ sort.IntSlice }
103
155
func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] }
104
156
func (hp) Push(interface{}) {}
105
157
func (hp) Pop() (_ interface{}) { return }
158
+ ```
159
+
160
+
161
+ ### ** TypeScript**
162
+
163
+ ``` ts
164
+
165
+ ```
166
+
167
+ ### ** ...**
168
+
169
+ ```
106
170
107
171
```
108
172
Original file line number Diff line number Diff line change @@ -56,19 +56,71 @@ It can be shown that we cannot reduce the sum by at least half in less than 3 op
56
56
### ** Python3**
57
57
58
58
``` python
59
-
59
+ class Solution :
60
+ def halveArray (self , nums : List[int ]) -> int :
61
+ s = sum (nums) / 2
62
+ h = []
63
+ for v in nums:
64
+ heappush(h, - v)
65
+ ans = 0
66
+ while s > 0 :
67
+ t = - heappop(h) / 2
68
+ s -= t
69
+ heappush(h, - t)
70
+ ans += 1
71
+ return ans
60
72
```
61
73
62
74
### ** Java**
63
75
64
76
``` java
65
-
77
+ class Solution {
78
+ public int halveArray (int [] nums ) {
79
+ long s = 0 ;
80
+ PriorityQueue<Double > q = new PriorityQueue<> (Collections . reverseOrder());
81
+ for (int v : nums) {
82
+ q. offer(v * 1.0 );
83
+ s += v;
84
+ }
85
+ double d = s / 2.0 ;
86
+ int ans = 0 ;
87
+ while (d > 0 ) {
88
+ double t = q. poll();
89
+ d -= t / 2.0 ;
90
+ q. offer(t / 2.0 );
91
+ ++ ans;
92
+ }
93
+ return ans;
94
+ }
95
+ }
66
96
```
67
97
68
- ### ** TypeScript**
69
-
70
- ``` ts
71
-
98
+ ### ** C++**
99
+
100
+ ``` cpp
101
+ class Solution {
102
+ public:
103
+ int halveArray(vector<int >& nums) {
104
+ priority_queue<double > q;
105
+ long long s = 0;
106
+ for (int& v : nums)
107
+ {
108
+ s += v;
109
+ q.push(v);
110
+ }
111
+ double d = s / 2.0;
112
+ int ans = 0;
113
+ while (d > 0)
114
+ {
115
+ double t = q.top() / 2;
116
+ q.pop();
117
+ d -= t;
118
+ q.push(t);
119
+ ++ans;
120
+ }
121
+ return ans;
122
+ }
123
+ };
72
124
```
73
125
74
126
### **Go**
@@ -95,6 +147,17 @@ type hp struct{ sort.IntSlice }
95
147
func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] }
96
148
func (hp) Push(interface{}) {}
97
149
func (hp) Pop() (_ interface{}) { return }
150
+ ```
151
+
152
+ ### ** TypeScript**
153
+
154
+ ``` ts
155
+
156
+ ```
157
+
158
+ ### ** ...**
159
+
160
+ ```
98
161
99
162
```
100
163
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int halveArray (vector<int >& nums) {
4
+ priority_queue<double > q;
5
+ long long s = 0 ;
6
+ for (int & v : nums)
7
+ {
8
+ s += v;
9
+ q.push (v);
10
+ }
11
+ double d = s / 2.0 ;
12
+ int ans = 0 ;
13
+ while (d > 0 )
14
+ {
15
+ double t = q.top () / 2 ;
16
+ q.pop ();
17
+ d -= t;
18
+ q.push (t);
19
+ ++ans;
20
+ }
21
+ return ans;
22
+ }
23
+ };
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int halveArray (int [] nums ) {
3
+ long s = 0 ;
4
+ PriorityQueue <Double > q = new PriorityQueue <>(Collections .reverseOrder ());
5
+ for (int v : nums ) {
6
+ q .offer (v * 1.0 );
7
+ s += v ;
8
+ }
9
+ double d = s / 2.0 ;
10
+ int ans = 0 ;
11
+ while (d > 0 ) {
12
+ double t = q .poll ();
13
+ d -= t / 2.0 ;
14
+ q .offer (t / 2.0 );
15
+ ++ans ;
16
+ }
17
+ return ans ;
18
+ }
19
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def halveArray (self , nums : List [int ]) -> int :
3
+ s = sum (nums ) / 2
4
+ h = []
5
+ for v in nums :
6
+ heappush (h , - v )
7
+ ans = 0
8
+ while s > 0 :
9
+ t = - heappop (h ) / 2
10
+ s -= t
11
+ heappush (h , - t )
12
+ ans += 1
13
+ return ans
You can’t perform that action at this time.
0 commit comments