Zero to Hero
article thumbnail
118. Pascal's Triangle
Algorithm 2021. 6. 7. 21:37

Pascal's Triangle - 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 generate(self, numRows: int) -> List[List[int]]: answer = [[1]] if numRows == 1: return answer elif numRows == 2: answer.append([1, 1]) return answer else: answer.appe..

article thumbnail
26. Remove Duplicates from Sorted Array
Algorithm 2021. 6. 6. 22:35

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..

article thumbnail
13. Roman to Integer
Algorithm 2021. 5. 31. 22:28

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..

article thumbnail
202. Happy Number
Algorithm 2021. 5. 30. 22:22

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..

article thumbnail
326. Power of Three
Algorithm 2021. 5. 28. 10:22

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승을 모두 구해서 매칭 되면 반환하게..

article thumbnail
122. Best Time to Buy and Sell Stock II
Algorithm 2021. 5. 27. 22:06

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 ] 첫째, 둘째, 셋째 날 주식의 가격이 위와 같다고 가..