随笔分类 - 解题报告
-
Leetcode 3261. 统计满足 K 约束的子字符串数量 II
摘要:1 class Solution { 2 public: 3 vector<long long> countKConstraintSubstrings(string s, int k, vector<vector<int>>& queries) { 4 int n = s.size(); 5 vec 阅读全文
-
Leetcode 3258. 统计满足 K 约束的子字符串数量 I
摘要:数据范围小,直接暴力枚举所有子字符串,统计每个子字符串的0和1的数量就行了。 1 class Solution { 2 public: 3 int countKConstraintSubstrings(string s, int k) { 4 int res=0; 5 for(int i=0;i<s 阅读全文
-
Leetcode 1547. 切棍子的最小成本
摘要:1 class Solution { 2 public: 3 int minCost(int n, vector<int>& cuts) { 4 int m = cuts.size(); 5 sort(cuts.begin(), cuts.end()); 6 cuts.insert(cuts.beg 阅读全文
-
Leetcode 540. 有序数组中的单一元素
摘要:要求O(logn)的复杂度基本就是二分。 分析序列1,1,2,2,3,4,4,5,5 (0,1),(2,3),(5,6),(7,8)各自成对。 0,1 10,11 101,110 111,1000 可以观察出目标的左边可用^异或找出成对的,右边则不行,所以可以据此来二分。 1 class Solut 阅读全文
-
Leetcode 3242. 设计相邻元素求和服务
摘要:模拟题,需要找到之后需要判定一下边界,别越界了。 1 class NeighborSum { 2 private: 3 int n; 4 vector<vector<int>> grid; 5 vector<vector<int>> adj={{-1,0},{0,1},{1,0},{0,-1}}; 阅读全文
-
Leetcode 3235. 判断矩形的两个角落是否可达
摘要:1 class Solution { 2 public: 3 bool canReachCorner(int xCorner, int yCorner, vector<vector<int>>& circles) { 4 vector<bool> visited(circles.size(), fa 阅读全文
-
Leetcode 3255. 长度为 K 的子数组的能量值 II
摘要:遍历nums数组,记录当前已有多少按1递增的元素。 1 class Solution { 2 public: 3 vector<int> resultsArray(vector<int>& nums, int k) { 4 int cnt=0; 5 int n=nums.size(); 6 vect 阅读全文
-
Leetcode 3254. 长度为 K 的子数组的能量值 I
摘要:1 class Solution { 2 public: 3 vector<int> resultsArray(vector<int>& nums, int k) { 4 int n = nums.size(); 5 int cnt = 0; 6 vector<int> ans(n - k + 1, 阅读全文
-
Leetcode 3222. 求出硬币游戏的赢家
摘要:模拟题。 1 class Solution { 2 public: 3 string losingPlayer(int x, int y) { 4 bool flag=false; 5 while(x>=1&&y>=4){ 6 flag=!flag; 7 x-=1,y-=4; 8 } 9 if(fl 阅读全文
-
Leetcode 633. 平方数之和
摘要:使用sqrt函数模拟。 1 typedef long long LL; 2 class Solution { 3 public: 4 bool judgeSquareSum(int c) { 5 for(LL i=0;i*i<=c;i++){ 6 LL a_2=i*i; 7 LL b_2=c-a_2 阅读全文
-
Leetcode 638. 大礼包
摘要:1 class Solution { 2 public: 3 map<vector<int>, int> memo; 4 5 int shoppingOffers(vector<int>& price, vector<vector<int>>& special, vector<int>& needs 阅读全文
-
Leetcode 3226. 使两个整数相等的位更改次数
摘要:模拟题,但是要注意按位与操作和比较运算符的优先级,比较运算符优先级更高,所以t1,t2这样写,不然就得加括号。 1 class Solution { 2 public: 3 int minChanges(int n, int k) { 4 int res=0; 5 while(n&&k){ 6 in 阅读全文
-
Leetcode 3259. 超级饮料的最大强化能量
摘要:动态规划。 f[i][0/1]表示前i个且最后选A或B的方案的集合。 所以f[i][0]=max(f[i-1][0],f[i-2][1])+A[i]。f[i][1]同理。 1 typedef long long LL; 2 const int N = 1e5+10; 3 LL f[N][2]; 4 阅读全文
-
Leetcode 3165. 不包含相邻元素的子序列的最大和
摘要:1 const int mod = 1e9+7; 2 struct SegNode { 3 SegNode() { 4 v00 = v01 = v10 = v11 = 0; 5 } 6 void set(long long v) { 7 v00 = v01 = v10 = 0; 8 v11 = ma 阅读全文
-
Leetcode 3216. 交换后字典序最小的字符串
摘要:因为字符串长度只有100,所以直接模拟就行了。字符串比较不想写的话,可以用C的strcmp 1 class Solution { 2 public: 3 string swap(string& s,int i,int j){ 4 string res=""; 5 for(int k=0;k<i;k+ 阅读全文
-
LeetCode 3211. 生成不含相邻零的二进制字符串
摘要:直接dfs暴搜所有串,2^18=1024*256,时间上是允许的。然后判断串是否合法。 1 const int N = 19; 2 bool path[N]; 3 class Solution { 4 public: 5 bool check(int n){ 6 for(int i=0;i<n-1; 阅读全文
-
Leecode 685. 冗余连接 II
摘要:分类讨论:两种情况,一是有节点有两个父节点,二是头尾相连 1 struct UnionFind { 2 vector <int> ancestor; 3 4 UnionFind(int n) { 5 ancestor.resize(n); 6 for (int i = 0; i < n; ++i) 阅读全文
-
684. 冗余连接
摘要:题目给定一个树添加一条边后的图,需要找出添加的边。思路为遍历所有边,用并查集维护集合,当一条边的两个端点已经在同一集合时,说明即为最后添加的边。 1 const int N=1010; 2 int p[N]; 3 class Solution { 4 public: 5 int find(int x 阅读全文
-
LeetCode 3181. 执行操作可获得的最大总奖励 II
摘要:1 class Solution { 2 public: 3 int maxTotalReward(vector<int>& rewardValues) { 4 int m = ranges::max(rewardValues); 5 unordered_set<int> s; 6 for (int 阅读全文
-
Leetcode 3180. 执行操作可获得的最大总奖励 I
摘要:f(i,j)表示从前i个中选,奖励为j是否有方案,有则为1。f(i,j) |= f(i-1,j),表示不选第i个。若是要选第i个,则需要j-rewardValues[i] < rewardValues[i],表示选第i个时手上的价值小于第i个的价值。 1 const int N = 2010; 2 阅读全文