1 2 3 4 5 6 7 8 9 10 11 12 13 14
| class Solution { public int maxTotalFruits(int[][] fruits, int startPos, int k) { int ans = 0, sum = 0; for (int i = 0, j = 0; j < fruits.length; ++j) { int pos = fruits[j][0], cnt = fruits[j][1]; sum += cnt; while (i <= j && pos - fruits[i][0] + Math.min(Math.abs(startPos - fruits[i][0]), Math.abs(startPos - pos)) > k) { sum -= fruits[i++][1]; } ans = Math.max(ans, sum); } return ans; } }
|