티스토리 뷰

728x90

 

https://leetcode.com/problems/best-time-to-buy-and-sell-stock/

 

일별로 주식 가격이 주어졌을 때, 최대 이익을 구하기

 

 

내 풀이

class Solution {
    func maxProfit(_ prices: [Int]) -> Int {
        guard prices.count > 1 else { return 0 }
        var result: [Int] = []

        for price in prices {
            for j in 1..<prices.count {
                if prices[j] - price > 0 {
                    var profit = prices[j] - price
                    print(prices[j], "-", price, "=", prices[j] - price)
                    result.append(profit)
                }
            }
        }
        return result.max() ?? 0
    }
}

Solution().maxProfit([7,1,5,3,6,4])

시간제약에 걸림. 

  • 반복문 두개로 prices[j] - price 값을 각각 구함
  • 각각 구해진 값은 result에 추가
  • 반복문이 다 실행된 후에 result 요소들중 가장 큰 값을 구함 ( max() 사용 )

 

 

다른사람의 풀이

class Solution {
    func maxProfit(_ prices: [Int]) -> Int {
        guard prices.count > 1 else { return 0 }
        var best = 0, current = 0
        for index in 1..<prices.endIndex {
            current = max(0, current + prices[index] - prices[index-1])
            best = max(best, current)
        }
        return best
    }
}

Solution().maxProfit([7,1,5,3,6,4])

 

  • 1 ~ 6 (주어진배열의 마지막) 만큼을 반복문을 사용해 돌림
  • current 값은 0(초기숫자), current + prices[index] - prices[index-1]를 이용해 가장 큰값을 current에 대입
    • current 값 순서: 0 → 4 → 2 → 5 → 3 
  • best 값은 best, current중 가장 큰수를 best에 대입
    • best 값 순서: 0 → 0 → 4 → 4 → 5 

 

 

배운것

max()

 

두 값을 비교해 가장 큰 숫자를 반환.

 

 

예제

let arr = [7,1,5,3,6,4]
print(arr.max()!) // Optional 처리필요
// 7

 

 

이외에도

  • 두가지 변수를 ' , ' 를 이용해 한줄로 코드 작성
  • array.endIndex로 마지막 index숫자를 구함 
    • array.count와 같은 값
728x90