[LeetCode/Python] 125. Valid Palindrome
·
Algorithm/LeetCode
문제 링크:https://leetcode.com/problems/valid-palindrome/description/?envType=study-plan-v2&envId=top-interview-150Palindrome 에 관한 문제이다. 문제만 잘 읽는다면 문제 없이 풀 수 있다.사실 leetcode를 풀 때 초반 장벽은 아마 영어 단어이지 않을까 싶다. 다행히 이 문제에서는 palindrome이 어떤 특징을 가진 단어인지 설명을 해주는데, 코테 문제에서는 설명을 안해주는 경우도 허다해서 난감했던 기억이 있다. [내 풀이]class Solution: def isPalindrome(self, s: str) -> bool: if len(s) == 0: return Tru..
[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] Duplicate Integer
·
Algorithm/NeetCode
문제 링크: https://neetcode.io/problems/duplicate-integer NeetCode neetcode.io 내 코드class Solution: def hasDuplicate(self, nums: List[int]) -> bool: nums.sort() for i in range(1, len(nums)): if nums[i] != nums[i-1]: continue else: return True return False duplicate가 없어야한다는 말은, 같은 숫자 없이 숫자들이 모두 unique 해야한다는 뜻이다.어떻게 할까 하다가 정렬해서..
[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..