https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
[Best Time to Buy and Sell Stock]
잘못 풀었다.
요구사항
1. prices
의 가격 차이 max가 얼마인가
→ 배열 오름차순 sort 후 0번 index(맨앞) 마지막 index(맨뒤) 비교
2. 가격차이 max 를 만드는 big
과 small
의 index가 오름차순 순서인가
→ index 비교(big
의 index가 small
의 index보다 큰지.
→ big
의 인덱스가 더 작다면 small
의 인덱스 1 증가 후 비교
로직
기존 prices
배열 map
에 key: prices[index] , value: index
로 저장
Map<Integer, Integer> priceMap = new HashMap<>();
// Map에 기존 prices 저장
for (int i = 0; i < prices.length; i++) {
priceMap.put(prices[i], i);
}
오름차순은 Arrays.sort()
사용
Arrays.sort(prices);