|
| 1 | +package com.fishercoder.solutions; |
| 2 | + |
| 3 | +/** |
| 4 | + * 1300. Sum of Mutated Array Closest to Target |
| 5 | + * |
| 6 | + * Given an integer array arr and a target value target, return the integer value such that when we change all the integers larger than value in the given array to be equal to value, |
| 7 | + * the sum of the array gets as close as possible (in absolute difference) to target. |
| 8 | + * In case of a tie, return the minimum such integer. |
| 9 | + * Notice that the answer is not neccesarilly a number from arr. |
| 10 | + * |
| 11 | + * Example 1: |
| 12 | + * Input: arr = [4,9,3], target = 10 |
| 13 | + * Output: 3 |
| 14 | + * Explanation: When using 3 arr converts to [3, 3, 3] which sums 9 and that's the optimal answer. |
| 15 | + * |
| 16 | + * Example 2: |
| 17 | + * Input: arr = [2,3,5], target = 10 |
| 18 | + * Output: 5 |
| 19 | + * |
| 20 | + * Example 3: |
| 21 | + * Input: arr = [60864,25176,27249,21296,20204], target = 56803 |
| 22 | + * Output: 11361 |
| 23 | + * */ |
| 24 | +public class _1300 { |
| 25 | + public static class Solution1 { |
| 26 | + public int findBestValue(int[] arr, int target) { |
| 27 | + int ave = target / arr.length; |
| 28 | + int min = findMin(arr); |
| 29 | + int max = findMax(arr); |
| 30 | + //if ave is the best value, what's the difference to target? |
| 31 | + int closetDiff = findClosestDiffIfReplaceWithVal(arr, ave, target); |
| 32 | + int bestValue = ave; |
| 33 | + //extend candidate towards the right to see how close the sum could be to target |
| 34 | + int candidateOnTheRight = ave; |
| 35 | + while (candidateOnTheRight <= max) { |
| 36 | + int thisOne = findClosestDiffIfReplaceWithVal(arr, ++candidateOnTheRight, target); |
| 37 | + if (thisOne >= closetDiff) { |
| 38 | + break; |
| 39 | + } else { |
| 40 | + closetDiff = thisOne; |
| 41 | + bestValue = candidateOnTheRight; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + //extend candidate towards the left to see how close the sum could be to target |
| 46 | + int candidateOnTheLeft = ave; |
| 47 | + while (candidateOnTheLeft >= min) { |
| 48 | + int thisOne = findClosestDiffIfReplaceWithVal(arr, --candidateOnTheLeft, target); |
| 49 | + if (thisOne >= closetDiff) { |
| 50 | + break; |
| 51 | + } else { |
| 52 | + closetDiff = thisOne; |
| 53 | + bestValue = candidateOnTheLeft; |
| 54 | + } |
| 55 | + } |
| 56 | + return bestValue; |
| 57 | + } |
| 58 | + |
| 59 | + private int findClosestDiffIfReplaceWithVal(int[] arr, int replaceValue, int target) { |
| 60 | + int sum = 0; |
| 61 | + for (int i = 0; i < arr.length; i++) { |
| 62 | + if (arr[i] > replaceValue) { |
| 63 | + sum += replaceValue; |
| 64 | + } else { |
| 65 | + sum += arr[i]; |
| 66 | + } |
| 67 | + } |
| 68 | + return Math.abs(sum - target); |
| 69 | + } |
| 70 | + |
| 71 | + private int findMax(int[] arr) { |
| 72 | + int max = arr[0]; |
| 73 | + for (int i = 1; i < arr.length; i++) { |
| 74 | + max = Math.max(max, arr[i]); |
| 75 | + } |
| 76 | + return max; |
| 77 | + } |
| 78 | + |
| 79 | + private int findMin(int[] arr) { |
| 80 | + int min = arr[0]; |
| 81 | + for (int i = 1; i < arr.length; i++) { |
| 82 | + min = Math.min(min, arr[i]); |
| 83 | + } |
| 84 | + return min; |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments