문제
- prices가 주어졌을때, 가장 이익이 높은 구간 찾기
public class Solution {
public static void main(String[] args) {
System.out.println(maxProfit(new int[]{7,1,5,3,6,4}));
System.out.println(maxProfit(new int[]{7,6,4,3,1}));
}
public static int maxProfit(int[] prices) {
}
}
결과 및 풀이
public static int maxProfit(int[] prices) {
int maxProfit = 0;
int left = prices[0];
for (int i = 1; i < prices.length; i++) {
int current = prices[i];
if (current < left) {
left = current;
} else {
maxProfit = Math.max(current - left, maxProfit);
}
}
return maxProfit;
}
배웠다
- StringBuild 클래스에서 reverse() 메소드를 제공해준다.