
Union-Find
文章平均质量分 84
flyatcmu
这个作者很懒,什么都没留下…
展开
-
Number of Provinces
There arencities. Some of them are connected, while some are not. If cityais connected directly with cityb, and citybis connected directly with cityc, then cityais connected indirectly with cityc.Aprovinceis a group of directly or indirectly...原创 2021-07-21 14:14:54 · 202 阅读 · 0 评论 -
Redundant Connection
In this problem, a tree is anundirectedgraph that is connected and has no cycles.The given input is a graph that started as a tree with N nodes (with distinct values 1, 2, ..., N), with one additi...原创 2020-04-08 09:24:00 · 186 阅读 · 0 评论 -
Graph Connectivity With Threshold
We havencities labeled from1ton. Two different cities with labelsxandyare directly connected by a bidirectional road if and only ifxandyshare a common divisorstrictly greaterthan somethreshold. More formally, cities with labelsxandyhave...原创 2020-10-22 11:29:07 · 263 阅读 · 0 评论 -
Min Cost to Connect All Points
You are given an arraypointsrepresenting integer coordinates of some points on a 2D-plane, wherepoints[i] = [xi, yi].The cost of connecting two points[xi, yi]and[xj, yj]is themanhattan distancebetween them:|xi- xj| + |yi- yj|, where|val|den...原创 2020-09-14 05:31:08 · 397 阅读 · 0 评论 -
Remove Max Number of Edges to Keep Graph Fully Traversable
Alice and Bob have an undirected graph ofnnodesand 3 types of edges:Type 1: Can be traversed by Alice only. Type 2: Can be traversed by Bob only. Type 3: Can by traversed by both Alice and Bob.Given an arrayedgeswhereedges[i] = [typei, ui, vi]r...原创 2020-09-06 13:55:27 · 270 阅读 · 0 评论 -
Find Latest Group of Size M
Given an arrayarrthat represents a permutation of numbers from1ton. You have a binary string of sizenthat initially has all its bits set to zero.At each stepi(assuming both the binary string andarrare 1-indexed) from1ton, the bit at positio...原创 2020-08-24 02:42:48 · 182 阅读 · 0 评论 -
Most Stones Removed with Same Row or Column
On a 2D plane, we place stones at some integer coordinate points. Each coordinate point may have at most one stone.Now, amoveconsists of removing a stonethat shares a column or row with another stone on the grid.What is the largest possible number ...原创 2020-06-18 13:53:17 · 228 阅读 · 0 评论 -
Redundant Connection II
In this problem, a rooted tree is adirectedgraph such that, there is exactly one node (the root) for which all other nodes are descendants of this node, plus every node has exactly one parent, excep...原创 2020-05-08 13:32:05 · 256 阅读 · 0 评论 -
Minimize Malware Spread
In a network of nodes, each nodeiis directly connected to another nodejif and only ifgraph[i][j] = 1.Some nodesinitialare initially infected by malware. Whenever two nodes are directly conne...原创 2020-04-06 13:26:02 · 133 阅读 · 0 评论 -
Largest Component Size by Common Factor
Given a non-emptyarray of unique positive integersA, consider the following graph:There areA.lengthnodes, labelledA[0]toA[A.length - 1]; There is an edge betweenA[i]andA[j]if and only if...原创 2020-04-06 03:28:25 · 299 阅读 · 0 评论 -
Connecting Cities With Minimum Cost
There areNcities numbered from 1 toN.You are givenconnections, where eachconnections[i] = [city1, city2, cost]represents the cost to connectcity1andcity2together. (Aconnectionis bidirec...原创 2020-03-29 10:14:52 · 408 阅读 · 0 评论 -
Sentence Similarity II
Given two sentenceswords1, words2(each represented as an array of strings), and a list of similar word pairspairs, determine if two sentences are similar.For example,words1 = ["great", "acting",...原创 2020-03-11 06:01:00 · 191 阅读 · 0 评论 -
Number of Connected Components in an Undirected Graph
Givennnodes labeled from0ton - 1and a list of undirected edges (each edge is a pair of nodes), write a function to find the number of connected components in an undirected graph.Example 1:原创 2016-09-02 11:03:49 · 830 阅读 · 0 评论 -
Keys and Rooms
There areNrooms and you start in room0. Each room has a distinct number in0, 1, 2, ..., N-1, and each room may havesome keys to access the next room.Formally, each roomihas a list of keysr...原创 2020-02-12 11:14:48 · 151 阅读 · 0 评论 -
Optimize Water Distribution in a Village
There arenhouses in a village. We want to supply water for all the houses by building wells and laying pipes.For each housei, we can either build a well inside it directly with costwells[i], or ...原创 2020-02-09 14:44:21 · 309 阅读 · 0 评论 -
Maximum Association Set
Amazon sells books, every book has books which are strongly associated with it. Given ListA and ListB,indicates thatListA [i]is associated withListB [i]which represents the book and associated boo...原创 2020-01-10 11:53:39 · 181 阅读 · 0 评论 -
Accounts Merge
Given a listaccounts, each elementaccounts[i]is a list of strings, where the first elementaccounts[i][0]is aname, and the rest of the elements areemailsrepresenting emails of the account.Now...原创 2019-03-31 13:59:11 · 754 阅读 · 0 评论 -
Union Find 题型总结
Connecting Graph给一个图中的n个节点, 记为1到n. 在开始的时候图中没有边。你需要完成下面两个方法:connect(a, b), 添加连接节点a,b的边. query(a, b), 检验两个节点是否联通思路:Union Find模板要会背诵;public class ConnectingGraph { private class Unio...原创 2020-01-09 14:32:32 · 1093 阅读 · 0 评论 -
Set Union
There is a list composed by sets. If two sets have the same elements, merge them. In the end, there are several sets left.ExampleExample 1:Input :list = [[1,2,3],[3,9,7],[4,5,10]]Output:2 .Ex...原创 2020-01-01 08:34:27 · 219 阅读 · 0 评论 -
Number of Islands II
Given a n,m which means the row and column of the 2D matrix and an array of pair A( size k). Originally, the 2D matrix is all 0 which means there is only sea in the matrix. The list pair has k operato...原创 2019-11-28 12:41:10 · 241 阅读 · 0 评论 -
Minimum Spanning Tree
Given a list of Connections, which is the Connection class (the city name at both ends of the edge and a cost between them), find edges that can connect all the cities and spend the least amount.Retu...原创 2019-11-28 08:18:55 · 346 阅读 · 0 评论 -
Graph Valid Tree
Givennnodes labeled from0ton - 1and a list ofundirectededges (each edge is a pair of nodes), write a function to check whether these edges make up a valid tree.ExampleExample 1:Input: n...原创 2019-11-28 05:45:06 · 504 阅读 · 0 评论 -
Connecting Graph III
Givennnodes in a graph labeled from1ton. There is no edges in the graph at beginning.You need to support the following method:connect(a, b), an edge to connect node a and node b query(), Ret...原创 2019-11-28 05:05:13 · 145 阅读 · 0 评论 -
Connecting Graph
Givennnodes in a graph labeled from1ton. There is no edges in the graph at beginning.You need to support the following method:connect(a, b), add an edge to connect nodeaand node b`. query(...原创 2019-11-28 03:02:05 · 432 阅读 · 0 评论 -
Connecting Graph II
Givennnodes in a graph labeled from1ton. There is no edges in the graph at beginning.You need to support the following method:connect(a, b), an edge to connect node a and node b query(a), Re...原创 2019-11-28 03:20:39 · 231 阅读 · 0 评论 -
Friend Circles
There areNstudents in a class. Some of them are friends, while some are not. Their friendship is transitive in nature. For example, if A is adirectfriend of B, and B is adirectfriend of C, then ...原创 2019-03-30 08:10:58 · 563 阅读 · 1 评论 -
纪念一下自己的Coursera Princeton Algorithm的课程第一个assignment
今天终于完成了第一个Union-Find的assignment,之前觉得特别的难,可是最后自己也搞定了。而且是100%满分。自己后来plot了一下自己的分数,也许这就是学习曲线吧。刚开始不会,到后来中期显著提高,但是要到100%,那就要经历更多的波折,甚至是下降都有可能。最后才能达到100%满分。我觉得最有用的还是下面这段源代码:/************************...原创 2019-03-18 09:13:48 · 831 阅读 · 0 评论