Skip to content

Commit

Permalink
update: Apr23 sliding windows
Browse files Browse the repository at this point in the history
  • Loading branch information
aucker committed Apr 23, 2024
1 parent 3f06d25 commit a6dffc2
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
44 changes: 44 additions & 0 deletions daily/Apr22.cpp
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];
}
};
40 changes: 40 additions & 0 deletions daily/Apr23.cpp
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;
}
};

0 comments on commit a6dffc2

Please sign in to comment.