摘要: int d[2][2]={-1,1,1,-1}; // -1 1 // 1 -1 class Solution { public: vector<int> findDiagonalOrder(vector<vector<int>>& mat) { vector<int> ans; int m=mat 阅读全文
posted @ 2022-06-14 22:35 wegret 阅读(21) 评论(0) 推荐(0)
摘要: class Solution { public: int heightChecker(vector<int>& heights) { vector<int> expected(heights); int t=expected.size(),ans=0; sort(expected.begin(),e 阅读全文
posted @ 2022-06-13 09:47 wegret 阅读(22) 评论(0) 推荐(0)
摘要: class Solution { public: vector<string> findAndReplacePattern(vector<string>& words, string pattern) { vector<string> ans; int t=words.size(),cnt=patt 阅读全文
posted @ 2022-06-12 17:09 wegret 阅读(22) 评论(0) 推荐(0)
摘要: 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 阅读(55) 评论(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 阅读(57) 评论(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 阅读(58) 评论(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 阅读(30) 评论(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 阅读(74) 评论(0) 推荐(0)
摘要: “任意两点之间 有且仅有一条简单路径时,返回将所有点连接的最小总费用”,即求MST(最小生成树)。 发现数据规模不超过1000,所以可以考虑到$O(n^2)$的算法。 $O(n^2)$算出任意两点的距离。然后用prim算最小生成树,同样$O(n^2)$。(这张图是稠密图,点数$n$远小于边数$n^2 阅读全文
posted @ 2021-06-19 12:07 wegret 阅读(69) 评论(0) 推荐(0)