반응형

2025/10/24 15

695. Max Area of Island (Array, Depth-First Search, Breadth-First Search, Union Find, Matrix)

링크 - https://leetcode.com/problems/max-area-of-island/description/?envType=company&envId=facebook&favoriteSlug=facebook-thirty-days난이도 - MediumIntuitionDFS, BFS모두 사용 가능하다. 난 여기서 BFS를 사용하였다.Codeclass Solution: def maxAreaOfIsland(self, grid: List[List[int]]) -> int: max_area = 0 rows = len(grid) cols = len(grid[0]) for row in range(rows): for col in range..

986. Interval List Intersections (Array, Two Pointers, Line Sweep)

링크 - https://leetcode.com/problems/interval-list-intersections/description/?envType=company&envId=facebook&favoriteSlug=facebook-thirty-days난이도 - MediumIntuition우리는 두개의 값중 첫번째 값의 max와 두번째 값의 min을 통해서 두개에 intersection이 있는지 없는지를 알 수 있다. 그리고 항상 end가 낮은 리스트 인덱스를 올려준다.Codeclass Solution: def intervalIntersection(self, firstList: List[List[int]], secondList: List[List[int]]) -> List[List[int]]: ..

1868. Product of Two Run-Length Encoded Arrays (Array, Two Pointers)

링크 - https://leetcode.com/problems/product-of-two-run-length-encoded-arrays/description/?envType=company&envId=facebook&favoriteSlug=facebook-thirty-days난이도 - MediumIntuition각 encoded list에 있는 frequency 값들을 빼주면서 계산하고 idx를 증가시키면 된다.Codeclass Solution: def findRLEArray(self, encoded1: List[List[int]], encoded2: List[List[int]]) -> List[List[int]]: idx1 = idx2 = 0 ans = [] w..

65. Valid Number (String)

링크 - https://leetcode.com/problems/valid-number/description/?envType=company&envId=facebook&favoriteSlug=facebook-thirty-days난이도 - HardIntuition규칙대로 코딩하면 된다. 1. 숫자가 나오면 had_digit을 True로 한다.2. 부호가 나오면 맨 앞인지, 아니면 exponential의 앞인지 확인하고 아니면 False반환한다.3. e나 E가 나오면 이전에 숫자가 나왔는지 또는 이미 exponential이 나왔는지 확인한다. 4. .이 나왔으면 이전에 .이 나왔거나 exponential이후인지 확인한다.5. 위의 조건이 아니면 False를 반환한다.Codeclass Solution: de..

127. Word Ladder (Hash Table, String, Breadth-First Search)

링크 - https://leetcode.com/problems/word-ladder/description/?envType=company&envId=facebook&favoriteSlug=facebook-thirty-days난이도 - HardIntuition먼저 word_combo를 사용해서 word[:i] + "*" + word[i+1:]의 단어들을 저장해놓는다. 그다음 bfs를 통해서 단어를 찾아 나가면 된다.Codefrom collections import defaultdictclass Solution(object): def ladderLength( self, beginWord: str, endWord: str, wordList: List[str] ) -> int: ..

398. Random Pick Index (Hash Table, Math, Reservoir Sampling, Randomized)

링크 - https://leetcode.com/problems/random-pick-index/description/?envType=company&envId=facebook&favoriteSlug=facebook-thirty-days난이도 - MediumIntuitionHash table에 num값과 그 idx들을 list로 저장해 놓고, 나중에 O(1)으로 pick할 수 있다.Codeclass Solution: def __init__(self, nums: List[int]): self.hash_table = defaultdict(list) for i, num in enumerate(nums): self.hash_table[num].append(i) ..