周日 | 周一 | 周二 | 周三 | 周四 | 周五 | 周六 |
---|---|---|---|---|---|---|
1 √ | 2 √ | 3 √ | 4 位运算 × | |||
5 √ | 6 思维 × | 7 栈 × | 8 √ | 9 √ | 10 √ | 11 √ |
12 √ | 13 √ | 14 贪心 × | 15 √ | 16 前缀和 枚举 × | 17 √ | 18 双指针 × |
19 √ | 20 数位DP × | 21 √ | 22 DP × | 23 √ | 24 字典树 × | 25 √ |
26 √ | 27 √ | 28 DP √ | 29 √ | 30 √ | 31 √ |
统计每个月的每日1题做题情况。统计规则为:
- 若正常解题且不为暴力做法,则日期后做标记 √
- 若正常解题且为暴力做法,则日期后做标记 √
- 若无法解题,则日期后做标记 ×
class Solution {
public:
vector<vector<int>> largestLocal(vector<vector<int>>& grid) {
vector<vector<int>> ans;
int n = grid.size();
int dy[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
for(int i = 1; i < n-1; i++){
vector<int> tmp;
for(int j = 1; j < n-1; j++){
int now_max = grid[i][j];
for(int k = 0; k < 8; k++) now_max = max(grid[i+dx[k]][j+dy[k]], now_max);
tmp.push_back(now_max);
}
ans.push_back(tmp);
}
return ans;
}
};
class Solution {
public:
string printBin(double num) {
string s ="0.";
double eps = 1e-6;
int cnt = 2;
while(num > eps && cnt++ <= 32){
num *= 2;
if(num >= 1) {
num -= 1;
s += "1";
}
else s += "0";
cnt++;
}
if(cnt >= 32)
return "ERROR";
else
return s;
}
};
class Solution {
public:
string toString(string s, int num){
stringstream ss;
ss << num;
string tmp;
ss >> tmp;
return s +"(" + tmp + ")";
}
vector<string> getFolderNames(vector<string>& names) {
set<string> st;
map<string, int> mp;
vector<string> ans;
for(int i = 0; i < names.size(); i++){
if(st.find(names[i]) == st.end()){
ans.push_back(names[i]);
st.insert(names[i]);
}
else {
int cnt = mp[names[i]];
while(++cnt){
string tmp = toString(names[i], cnt);
if(st.find(tmp) == st.end()){
ans.push_back(tmp);
st.insert(tmp);
mp[names[i]] = cnt;
break;
}
}
}
}
return ans;
}
};
class Solution {
public:
int countTriplets(vector<int>& nums) {
int n = nums.size();
int x[1 << 16] = {0};
int sum = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++)
++x[nums[i] & nums[j]];
}
for(int i = 0; i < n; i++){
for(int j = 0; j < (1<<16); j++){
if((j & nums[i]) == 0) sum += x[j];
}
}
return sum;
}
};
class Solution {
public:
int minOperationsMaxProfit(vector<int>& customers, int boardingCost, int runningCost) {
int max_value = -1000;
int sum = 0;
int index = 0;
int n = customers.size();
for(int i = 0; i < n; i++){
if(customers[i] >= 4){
sum += 4*boardingCost - runningCost;
if(i != n-1) customers[i+1] += customers[i] - 4;
}
else sum += customers[i]*boardingCost - runningCost;
if(sum > max_value){
max_value = sum;
index = i;
}
}
int tmp = customers[n-1];
int tmp_index = n;
while(tmp){
if(tmp >= 4){
sum += 4*boardingCost - runningCost;
tmp -= 4;
}
else {
sum += tmp*boardingCost - runningCost;
tmp = 0;
}
if(sum > max_value){
max_value = sum;
index = tmp_index;
}
tmp_index++;
}
if(max_value <= 0)
return -1;
else
return index;
}
};
class Solution {
public:
int minimumDeletions(string s) {
int n = s.length();
int left_b = 0;
int right_a = 0;
for(int i = 0; i < n; i++){
if(s[i] == 'a') ++right_a;
}
int res = right_a;
for(int i = 0; i < n; i++){
if(s[i] == 'a') --right_a;
else ++left_b;
res = min(res, left_b+right_a);
}
return res;
}
};
class Solution {
public:
void operate(vector<char> &op, vector<set<string>> &stk){
int left = stk.size() - 2;
int right = stk.size() - 1;
if(op.back() == '+') {
stk[left].merge(stk[right]);
}
else {
set<string> tmp;
for(auto &l: stk[left])
for(auto &r: stk[right]){
tmp.insert(l+r);
}
stk[left] = move(tmp);
}
op.pop_back();
stk.pop_back();
}
vector<string> braceExpansionII(string expression) {
vector<char> op;
vector<set<string>> stk;
for(int i = 0; i < expression.size(); i++){
if(expression[i] == ','){
while(op.size() && op.back() == '*') operate(op, stk);
op.push_back('+');
}
else if(expression[i] == '{'){
if(i > 0 && (isalpha(expression[i-1]) || expression[i-1] == '}')) op.push_back('*');
op.push_back(expression[i]);
}
else if(expression[i] == '}'){
while(op.size()) {
operate(op, stk);
if(op.back() == '{'){
op.pop_back();
break;
}
}
}
else {
if(i > 0 && (isalpha(expression[i-1]) || expression[i-1] == '}')) op.push_back('*');
stk.push_back({string(1, expression[i])});
}
}
while(op.size()) {
operate(op, stk);
}
return {stk.back().begin(), stk.back().end()};
}
};
class Solution {
public:
int maxValue(vector<vector<int>>& grid) {
const int m = grid.size();
const int n = grid[0].size();
int dp[m][n];
dp[0][0] = grid[0][0];
for(int i = 1; i < n; i++) dp[0][i] = grid[0][i] + dp[0][i-1];
for(int j = 1; j < m; j++) dp[j][0] = grid[j][0] + dp[j-1][0];
for(int i = 1; i < m; i++)
for(int j = 1; j < n; j++)
dp[i][j] = grid[i][j] + max(dp[i-1][j], dp[i][j-1]);
return dp[m-1][n-1];
}
};
class Solution {
public:
int minimumRecolors(string blocks, int k) {
int left = 0;
int right = 0;
int cnt = 0;
while(right < k) {
cnt += (blocks[right++]=='W')?1:0;
}
int ans = cnt;
while(right < blocks.size()) {
cnt += (blocks[right++]=='W')?1:0;
cnt -= (blocks[left++]=='W')?1:0;
ans = min(ans, cnt);
}
return ans;
}
};
class Solution {
public:
int minSubarray(vector<int>& nums, int p) {
int res = 0;
for(int i = 0; i < nums.size(); i++)
res = (res + nums[i]) % p;
if(res == 0) return 0;
map<int, int> mp; // = <余数, 下标>
mp[0] = -1; //下标为-1代表前缀和为空,空即为0
int sum = 0;
int ans = nums.size();
for(int i = 0; i < nums.size(); i++){
sum = (sum + nums[i]) % p;
mp[sum] = i;
if(mp.count((sum - res + p) % p) > 0){
ans = min(ans, i - mp[(sum - res + p) % p]);
}
}
return ans == nums.size()? -1: ans;
}
};
class Solution {
public:
vector<string> findLongestSubarray(vector<string>& array) {
int n = array.size();
int presum[n+1];
int max_line = 0;
int max_left = 100000;
int max_right = -1;
map<int, int> mp;
set<int> st;
st.insert(0);
mp[0] = 0;
presum[0] = 0;
for(int i = 0; i < n; i++){
if(isalpha(array[i][0])) presum[i+1] = presum[i] - 1;
else presum[i+1] = presum[i] + 1;
if(!st.count(presum[i+1])) {
mp[presum[i+1]] = i;
st.insert(presum[i+1]);
}
int left = mp[presum[i+1]];
if(!presum[i+1] && i+1 > max_line) {
max_left = 0;
max_right = i;
max_line = i+1;
}
else if(i - left > max_line) {
max_left = left+1;
max_right = i;
max_line = i - left;
}
}
vector<string> ans;
if(max_line) {
auto left = array.begin() + max_left;
auto right = array.begin() + max_right + 1;
ans.assign(left, right);
}
return ans;
}
};
class Solution {
private:
int g[20][20];
public:
void dfs(int vis[], int point, int n){
vis[point] = 0;
for(int i = 1; i <= n; i++){
if(vis[i] && g[point][i] != n+1)
dfs(vis, i, n);
}
}
vector<int> countSubgraphsForEachDiameter(int n, vector<vector<int>>& edges) {
int vis[n+5];
int mark[n+5];
vector<int> ans(n-1);
for(int i = 0; i < n-1; i++) ans[i] = 0;
for(int i = 1; i < (1 << n); i++){
int start;
for(int j = 0; j < n ; j++) {
vis[j+1] = (i >> j) % 2;
mark[j+1] = vis[j+1];
if(vis[j+1]) start = j+1;
}
for(int k = 1; k <= n; k++)
for(int j = 1; j <= n; j++) {
if(k != j) g[k][j] = n+1;
else g[k][j] = 0;
}
for(int k = 0; k < edges.size(); k++) {
if(vis[edges[k][0]] && vis[edges[k][1]])
g[edges[k][0]][edges[k][1]] = g[edges[k][1]][edges[k][0]] = 1;
}
dfs(mark, start, n);
int v = 0;
for(int i = 1; i <= n; i++){
if(mark[i]){
v = 1;
break;
}
}
if(v) continue;
for(int x = 1; x <= n; x++){
for(int y = 1; y <= n; y++)
for(int z = 1; z <= n; z++)
g[y][z] = min(g[y][z], g[y][x] + g[x][z]);
}
int max_edge = -1;
int flag = 1;
for(int y = 1; y <= n; y++)
for(int z = 1; z <= n; z++){
if(vis[y] && vis[z] && y != z) {
if(g[y][z] == n+1){
flag = 0;
break;
}
else max_edge = max(max_edge, g[y][z]);
}
}
if(flag && max_edge != -1)
ans[max_edge-1]++;
}
return ans;
}
};
class Solution {
public:
int minNumberOfHours(int initialEnergy, int initialExperience, vector<int>& energy, vector<int>& experience) {
int n = energy.size();
int sum_energy = 1;
int sum_experience = 0;
for(int i = 0; i < n; i++){
sum_energy += energy[i];
if(experience[i] >= initialExperience){
sum_experience += experience[i] - initialExperience + 1;
initialExperience += experience[i] - initialExperience + 1;;
}
initialExperience += experience[i];
}
int tmp;
if(sum_energy - initialEnergy > 0) tmp = sum_energy - initialEnergy;
else tmp = 0;
return (tmp + sum_experience > 0)?tmp + sum_experience:0;
}
};
class Solution {
public:
vector<vector<int>> restoreMatrix(vector<int>& rowSum, vector<int>& colSum) {
vector<vector<int>> ans;
int n = rowSum.size();
int m = colSum.size();
for(int i = 0; i < n; i++){
vector<int> tmp;
for(int j = 0; j < m; j++){
int val = min(rowSum[i], colSum[j]);
tmp.push_back(val);
rowSum[i] -= val;
colSum[j] -= val;
}
ans.push_back(tmp);
}
return ans;
}
};
class Solution {
public:
int maximalNetworkRank(int n, vector<vector<int>>& roads) {
int g[n][n];
memset(g, 0, sizeof(g));
for(int i = 0; i < roads.size(); i++)
g[roads[i][0]][roads[i][1]] = g[roads[i][1]][roads[i][0]] = 1;
int ans = 0;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++) {
if(i != j){
int tmp_sum = 0;
for(int k = 0; k < n; k++) {
tmp_sum += g[i][k] + g[j][k];
}
// cout << " i = " << i << " j = " << j << " tmp_sum = " << tmp_sum << endl;
if(g[i][j]) tmp_sum -= 1;
ans = max(ans, tmp_sum);
}
}
return ans;
}
};
class Solution {
public:
inline int trans(int num){
if(num == 0) return 0;
else return (num > 0)? 1:-1;
}
int countSubarrays(vector<int>& nums, int k) {
int ans_index = 0;
int n = nums.size();
for(int i = 0; i < n; i++){
if(nums[i] == k) {
ans_index = i;
break;
}
}
int pre_sum = 0;
int ans = 0;
map<int, int> mp;
mp[0] = 1;
for(int i = 0; i < n; i++){
pre_sum += trans(nums[i] - k);
if(i < ans_index)
mp[pre_sum]++;
else {
int pre0 = mp[pre_sum];
int pre1 = mp[pre_sum-1];
ans += pre0 + pre1;
}
}
return ans;
}
};
class Solution {
public:
vector<int> answerQueries(vector<int>& nums, vector<int>& queries) {
int n = nums.size();
int m = queries.size();
vector<int> pre_sum(n+1);
pre_sum[0] = 0;
sort(nums.begin(), nums.end());
for(int i = 0; i < n; i++)
pre_sum[i+1] = pre_sum[i] + nums[i];
vector<int> ans(m);
for(int i = 0; i < m; i++){
ans[i] = upper_bound(pre_sum.begin(), pre_sum.end(), queries[i]) - pre_sum.begin() -1;
}
return ans;
}
};
class Solution {
public:
bool checkSplitPoint(string &a, int left, int right) {
while(left < right && a[left] == a[right]){
left++;
right--;
}
return left >= right;
}
bool TwoPointCheck(string &a, string &b) {
int n = a.length();
int left = 0;
int right = n-1;
while(left < right && a[left] == b[right]){
left++;
right--;
}
if(left >= right) return true;
return checkSplitPoint(a, left, right) || checkSplitPoint(b, left, right);
}
bool checkPalindromeFormation(string a, string b) {
return TwoPointCheck(a, b) || TwoPointCheck(b, a);
}
};
class Solution {
public:
string findLexSmallestString(string s, int a, int b) {
string ans = s;
int n = s.length();
queue<string> q;
set<string> st;
st.insert(s);
q.push(s);
int cnt = 0;
while(!q.empty()){
string tmp_aop = q.front();
string tmp_bop = tmp_aop;
q.pop();
for(int l = 1; l < n; l+=2){
tmp_aop[l] = ((tmp_aop[l] - '0') + a) % 10 + '0';
}
for(int k = 0; k < n; k++){
tmp_bop[k] = tmp_aop[(n-b+k)%n];
}
if(!st.count(tmp_aop)){
q.push(tmp_aop);
st.insert(tmp_aop);
}
if(!st.count(tmp_bop)){
q.push(tmp_bop);
st.insert(tmp_bop);
}
ans = min(ans, tmp_bop);
ans = min(ans, tmp_aop);
}
return ans;
}
};
class Solution {
private:
int dp[11][1<<11];
public:
int dfs(int i, int mask, string s, bool same){
if(i == s.length()) // 取数完成,返回1
return 1;
if(!same && dp[i][mask]) // 当前所取数小于n, 并且之前已经生成相关子树,则直接返回
return dp[i][mask];
int up = same?(s[i] - '0'):9;
int res = 0;
for(int k = 0; k <= up; k++){
if(mask & (1 << k)) continue;
res += dfs(i+1, (!mask && !k)?0:mask|(1<<k), s, same && k == up);
}
if(!same)
dp[i][mask] = res;
return res;
}
int numDupDigitsAtMostN(int n) {
for(int i = 0; i < 11; i++)
for(int j = 0; j < (1<<11); j++) dp[i][j] = 0;
string s = to_string(n);
return n + 1 - dfs(0, 0, s, true);
}
};
class Solution {
public:
vector<double> convertTemperature(double celsius) {
vector<double> ans;
ans.push_back(celsius+273.15);
ans.push_back(celsius*1.80 + 32.00);
return ans;
}
};
class Solution {
public:
struct player{
int scores;
int ages;
};
private:
player p[1005];
public:
static bool cmp(player p1, player p2){
if(p1.ages != p2.ages)
return p1.ages < p2.ages;
else
return p1.scores < p2.scores;
}
int bestTeamScore(vector<int>& scores, vector<int>& ages) {
int n = scores.size();
int ans = 0;
for(int i = 0; i < n; i++){
p[i].scores = scores[i];
p[i].ages = ages[i];
}
sort(p, p+n, cmp);
int dp[n];
for(int i = 0; i < n; i++){
dp[i] = p[i].scores;
for(int j = 0; j < i; j++){
if(p[j].scores <= p[i].scores)
dp[i] = max(dp[i], dp[j] + p[i].scores);
}
}
for(int i = 0; i < n; i++) ans = max(ans, dp[i]);
return ans;
}
};
class Solution {
public:
vector<bool> checkArithmeticSubarrays(vector<int>& nums, vector<int>& l, vector<int>& r) {
vector<bool> ans;
for(int i = 0; i < l.size(); i++){
vector<int> tmp(nums.begin() + l[i], nums.begin() + r[i]+1);
sort(tmp.begin(), tmp.end());
int flag = 1;
int gap = tmp[1] - tmp[0];
for(int i = 2; i < tmp.size(); i++) {
if(tmp[i] - tmp[i-1] != gap) {
flag = 0; break;
}
}
if(flag) ans.push_back(true);
else ans.push_back(false);
}
return ans;
}
};
class Tire{
private:
vector<Tire *> children;
bool is_End;
public:
Tire():children(26), is_End(false){}
void insert(string &w){
Tire *p = this;
reverse(w.begin(), w.end());
for(char &c: w){
int idx = c - 'a';
if(!p->children[idx]){
p->children[idx] = new Tire();
}
p = p->children[idx];
}
p->is_End = true;
}
bool search(string &s){
Tire *p = this;
for(int i = s.length()-1, j = 0; i >= 0 && j < 201; i--, j++){
int idx = s[i] - 'a';
if(p->is_End){
return true;
}
if(!p->children[idx]){
return false;
}
p = p->children[idx];
}
return p->is_End;
}
};
class StreamChecker {
public:
Tire *t = new Tire();
string s;
public:
StreamChecker(vector<string>& words) {
for(string &w: words)
t->insert(w);
}
bool query(char letter) {
s += letter;
return t->search(s);
}
};
class Solution {
public:
int findLengthOfShortestSubarray(vector<int>& arr) {
int n = arr.size();
int i = 0, j = n - 1;
while(j > 0 && arr[j] >= arr[j-1]) j--;
if(j == 0) return 0;
int res = j;
for(; i < n; i++){
while(j < n && arr[j] < arr[i]) j++;
res = min(res, j-i-1);
if(i+1 < n && arr[i] > arr[i+1]) break;
}
return res;
}
};
class Solution {
public:
bool findSubarrays(vector<int>& nums) {
int windows = 2;
int n = nums.size();
map<int, int> mp;
for(int i = 0; i+1 < n; i++){
if(mp.count(nums[i]+nums[i+1]) > 0 && mp[nums[i]+nums[i+1]] != i)
return true;
if(!mp.count(nums[i]+nums[i+1]))
mp[nums[i]+nums[i+1]] = i;
}
return false;
}
};
class Solution {
public:
int countSubstrings(string s, string t) {
int windows = 0;
int n = s.length();
int m = t.length();
int ans = 0;
while(windows++ < n){
for(int i = 0; i + windows <= n; i++){
for(int j = 0; j + windows <= m; j++){
int diff = 0;
for(int k = 0; k < windows; k++){
if(diff > 1) break;
if(s[i+k] != t[j+k]) diff++;
}
if(diff == 1) ans++;
}
}
}
return ans;
}
};
class Solution {
public:
string shortestCommonSupersequence(string str1, string str2) {
int n = str1.length();
int m = str2.length();
int dp[m+1][n+1];
for(int i = 0; i <= n; i++) dp[0][i] = i;
for(int i = 0; i <= m; i++) dp[i][0] = i;
for(int i = 1; i <= m; i++){
for(int j = 1; j <= n; j++){
if(str1[j-1] == str2[i-1])
dp[i][j] = dp[i-1][j-1] + 1;
else
dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + 1;
}
}
int i = m, j = n;
string ans;
while(i > 0 && j > 0){
if(str1[j-1] == str2[i-1]){
ans += str2[i-1]; j--; i--;
}
else {
if(i > 0 && dp[i][j] == dp[i-1][j] + 1){
ans += str2[i-1]; i--;
}
else{
ans += str1[j-1]; j--;
}
}
}
while(i > 0){ans += str2[i-1];i--;}
while(j > 0){ans += str1[j-1];j--;}
reverse(ans.begin(), ans.end());
return ans;
}
};
class Solution {
public:
int countVowelStrings(int n) {
int dp[n][5];
for(int i = 0; i < n; i++)
for(int j = 0; j < 5; j++)
dp[i][j] = 0;
for(int i = 0; i < 5; i++) dp[0][i] = 1;
for(int i = 1; i < n; i++)
for(int j = 0; j < 5; j++)
for(int k = 4; k >= j; k--)
dp[i][j] += dp[i-1][k];
int sum = 0;
for(int i = 0; i < 5; i++) sum += dp[n-1][i];
return sum;
}
};
class Solution {
public:
struct point{
int x;
int y;
};
static bool cmp(point p1, point p2){
return p1.x < p2.x;
}
int maxWidthOfVerticalArea(vector<vector<int>>& points) {
point p[100005];
int n = points.size();
for(int i = 0; i < n; i++){
p[i].x = points[i][0];
p[i].y = points[i][1];
}
sort(p, p+n, cmp);
int ans = p[1].x - p[0].x;
for(int i = 1; i < n-1; i++)
ans = max(ans, p[i+1].x - p[i].x);
return ans;
}
};
class Solution {
public:
int arithmeticTriplets(vector<int>& nums, int diff) {
int sum = 0;
int n = nums.size();
for(int i = 0; i < n; i++){
for(int j = i+1; j < n; j++)
for(int k = j+1; k < n; k++)
if(nums[j] - nums[i] == diff && nums[k] - nums[j] == diff)
sum++;
}
return sum;
}
};