-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
class Solution { | ||
int dfs(int i, vector<int>& nums, vector<int>& memo) { | ||
if (i == 0) { // finish | ||
return 1; | ||
} | ||
int& res = memo[i]; // reference | ||
if (res != -1) { | ||
return res; | ||
} | ||
res = 0; | ||
|
||
for (int x : nums) { | ||
if (x <= i) { | ||
res += dfs(i - x, nums, memo); | ||
} | ||
} | ||
return res; | ||
} | ||
|
||
public: | ||
int combinationSum4(vector<int>& nums, int target) { | ||
vector<int> memo(target + 1, -1); // -1 not computed before | ||
|
||
return dfs(target, nums, memo); | ||
} | ||
|
||
int combinationSum4Op(vector<int>& nums, int target) { | ||
// use unsigned to make overflow still returns normally | ||
// for the overflow dsata, | ||
vector<unsigned> f(target + 1); | ||
f[0] = 1; | ||
for (int i = 1; i <= target; i++) { | ||
for (int x : nums) { | ||
if (x <= i) { | ||
f[i] += f[i - x]; | ||
} | ||
} | ||
} | ||
return f[target]; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
int maxSatisfied(vector<int>& customers, vector<int>& grumpy, int minutes) { | ||
int s[2]{}, max_s1 = 0; | ||
for (int i = 0; i < customers.size(); i++) { | ||
s[grumpy[i]] += customers[i]; | ||
if (i < minutes - 1) { // windows size less than minutes | ||
continue; | ||
} | ||
max_s1 = max(max_s1, s[1]); | ||
// leftmost goes out of window | ||
s[1] -= grumpy[i - minutes + 1] ? customers[i - minutes + 1] : 0; | ||
} | ||
return s[0] + max_s1; | ||
} | ||
|
||
int maxSatisfied1(vector<int>& customers, vector<int>& grumpy, int minutes) { | ||
int total = 0; | ||
int n = customers.size(); | ||
for (int i = 0; i < n; i++) { | ||
if (grumpy[i] == 0) { | ||
total += customers[i]; | ||
} | ||
} | ||
int increase = 0; | ||
for (int i = 0; i < minutes; i++) { | ||
increase += customers[i] * grumpy[i]; | ||
} | ||
int maxIncrease = increase; | ||
for (int i = minutes; i < n; i++) { | ||
increase = increase - customers[i - minutes] * grumpy[i - minutes] + | ||
customers[i] * grumpy[i]; | ||
maxIncrease = max(maxIncrease, increase); | ||
} | ||
return total + maxIncrease; | ||
} | ||
}; |