Skip to content

Commit e6ccdf9

Browse files
committed
feat: add solutions to lc problem: No.1365
No.1365.How Many Numbers Are Smaller Than the Current Number
1 parent cfc58ab commit e6ccdf9

File tree

6 files changed

+283
-52
lines changed

6 files changed

+283
-52
lines changed

Diff for: solution/1300-1399/1365.How Many Numbers Are Smaller Than the Current Number/README.md

+126-18
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,19 @@
5151

5252
<!-- 这里可写通用的实现逻辑 -->
5353

54-
计数排序,求前缀和。
54+
**方法一:排序 + 二分查找**
55+
56+
我们可以将数组 $nums$ 复制一份,记为 $arr$,然后对 $arr$ 进行升序排序。
57+
58+
接下来,对于 $nums$ 中的每个元素 $x$,我们可以通过二分查找的方法找到第一个大于等于 $x$ 的元素的下标 $j$,那么 $j$ 就是比 $x$ 小的元素的个数,我们将 $j$ 存入答案数组中即可。
59+
60+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $nums$ 的长度。
61+
62+
**方法二:计数排序 + 前缀和**
63+
64+
我们注意到数组 $nums$ 中的元素的范围是 $[0, 100]$,因此我们可以使用计数排序的方法,先统计数组 $nums$ 中每个元素的个数。然后对计数数组进行前缀和计算,最后遍历数组 $nums$,对于每个元素 $x$,我们直接将计数数组中下标为 $x$ 的元素的值加入答案数组即可。
65+
66+
时间复杂度 $O(n + M)$,空间复杂度 $O(M)$,其中 $n$ 和 $M$ 分别是数组 $nums$ 的长度和最大值。
5567

5668
<!-- tabs:start -->
5769

@@ -62,15 +74,18 @@
6274
```python
6375
class Solution:
6476
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
65-
cnt = [0] * 101
66-
for num in nums:
67-
cnt[num] += 1
68-
for i in range(1, 101):
69-
cnt[i] += cnt[i - 1]
70-
res = []
71-
for num in nums:
72-
res.append(0 if num == 0 else cnt[num - 1])
73-
return res
77+
arr = sorted(nums)
78+
return [bisect_left(arr, x) for x in nums]
79+
```
80+
81+
```python
82+
class Solution:
83+
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
84+
cnt = [0] * 102
85+
for x in nums:
86+
cnt[x + 1] += 1
87+
s = list(accumulate(cnt))
88+
return [s[x] for x in nums]
7489
```
7590

7691
### **Java**
@@ -80,19 +95,112 @@ class Solution:
8095
```java
8196
class Solution {
8297
public int[] smallerNumbersThanCurrent(int[] nums) {
83-
int[] cnt = new int[101];
84-
for (int e : nums) {
85-
++cnt[e];
98+
int[] arr = nums.clone();
99+
Arrays.sort(arr);
100+
for (int i = 0; i < nums.length; ++i) {
101+
nums[i] = search(arr, nums[i]);
102+
}
103+
return nums;
104+
}
105+
106+
private int search(int[] nums, int x) {
107+
int l = 0, r = nums.length;
108+
while (l < r) {
109+
int mid = (l + r) >> 1;
110+
if (nums[mid] >= x) {
111+
r = mid;
112+
} else {
113+
l = mid + 1;
114+
}
115+
}
116+
return l;
117+
}
118+
}
119+
```
120+
121+
```java
122+
class Solution {
123+
public int[] smallerNumbersThanCurrent(int[] nums) {
124+
int[] cnt = new int[102];
125+
for (int x : nums) {
126+
++cnt[x + 1];
86127
}
87-
for (int i = 1; i < 101; ++i) {
128+
for (int i = 1; i < cnt.length; ++i) {
88129
cnt[i] += cnt[i - 1];
89130
}
90-
int[] res = new int[nums.length];
91-
for (int i = 0; i < nums.length; ++i) {
92-
res[i] = nums[i] == 0 ? 0 : cnt[nums[i] - 1];
131+
int n = nums.length;
132+
int[] ans = new int[n];
133+
for (int i = 0; i < n; ++i) {
134+
ans[i] = cnt[nums[i]];
135+
}
136+
return ans;
137+
}
138+
}
139+
```
140+
141+
### **C++**
142+
143+
```cpp
144+
class Solution {
145+
public:
146+
vector<int> smallerNumbersThanCurrent(vector<int>& nums) {
147+
vector<int> arr = nums;
148+
sort(arr.begin(), arr.end());
149+
for (int i = 0; i < nums.size(); ++i) {
150+
nums[i] = lower_bound(arr.begin(), arr.end(), nums[i]) - arr.begin();
151+
}
152+
return nums;
153+
}
154+
};
155+
```
156+
157+
```cpp
158+
class Solution {
159+
public:
160+
vector<int> smallerNumbersThanCurrent(vector<int>& nums) {
161+
int cnt[102]{};
162+
for (int& x : nums) {
163+
++cnt[x + 1];
164+
}
165+
for (int i = 1; i < 102; ++i) {
166+
cnt[i] += cnt[i - 1];
167+
}
168+
vector<int> ans;
169+
for (int& x : nums) {
170+
ans.push_back(cnt[x]);
93171
}
94-
return res;
172+
return ans;
95173
}
174+
};
175+
```
176+
177+
### **Go**
178+
179+
```go
180+
func smallerNumbersThanCurrent(nums []int) (ans []int) {
181+
arr := make([]int, len(nums))
182+
copy(arr, nums)
183+
sort.Ints(arr)
184+
for i, x := range nums {
185+
nums[i] = sort.SearchInts(arr, x)
186+
}
187+
return nums
188+
}
189+
```
190+
191+
```go
192+
func smallerNumbersThanCurrent(nums []int) (ans []int) {
193+
cnt := [102]int{}
194+
for _, x := range nums {
195+
cnt[x+1]++
196+
}
197+
for i := 1; i < len(cnt); i++ {
198+
cnt[i] += cnt[i-1]
199+
}
200+
for _, x := range nums {
201+
ans = append(ans, cnt[x])
202+
}
203+
return
96204
}
97205
```
98206

