From 6543726f8fb260053ad827c488b5770a0d1fea60 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Wed, 11 Sep 2024 08:56:25 +0800 Subject: [PATCH 1/2] feat: add solutions to lc problem: No.2148 No.2148.Count Elements With Strictly Smaller and Greater Elements --- .../README.md | 69 +++++++------------ .../README_EN.md | 69 +++++++------------ .../Solution.cpp | 16 ++--- .../Solution.go | 22 ++---- .../Solution.java | 16 ++--- .../Solution.py | 2 +- .../Solution.ts | 13 +--- 7 files changed, 70 insertions(+), 137 deletions(-) diff --git a/solution/2100-2199/2148.Count Elements With Strictly Smaller and Greater Elements/README.md b/solution/2100-2199/2148.Count Elements With Strictly Smaller and Greater Elements/README.md index 21e6a7623ca42..a6d5202af1a12 100644 --- a/solution/2100-2199/2148.Count Elements With Strictly Smaller and Greater Elements/README.md +++ b/solution/2100-2199/2148.Count Elements With Strictly Smaller and Greater Elements/README.md @@ -57,7 +57,11 @@ tags: -### 方法一 +### 方法一:求最小值和最大值 + +根据题目描述,我们可以先求出数组 $\textit{nums}$ 的最小值 $\textit{mi}$ 和最大值 $\textit{mx}$,然后遍历数组 $\textit{nums}$,统计满足 $\textit{mi} < x < \textit{mx}$ 的元素个数即可。 + +时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。 @@ -67,24 +71,20 @@ tags: class Solution: def countElements(self, nums: List[int]) -> int: mi, mx = min(nums), max(nums) - return sum(mi < num < mx for num in nums) + return sum(mi < x < mx for x in nums) ``` #### Java ```java class Solution { - public int countElements(int[] nums) { - int mi = 1000000, mx = -1000000; - for (int num : nums) { - mi = Math.min(mi, num); - mx = Math.max(mx, num); - } + int mi = Arrays.stream(nums).min().getAsInt(); + int mx = Arrays.stream(nums).max().getAsInt(); int ans = 0; - for (int num : nums) { - if (mi < num && num < mx) { - ++ans; + for (int x : nums) { + if (mi < x && x < mx) { + ans++; } } return ans; @@ -98,16 +98,10 @@ class Solution { class Solution { public: int countElements(vector& nums) { - int mi = 1e6, mx = -1e6; - for (int num : nums) { - mi = min(mi, num); - mx = max(mx, num); - } - int ans = 0; - for (int num : nums) - if (mi < num && num < mx) - ++ans; - return ans; + auto [mi, mx] = std::ranges::minmax_element(nums); + return std::ranges::count_if(nums, [mi, mx](int x) { + return *mi < x && x < *mx; + }); } }; ``` @@ -115,23 +109,15 @@ public: #### Go ```go -func countElements(nums []int) int { - mi, mx := int(1e6), -int(1e6) - for _, num := range nums { - if num < mi { - mi = num - } - if num > mx { - mx = num - } - } - ans := 0 - for _, num := range nums { - if mi < num && num < mx { +func countElements(nums []int) (ans int) { + mi := slices.Min(nums) + mx := slices.Max(nums) + for _, x := range nums { + if mi < x && x < mx { ans++ } } - return ans + return } ``` @@ -139,16 +125,9 @@ func countElements(nums []int) int { ```ts function countElements(nums: number[]): number { - const min = Math.min(...nums), - max = Math.max(...nums); - let ans = 0; - for (let i = 0; i < nums.length; ++i) { - let cur = nums[i]; - if (cur < max && cur > min) { - ++ans; - } - } - return ans; + const mi = Math.min(...nums); + const mx = Math.max(...nums); + return nums.filter(x => mi < x && x < mx).length; } ``` diff --git a/solution/2100-2199/2148.Count Elements With Strictly Smaller and Greater Elements/README_EN.md b/solution/2100-2199/2148.Count Elements With Strictly Smaller and Greater Elements/README_EN.md index bcbba03f2f12b..30a9eda7c7867 100644 --- a/solution/2100-2199/2148.Count Elements With Strictly Smaller and Greater Elements/README_EN.md +++ b/solution/2100-2199/2148.Count Elements With Strictly Smaller and Greater Elements/README_EN.md @@ -55,7 +55,11 @@ Since there are two elements with the value 3, in total there are 2 elements hav -### Solution 1 +### Solution 1: Find Minimum and Maximum Values + +According to the problem description, we can first find the minimum value $\textit{mi}$ and the maximum value $\textit{mx}$ of the array $\textit{nums}$. Then, traverse the array $\textit{nums}$ and count the number of elements that satisfy $\textit{mi} < x < \textit{mx}$. + +The time complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$. @@ -65,24 +69,20 @@ Since there are two elements with the value 3, in total there are 2 elements hav class Solution: def countElements(self, nums: List[int]) -> int: mi, mx = min(nums), max(nums) - return sum(mi < num < mx for num in nums) + return sum(mi < x < mx for x in nums) ``` #### Java ```java class Solution { - public int countElements(int[] nums) { - int mi = 1000000, mx = -1000000; - for (int num : nums) { - mi = Math.min(mi, num); - mx = Math.max(mx, num); - } + int mi = Arrays.stream(nums).min().getAsInt(); + int mx = Arrays.stream(nums).max().getAsInt(); int ans = 0; - for (int num : nums) { - if (mi < num && num < mx) { - ++ans; + for (int x : nums) { + if (mi < x && x < mx) { + ans++; } } return ans; @@ -96,16 +96,10 @@ class Solution { class Solution { public: int countElements(vector& nums) { - int mi = 1e6, mx = -1e6; - for (int num : nums) { - mi = min(mi, num); - mx = max(mx, num); - } - int ans = 0; - for (int num : nums) - if (mi < num && num < mx) - ++ans; - return ans; + auto [mi, mx] = std::ranges::minmax_element(nums); + return std::ranges::count_if(nums, [mi, mx](int x) { + return *mi < x && x < *mx; + }); } }; ``` @@ -113,23 +107,15 @@ public: #### Go ```go -func countElements(nums []int) int { - mi, mx := int(1e6), -int(1e6) - for _, num := range nums { - if num < mi { - mi = num - } - if num > mx { - mx = num - } - } - ans := 0 - for _, num := range nums { - if mi < num && num < mx { +func countElements(nums []int) (ans int) { + mi := slices.Min(nums) + mx := slices.Max(nums) + for _, x := range nums { + if mi < x && x < mx { ans++ } } - return ans + return } ``` @@ -137,16 +123,9 @@ func countElements(nums []int) int { ```ts function countElements(nums: number[]): number { - const min = Math.min(...nums), - max = Math.max(...nums); - let ans = 0; - for (let i = 0; i < nums.length; ++i) { - let cur = nums[i]; - if (cur < max && cur > min) { - ++ans; - } - } - return ans; + const mi = Math.min(...nums); + const mx = Math.max(...nums); + return nums.filter(x => mi < x && x < mx).length; } ``` diff --git a/solution/2100-2199/2148.Count Elements With Strictly Smaller and Greater Elements/Solution.cpp b/solution/2100-2199/2148.Count Elements With Strictly Smaller and Greater Elements/Solution.cpp index cab461850652b..1e6894d6eb0ad 100644 --- a/solution/2100-2199/2148.Count Elements With Strictly Smaller and Greater Elements/Solution.cpp +++ b/solution/2100-2199/2148.Count Elements With Strictly Smaller and Greater Elements/Solution.cpp @@ -1,15 +1,9 @@ class Solution { public: int countElements(vector& nums) { - int mi = 1e6, mx = -1e6; - for (int num : nums) { - mi = min(mi, num); - mx = max(mx, num); - } - int ans = 0; - for (int num : nums) - if (mi < num && num < mx) - ++ans; - return ans; + auto [mi, mx] = std::ranges::minmax_element(nums); + return std::ranges::count_if(nums, [mi, mx](int x) { + return *mi < x && x < *mx; + }); } -}; \ No newline at end of file +}; diff --git a/solution/2100-2199/2148.Count Elements With Strictly Smaller and Greater Elements/Solution.go b/solution/2100-2199/2148.Count Elements With Strictly Smaller and Greater Elements/Solution.go index cbd04ecba1f7f..1efcb0f253fd3 100644 --- a/solution/2100-2199/2148.Count Elements With Strictly Smaller and Greater Elements/Solution.go +++ b/solution/2100-2199/2148.Count Elements With Strictly Smaller and Greater Elements/Solution.go @@ -1,18 +1,10 @@ -func countElements(nums []int) int { - mi, mx := int(1e6), -int(1e6) - for _, num := range nums { - if num < mi { - mi = num - } - if num > mx { - mx = num - } - } - ans := 0 - for _, num := range nums { - if mi < num && num < mx { +func countElements(nums []int) (ans int) { + mi := slices.Min(nums) + mx := slices.Max(nums) + for _, x := range nums { + if mi < x && x < mx { ans++ } } - return ans -} \ No newline at end of file + return +} diff --git a/solution/2100-2199/2148.Count Elements With Strictly Smaller and Greater Elements/Solution.java b/solution/2100-2199/2148.Count Elements With Strictly Smaller and Greater Elements/Solution.java index 1077ac80373d0..5667568136d45 100644 --- a/solution/2100-2199/2148.Count Elements With Strictly Smaller and Greater Elements/Solution.java +++ b/solution/2100-2199/2148.Count Elements With Strictly Smaller and Greater Elements/Solution.java @@ -1,17 +1,13 @@ class Solution { - public int countElements(int[] nums) { - int mi = 1000000, mx = -1000000; - for (int num : nums) { - mi = Math.min(mi, num); - mx = Math.max(mx, num); - } + int mi = Arrays.stream(nums).min().getAsInt(); + int mx = Arrays.stream(nums).max().getAsInt(); int ans = 0; - for (int num : nums) { - if (mi < num && num < mx) { - ++ans; + for (int x : nums) { + if (mi < x && x < mx) { + ans++; } } return ans; } -} \ No newline at end of file +} diff --git a/solution/2100-2199/2148.Count Elements With Strictly Smaller and Greater Elements/Solution.py b/solution/2100-2199/2148.Count Elements With Strictly Smaller and Greater Elements/Solution.py index 2a22dde3b09b5..be718c63e3c88 100644 --- a/solution/2100-2199/2148.Count Elements With Strictly Smaller and Greater Elements/Solution.py +++ b/solution/2100-2199/2148.Count Elements With Strictly Smaller and Greater Elements/Solution.py @@ -1,4 +1,4 @@ class Solution: def countElements(self, nums: List[int]) -> int: mi, mx = min(nums), max(nums) - return sum(mi < num < mx for num in nums) + return sum(mi < x < mx for x in nums) diff --git a/solution/2100-2199/2148.Count Elements With Strictly Smaller and Greater Elements/Solution.ts b/solution/2100-2199/2148.Count Elements With Strictly Smaller and Greater Elements/Solution.ts index 277a9887ad8cf..44d66887226c6 100644 --- a/solution/2100-2199/2148.Count Elements With Strictly Smaller and Greater Elements/Solution.ts +++ b/solution/2100-2199/2148.Count Elements With Strictly Smaller and Greater Elements/Solution.ts @@ -1,12 +1,5 @@ function countElements(nums: number[]): number { - const min = Math.min(...nums), - max = Math.max(...nums); - let ans = 0; - for (let i = 0; i < nums.length; ++i) { - let cur = nums[i]; - if (cur < max && cur > min) { - ++ans; - } - } - return ans; + const mi = Math.min(...nums); + const mx = Math.max(...nums); + return nums.filter(x => mi < x && x < mx).length; } From f8ef1ac34e075e433d92999bfb7f20ed4b4e6884 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Wed, 11 Sep 2024 08:58:58 +0800 Subject: [PATCH 2/2] fix: update --- .../README.md | 6 ++---- .../README_EN.md | 6 ++---- .../Solution.cpp | 6 ++---- 3 files changed, 6 insertions(+), 12 deletions(-) diff --git a/solution/2100-2199/2148.Count Elements With Strictly Smaller and Greater Elements/README.md b/solution/2100-2199/2148.Count Elements With Strictly Smaller and Greater Elements/README.md index a6d5202af1a12..ca31b6a2b0c95 100644 --- a/solution/2100-2199/2148.Count Elements With Strictly Smaller and Greater Elements/README.md +++ b/solution/2100-2199/2148.Count Elements With Strictly Smaller and Greater Elements/README.md @@ -98,10 +98,8 @@ class Solution { class Solution { public: int countElements(vector& nums) { - auto [mi, mx] = std::ranges::minmax_element(nums); - return std::ranges::count_if(nums, [mi, mx](int x) { - return *mi < x && x < *mx; - }); + auto [mi, mx] = ranges::minmax_element(nums); + return ranges::count_if(nums, [mi, mx](int x) { return *mi < x && x < *mx; }); } }; ``` diff --git a/solution/2100-2199/2148.Count Elements With Strictly Smaller and Greater Elements/README_EN.md b/solution/2100-2199/2148.Count Elements With Strictly Smaller and Greater Elements/README_EN.md index 30a9eda7c7867..aac65491f8b49 100644 --- a/solution/2100-2199/2148.Count Elements With Strictly Smaller and Greater Elements/README_EN.md +++ b/solution/2100-2199/2148.Count Elements With Strictly Smaller and Greater Elements/README_EN.md @@ -96,10 +96,8 @@ class Solution { class Solution { public: int countElements(vector& nums) { - auto [mi, mx] = std::ranges::minmax_element(nums); - return std::ranges::count_if(nums, [mi, mx](int x) { - return *mi < x && x < *mx; - }); + auto [mi, mx] = ranges::minmax_element(nums); + return ranges::count_if(nums, [mi, mx](int x) { return *mi < x && x < *mx; }); } }; ``` diff --git a/solution/2100-2199/2148.Count Elements With Strictly Smaller and Greater Elements/Solution.cpp b/solution/2100-2199/2148.Count Elements With Strictly Smaller and Greater Elements/Solution.cpp index 1e6894d6eb0ad..6ff223f98ea0a 100644 --- a/solution/2100-2199/2148.Count Elements With Strictly Smaller and Greater Elements/Solution.cpp +++ b/solution/2100-2199/2148.Count Elements With Strictly Smaller and Greater Elements/Solution.cpp @@ -1,9 +1,7 @@ class Solution { public: int countElements(vector& nums) { - auto [mi, mx] = std::ranges::minmax_element(nums); - return std::ranges::count_if(nums, [mi, mx](int x) { - return *mi < x && x < *mx; - }); + auto [mi, mx] = ranges::minmax_element(nums); + return ranges::count_if(nums, [mi, mx](int x) { return *mi < x && x < *mx; }); } };