06 2021 档案

摘要:DP的算法就不说了,记一种打表找规律法(大草)。 通过打表,我们发现答案如下: 2 13-4 2 15-8 2 2 3 19-16 2 2 3 2 3 3 4 117-32 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5 133-64 2 2 3 2 3 3 4 2 3 3 4 3 4 阅读全文
posted @ 2021-06-23 23:53 wegret 阅读(56) 评论(0) 推荐(0)
摘要:(两年前学过,没什么印象了,重新复习一下orz。) 拓扑排序,即在一个有向图$G$中对节点进行线性排序,使图中任意一条有向边$<u,v>$,在该序列中都满足$u$在$v$前面。 举例: 该图的拓扑序是 6,1,2,3,4,5 。 又比如: 该图的拓扑序是 1,2,3,4,7,5,6,当然也可以是 1 阅读全文
posted @ 2021-06-20 22:46 wegret 阅读(64) 评论(0) 推荐(0)
摘要:拓扑排序。能得到拓扑序就是true,不能得到就是false。 vector<int> to[100005]; int in[100005]; int temp; class Solution { public: bool canFinish(int numCourses, vector<vector 阅读全文
posted @ 2021-06-20 22:21 wegret 阅读(66) 评论(0) 推荐(0)
摘要:直接找入度为$n-1$,出度为$0$的点就行了,时间复杂度是$O(n)$。 int in[1007],out[1007]; class Solution { public: int findJudge(int n, vector<vector<int>>& trust) { int t=trust. 阅读全文
posted @ 2021-06-19 22:31 wegret 阅读(57) 评论(0) 推荐(0)
摘要:脑筋急转弯。其实就是把当前节点伪装成下一个节点,取代next。 class Solution { public: void deleteNode(ListNode* node) { node->val=(node->next)->val; node->next=(node->next)->next; 阅读全文
posted @ 2021-06-19 18:09 wegret 阅读(31) 评论(0) 推荐(0)
摘要:已经连通的点可以视为集合,用并查集记录集合,搜一遍得到集合个数。设点数为$n$,边数为$s$,集合个数为$num$。那么有两种情况: 1)$s<n-1$,不能全部连通。 2)$s>=n-1$,最少操作数是$num-1$。(随便取冗边,反正能把集合都连起来就行。因为$s>=n-1$,所以是肯定能取到冗 阅读全文
posted @ 2021-06-19 16:55 wegret 阅读(78) 评论(0) 推荐(0)
摘要:“任意两点之间 有且仅有一条简单路径时,返回将所有点连接的最小总费用”,即求MST(最小生成树)。 发现数据规模不超过1000,所以可以考虑到$O(n^2)$的算法。 $O(n^2)$算出任意两点的距离。然后用prim算最小生成树,同样$O(n^2)$。(这张图是稠密图,点数$n$远小于边数$n^2 阅读全文
posted @ 2021-06-19 12:07 wegret 阅读(70) 评论(0) 推荐(0)
摘要:vector<int> ans; void dfs(TreeNode* now){ if (now==nullptr) return ; dfs(now->left); ans.push_back(now->val); dfs(now->right); } class Solution { publ 阅读全文
posted @ 2021-06-17 22:54 wegret 阅读(29) 评论(0) 推荐(0)
摘要:class Solution { public: int guessNumber(int n) { long long l=1,r=(long long)n; long long mid=((long long)1+n)>>1,temp; while (true){ temp=guess(mid); 阅读全文
posted @ 2021-06-17 22:44 wegret 阅读(30) 评论(0) 推荐(0)
摘要:/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), rig 阅读全文
posted @ 2021-06-12 21:12 wegret 阅读(48) 评论(0) 推荐(0)
摘要:简单地dfs一遍即可。 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(n 阅读全文
posted @ 2021-06-12 18:56 wegret 阅读(46) 评论(0) 推荐(0)
摘要:其实就是一个求LCA的模板问题。 我的实现方法是在p、q上分别打一个标记。然后递归把标记向上传递。当找到一个拥有两个标记的节点,它就是最近公共祖先。 /** * Definition for a binary tree node. * struct TreeNode { * int val; * T 阅读全文
posted @ 2021-06-12 17:57 wegret 阅读(33) 评论(0) 推荐(0)
摘要:中序遍历得到一个队列。然后找left和right之间的和就行了。 (第一遍理解错了意思。看懂了就不难。) int l,h; int que[20007],last; void dfs(TreeNode* now){ if (now==NULL) return ; dfs(now->left); qu 阅读全文
posted @ 2021-06-12 17:38 wegret 阅读(26) 评论(0) 推荐(0)
摘要:warning: if statement has empty body [-Wempty-body] 可能句末多打了分号。 warning: using the result of an assignment as a condition without parentheses [-Wparent 阅读全文
posted @ 2021-06-12 17:06 wegret 阅读(3236) 评论(0) 推荐(1)
摘要:int d[100007]; class Solution { public: int jump(vector<int>& nums) { memset(d,127,sizeof(d)); d[0]=0; for (int i=0;i<nums.size();i++) for (int j=1;i+ 阅读全文
posted @ 2021-06-11 18:00 wegret 阅读(29) 评论(0) 推荐(0)
摘要:贪心,$O(n)$。 扫一遍数组。声明一个last存从1出发可以跳到的最远位置。如果可以跳到$i$,那么最远的位置起码可以到$i+nums[i]$。如果扫到最远都到不了的点,就return 0。 class Solution { public: bool canJump(vector<int>& n 阅读全文
posted @ 2021-06-11 17:03 wegret 阅读(48) 评论(0) 推荐(0)
摘要:贪心。一串数字中取最大值。 class Solution { public: int minPartitions(string n) { char result=0; for (int i=0;i<n.size();i++) result=n[i]>result?n[i]:result; retur 阅读全文
posted @ 2021-06-10 21:32 wegret 阅读(46) 评论(0) 推荐(0)
摘要:贪心。 class Solution { public: int maxProfit(vector<int>& prices) { int now=prices.front(); //现在手中买入价 int profit=0; prices.push_back(prices.back()); for 阅读全文
posted @ 2021-06-10 19:16 wegret 阅读(32) 评论(0) 推荐(0)
摘要:贪心+栈。 int cnt[5]; int l[10007],t=0; class Solution { public: bool isValid(string s) { cnt[1]=0,cnt[2]=0,cnt[3]=0; int lim=s.size(); for (int i=0;i<lim 阅读全文
posted @ 2021-06-10 18:10 wegret 阅读(45) 评论(0) 推荐(0)
摘要:暴力解法 class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { for (int i=0;i<nums.size();i++) for (int j=i+1;j<nums.size();j++) if 阅读全文
posted @ 2021-06-10 15:49 wegret 阅读(34) 评论(0) 推荐(0)