From 9497eba3264bc667a24049b0179b6c6fe321992e Mon Sep 17 00:00:00 2001 From: imp Date: Fri, 24 Sep 2021 00:16:17 +0800 Subject: [PATCH 1/3] feat: add cpp solution to lc problem: No.2009 Minimum Number of Operations to Make Array --- .../README.md | 53 ++++++++++++++++++- .../README_EN.md | 53 ++++++++++++++++++- .../Solution.cpp | 15 ++++++ 3 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous/Solution.cpp diff --git a/solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous/README.md b/solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous/README.md index 01f9d06518802..15fb32e4965c5 100644 --- a/solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous/README.md +++ b/solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous/README.md @@ -68,7 +68,18 @@ ```python - +class Solution: + def minOperations(self, nums: List[int]) -> int: + n = len(nums) + nums = sorted(set(nums)) + + ans = n + for i, start in enumerate(nums): + end = start + n - 1 + j = bisect_right(nums, end) + remainLen = j - i + ans = min(ans, n - remainLen) + return ans ``` ### **Java** @@ -76,7 +87,47 @@ ```java +class Solution { + public int minOperations(int[] nums) { + int N = nums.length; + if (N == 1) return 0; + Arrays.sort(nums); + int M = 1; + for (int i = 1; i < N; i++) { + if (nums[i] != nums[i - 1]) + nums[M++] = nums[i]; + } + + int j = 0; + int ans = N; + for (int i = 0; i < M; i++) { + while (j < M && nums[j] <= N + nums[i] - 1) j++; + ans = Math.min(ans, N - j + i); + } + + return ans; + } +} +``` +### **C++** + +```cpp +class Solution { +public: + int minOperations(vector& nums) { + sort(nums.begin(), nums.end()); + int End = unique(nums.begin(), nums.end()) - nums.begin(); + int n = nums.size(); + + int len = 0; + for (int i = 0; i < End; ++i) { + int temp = upper_bound(nums.begin(), nums.begin() + End, n + nums[i] - 1) - nums.begin() - i; + len = max(len, temp); + } + return n - len; + } +}; ``` ### **...** diff --git a/solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous/README_EN.md b/solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous/README_EN.md index 4bc90ba92d21d..31f964dda00f6 100644 --- a/solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous/README_EN.md +++ b/solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous/README_EN.md @@ -63,13 +63,64 @@ The resulting array is [1,2,3,4], which is continuous. ### **Python3** ```python - +class Solution: + def minOperations(self, nums: List[int]) -> int: + n = len(nums) + nums = sorted(set(nums)) + + ans = n + for i, start in enumerate(nums): + end = start + n - 1 + j = bisect_right(nums, end) + remainLen = j - i + ans = min(ans, n - remainLen) + return ans ``` ### **Java** ```java +class Solution { + public int minOperations(int[] nums) { + int N = nums.length; + if (N == 1) return 0; + Arrays.sort(nums); + int M = 1; + for (int i = 1; i < N; i++) { + if (nums[i] != nums[i - 1]) + nums[M++] = nums[i]; + } + + int j = 0; + int ans = N; + for (int i = 0; i < M; i++) { + while (j < M && nums[j] <= N + nums[i] - 1) j++; + ans = Math.min(ans, N - j + i); + } + + return ans; + } +} +``` +### **C++** + +```cpp +class Solution { +public: + int minOperations(vector& nums) { + sort(nums.begin(), nums.end()); + int End = unique(nums.begin(), nums.end()) - nums.begin(); + int n = nums.size(); + + int len = 0; + for (int i = 0; i < End; ++i) { + int temp = upper_bound(nums.begin(), nums.begin() + End, n + nums[i] - 1) - nums.begin() - i; + len = max(len, temp); + } + return n - len; + } +}; ``` ### **...** diff --git a/solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous/Solution.cpp b/solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous/Solution.cpp new file mode 100644 index 0000000000000..55854c4811c81 --- /dev/null +++ b/solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous/Solution.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + int minOperations(vector& nums) { + sort(nums.begin(), nums.end()); + int End = unique(nums.begin(), nums.end()) - nums.begin(); + int n = nums.size(); + + int len = 0; + for (int i = 0; i < End; ++i) { + int temp = upper_bound(nums.begin(), nums.begin() + End, n + nums[i] - 1) - nums.begin() - i; + len = max(len, temp); + } + return n - len; + } +}; From a48db535f0352106b1447c55e7479a4f17b26488 Mon Sep 17 00:00:00 2001 From: imp Date: Fri, 24 Sep 2021 00:16:26 +0800 Subject: [PATCH 2/3] feat: add java solution to lc problem: No.2009 Minimum Number of Operations to Make Array --- .../Solution.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous/Solution.java diff --git a/solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous/Solution.java b/solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous/Solution.java new file mode 100644 index 0000000000000..e1ea771c11079 --- /dev/null +++ b/solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous/Solution.java @@ -0,0 +1,22 @@ +class Solution { + public int minOperations(int[] nums) { + int N = nums.length; + if (N == 1) return 0; + Arrays.sort(nums); + int M = 1; + for (int i = 1; i < N; i++) { + if (nums[i] != nums[i - 1]) + nums[M++] = nums[i]; + } + + int j = 0; + int ans = N; + for (int i = 0; i < M; i++) { + while (j < M && nums[j] <= N + nums[i] - 1) + j++; + ans = Math.min(ans, N - j + i); + } + + return ans; + } +} From 23c5f83ff55742cb7f8df26f20d070a401a2fe67 Mon Sep 17 00:00:00 2001 From: imp Date: Fri, 24 Sep 2021 00:16:39 +0800 Subject: [PATCH 3/3] feat: add python3 solution to lc problem: No.2009 Minimum Number of Operations to Make Array --- .../Solution.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous/Solution.py diff --git a/solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous/Solution.py b/solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous/Solution.py new file mode 100644 index 0000000000000..22d8d24569ceb --- /dev/null +++ b/solution/2000-2099/2009.Minimum Number of Operations to Make Array Continuous/Solution.py @@ -0,0 +1,12 @@ +class Solution: + def minOperations(self, nums: List[int]) -> int: + n = len(nums) + nums = sorted(set(nums)) + + ans = n + for i, start in enumerate(nums): + end = start + n - 1 + j = bisect_right(nums, end) + remainLen = j - i + ans = min(ans, n - remainLen) + return ans