(진행중) [leetcode] Best Time to Buy and Sell Stock

데린이·2022년 5월 30일
0

시간순으로 작성된 배열의 숫자들의 차이값 중 가장 큰 값을 구하라.
단, 나중 값에서 먼저 작성된 값을 빼야한다.
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/

22-05-30

class Solution(object):
    def maxProfit(self, prices):
        """
        :type prices: List[int]
        :rtype: int
        """
        
        buy = 10000
        profit = 0
        n = len(prices)
        for i in range(0,n,2):
            if i == n-1:
                profit = max(profit,prices[i]-buy)
            else:
                j = i + 1
                profit = max(profit,prices[j]-prices[i],prices[j]-buy,prices[i]-buy)
                buy = min(buy,prices[i],prices[j])
            print(profit)
        return profit

인덱스 두 개를 사용하여 시간 복잡도를 줄이고자 하였다.
1475ms로, 아쉬운 성능.

Next what to do.
1. 70ms 이하로 줄일 수 있다!

profile
취뽀를 기원하는 취준생입니다!

0개의 댓글