๐Ÿฅž BE
home

122_Best Time to Buy and Sell Stock 2

๋‹ด๋‹น์ž
์™„๋ฃŒ ์—ฌ๋ถ€
Solved
์š”์•ฝ
๋‚ ์งœ
2024/08/27
ํƒœ๊ทธ
๊ทธ๋ฆฌ๋””
๋‚œ์ด๋„
Medium
์ถœ์ฒ˜
LeetCode

์ฝ”๋“œ

class Solution { public int maxProfit(int[] prices) { int result = 0; for (int i = 0; i < prices.length - 1; i++) { if (prices[i] < prices[i + 1]) { result += prices[i + 1] - prices[i]; } } return result; } }
Java
๋ณต์‚ฌ

๋ฌธ์ œ ํ•ด๊ฒฐ ์•„์ด๋””์–ด

๋‚ด๋ฆฌ๊ธฐ ์ „์— ํŒ”๊ณ , ์˜ค๋ฅด๊ธฐ ์ „์— ์‚ฌ๋ฉด ๋œ๋‹ค. ๋งค ๋‹จ๊ณ„ ์ด์ต์„ ์ทจํ–ˆ์„ ๋•Œ, ์–ผ๋งˆ๋‚˜ ๋งŽ์ด ์ทจํ•  ์ˆ˜ ์žˆ๋Š”์ง€๋ฅผ ๊ตฌํ•œ๋‹ค.