Minimum Number of Increments on Subarrays to Form a Target Array LeetCode solution in Java
1// Imagine 3 cases2// Case 1. [3,2,1], we need 3 operations.3// Case 2. [1,2,3], we need 3 operations.4// Case 3. [3,2,1,2,3], we need 5 operations.5// What we need to add is actually the diff (cur - prev)6// Time complexity: O(N)7// Space complexity: O(1)8class Solution {9public int minNumberOperations(int[] target) {10int res = 0;11int prev = 0;12for (int cur : target) {13if (cur > prev) {14res += cur - prev;15}16prev = cur;17}18return res;19}20}