-
Notifications
You must be signed in to change notification settings - Fork 0
/
1799_Maximize_Score_After_N_Operations.cpp
60 lines (50 loc) · 1.44 KB
/
1799_Maximize_Score_After_N_Operations.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
#include<bits/stdc++.h>
using namespace std;
class Solution {
map<int, int> mp;
bool isBitSet(int& num, int bitInd)
{
if ((num & (1 << bitInd)) != 0)
return true;
return false;
}
void setBit(int& num, int bitIdx) {
num |= (1 << bitIdx);
}
int solve(vector<int>&nums, int visited, int operation)
{
if (mp.find(visited) != mp.end()) return mp[visited];
int ans = 0;
for (int i = 0; i < nums.size() - 1; i++)
{
if (isBitSet(visited, i)) continue;
for (int j = i + 1; j < nums.size(); j++)
{
if (isBitSet(visited, j)) continue;
int newVisited = visited;
setBit(newVisited, i);
setBit(newVisited, j);
int currScore = operation * __gcd(nums[i], nums[j]);
int nextMaxScore = solve(nums, newVisited, operation + 1);
int totalScore = currScore + nextMaxScore;
ans = max(ans, totalScore);
}
}
return mp[visited] = ans;
}
public:
int maxScore(vector<int>& nums)
{
int n = nums.size();
int visited = 0;
mp.clear();
int ans = solve(nums, visited, 1);
return ans;
}
};
int main() {
Solution s;
vector<int>nums = {1,2,3,4,5,6};
cout << s.maxScore(nums) << endl;
return 0;
}