Skip to content

Commit 4ef0040

Browse files
authored
feat: add solutions to lc problem: No.2148 (doocs#3508)
No.2148.Count Elements With Strictly Smaller and Greater Elements
1 parent 2e81370 commit 4ef0040

File tree

7 files changed

+64
-137
lines changed

7 files changed

+64
-137
lines changed

solution/2100-2199/2148.Count Elements With Strictly Smaller and Greater Elements/README.md

Lines changed: 22 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ tags:
5757

5858
<!-- solution:start -->
5959

60-
### 方法一
60+
### 方法一:求最小值和最大值
61+
62+
根据题目描述,我们可以先求出数组 $\textit{nums}$ 的最小值 $\textit{mi}$ 和最大值 $\textit{mx}$,然后遍历数组 $\textit{nums}$,统计满足 $\textit{mi} < x < \textit{mx}$ 的元素个数即可。
63+
64+
时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。
6165

6266
<!-- tabs:start -->
6367

@@ -67,24 +71,20 @@ tags:
6771
class Solution:
6872
def countElements(self, nums: List[int]) -> int:
6973
mi, mx = min(nums), max(nums)
70-
return sum(mi < num < mx for num in nums)
74+
return sum(mi < x < mx for x in nums)
7175
```
7276

7377
#### Java
7478

7579
```java
7680
class Solution {
77-
7881
public int countElements(int[] nums) {
79-
int mi = 1000000, mx = -1000000;
80-
for (int num : nums) {
81-
mi = Math.min(mi, num);
82-
mx = Math.max(mx, num);
83-
}
82+
int mi = Arrays.stream(nums).min().getAsInt();
83+
int mx = Arrays.stream(nums).max().getAsInt();
8484
int ans = 0;
85-
for (int num : nums) {
86-
if (mi < num && num < mx) {
87-
++ans;
85+
for (int x : nums) {
86+
if (mi < x && x < mx) {
87+
ans++;
8888
}
8989
}
9090
return ans;
@@ -98,57 +98,34 @@ class Solution {
9898
class Solution {
9999
public:
100100
int countElements(vector<int>& nums) {
101-
int mi = 1e6, mx = -1e6;
102-
for (int num : nums) {
103-
mi = min(mi, num);
104-
mx = max(mx, num);
105-
}
106-
int ans = 0;
107-
for (int num : nums)
108-
if (mi < num && num < mx)
109-
++ans;
110-
return ans;
101+
auto [mi, mx] = ranges::minmax_element(nums);
102+
return ranges::count_if(nums, [mi, mx](int x) { return *mi < x && x < *mx; });
111103
}
112104
};
113105
```
114106
115107
#### Go
116108
117109
```go
118-
func countElements(nums []int) int {
119-
mi, mx := int(1e6), -int(1e6)
120-
for _, num := range nums {
121-
if num < mi {
122-
mi = num
123-
}
124-
if num > mx {
125-
mx = num
126-
}
127-
}
128-
ans := 0
129-
for _, num := range nums {
130-
if mi < num && num < mx {
110+
func countElements(nums []int) (ans int) {
111+
mi := slices.Min(nums)
112+
mx := slices.Max(nums)
113+
for _, x := range nums {
114+
if mi < x && x < mx {
131115
ans++
132116
}
133117
}
134-
return ans
118+
return
135119
}
136120
```
137121

138122
#### TypeScript
139123

140124
```ts
141125
function countElements(nums: number[]): number {
142-
const min = Math.min(...nums),
143-
max = Math.max(...nums);
144-
let ans = 0;
145-
for (let i = 0; i < nums.length; ++i) {
146-
let cur = nums[i];
147-
if (cur < max && cur > min) {
148-
++ans;
149-
}
150-
}
151-
return ans;
126+
const mi = Math.min(...nums);
127+
const mx = Math.max(...nums);
128+
return nums.filter(x => mi < x && x < mx).length;
152129
}
153130
```
154131

solution/2100-2199/2148.Count Elements With Strictly Smaller and Greater Elements/README_EN.md

Lines changed: 22 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ Since there are two elements with the value 3, in total there are 2 elements hav
5555

5656
<!-- solution:start -->
5757

58-
### Solution 1
58+
### Solution 1: Find Minimum and Maximum Values
59+
60+
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}$.
61+
62+
The time complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$.
5963

6064
<!-- tabs:start -->
6165

@@ -65,24 +69,20 @@ Since there are two elements with the value 3, in total there are 2 elements hav
6569
class Solution:
6670
def countElements(self, nums: List[int]) -> int:
6771
mi, mx = min(nums), max(nums)
68-
return sum(mi < num < mx for num in nums)
72+
return sum(mi < x < mx for x in nums)
6973
```
7074

7175
#### Java
7276

7377
```java
7478
class Solution {
75-
7679
public int countElements(int[] nums) {
77-
int mi = 1000000, mx = -1000000;
78-
for (int num : nums) {
79-
mi = Math.min(mi, num);
80-
mx = Math.max(mx, num);
81-
}
80+
int mi = Arrays.stream(nums).min().getAsInt();
81+
int mx = Arrays.stream(nums).max().getAsInt();
8282
int ans = 0;
83-
for (int num : nums) {
84-
if (mi < num && num < mx) {
85-
++ans;
83+
for (int x : nums) {
84+
if (mi < x && x < mx) {
85+
ans++;
8686
}
8787
}
8888
return ans;
@@ -96,57 +96,34 @@ class Solution {
9696
class Solution {
9797
public:
9898
int countElements(vector<int>& nums) {
99-
int mi = 1e6, mx = -1e6;
100-
for (int num : nums) {
101-
mi = min(mi, num);
102-
mx = max(mx, num);
103-
}
104-
int ans = 0;
105-
for (int num : nums)
106-
if (mi < num && num < mx)
107-
++ans;
108-
return ans;
99+
auto [mi, mx] = ranges::minmax_element(nums);
100+
return ranges::count_if(nums, [mi, mx](int x) { return *mi < x && x < *mx; });
109101
}
110102
};
111103
```
112104
113105
#### Go
114106
115107
```go
116-
func countElements(nums []int) int {
117-
mi, mx := int(1e6), -int(1e6)
118-
for _, num := range nums {
119-
if num < mi {
120-
mi = num
121-
}
122-
if num > mx {
123-
mx = num
124-
}
125-
}
126-
ans := 0
127-
for _, num := range nums {
128-
if mi < num && num < mx {
108+
func countElements(nums []int) (ans int) {
109+
mi := slices.Min(nums)
110+
mx := slices.Max(nums)
111+
for _, x := range nums {
112+
if mi < x && x < mx {
129113
ans++
130114
}
131115
}
132-
return ans
116+
return
133117
}
134118
```
135119

136120
#### TypeScript
137121

138122
```ts
139123
function countElements(nums: number[]): number {
140-
const min = Math.min(...nums),
141-
max = Math.max(...nums);
142-
let ans = 0;
143-
for (let i = 0; i < nums.length; ++i) {
144-
let cur = nums[i];
145-
if (cur < max && cur > min) {
146-
++ans;
147-
}
148-
}
149-
return ans;
124+
const mi = Math.min(...nums);
125+
const mx = Math.max(...nums);
126+
return nums.filter(x => mi < x && x < mx).length;
150127
}
151128
```
152129

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
11
class Solution {
22
public:
33
int countElements(vector<int>& nums) {
4-
int mi = 1e6, mx = -1e6;
5-
for (int num : nums) {
6-
mi = min(mi, num);
7-
mx = max(mx, num);
8-
}
9-
int ans = 0;
10-
for (int num : nums)
11-
if (mi < num && num < mx)
12-
++ans;
13-
return ans;
4+
auto [mi, mx] = ranges::minmax_element(nums);
5+
return ranges::count_if(nums, [mi, mx](int x) { return *mi < x && x < *mx; });
146
}
15-
};
7+
};
Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
1-
func countElements(nums []int) int {
2-
mi, mx := int(1e6), -int(1e6)
3-
for _, num := range nums {
4-
if num < mi {
5-
mi = num
6-
}
7-
if num > mx {
8-
mx = num
9-
}
10-
}
11-
ans := 0
12-
for _, num := range nums {
13-
if mi < num && num < mx {
1+
func countElements(nums []int) (ans int) {
2+
mi := slices.Min(nums)
3+
mx := slices.Max(nums)
4+
for _, x := range nums {
5+
if mi < x && x < mx {
146
ans++
157
}
168
}
17-
return ans
18-
}
9+
return
10+
}
Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
class Solution {
2-
32
public int countElements(int[] nums) {
4-
int mi = 1000000, mx = -1000000;
5-
for (int num : nums) {
6-
mi = Math.min(mi, num);
7-
mx = Math.max(mx, num);
8-
}
3+
int mi = Arrays.stream(nums).min().getAsInt();
4+
int mx = Arrays.stream(nums).max().getAsInt();
95
int ans = 0;
10-
for (int num : nums) {
11-
if (mi < num && num < mx) {
12-
++ans;
6+
for (int x : nums) {
7+
if (mi < x && x < mx) {
8+
ans++;
139
}
1410
}
1511
return ans;
1612
}
17-
}
13+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
class Solution:
22
def countElements(self, nums: List[int]) -> int:
33
mi, mx = min(nums), max(nums)
4-
return sum(mi < num < mx for num in nums)
4+
return sum(mi < x < mx for x in nums)
Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
function countElements(nums: number[]): number {
2-
const min = Math.min(...nums),
3-
max = Math.max(...nums);
4-
let ans = 0;
5-
for (let i = 0; i < nums.length; ++i) {
6-
let cur = nums[i];
7-
if (cur < max && cur > min) {
8-
++ans;
9-
}
10-
}
11-
return ans;
2+
const mi = Math.min(...nums);
3+
const mx = Math.max(...nums);
4+
return nums.filter(x => mi < x && x < mx).length;
125
}

0 commit comments

Comments
 (0)