728x90 Programming/Python46 [LeetCode] 9. Palindrome Number 기본 아이디어덱(deque)으로 양쪽에서 하나씩 빼도 되고리스트로 양쪽 인덱스 접근해도 되겠다덱from collections import dequeclass Solution: def isPalindrome(self, x: int) -> bool: if x 1: if queue.popleft() != queue.pop(): return False return True음수는 팰린드롬 될 수 없어서 미리 쳐내줄 수 있음인덱스class Solution: def isPalindrome(self, x: int) -> bool: if x l: if nums[l] != nums[r]: .. 2025. 5. 5. [LeetCode] 1. Two Sum 기본 아이디어이중포문하면 되겠지만 느릴듯이중포문 - O(n²)class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: for i in range(len(nums)-1): for j in range(i+1, len(nums)): if nums[i] + nums[j] == target: return [i, j]해시 - O(n)class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: num_map = {} for i, num.. 2025. 5. 4. [프로그래머스] 의상 기본 아이디어콤비네이션?아무튼 경우의수 다 구하기코드from collections import defaultdictdef solution(clothes): dic = defaultdict(int) for name, category in clothes: dic[category] += 1 total = 1 for count in dic.values(): total *= (count + 1) return total - 1defaultdict을 쓰면 dict만들때 초기에 if category in dic~이런거 체크 안 해도 된다!카테고리별 경우의 수 + 1을 해준다 (그 카테코리에서 옷을 안 고르는 경우도 하나의 경우의수로 침)마지막에 전체에서 아예 아무것도.. 2025. 4. 30. [프로그래머스] H-Index 기본 아이디어역순 정렬해서 하나씩 카운트def solution(citations): sorted_ct = sorted(citations, reverse=True) max_h = 0 for i in range(len(citations) + 1): cnt = 0 for c in sorted_ct: if c >= i: cnt += 1 else: break if cnt >= i: max_h = i return max_h문제의 조건 자체가 되게 헷갈렸음. 논문수, 인용수 모두 h여야하는 것이 아닌 h이상인지를 체크하는 것 2025. 4. 14. [프로그래머스] 네트워크 기본 아이디어단순한 bfs코드from collections import dequedef solution(n, computers): cnt = 0 visited = set() for i in range(n): if i not in visited: cnt += 1 queue = deque([i]) visited.add(i) while queue: node = queue.popleft() for j in range(n): if computers[node][j] == 1 and j not in visited: .. 2025. 4. 13. [프로그래머스] 모음사전 아이디어경우의수 다 만들기?def solution(word): dic = {'A': 0, 'E': 1, 'I': 2, 'O': 3, 'U': 4} weights = [781, 156, 31, 6, 1] cnt = 0 for i, chr in enumerate(word): cnt += dic[chr] * weights[i] + 1 return cnt가중치는 "해당 자리 이후로 남은 자리수의 조합 수" 총합그래서 처음부터 글자 하나하나 당 그 자리에서 스킵하는 단어 수가 계산된 것.최종적으로는 각 글자 위치마다 (몇 번째 알파벳인지) × (자리수 가중치) 를 더하면 됨.첫번째자리수: 5^4 + 5^3 + 5^2 + 5^1 + 5^0두번째자리수: 5^3 + 5^2 + 5.. 2025. 4. 12. 이전 1 2 3 4 ··· 8 다음 728x90