대충이라도 하자

Best Time to Buy and Sell Stock ( Leetcode) 본문

꼬꼬마 개발자 노트/Coding Problems

Best Time to Buy and Sell Stock ( Leetcode)

Sueeeeee
반응형

At first, I used 2 for loops, but in aspects of time space, it was not efficient.

I thought about several ways, then, chose to use dynamic programming.( Need to study more about dp)

 

This is what I solved the problem.

class Solution {
    public int maxProfit(int[] prices) {
    	int min = Integer.MAX_VALUE;
        int max = 0;
        for(int i = 0; i<prices.length;i++){
            if(prices[i]<min){
                min = prices[i];
            }else {
                max = Math.max(max, prices[i]-min);
            }
        }
        return max;
	}
}
반응형

'꼬꼬마 개발자 노트 > Coding Problems' 카테고리의 다른 글

Blind 75 Must Do Leetcode - Two sum  (0) 2022.06.07
Diagonal Traverse  (0) 2022.03.22
Array and String - Introduction to Array  (0) 2022.02.07
Leetcode - Graph  (0) 2022.01.26
Graph -chaper 1(2)  (0) 2022.01.20
Comments