본문 바로가기

자료구조와 알고리즘15

[DSA][Array & Hashing] 08. Longest Consecutive Sequence LeetCode 128 08. Longest Consecutive SequenceGiven an unsorted array of integers nums, return the length of the longest consecutive elements sequence.You must write an algorithm that runs in O(n) time. [질문하기]- 음수가 포함될 수 있나요? YES- 정수 배열에 중복된 값이 존재할 수 있나요? YES [아이디어]연속된 숫자를 판단할 때, num을 기준으로 num + 1이 있는지 확인하자. 그러기 위해 탐색에 유리한 자료구조인 set을 사용하자. [풀이 1] Setclass Solution: def longestConsecutive(se.. 2025. 5. 22.
[DSA][Array & Hashing] 07. Valid Sudoku LeetCode 36 07. Valid SudokuDetermine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:Each row must contain the digits 1-9 without repetition.Each column must contain the digits 1-9 without repetition.Each of the nine 3 x 3 sub-boxes of the grid must contain the digits 1-9 without repetition.Note:A Sudoku board (partially filled) coul.. 2025. 5. 21.
[DSA][Array & Hashing] 06. Product of Array Except Self LeetCode 238 06. Product of Array Except SelfGiven an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.You must write an algorithm that runs in O(n) time and without using the division operation. [질문하기]- 배열에 0이 하나 이상 포함될 수 있나요? [아.. 2025. 5. 20.
[DSA][Array & Hashing] 05. Top K Frequent Elements LeetCode 347 05. Top K Frequent ElementsGiven an integer array nums and an integer k, return the k most frequent elements.You may return the answer in any order. [질문하기]- 빈도가 같은 요소가 여러 개 있는 경우 모두 반환해야 하나요?- 배열에 음수도 포함될 수 있나요? YES [아이디어]- 정렬- Heap- Bucket Sort [풀이 1] Hash with Sortingclass Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: # 1. 빈도 hash 생성 c.. 2025. 5. 19.