본문 바로가기

전체 글

(113)
55. Jump Game leetcode.com/problems/jump-game/ Jump Game - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com class Solution: def canJump(self, nums: List[int]) -> bool: n = len(nums) i = 0 farthest = nums[0] while i < n and i = n-1
207. Course Schedule leetcode.com/problems/course-schedule/ Course Schedule - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com import collections class Solution: def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool: indegrees = [0]*numCourses graph = defaultdict(list) for u, v in..
41. First Missing Positive leetcode.com/problems/first-missing-positive/ First Missing Positive - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com class Solution: def firstMissingPositive(self, nums: List[int]) -> int: if 1 not in nums : return 1 n = len(nums) for i in range(n) : if nums[i] n : nums[i] = 1 fo..
988. Smallest String Starting From Leaf leetcode.com/problems/smallest-string-starting-from-leaf/ Smallest String Starting From Leaf - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = l..
16. 3Sum Closest leetcode.com/problems/3sum-closest/ 3Sum Closest - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com class Solution: def threeSumClosest(self, nums: List[int], target: int) -> int: diff = float('inf') n = len(nums) nums.sort() for i in range(n-2) : l, r = i+1, n-1 while l < r : curr ..
835. Image Overlap leetcode.com/problems/image-overlap/ Image Overlap - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com class Solution: def largestOverlap(self, A: List[List[int]], B: List[List[int]]) -> int: posa = [] posb = [] for i in range(len(A)) : for j in range(len(A[0])) : if A[i][j] == 1 : p..
1305. All elements in two binary search trees leetcode.com/problems/all-elements-in-two-binary-search-trees/ All Elements in Two Binary Search Trees - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # sel..
Union Find Find(x): get the identity of the group that the element x belongs to. Union(x, y): merge the two groups that the two elements belong to respectively. class DisjointSetUnion(object): def __init__(self, size) : self.parent = [i for i in range(size+1)] self.size = [1]*(size+1) def find(self, x): if self.parent[x] != x : self.parent[x] = self.find(self.parent[x]) return self.parent[x] def union(self..