[TIL_Carrotww] 88 - 23/01/17

유형석·2023년 1월 17일
0

TIL

목록 보기
103/138
post-thumbnail

📝Carrotww의 코딩 기록장

🧲 python algorithm interview

🔍 Leetcode 238 Product of Array Except Self Medium

문제 조건에 나누지 말고 O(1) 안에 풀라고 나와있다.
투 포인터 방식의 문제가 많이 나왔어서 응용하여 풀어볼라고 했지만 O(1) 안에 풀 수 있는 방법이 떠오르지 않았다...

책을 조금 본 후 알았는데 양쪽 index에 있는 숫자들을 곱한다는 생각으로 접근하면 중복없이 for문 2번으로 O(1) 안에 풀 수 있다.

  • 풀이
def Solution:
	def productExceptSelf(self, nums: List[int]) -> List[int]:
    	result = []
        temp = 1
        for i in range(len(nums)):
        	result.append(temp)
            temp *= nums[i]
        temp = 1
        for i in range(len(nums) - 1, -1, -1):
        	result[i] = result[i] * temp
            temp *= nums[i]
        return result

🔍 Leetcode 121 Best Time to Buy and Sell Stock Easy

딱히 설명이 필요 없이 쉬운 문제이다.

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        best_pr = 10000
        sell_stock = 0
        for pr in prices:
            if best_pr < pr:
                sell_stock = pr - best_pr
            best_pr = min(best_pr, pr)
        return sell_stock

🧲 잡설

🔍 오늘 이사를 해서 프로젝트를 좀 하고 알고리즘은 많이 못했다...
책에 있는문제 2개씩 풀기로 했는데 다행히(?) 쉬운 문제들이 나와서 슉슉 풀고 지나쳤다.
프로젝트 로그인 기능까지 구현을 해놓았다.
이번에는 중간에 배포를 한 후 깃허브 액션으로 자동화하여 개발을 진행해보려고 한다.
오늘 얼마 못했지만.. 오늘 끝!

profile
Carrot_hyeong

0개의 댓글