2016 - Maximum Difference Between Increasing Elements
정보
- 문제 보기: 2016 - Maximum Difference Between Increasing Elements
- 소요 시간: 7분 11초
- 풀이 언어:
java - 체감 난이도: 2️⃣
- 리뷰 횟수: ✅
풀이 키워드
스포주의
그리디
풀이 코드
정보
- 메모리: 42140 KB
- 시간: 0 ms
class Solution {
public int maximumDifference(int[] nums) {
// init w/ -1
// greedy w/ reverse iteration
int ans = -1;
int mx = nums[nums.length-1];
for (int i = nums.length-2; -1 < i; --i) {
int n = nums[i];
if (n < mx)
ans = mx-n < ans ? ans : mx-n;
mx = mx < n ? n : mx;
}
return ans;
}
}