10 2024 档案
-
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 阅读全文
-
Leetcode每日一题:3175. 找到连续赢 K 场比赛的第一位玩家
摘要:题意为给定一个数组,数组头两数比大小,大的放队首,小的放队尾,找到能够连续赢K次的数的编号。 思路:如果一轮比较完了,没有赢K次的,那答案就是数组最大值。利用双指针,模拟一轮比较,计算每个数的胜利次数,注意,若起点不是0的话,意味着他和之前的数比较是胜出的,所以初始为1,否则初始为0; 1 clas 阅读全文