Since there is such an important conclusion, we can safely drop the first k/2 element in A, which are definitaly smaller than k-th element in the union of A and B. This is also true for the A[k/2-1] > B[k/2-1] condition, which we should drop the elements in B. When A[...
set_union 这个会把重复元素除掉,需要注意。 + View Code
1 -- set_intersection(交集) 2 -- set_union(并集) 3 -- set_difference(差集) 4 -- set_symeetric_difference(对称差集) 去重的unique。 classSolution{ public: vector<int> unique_element_in_vector(vector<int>& v){ vector<int>::iterator vector_iterator; sort(v.begin(),v.end()); vector...
class Solution { private val directions = arrayOf(intArrayOf(0, 1), intArrayOf(0, -1), intArrayOf(1, 0), intArrayOf(-1, 0)) fun findMaxFish(grid: Array<IntArray>): Int { val n = grid.size val m = grid[0].size var ret = 0 // 并查集 val helper = UnionFind(grid) /...
leetcode349. Intersection of Two Arrays 文章分类运维 vector的一系列操作。 1 -- set_intersection(交集) 2 -- set_union(并集) 3 -- set_difference(差集) 4 -- set_symeetric_difference(对称差集) 去重的unique。 classSolution{ public: vector<int>unique_element_in_vector(vector<int>&v){...
1.4 Median of Two Sorted Arrays 1.5 Longest Consecutive Sequence 1.6 Two Sum 1.7 Valid ...
Each union and find operation is nearly constant time due to path compression and union by rank, making the amortized time complexity very efficient. Space Complexity: O(V), as we need to maintain the parent and rank arrays. The space used is proportional to the number of vertices in the ...
[Array, Hash Table, Union Find]|| |294|Binary Tree Longest Consecutive Sequence.java|Medium|Java|[DFS, Divide and Conquer, Tree]|| |295|Number of Connected Components in an Undirected Graph.java|Medium|Java|[BFS, DFS, Graph, Union Find]|| |296|Next Closest Time.java|Medium|Java|[Basic...
https://leetcode.cn/problems/checking-existence-of-edge-length-limited-paths-ii/ 1697的在线版本,但难度完全不是一个次元。这道题需要对基础并查集模板的Union做改进,不能直接压缩路径,而是按秩序合并的同时,记录每条边的权重信息,查询时如果某个合并方向的权重不满足,就不能朝这个方向合并。
0349-intersection-of-two-arrays 1915-check-if-one-string-swap-can-make-strings-equal Union Find 0128-longest-consecutive-sequence Linked List 0002-add-two-numbers 0019-remove-nth-node-from-end-of-list 0021-merge-two-sorted-lists 0141-linked-list-cycle 0528-swapping-nodes-in-a-linked-list Math...