Untitled
long solution(int n, long c, int d, int[] arr) { long left = 0, right = d, bestK = -1; while (left <= right) { long mid = (left + right) / 2; if (canAchieve(arr, d, c, mid)) { bestK = mid; left = mid + 1; } else { right = mid - 1; } } return bestK; } private static boolean canAchieve(int[] arr, long d, long c, long k) { int n = arr.length; long[] dp = new long[(int) (d + 1)]; for (int i = 1; i <= d; i++) { dp[i] = dp[i - 1]; // Default: continue with max points without new exercise for (int j = 0; j < n; j++) { long previousDay = i - j - k - 1; if (previousDay >= 0) { dp[i] = Math.max(dp[i], dp[(int) previousDay] + arr[j]); // Gain points from valid previous k slot } } } return dp[(int) d] >= c; }
Leave a Comment