-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2708.cpp
75 lines (62 loc) · 1.94 KB
/
2708.cpp
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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
class Solution {
public:
long long maxStrength(vector<int>& nums) {
int n = nums.size();
if(n == 1) return (ll)nums[0];
ll res = 1LL;
int mx = -10;
vector<int> neg;
for(auto val : nums){
mx = max(mx, val);
if(val < 0) neg.push_back(val);
else if(val > 0) res *= 1LL*val;
}
// cout << res << "\n";
// for(auto val : neg) cout << val;
// cout << "\n";
int m = neg.size();
if(mx == 0){
if(m == 0) return 0LL;
else{
if(m%2 == 0){
for(auto val : neg) res *= 1LL * val;
return res;
}
else{
if(m == 1) return 0LL;
sort(neg.begin(), neg.end());
for(int i = 0; i < neg.size()-1; ++i) res *= 1LL * neg[i];
return res;
}
}
}
else{
if(mx > 0){
if(m%2 == 0){
for(auto val : neg) res *= 1LL * val;
return res;
}
else{
sort(neg.begin(), neg.end());
for(int i = 0; i < neg.size()-1; ++i) res *= 1LL * neg[i];
return res;
}
}
else{
if(m%2 == 0){
for(auto val : neg) res *= 1LL * val;
return res;
}
else{
sort(neg.begin(), neg.end());
for(int i = 0; i < neg.size()-1; ++i) res *= 1LL * neg[i];
return res;
}
}
}
return -1LL;
}
};