-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2708.cpp
More file actions
52 lines (50 loc) · 1.33 KB
/
2708.cpp
File metadata and controls
52 lines (50 loc) · 1.33 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
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
Solution()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
long long maxStrength(vector<int> &nums)
{
if (nums.size() == 1)
return nums[0];
sort(nums.begin(), nums.end());
long long positive_sum = 1, negative_sum = 1, last_negative = 1, last_positive = -1, count_negative = 0;
bool flag = false, hasZero = false;
for (int i = 0; i < nums.size(); i++)
{
if (nums[i] == 0)
{
hasZero = true;
}
if (nums[i] < 0)
{
negative_sum *= nums[i];
last_negative = nums[i];
count_negative++;
}
else if (nums[i] > 0)
{
flag = true;
positive_sum *= nums[i];
last_positive = nums[i];
}
}
if (count_negative == 0 && !flag)
return 0;
if (count_negative % 2 == 0)
return negative_sum * positive_sum;
if (count_negative >= 3)
return negative_sum / last_negative * positive_sum;
if (flag)
return positive_sum;
if (hasZero)
return 0;
return last_negative;
}
};