Skip to content

feat: add solutions to lc problem: No.2148 #3508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:求最小值和最大值

根据题目描述,我们可以先求出数组 $\textit{nums}$ 的最小值 $\textit{mi}$ 和最大值 $\textit{mx}$,然后遍历数组 $\textit{nums}$,统计满足 $\textit{mi} < x < \textit{mx}$ 的元素个数即可。

时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand All @@ -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;
Expand All @@ -98,57 +98,34 @@ class Solution {
class Solution {
public:
int countElements(vector<int>& 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] = ranges::minmax_element(nums);
return ranges::count_if(nums, [mi, mx](int x) { return *mi < x && x < *mx; });
}
};
```

#### 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
}
```

#### TypeScript

```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;
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ Since there are two elements with the value 3, in total there are 2 elements hav

<!-- solution:start -->

### 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)$.

<!-- tabs:start -->

Expand All @@ -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;
Expand All @@ -96,57 +96,34 @@ class Solution {
class Solution {
public:
int countElements(vector<int>& 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] = ranges::minmax_element(nums);
return ranges::count_if(nums, [mi, mx](int x) { return *mi < x && x < *mx; });
}
};
```

#### 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
}
```

#### TypeScript

```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;
}
```

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
class Solution {
public:
int countElements(vector<int>& 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] = ranges::minmax_element(nums);
return ranges::count_if(nums, [mi, mx](int x) { return *mi < x && x < *mx; });
}
};
};
Original file line number Diff line number Diff line change
@@ -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
}
return
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -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;
}
Loading