11 2024 档案
-
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 阅读全文