Remove Duplicates from Sorted Array - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. 선형 탐색 로직 from typing import * class Solution: def removeDuplicates(self, nums: List[int]) -> int: if not nums: return 0 saved_val, target_index = nums[0], 1 for i in range(len(nums)): if i == 0..
Roman to Integer - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. 내 Trash Garbage Code class Solution: def romanToInt(self, s: str) -> int: dict1 = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000} answer, index = 0, 0 while index < len(s): if s[index] == 'I' an..
Happy Number - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. 집합을 이용한 풀이 from functools import reduce class Solution: def isHappy(self, n: int) -> bool: def phase_one(num): return list(map(int, list(str(num)))) def phase_two(input_list): return reduce(lambda acc, cur: acc + cur..
Power of Three - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. Iteration class Solution: def isPowerOfThree(self, n: int) -> bool: cur = 0 while True: temp=pow(3,cur) if temp>n: return False if temp==n: return True cur+=1 입력받은 수가 3의 N승의 숫자인지 찾는 문제다. 그냥 3의 N승을 모두 구해서 매칭 되면 반환하게..
1. Greedy class Solution: def maxProfit(self, prices: List[int]) -> int: stack = [] answer = 0 for price in prices: if not stack: stack.append(price) else: if stack[-1] < price: answer+=price-stack[-1] stack.pop() stack.append(price) else: stack.pop() stack.append(price) return answer 주어진 날짜에 대한 주식 가격 목록에서 주식을 사고 팔 최댓값을 반환하면 된다. 그리디 하게 생각해서 해결할 수 있는 문제다. [ 1, 3, 7 ] 첫째, 둘째, 셋째 날 주식의 가격이 위와 같다고 가..
Kth Smallest Element in a Sorted Matrix - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. 단순하게... class Solution: def kthSmallest(self, matrix: List[List[int]], k: int) -> int: temp = [] for item in matrix: temp += item return sorted(temp)[k-1] 2차원 배열에서 k번째로 큰 수를 반환하는 문제다. 그냥 2차..