๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/Leetcode

[Leetcode,c++] Best Time to Buy and Sell Stock

by dkswnkk 2021. 11. 10.

๋ฌธ์ œ

 

Best Time to Buy and Sell Stock - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

์ฝ”๋“œ

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int min_index,min_value=1e9,ans;

        for(int i=0; i<prices.size(); i++){
            if(prices[i]<min_value){
                min_value = prices[i];
            }
            else{
                ans = max(-min_value+prices[i],ans);
            }
        }
        if(ans<0) return 0;
        return ans;
    }
};

 

ํ’€์ด

dp์ธ ์ค„ ์•Œ๊ณ  dp๋กœ ๊ตฌํ˜„ํ•˜๋ ค๋‹ค๊ฐ€ ๋„์ €ํžˆ ํ’€๋ฆฌ์ง€๊ฐ€ ์•Š์•„์„œ ๊ทธ๋ƒฅ ์กฐ๊ฑด๋ฌธ ์ค˜๊ฐ€๋ฉด์„œ ํ’€์—ˆ๋”๋‹ˆ ํ’€๋ ธ์Šต๋‹ˆ๋‹ค......... ใ… 

'Algorithm ๐Ÿง‘๐Ÿปโ€๐Ÿ’ป > Leetcode' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Leetcode,c++] Remove Duplicates from Sorted Array  (0) 2021.11.14
[Leetcode,c++] Longest Common Prefix  (0) 2021.11.11
[Leetcode,c++] Permutations  (0) 2021.11.10
[Leetcode,c++] Climbing Stairs  (0) 2021.11.09
[Leetcode,c++] Maximum Subarray  (0) 2021.11.09
[Leetcode,c++] Valid Parentheses  (0) 2021.11.09

๋Œ“๊ธ€