随笔分类 - ALG Records
摘要:135. 分发糖果 法一:记忆化搜索,找最长下降路径 class Solution { public: int dp(int x, vector<int>&ratings, vector<int>& f){ if(~f[x])return f[x]; int a = x - 1, b = x + 1
阅读全文
摘要:524. 通过删除字母匹配到字典里最长单词 判断 \(b\) 是否是 \(a\) 的子序列 class Solution { public: bool isP(string& a, string& b){ int n = a.size(), m = b.size(), i = 0, j = 0; w
阅读全文
摘要:447. 回旋镖的数量 计数问题,枚举i,统计与i坐标不同距离的坐标个数(哈希表保存),然后排列组合求结果。 假设有 \(m\) 个点到 $points[i]$距离相同,我们需要从这 $ m$个点中选择2个点作为另外两个点,由于考虑顺序,结果为 \(A_{m}^{2} = m *(m-1)\) 据此
阅读全文
摘要:1894. 找到需要补充粉笔的学生编号 法1:遍历,时间复杂度$O(n)$ 先走一轮,同时统计一轮共需多少粉笔数,并且如果发现粉笔数不够直接返回答案。 然后将剩余$k$对一轮总和取$mod$,最后再走一轮,如果发现不够返回答案。 class Solution { public: int chalkR
阅读全文
摘要:68. 文本左右对齐 贪心 + 模拟 将单词存入数组后调整单词之间的距离。 class Solution { public: string adjust1(vector<string>& t, int maxWidth){//非最后一行调整 string res = ""; if(t.size()
阅读全文
摘要:470. 用 Rand7() 实现 Rand10() 关键在于证明“等概率” ① rand10 \(\rightarrow\) rand7 基本想法是当随机到$1\sim7$的数时返回,否则舍弃,继续随机(拒绝采样)。下面证明返回的$1\sim7$都是等概率的。 对于$1\sim7$的任意一个数,则
阅读全文
摘要:1893. 检查是否区域内所有整数都被覆盖 法1:时间复杂度$O(n^2)$ 先暴力标记每个位置是否被覆盖,再检查目标区间 const int N = 55; bool st[N]; class Solution { public: bool isCovered(vector<vector<int>
阅读全文
摘要:881. 救生艇 贪心,时间复杂度:\(O(nlogn)\) 先排序,然后每次尝试将当前质量最大的人和质量最小的人放一块。 class Solution { public: int numRescueBoats(vector<int>& people, int limit) { int res =
阅读全文
摘要:789. 逃脱阻碍者 关键:算曼哈顿距离来判断 解析:简化一下 让所有的鬼都去终点等着人,如果鬼先到终点就不行,人先到终点就可。 可以证明出**:如果一个阻碍者能够抓到玩家,必然不会比玩家更晚到达终点**。 为了方便,我们设玩家起点、阻碍者起点、终点分别为 \(s\)、\(e\) 和 \(t\),计
阅读全文
摘要:1583. 统计不开心的朋友 不开心的朋友算我一个 按题意做就好了 class Solution { public: int unhappyFriends(int n, vector<vector<int>>& preferences, vector<vector<int>>& pairs) { v
阅读全文
摘要:198. 打家劫舍 法1:状态定义:f[i] 表示抢劫前i个 可以获得最高金额 class Solution { public: int rob(vector<int>& nums) { int n = nums.size(); if(n == 0)return 0; if(n == 1)retur
阅读全文
摘要:446. 等差数列划分 II - 子序列 思路:类似最长上升子序列,注意开long long 就好。 体会:dp开一维不够表示的时候多开一维 using ll = long long; class Solution { public: int numberOfArithmeticSlices(vec
阅读全文
摘要:918. 环形子数组的最大和 题意为求环状数组最大子序列和,长度不超过限制n。 对于非环状数组情况我们很容易解决,那么处理环状情况一般只需要将数组copy一份,“破环成链”。 对处理后的数组求其前缀和数组s,题意转为求: \[ 对\space \space \forall i,\space j \s
阅读全文
摘要:802. 找到最终的安全状态 出度为0的点是安全的,可以到达安全点的点也是安全的,因此反向建图拓扑排序即可。 const int N = 1e4 + 10; const int M = 4e4 + 10; int d[N],q[N],hh,tt = -1; int h[N],e[M],ne[M],i
阅读全文
摘要:611. 有效三角形的个数 法1:二分,时间复杂度:\(O(n^2logn)\) 设 a < b < c, 要使a,b,c构成有效三角形需满足 c < a + b, b - a < c, c - a < b, c - b < a 又有:c > b > b - a, c < a + b => c -
阅读全文
摘要:981. 基于时间的键值存储 二分 class TimeMap { public: unordered_map<string,vector<pair<int,string>>>hash; /** Initialize your data structure here. */ TimeMap() {}
阅读全文
摘要:229. 求众数 II 法1:哈希暴力计数 时间复杂度 \(O(n)\) 空间复杂度 \(O(n)\) class Solution { public: vector<int> majorityElement(vector<int>& nums) { unordered_map<int,int>cn
阅读全文
摘要:169. 多数元素 法1:(投票算法) //投票算法 class Solution { public: int majorityElement(vector<int>& nums) { int r, c = 0; for (auto x: nums) if (!c) r = x, c = 1; el
阅读全文
摘要:LCP 07. 传递信息 法1:dfs 若步数cnt超过了 k 且没有到达 终点 则剪枝,若步数为k且到达终点则方案+1. const int M = 110,N = 12; int h[N],ne[M],e[M],idx,ans; class Solution { public: void add
阅读全文
摘要:1915. 最美子字符串的数目 解析:为了解决题目的问题,我们需要知道某一区间某个字符出现的次数,这里利用前缀和解决。 s[i][j] 表示前i个字符中,字符j + 'a' 出现的次数。 对于s[i] 的10个分量,如果是偶数用0表示,如果是奇数则用1表示,最后压成一个二进制数 state。
阅读全文
浙公网安备 33010602011771号