Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- codingame #코딩게임 #codingame fall challenge2023 #코딩게임 2023 가을 챌린지
- 도메인 주도 설계 핵심 #DDD #도메인 주도 설계 #도메인
- leetcode
- leetcode #알고리즘 #릿코드
- #move zeroes
- #20. Valid Parentheses java
- DDD #도메인 #도메인 주도 설계 #도메인 주도 설계 핵심
- leetcode #2206. Divide Array Into Equal Pairs
- base7
- 도메인 #도메인 주도 설계 #도메인 주도 설계 핵심 #DDD
- leetcode #20. Valid Parentheses #알고리즘 #leetcode Valid Parentheses
- ddd
- Find Pivot Index
- 반 버논
- 867. Transpose Matrix #Transpose Matrix
- DDD #도메인 #도메인 주도 설계 #도메인 주도 섥계 핵심
- 코딩게임
- aws #cloudwatch #log insight
- codinGame
- 도메인 주도 설계 핵심
- LeetCode #
- Fall Challenge 2023
Archives
- Today
- Total
서하아빠의 개발 블로그
121. Best Time to Buy and Sell Stock 본문
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
문제설명
Stock의 가격이 배열로 주어지고 하루는 하루는 판다고 했을 때, 가장 큰 이익(maximum profit)을 구하는 문제
풀이설명
min과 result 변수를 선언하고 해당값들을 update해가면서 최대 이익을 계산함.
- min : for문을 순회하면서 가장 최저값을 업데이트
- result(최대이익) : 현재 주식가격 - 최저 주식(min) 값을 result값과 비교하면서 최대 값으로 업데이트 함
class Solution {
public int maxProfit(int[] prices) {
if(prices==null || prices.length <= 1)
return 0;
int min = prices[0];
int result = 0;
for(int i=0; i< prices.length; i++) {
min = Math.min(min, prices[i]);
result = Math.max(result, prices[i]-min);
}
return result;
}
}
'알고리즘 > LeetCode' 카테고리의 다른 글
724. Find Pivot Index (3) | 2023.06.17 |
---|---|
20. Valid Parentheses (0) | 2022.05.24 |
217. Contains Duplicate (0) | 2022.05.20 |
25. Reverse Nodes in k-Group (0) | 2022.05.20 |
61. Rotate List (0) | 2022.05.19 |