From 832ef0d4a359eb6eff9c4a5378924c76489f3258 Mon Sep 17 00:00:00 2001
From: yanglbme <szuyanglb@outlook.com>
Date: Sun, 7 Apr 2024 17:29:03 +0800
Subject: [PATCH] feat: add solutions to lc problem: No.3107

No.3107.Minimum Operations to Make Median of Array Equal to K
---
 .../README.md                                 | 109 +++++++++++++++++-
 .../README_EN.md                              | 109 +++++++++++++++++-
 .../Solution.cpp                              |  19 +++
 .../Solution.go                               |  23 ++++
 .../Solution.java                             |  18 +++
 .../Solution.py                               |  17 +++
 .../Solution.ts                               |  16 +++
 7 files changed, 303 insertions(+), 8 deletions(-)
 create mode 100644 solution/3100-3199/3107.Minimum Operations to Make Median of Array Equal to K/Solution.cpp
 create mode 100644 solution/3100-3199/3107.Minimum Operations to Make Median of Array Equal to K/Solution.go
 create mode 100644 solution/3100-3199/3107.Minimum Operations to Make Median of Array Equal to K/Solution.java
 create mode 100644 solution/3100-3199/3107.Minimum Operations to Make Median of Array Equal to K/Solution.py
 create mode 100644 solution/3100-3199/3107.Minimum Operations to Make Median of Array Equal to K/Solution.ts

diff --git a/solution/3100-3199/3107.Minimum Operations to Make Median of Array Equal to K/README.md b/solution/3100-3199/3107.Minimum Operations to Make Median of Array Equal to K/README.md
index 3835b453045c4..a61515c7658d2 100644
--- a/solution/3100-3199/3107.Minimum Operations to Make Median of Array Equal to K/README.md	
+++ b/solution/3100-3199/3107.Minimum Operations to Make Median of Array Equal to K/README.md	
@@ -60,24 +60,125 @@
 
 ## 解法
 
-### 方法一
+### 方法一:贪心 + 排序
+
+我们首先对数组 $nums$ 进行排序,然后找到中位数的位置 $m$,那么初始时我们需要的操作次数就是 $|nums[m] - k|$。
+
+接下来,我们分情况讨论:
+
+-   如果 $nums[m] \gt k$,那么 $m$ 右侧的元素都大于等于 $k$,我们只需要将 $m$ 左侧的元素中,大于 $k$ 的元素减小到 $k$ 即可。
+-   如果 $nums[m] \le k$,那么 $m$ 左侧的元素都小于等于 $k$,我们只需要将 $m$ 右侧的元素中,小于 $k$ 的元素增大到 $k$ 即可。
+
+时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 是数组 $nums$ 的长度。
 
 <!-- tabs:start -->
 
 ```python
-
+class Solution:
+    def minOperationsToMakeMedianK(self, nums: List[int], k: int) -> int:
+        nums.sort()
+        n = len(nums)
+        m = n >> 1
+        ans = abs(nums[m] - k)
+        if nums[m] > k:
+            for i in range(m - 1, -1, -1):
+                if nums[i] <= k:
+                    break
+                ans += nums[i] - k
+        else:
+            for i in range(m + 1, n):
+                if nums[i] >= k:
+                    break
+                ans += k - nums[i]
+        return ans
 ```
 
 ```java
-
+class Solution {
+    public long minOperationsToMakeMedianK(int[] nums, int k) {
+        Arrays.sort(nums);
+        int n = nums.length;
+        int m = n >> 1;
+        long ans = Math.abs(nums[m] - k);
+        if (nums[m] > k) {
+            for (int i = m - 1; i >= 0 && nums[i] > k; --i) {
+                ans += nums[i] - k;
+            }
+        } else {
+            for (int i = m + 1; i < n && nums[i] < k; ++i) {
+                ans += k - nums[i];
+            }
+        }
+        return ans;
+    }
+}
 ```
 
 ```cpp
-
+class Solution {
+public:
+    long long minOperationsToMakeMedianK(vector<int>& nums, int k) {
+        sort(nums.begin(), nums.end());
+        int n = nums.size();
+        int m = n >> 1;
+        long long ans = abs(nums[m] - k);
+        if (nums[m] > k) {
+            for (int i = m - 1; i >= 0 && nums[i] > k; --i) {
+                ans += nums[i] - k;
+            }
+        } else {
+            for (int i = m + 1; i < n && nums[i] < k; ++i) {
+                ans += k - nums[i];
+            }
+        }
+        return ans;
+    }
+};
 ```
 
 ```go
+func minOperationsToMakeMedianK(nums []int, k int) (ans int64) {
+	sort.Ints(nums)
+	n := len(nums)
+	m := n >> 1
+	ans = int64(abs(nums[m] - k))
+	if nums[m] > k {
+		for i := m - 1; i >= 0 && nums[i] > k; i-- {
+			ans += int64(nums[i] - k)
+		}
+	} else {
+		for i := m + 1; i < n && nums[i] < k; i++ {
+			ans += int64(k - nums[i])
+		}
+	}
+	return
+}
+
+func abs(x int) int {
+	if x < 0 {
+		return -x
+	}
+	return x
+}
+```
 
