|
| 1 | +""" |
| 2 | +Problem: Minimum Absolute Sum Difference |
| 3 | +Link: https://leetcode.com/problems/minimum-absolute-sum-difference/ |
| 4 | +
|
| 5 | +You are given two positive integer arrays nums1 and nums2, both of length n. |
| 6 | +
|
| 7 | +The absolute sum difference is sum of |nums1[i] - nums2[i]| for all i. |
| 8 | +
|
| 9 | +You can replace at most one element of nums1 with any other element in nums1 |
| 10 | +to minimize the absolute sum difference. |
| 11 | +
|
| 12 | +Return the minimum absolute sum difference after replacing at most one element. |
| 13 | +Since the answer may be large, return it modulo 10^9 + 7. |
| 14 | +
|
| 15 | +Example 1: |
| 16 | + Input: nums1 = [1,7,5], nums2 = [2,3,5] |
| 17 | + Output: 3 |
| 18 | + Explanation: Replace 7 with 1 or 5 -> |1-2| + |1-3| + |5-5| = 3 |
| 19 | +
|
| 20 | +Example 2: |
| 21 | + Input: nums1 = [2,4,6,8,10], nums2 = [2,4,6,8,10] |
| 22 | + Output: 0 |
| 23 | +
|
| 24 | +Example 3: |
| 25 | + Input: nums1 = [1,10,4,4,2,7], nums2 = [9,3,5,1,7,4] |
| 26 | + Output: 20 |
| 27 | +
|
| 28 | +Constraints: |
| 29 | +- n == nums1.length |
| 30 | +- n == nums2.length |
| 31 | +- 1 <= n <= 10^5 |
| 32 | +- 1 <= nums1[i], nums2[i] <= 10^5 |
| 33 | +
|
| 34 | +Topics: Array, Binary Search, Greedy, Sorting |
| 35 | +""" |
| 36 | +from typing import List |
| 37 | +from bisect import bisect_left |
| 38 | +from _runner import get_solver |
| 39 | + |
| 40 | + |
| 41 | +SOLUTIONS = { |
| 42 | + "default": { |
| 43 | + "class": "Solution", |
| 44 | + "method": "minAbsoluteSumDiff", |
| 45 | + "complexity": "O(n log n) time, O(n) space", |
| 46 | + "description": "Binary search for best replacement at each position", |
| 47 | + }, |
| 48 | +} |
| 49 | + |
| 50 | + |
| 51 | +# ============================================================================ |
| 52 | +# Solution: Binary Search for Best Replacement |
| 53 | +# Time: O(n log n), Space: O(n) |
| 54 | +# |
| 55 | +# Key insight: We can only replace ONE element. For each position i, find |
| 56 | +# the element in nums1 that is closest to nums2[i]. This minimizes the |
| 57 | +# contribution at position i. |
| 58 | +# |
| 59 | +# Strategy: |
| 60 | +# 1. Calculate total absolute sum without any replacement |
| 61 | +# 2. Sort a copy of nums1 for efficient binary search |
| 62 | +# 3. For each position i, find max possible reduction by replacing nums1[i] |
| 63 | +# with the closest value to nums2[i] in sorted nums1 |
| 64 | +# 4. Return (total - max_reduction) % MOD |
| 65 | +# ============================================================================ |
| 66 | +class Solution: |
| 67 | + def minAbsoluteSumDiff(self, nums1: List[int], nums2: List[int]) -> int: |
| 68 | + """ |
| 69 | + Minimize absolute sum difference by replacing at most one element. |
| 70 | +
|
| 71 | + For each position, calculate how much we can reduce the absolute |
| 72 | + difference by replacing with the optimal element from nums1. |
| 73 | + Use binary search on sorted nums1 to find closest values. |
| 74 | +
|
| 75 | + Args: |
| 76 | + nums1: First array (can replace one element) |
| 77 | + nums2: Second array (fixed) |
| 78 | +
|
| 79 | + Returns: |
| 80 | + Minimum absolute sum difference modulo 10^9 + 7 |
| 81 | + """ |
| 82 | + MOD = 10**9 + 7 |
| 83 | + n = len(nums1) |
| 84 | + |
| 85 | + # Calculate original total absolute sum |
| 86 | + total = sum(abs(a - b) for a, b in zip(nums1, nums2)) |
| 87 | + |
| 88 | + # Sort nums1 for binary search |
| 89 | + sorted_nums1 = sorted(nums1) |
| 90 | + |
| 91 | + # Find maximum reduction achievable by replacing one element |
| 92 | + max_reduction = 0 |
| 93 | + |
| 94 | + for i in range(n): |
| 95 | + original_diff = abs(nums1[i] - nums2[i]) |
| 96 | + target = nums2[i] |
| 97 | + |
| 98 | + # Binary search for closest value to target in sorted_nums1 |
| 99 | + pos = bisect_left(sorted_nums1, target) |
| 100 | + |
| 101 | + # Check value at pos (>= target) if exists |
| 102 | + if pos < n: |
| 103 | + new_diff = abs(sorted_nums1[pos] - target) |
| 104 | + reduction = original_diff - new_diff |
| 105 | + max_reduction = max(max_reduction, reduction) |
| 106 | + |
| 107 | + # Check value at pos-1 (< target) if exists |
| 108 | + if pos > 0: |
| 109 | + new_diff = abs(sorted_nums1[pos - 1] - target) |
| 110 | + reduction = original_diff - new_diff |
| 111 | + max_reduction = max(max_reduction, reduction) |
| 112 | + |
| 113 | + return (total - max_reduction) % MOD |
| 114 | + |
| 115 | + |
| 116 | +def solve(): |
| 117 | + """ |
| 118 | + Input format: |
| 119 | + Line 1: nums1 (JSON array) |
| 120 | + Line 2: nums2 (JSON array) |
| 121 | + """ |
| 122 | + import sys |
| 123 | + import json |
| 124 | + |
| 125 | + lines = sys.stdin.read().strip().split('\n') |
| 126 | + |
| 127 | + nums1 = json.loads(lines[0]) |
| 128 | + nums2 = json.loads(lines[1]) |
| 129 | + |
| 130 | + solver = get_solver(SOLUTIONS) |
| 131 | + result = solver.minAbsoluteSumDiff(nums1, nums2) |
| 132 | + |
| 133 | + print(json.dumps(result, separators=(',', ':'))) |
| 134 | + |
| 135 | + |
| 136 | +if __name__ == "__main__": |
| 137 | + solve() |
0 commit comments