LeetCode/Grind169 60

128. Longest Consecutive Sequence ☆★★★★★★★★★

128. Longest Consecutive Sequence https://youtu.be/8sF5-yK2jsk?si=azgL_NMsGEf7q6pV 기본적으로 정렬을 하면, => 시간 복잡도 O(n log n) 그러나, 문제에서 요구하는 것은, 시간 복잡도 O(n) 임 !! Set 을 사용하여. 속도 개선! class Solution: def longestConsecutive(self, nums: List[int]) -> int: """ time: O(n^3): for + [while + while 안에 in 조건문] O(2n) => O(n) - O(n^2) 으로 보이지만, 자세히 살펴보면, 그렇지 않음!! ..

LeetCode/Grind169 2024.12.03

[Top150] 36. Valid Sudoku ★★★

36. Valid Sudoku https://youtu.be/TjFXEUCMqI8?si=mfzpl_jlRUHMsnOR from typing import Listimport collectionsclass Solution: def isValidSudoku(self, board: List[List[str]]) -> bool: # 각 열에 있는 숫자를 추적하기 위한 딕셔너리 (기본값은 set) cols = collections.defaultdict(set) # 각 행에 있는 숫자를 추적하기 위한 딕셔너리 (기본값은 set) rows = collections.defaultdict(set) # 각 3x3 서브그리드에 있는 숫자를 추적하기 위한 딕셔..

LeetCode/Grind169 2024.11.30

[LeetCode 75] Medium - 394. Decode String ★★★

394. Decode String "Decode String" 문제는 **스택(stack)**을 이용한 문자열 복호화 문제입니다.🧾 문제 요약입력 문자열 s는 인코딩된 문자열입니다.인코딩 형식은: k[내용] (내용을 k번 반복)중첩(nested) 구조도 허용됩니다. 예: "3[a2[c]]" → "accaccacc"문자열을 디코딩하여 반환해야 합니다.✅ 풀이 전략: 스택 사용중첩 구조 때문에 재귀나 스택이 필요합니다. 여기서는 스택 기반 풀이를 소개합니다.✅ Python 코드class Solution: def decodeString(self, s: str) -> str: stack = [] current_num = 0 current_str = "" ..

LeetCode/Grind169 2024.11.14

[LeetCode 75] Easy - 136. Single Number

136. Single Number class Solution: def singleNumber(self, nums: List[int]) -> int: # 2번 나온거는 무시하고, 1번 나온것만 찾음 # You must implement a solution with a linear runtime complexity and use only constant extra space. # 시간: O(n), 공간: O(1) 으로 풀어야 함 # for 루프 여러개? 단일 변수 여러개? # xor 하면(^) 2번 계산한건 0으로 사라짐 res = nums[0] for i in range(1, len(nums)): ..

LeetCode/Grind169 2024.11.09