本文最后更新于:2022年10月16日 晚上
2022/10/12 817.链表组件
当需要多次判断值是否位于数组中时,可以采用哈希集合保存数组中的值来降低时间复杂度
1 2 3 4 5 6 7 8
| unordered_set<int> hash; for(int num : numList) hash.emplace(num);
int num = 0; if(hash.count(num)) cout << "Find!" << endl;
|
2022/10/13 769.最多能完成排序的块
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| class Solution { public: int maxChunksToSorted(vector<int>& arr) { int maxNum = -1; int ans = 0; int n = arr.size(); for(int i = 0; i < n; i++) { maxNum = max(maxNum, arr[i]); if(maxNum == i) ans++; } return ans++; } };
|
2022/10/14 940.不同的子序列 II
2022/10/15 1441. 用栈操作构建数组
2022/10/16 886.可能的二分法
一道判定二分图的模板题
可以用染色法或扩展域并查集解决
2022/10/17 904. 水果成篮
双指针
注意使用哈希表存储这个窗口内的数以及出现的次数更优
数位 DP 模板题
视频讲解
简单模拟
找规律

动态规划
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
| const int N = 500010;
class Solution { public: int curMax[N];
int jobScheduling(vector<int>& startTime, vector<int>& endTime, vector<int>& profit) { int n = profit.size(); vector<int> idList; for(int i = 0; i < n; i++) idList.emplace_back(i);
sort(idList.begin(), idList.end(), [&](int i, int j) { return endTime[i] < endTime[j]; });
curMax[0] = profit[idList[0]]; for(int i = 1; i < n; i++) { int curId = idList[i]; curMax[i] = max(profit[curId], curMax[i - 1]); for(int j = i - 1; j >= 0; j--) { int preId = idList[j]; if(startTime[curId] >= endTime[preId]) { curMax[i] = max(curMax[i], curMax[j] + profit[curId]); break; } } }
return curMax[n - 1]; } };
|
双指针超时><
涉及到 子数组和 的问题,可能与前缀和有关
前缀和
可以用 两个前缀和的差表示子数组的和
单调队列
两张图秒懂单调队列 - 和至少为 K 的最短子数组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| class Solution { public: int shortestSubarray(vector<int> &nums, int k) { int n = nums.size(), ans = n + 1; long s[n + 1]; s[0] = 0L; for (int i = 0; i < n; ++i) s[i + 1] = s[i] + nums[i]; deque<int> q; for (int i = 0; i <= n; ++i) { long cur_s = s[i]; while (!q.empty() && cur_s - s[q.front()] >= k) { ans = min(ans, i - q.front()); q.pop_front(); } while (!q.empty() && s[q.back()] >= cur_s) q.pop_back(); q.push_back(i); } return ans > n ? -1 : ans; } };
|