๋ฌธ์
์ฝ๋
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 |
๋๊ธ