+```ts
+function minOperationsToMakeMedianK(nums: number[], k: number): number {
+    nums.sort((a, b) => a - b);
+    const n = nums.length;
+    const m = n >> 1;
+    let ans = Math.abs(nums[m] - k);
+    if (nums[m] > k) {
+        for (let i = m - 1; i >= 0 && nums[i] > k; --i) {
+            ans += nums[i] - k;
+        }
+    } else {
+        for (let i = m + 1; i < n && nums[i] < k; ++i) {
+            ans += k - nums[i];
+        }
+    }
+    return ans;
+}
 ```
 
 <!-- tabs:end -->
diff --git a/solution/3100-3199/3107.Minimum Operations to Make Median of Array Equal to K/README_EN.md b/solution/3100-3199/3107.Minimum Operations to Make Median of Array Equal to K/README_EN.md
index 121032df5ebd3..ca4e8ddda30ca 100644
--- a/solution/3100-3199/3107.Minimum Operations to Make Median of Array Equal to K/README_EN.md	
+++ b/solution/3100-3199/3107.Minimum Operations to Make Median of Array Equal to K/README_EN.md	
@@ -58,24 +58,125 @@
 
 ## Solutions
 
-### Solution 1
+### Solution 1: Greedy + Sorting
+
+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|$.
+
+Next, we discuss in two cases:
+
+-   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$.
+-   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$.
+
+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$.
 
 <!-- tabs:start -->
 
 ```python
-
+class Solution:
+    def minOperationsToMakeMedianK(self, nums: List[int], k: int) -> int:
+        nums.sort()
+        n = len(nums)
+        m = n >> 1
+        ans = abs(nums[m] - k)
+        if nums[m] > k:
+            for i in range(m - 1, -1, -1):
+                if nums[i] <= k:
+                    break
+                ans += nums[i] - k
+        else:
+            for i in range(m + 1, n):
+                if nums[i] >= k:
+                    break
+                ans += k - nums[i]
+        return ans
 ```
 
 ```java
-
+class Solution {
+    public long minOperationsToMakeMedianK(int[] nums, int k) {
+        Arrays.sort(nums);
+        int n = nums.length;
+        int m = n >> 1;
+        long ans = Math.abs(nums[m] - k);
+        if (nums[m] > k) {
+            for (int i = m - 1; i >= 0 && nums[i] > k; --i) {
+                ans += nums[i] - k;
+            }
+        } else {
+            for (int i = m + 1; i < n && nums[i] < k; ++i) {
+                ans += k - nums[i];
+            }
+        }
+        return ans;
+    }
+}
 ```
 
 ```cpp
-
+class Solution {
+public:
+    long long minOperationsToMakeMedianK(vector<int>& nums, int k) {
+        sort(nums.begin(), nums.end());
+        int n = nums.size();
+        int m = n >> 1;
+        long long ans = abs(nums[m] - k);
+        if (nums[m] > k) {
+            for (int i = m - 1; i >= 0 && nums[i] > k; --i) {
+                ans += nums[i] - k;
+            }
+        } else {
+            for (int i = m + 1; i < n && nums[i] < k; ++i) {
+                ans += k - nums[i];
+            }
+        }
+        return ans;
+    }
+};
 ```
 
 ```go
+func minOperationsToMakeMedianK(nums []int, k int) (ans int64) {
+	sort.Ints(nums)
+	n := len(nums)
+	m := n >> 1
+	ans = int64(abs(nums[m] - k))
+	if nums[m] > k {
+		for i := m - 1; i >= 0 && nums[i] > k; i-- {
+			ans += int64(nums[i] - k)
+		}
+	} else {
+		for i := m + 1; i < n && nums[i] < k; i++ {
+			ans += int64(k - nums[i])
+		}
+	}
+	return
+}
+
+func abs(x int) int {
+	if x < 0 {
+		return -x
+	}
+	return x
+}
+```
 
+```ts
+function minOperationsToMakeMedianK(nums: number[], k: number): number {
+    nums.sort((a, b) => a - b);
+    const n = nums.length;
+    const m = n >> 1;
+    let ans = Math.abs(nums[m] - k);
+    if (nums[m] > k) {
+        for (let i = m - 1; i >= 0 && nums[i] > k; --i) {
+            ans += nums[i] - k;
+        }
+    } else {
+        for (let i = m + 1; i < n && nums[i] < k; ++i) {
+            ans += k - nums[i];
+        }
+    }
+    return ans;
+}
 ```
 
 <!-- tabs:end -->