Diff for: solution/1300-1399/1365.How Many Numbers Are Smaller Than the Current Number/README_EN.md

+113-17
Original file line numberDiff line numberDiff line change
@@ -53,35 +53,131 @@ For nums[4]=3 there exist three smaller numbers than it (1, 2 and 2).
5353
```python
5454
class Solution:
5555
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
56-
cnt = [0] * 101
57-
for num in nums:
58-
cnt[num] += 1
59-
for i in range(1, 101):
60-
cnt[i] += cnt[i - 1]
61-
res = []
62-
for num in nums:
63-
res.append(0 if num == 0 else cnt[num - 1])
64-
return res
56+
arr = sorted(nums)
57+
return [bisect_left(arr, x) for x in nums]
58+
```
59+
60+
```python
61+
class Solution:
62+
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
63+
cnt = [0] * 102
64+
for x in nums:
65+
cnt[x + 1] += 1
66+
s = list(accumulate(cnt))
67+
return [s[x] for x in nums]
6568
```
6669

6770
### **Java**
6871

6972
```java
7073
class Solution {
7174
public int[] smallerNumbersThanCurrent(int[] nums) {
72-
int[] cnt = new int[101];
73-
for (int e : nums) {
74-
++cnt[e];
75+
int[] arr = nums.clone();
76+
Arrays.sort(arr);
77+
for (int i = 0; i < nums.length; ++i) {
78+
nums[i] = search(arr, nums[i]);
7579
}
76-
for (int i = 1; i < 101; ++i) {
80+
return nums;
81+
}
82+
83+
private int search(int[] nums, int x) {
84+
int l = 0, r = nums.length;
85+
while (l < r) {
86+
int mid = (l + r) >> 1;
87+
if (nums[mid] >= x) {
88+
r = mid;
89+
} else {
90+
l = mid + 1;
91+
}
92+
}
93+
return l;
94+
}
95+
}
96+
```
97+
98+
```java
99+
class Solution {
100+
public int[] smallerNumbersThanCurrent(int[] nums) {
101+
int[] cnt = new int[102];
102+
for (int x : nums) {
103+
++cnt[x + 1];
104+
}
105+
for (int i = 1; i < cnt.length; ++i) {
77106
cnt[i] += cnt[i - 1];
78107
}
79-
int[] res = new int[nums.length];
80-
for (int i = 0; i < nums.length; ++i) {
81-
res[i] = nums[i] == 0 ? 0 : cnt[nums[i] - 1];
108+
int n = nums.length;
109+
int[] ans = new int[n];
110+
for (int i = 0; i < n; ++i) {
111+
ans[i] = cnt[nums[i]];
112+
}
113+
return ans;
114+
}
115+
}
116+
```
117+
118+
### **C++**
119+
120+
```cpp
121+
class Solution {
122+
public:
123+
vector<int> smallerNumbersThanCurrent(vector<int>& nums) {
124+
vector<int> arr = nums;
125+
sort(arr.begin(), arr.end());
126+
for (int i = 0; i < nums.size(); ++i) {
127+
nums[i] = lower_bound(arr.begin(), arr.end(), nums[i]) - arr.begin();
82128
}
83-
return res;
129+
return nums;
84130
}
131+
};
132+
```
133+
134+
```cpp
135+
class Solution {
136+
public:
137+
vector<int> smallerNumbersThanCurrent(vector<int>& nums) {
138+
int cnt[102]{};
139+
for (int& x : nums) {
140+
++cnt[x + 1];
141+
}
142+
for (int i = 1; i < 102; ++i) {
143+
cnt[i] += cnt[i - 1];
144+
}
145+
vector<int> ans;
146+
for (int& x : nums) {
147+
ans.push_back(cnt[x]);
148+
}
149+
return ans;
150+
}
151+
};
152+
```
153+
154+
### **Go**
155+
156+
```go
157+
func smallerNumbersThanCurrent(nums []int) (ans []int) {
158+
arr := make([]int, len(nums))
159+
copy(arr, nums)
160+
sort.Ints(arr)
161+
for i, x := range nums {
162+
nums[i] = sort.SearchInts(arr, x)
163+
}
164+
return nums
165+
}
166+
```
167+
168+
```go
169+
func smallerNumbersThanCurrent(nums []int) (ans []int) {
170+
cnt := [102]int{}
171+
for _, x := range nums {
172+
cnt[x+1]++
173+
}
174+
for i := 1; i < len(cnt); i++ {
175+
cnt[i] += cnt[i-1]
176+
}
177+
for _, x := range nums {
178+
ans = append(ans, cnt[x])
179+
}
180+
return
85181
}
86182
```
87183

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
vector<int> smallerNumbersThanCurrent(vector<int>& nums) {
4+
int cnt[102]{};
5+
for (int& x : nums) {
6+
++cnt[x + 1];
7+
}
8+
for (int i = 1; i < 102; ++i) {
9+
cnt[i] += cnt[i - 1];
10+
}
11+
vector<int> ans;
12+
for (int& x : nums) {
13+
ans.push_back(cnt[x]);
14+
}
15+
return ans;
16+
}
17+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
func smallerNumbersThanCurrent(nums []int) (ans []int) {
2+
cnt := [102]int{}
3+
for _, x := range nums {
4+
cnt[x+1]++
5+
}
6+
for i := 1; i < len(cnt); i++ {
7+
cnt[i] += cnt[i-1]
8+
}
9+
for _, x := range nums {
10+
ans = append(ans, cnt[x])
11+
}
12+
return
13+
}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
class Solution {
22
public int[] smallerNumbersThanCurrent(int[] nums) {
3-
int[] cnt = new int[101];
4-
for (int e : nums) {
5-
++cnt[e];
3+
int[] cnt = new int[102];
4+
for (int x : nums) {
5+
++cnt[x + 1];
66
}
7-
for (int i = 1; i < 101; ++i) {
7+
for (int i = 1; i < cnt.length; ++i) {
88
cnt[i] += cnt[i - 1];
99
}
10-
int[] res = new int[nums.length];
11-
for (int i = 0; i < nums.length; ++i) {
12-
res[i] = nums[i] == 0 ? 0 : cnt[nums[i] - 1];
10+
int n = nums.length;
11+
int[] ans = new int[n];
12+
for (int i = 0; i < n; ++i) {
13+
ans[i] = cnt[nums[i]];
1314
}
14-
return res;
15+
return ans;
1516
}
1617
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
class Solution:
22
def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
3-
cnt = [0] * 101
4-
for num in nums:
5-
cnt[num] += 1
6-
for i in range(1, 101):
7-
cnt[i] += cnt[i - 1]
8-
res = []
9-
for num in nums:
10-
res.append(0 if num == 0 else cnt[num - 1])
11-
return res
3+
cnt = [0] * 102
4+
for x in nums:
5+
cnt[x + 1] += 1
6+
s = list(accumulate(cnt))
7+
return [s[x] for x in nums]

0 commit comments

Comments
 (0)