Skip to content

Commit fc508de

Browse files
committed
v2: RE accumulate溢出
Line 132: Char 39: runtime error: signed integer overflow: 2147434857 + 90954 cannot be represented in type 'int' (stl_numeric.h) SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/stl_numeric.h:141:39
1 parent 3db2700 commit fc508de

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

Codes/2171-removing-minimum-number-of-magic-beans.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @Author: LetMeFly
33
* @Date: 2024-01-18 19:20:51
44
* @LastEditors: LetMeFly
5-
* @LastEditTime: 2024-01-18 19:28:38
5+
* @LastEditTime: 2024-01-18 19:30:51
66
*/
77
#ifdef _WIN32
88
#include "_[1,2]toVector.h"
@@ -11,12 +11,11 @@
1111
class Solution {
1212
public:
1313
long long minimumRemoval(vector<int>& beans) {
14-
long long left = 0, right = accumulate(beans.begin(), beans.end(), 0);
15-
long long ans = right;
14+
sort(beans.begin(), beans.end());
15+
long long all = accumulate(beans.begin(), beans.end(), 0);
16+
long long ans = all;
1617
for (int i = 0; i < beans.size(); i++) {
17-
left += beans[i];
18-
right -= beans[i];
19-
ans = min(ans, left + right - beans[i] * ((long long)beans.size() - i - 1));
18+
ans = min(ans, all - ((long long)beans.size() - i) * beans[i]);
2019
}
2120
return ans;
2221
}

0 commit comments

Comments
 (0)