Coding Test Study
Problems
Categories
π Learning Paths
Edit Solution
Language
JAVA
PYTHON
CPP
JAVASCRIPT
Code
class Solution { public int maxProfit(int[] prices) { int minPrice = Integer.MAX_VALUE; int maxProfit = 0; for (int price : prices) { if (price < minPrice) minPrice = price; else if (price - minPrice > maxProfit) maxProfit = price - minPrice; } return maxProfit; } }
Notes
[μλ νμ΄ Β· gpt-4.1-mini] μ£Όμ κ°κ²© λ°°μ΄μ μννλ©° μ΅μ κ°λ₯Ό κ°±μ νκ³ , νμ¬ κ°κ²©μμ μ΅μ κ°λ₯Ό λΊ μ΄μ΅μ κ³μ°ν΄ μ΅λ μ΄μ΅μ μ λ°μ΄νΈνλ€. ν λ²μ μνλ‘ μ΅λ μ΄μ΅μ ꡬν μ μλ€. μκ° O(n) Β· κ³΅κ° O(1) μ£Όμ: μ΅μ κ° κ°±μ μ μ΅λ μ΄μ΅μ λ°λ‘ κ³μ°νμ§ μκ³ λ°λ‘ κ΄λ¦¬ν΄μΌ νλ€.
Save
Cancel