본문 바로가기

코테 준비/Hash Table12

[LeetCode] 36. Valid Sudoku I. DescriptionII. Codeclass Solution: def isValidSudoku(self, board: List[List[str]]) -> bool: # 가로 중복 체크 for row in board: for i in row: if i != '.' and row.count(i) > 1: return False # 세로 중복 체크 for i in range(9): col = [row[i] for row in board] for j in col: if j != '.' and col.coun.. 2025. 1. 25.
[LeetCode] 17. Letter Combinations of a Phone Number I. DescriptionII. Codeclass Solution: def letterCombinations(self, digits: str) -> List[str]: dict = {'2': 'abc', '3': 'def', '4': 'ghi', '5': 'jkl', '6': 'mno', '7': 'pqrs', '8': 'tuv', '9': 'wxyz'} result = [] for digit in digits: if not result: result = [i for i in dict[digit]] else: temp = result.copy() .. 2025. 1. 25.
[LeetCode] 12. Integer to Roman I. DescriptionII. Codeclass Solution: def intToRoman(self, num: int) -> str: res = 'M' * (num // 1000) num %= 1000 val = num // 100 if val == 9: res += 'CM' elif val == 4: res += 'CD' else: res += 'D' * (val // 5) + 'C' * (val % 5) num %= 100 val = num // 10 if val == 9: .. 2025. 1. 25.
[LeetCode] 1930. Unique Length-3 Palindromic Subsequences I. DescriptionII. Codeclass Solution: def countPalindromicSubsequence(self, s: str) -> int: # 변수 초기화 result = 0 for char in set(s): # char가 문자열에서 등장하는 처음과 마지막 인덱스 찾기 first = s.find(char) last = s.rfind(char) if last - first > 1: # 두 char 사이에 문자가 존재할 경우 unique_middle = set(s[first+1:last]) re.. 2025. 1. 25.
[LeetCode] 1943. Describe the Painting I. Description문제 링크: https://leetcode.com/problems/describe-the-painting/description/?envType=problem-list-v2&envId=hash-tableII. Codei)class Solution: def splitPainting(self, segments: List[List[int]]) -> List[List[int]]: # events 초기화 events = defaultdict(int) # 사건들 기록 for start, end, color in segments: events[start] += color events[e.. 2025. 1. 23.
[LeetCode] 1942. The Number of the Smallest Unoccupied Chair I. Description문제 링크: https://leetcode.com/problems/the-number-of-the-smallest-unoccupied-chair/description/?envType=problem-list-v2&envId=hash-tableII. Codei)class Solution: def smallestChair(self, times: List[List[int]], targetFriend: int) -> int: # 목적 time target_time = times[targetFriend] # 시간 순서대로 정렬 ([[3, 10], [1, 5], [2, 6]] -> [[1, 5], [2, 6], [3, 10]]) time.. 2025. 1. 22.
[LeetCode] 3. Longest Substring Without Repeating Characters I. Description문제 링크: https://leetcode.com/problems/longest-substring-without-repeating-characters/description/?envType=problem-list-v2&envId=hash-table&II. Codei)class Solution: def lengthOfLongestSubstring(self, s: str) -> int: left = max_len = 0 char_set = set() # right 포인터를 옮겨가며 for right in range(len(s)): # 중복 문자 나오면 중복 문자 없을 때까지 left 포인터 옮기기 .. 2024. 12. 5.
[LeetCode] 1. Two Sum I. Description문제 링크: https://leetcode.com/problems/two-sum/description/?envType=problem-list-v2&envId=hash-table&II. Codei)class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: for i in range(0, len(nums)-1): for j in range(i+1, len(nums)): if nums[i] + nums[j] == target: return [i, j]가장 쉽게 떠올릴 수 있는 방법이다. 하지만 첫 번째 for .. 2024. 12. 2.