[LeetCode/Python] 58. Length of Last Word
·
Algorithm/LeetCode
다시 알고리즘 공부를 꾸준히 하기 위한 작심삼일 x 1년! 프로젝트를 시작했다. (작심삼일이라도 꾸준히 하자라는 마음가짐이다) 문제 링크:https://leetcode.com/problems/length-of-last-word/description/?envType=study-plan-v2&envId=top-interview-150 Easy 난이도인 만큼, 특히 python으로서는 아주 풀기 쉬운 문제였다. [내 풀이]class Solution: def lengthOfLastWord(self, s: str) -> int: word_list = s.strip().split() return len(word_list[-1]) 시간 복잡도where n is the length of ..
[NeetCode/Python] Meeting Schedule
·
Algorithm/NeetCode
문제 링크: https://neetcode.io/problems/meeting-schedule NeetCode neetcode.io [내 풀이]"""Definition of Interval:class Interval(object): def __init__(self, start, end): self.start = start self.end = end"""class Solution: def canAttendMeetings(self, intervals: List[Interval]) -> bool: intervals.sort(key=lambda x: x.start) for i in range(len(intervals) - 1): ..
[NeetCode/Python] Buy and Sell Crypto
·
Algorithm/NeetCode
문제 링크: https://neetcode.io/problems/buy-and-sell-crypto NeetCode neetcode.io 투 포인터/슬라이딩 윈도우 [내 풀이]class Solution: def maxProfit(self, prices: List[int]) -> int: maxProfit = 0 for i in range(len(prices)-1): for j in range(i, len(prices)): profit = prices[j] - prices[i] if profit > maxProfit: maxProfit = profit ret..
[NeetCode/Python] Min Cost Climbing Stairs
·
Algorithm/NeetCode
문제 링크: https://neetcode.io/problems/min-cost-climbing-stairs NeetCode neetcode.io 예전에 알고리즘 감을 살릴겸, 순차적으로 구하는 방법, 역순으로 구하는 방법 두 방법 모두를 사용해서 풀어보았다.점점 이제 이지 난이도에서 벗어나야할텐데... 준비중이다!  [내 코드 1: 순차적 풀이]class Solution: def minCostClimbingStairs(self, cost: List[int]) -> int: n = len(cost) result = [0 for i in range(len(cost))] # cost.length >= 2 result[0], result[1] = cost..
[LeetCode/Python] 9. Palindrome Number
·
Algorithm/LeetCode
문제 링크: https://leetcode.com/problems/palindrome-number/?envType=study-plan-v2&envId=top-interview-150 숫자를 문자열로 바꿔서 풀면 아주 쉬운 문제다.근데 문제 말미에 문자열로 안바꾸고 풀어보라는 챌린지가 있길래 시도했다가 처참히 실패했다..ㅠ 일단 인풋을 문자열로 바꾸지 않고 숫자 자체로 두고 푼 코드는 아래와 같다.class Solution: def isPalindrome(self, x: int) -> bool: if x 0: n = len(str(x)) divisor = 10 **(n-1) left = x // divisor ..
[LeetCode/Python] 67. Add Binary
·
Algorithm/LeetCode
문제 링크: https://leetcode.com/problems/add-binary/description/?envType=study-plan-v2&envId=top-interview-150 숫자로 쉽게 치환해서 패키지를 써서 이진법 덧셈을 할 생각이었는데, string 자체로 풀어보고 싶다고 생각했다.  [내 풀이]class Solution: def addBinary(self, a: str, b: str) -> str: # without converting string to int carry = 0 i, j = len(a) - 1, len(b) - 1 result = [] while i >= 0 or j >= 0 or carry: ..