diff --git a/solution/3100-3199/3107.Minimum Operations to Make Median of Array Equal to K/Solution.cpp b/solution/3100-3199/3107.Minimum Operations to Make Median of Array Equal to K/Solution.cpp
new file mode 100644
index 0000000000000..3a037af1c0727
--- /dev/null
+++ b/solution/3100-3199/3107.Minimum Operations to Make Median of Array Equal to K/Solution.cpp	
@@ -0,0 +1,19 @@
+class Solution {
+public:
+    long long minOperationsToMakeMedianK(vector<int>& nums, int k) {
+        sort(nums.begin(), nums.end());
+        int n = nums.size();
+        int m = n >> 1;
+        long long ans = abs(nums[m] - k);
+        if (nums[m] > k) {
+            for (int i = m - 1; i >= 0 && nums[i] > k; --i) {
+                ans += nums[i] - k;
+            }
+        } else {
+            for (int i = m + 1; i < n && nums[i] < k; ++i) {
+                ans += k - nums[i];
+            }
+        }
+        return ans;
+    }
+};
\ No newline at end of file
diff --git a/solution/3100-3199/3107.Minimum Operations to Make Median of Array Equal to K/Solution.go b/solution/3100-3199/3107.Minimum Operations to Make Median of Array Equal to K/Solution.go
new file mode 100644
index 0000000000000..6185bf2b8e399
--- /dev/null
+++ b/solution/3100-3199/3107.Minimum Operations to Make Median of Array Equal to K/Solution.go	
@@ -0,0 +1,23 @@
+func minOperationsToMakeMedianK(nums []int, k int) (ans int64) {
+	sort.Ints(nums)
+	n := len(nums)
+	m := n >> 1
+	ans = int64(abs(nums[m] - k))
+	if nums[m] > k {
+		for i := m - 1; i >= 0 && nums[i] > k; i-- {
+			ans += int64(nums[i] - k)
+		}
+	} else {
+		for i := m + 1; i < n && nums[i] < k; i++ {
+			ans += int64(k - nums[i])
+		}
+	}
+	return
+}
+
+func abs(x int) int {
+	if x < 0 {
+		return -x
+	}
+	return x
+}
\ No newline at end of file
diff --git a/solution/3100-3199/3107.Minimum Operations to Make Median of Array Equal to K/Solution.java b/solution/3100-3199/3107.Minimum Operations to Make Median of Array Equal to K/Solution.java
new file mode 100644
index 0000000000000..ce1191d540b66
--- /dev/null
+++ b/solution/3100-3199/3107.Minimum Operations to Make Median of Array Equal to K/Solution.java	
@@ -0,0 +1,18 @@
+class Solution {
+    public long minOperationsToMakeMedianK(int[] nums, int k) {
+        Arrays.sort(nums);
+        int n = nums.length;
+        int m = n >> 1;
+        long ans = Math.abs(nums[m] - k);
+        if (nums[m] > k) {
+            for (int i = m - 1; i >= 0 && nums[i] > k; --i) {
+                ans += nums[i] - k;
+            }
+        } else {
+            for (int i = m + 1; i < n && nums[i] < k; ++i) {
+                ans += k - nums[i];
+            }
+        }
+        return ans;
+    }
+}
\ No newline at end of file
diff --git a/solution/3100-3199/3107.Minimum Operations to Make Median of Array Equal to K/Solution.py b/solution/3100-3199/3107.Minimum Operations to Make Median of Array Equal to K/Solution.py
new file mode 100644
index 0000000000000..0374b49e4590a
--- /dev/null
+++ b/solution/3100-3199/3107.Minimum Operations to Make Median of Array Equal to K/Solution.py	
@@ -0,0 +1,17 @@
+class Solution:
+    def minOperationsToMakeMedianK(self, nums: List[int], k: int) -> int:
+        nums.sort()
+        n = len(nums)
+        m = n >> 1
+        ans = abs(nums[m] - k)
+        if nums[m] > k:
+            for i in range(m - 1, -1, -1):
+                if nums[i] <= k:
+                    break
+                ans += nums[i] - k
+        else:
+            for i in range(m + 1, n):
+                if nums[i] >= k:
+                    break
+                ans += k - nums[i]
+        return ans
diff --git a/solution/3100-3199/3107.Minimum Operations to Make Median of Array Equal to K/Solution.ts b/solution/3100-3199/3107.Minimum Operations to Make Median of Array Equal to K/Solution.ts
new file mode 100644
index 0000000000000..e8605cac83f35
--- /dev/null
+++ b/solution/3100-3199/3107.Minimum Operations to Make Median of Array Equal to K/Solution.ts	
@@ -0,0 +1,16 @@
+function minOperationsToMakeMedianK(nums: number[], k: number): number {
+    nums.sort((a, b) => a - b);
+    const n = nums.length;
+    const m = n >> 1;
+    let ans = Math.abs(nums[m] - k);
+    if (nums[m] > k) {
+        for (let i = m - 1; i >= 0 && nums[i] > k; --i) {
+            ans += nums[i] - k;
+        }
+    } else {
+        for (let i = m + 1; i < n && nums[i] < k; ++i) {
+            ans += k - nums[i];
+        }
+    }
+    return ans;
+}