|
58 | 58 |
|
59 | 59 | ## Solutions
|
60 | 60 |
|
61 |
| -### Solution 1 |
| 61 | +### Solution 1: Greedy + Sorting |
| 62 | + |
| 63 | +First, we sort the array $nums$ and find the position $m$ of the median. The initial number of operations we need is $|nums[m] - k|$. |
| 64 | + |
| 65 | +Next, we discuss in two cases: |
| 66 | + |
| 67 | +- If $nums[m] > k$, then all elements to the right of $m$ are greater than or equal to $k$. We only need to reduce the elements greater than $k$ on the left of $m$ to $k$. |
| 68 | +- If $nums[m] \le k$, then all elements to the left of $m$ are less than or equal to $k$. We only need to increase the elements less than $k$ on the right of $m$ to $k$. |
| 69 | + |
| 70 | +The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the length of the array $nums$. |
62 | 71 |
|
63 | 72 | <!-- tabs:start -->
|
64 | 73 |
|
65 | 74 | ```python
|
66 |
| - |
| 75 | +class Solution: |
| 76 | + def minOperationsToMakeMedianK(self, nums: List[int], k: int) -> int: |
| 77 | + nums.sort() |
| 78 | + n = len(nums) |
| 79 | + m = n >> 1 |
| 80 | + ans = abs(nums[m] - k) |
| 81 | + if nums[m] > k: |
| 82 | + for i in range(m - 1, -1, -1): |
| 83 | + if nums[i] <= k: |
| 84 | + break |
| 85 | + ans += nums[i] - k |
| 86 | + else: |
| 87 | + for i in range(m + 1, n): |
| 88 | + if nums[i] >= k: |
| 89 | + break |
| 90 | + ans += k - nums[i] |
| 91 | + return ans |
67 | 92 | ```
|
68 | 93 |
|
69 | 94 | ```java
|
70 |
| - |
| 95 | +class Solution { |
| 96 | + public long minOperationsToMakeMedianK(int[] nums, int k) { |
| 97 | + Arrays.sort(nums); |
| 98 | + int n = nums.length; |
| 99 | + int m = n >> 1; |
| 100 | + long ans = Math.abs(nums[m] - k); |
| 101 | + if (nums[m] > k) { |
| 102 | + for (int i = m - 1; i >= 0 && nums[i] > k; --i) { |
| 103 | + ans += nums[i] - k; |
| 104 | + } |
| 105 | + } else { |
| 106 | + for (int i = m + 1; i < n && nums[i] < k; ++i) { |
| 107 | + ans += k - nums[i]; |
| 108 | + } |
| 109 | + } |
| 110 | + return ans; |
| 111 | + } |
| 112 | +} |
71 | 113 | ```
|
72 | 114 |
|
73 | 115 | ```cpp
|
74 |
| - |
| 116 | +class Solution { |
| 117 | +public: |
| 118 | + long long minOperationsToMakeMedianK(vector<int>& nums, int k) { |
| 119 | + sort(nums.begin(), nums.end()); |
| 120 | + int n = nums.size(); |
| 121 | + int m = n >> 1; |
| 122 | + long long ans = abs(nums[m] - k); |
| 123 | + if (nums[m] > k) { |
| 124 | + for (int i = m - 1; i >= 0 && nums[i] > k; --i) { |
| 125 | + ans += nums[i] - k; |
| 126 | + } |
| 127 | + } else { |
| 128 | + for (int i = m + 1; i < n && nums[i] < k; ++i) { |
| 129 | + ans += k - nums[i]; |
| 130 | + } |
| 131 | + } |
| 132 | + return ans; |
| 133 | + } |
| 134 | +}; |
75 | 135 | ```
|
76 | 136 |
|
77 | 137 | ```go
|
| 138 | +func minOperationsToMakeMedianK(nums []int, k int) (ans int64) { |
| 139 | + sort.Ints(nums) |
| 140 | + n := len(nums) |
| 141 | + m := n >> 1 |
| 142 | + ans = int64(abs(nums[m] - k)) |
| 143 | + if nums[m] > k { |
| 144 | + for i := m - 1; i >= 0 && nums[i] > k; i-- { |
| 145 | + ans += int64(nums[i] - k) |
| 146 | + } |
| 147 | + } else { |
| 148 | + for i := m + 1; i < n && nums[i] < k; i++ { |
| 149 | + ans += int64(k - nums[i]) |
| 150 | + } |
| 151 | + } |
| 152 | + return |
| 153 | +} |
| 154 | +
|
| 155 | +func abs(x int) int { |
| 156 | + if x < 0 { |
| 157 | + return -x |
| 158 | + } |
| 159 | + return x |
| 160 | +} |
| 161 | +``` |
78 | 162 |
|
| 163 | +```ts |
| 164 | +function minOperationsToMakeMedianK(nums: number[], k: number): number { |
| 165 | + nums.sort((a, b) => a - b); |
| 166 | + const n = nums.length; |
| 167 | + const m = n >> 1; |
| 168 | + let ans = Math.abs(nums[m] - k); |
| 169 | + if (nums[m] > k) { |
| 170 | + for (let i = m - 1; i >= 0 && nums[i] > k; --i) { |
| 171 | + ans += nums[i] - k; |
| 172 | + } |
| 173 | + } else { |
| 174 | + for (let i = m + 1; i < n && nums[i] < k; ++i) { |
| 175 | + ans += k - nums[i]; |
| 176 | + } |
| 177 | + } |
| 178 | + return ans; |
| 179 | +} |
79 | 180 | ```
|
80 | 181 |
|
81 | 182 | <!-- tabs:end -->
|
|
0 commit comments