Skip to content

Commit 7686441

Browse files
committed
feat: update solutions to lc problem: No.1636
No.1636.Sort Array by Increasing Frequency
1 parent dd89bfa commit 7686441

File tree

6 files changed

+68
-138
lines changed

6 files changed

+68
-138
lines changed

solution/1600-1699/1636.Sort Array by Increasing Frequency/README.md

Lines changed: 28 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@
4444

4545
<!-- 这里可写通用的实现逻辑 -->
4646

47+
**方法一:数组或哈希表计数**
48+
49+
用数组或者哈希表统计 `nums` 中每个数字出现的次数,由于题目中数字的范围是 `[-100, 100]`,我们可以直接创建一个大小为 $201$ 的数组来统计。
50+
51+
然后对 `nums` 按照数字出现次数升序排序,如果出现次数相同,则按照数字降序排序。
52+
53+
时间复杂度为 $O(n\log n)$,空间复杂度 $O(n)$。其中 $n$ 为 `nums` 的长度。
54+
4755
<!-- tabs:start -->
4856

4957
### **Python3**
@@ -54,11 +62,7 @@
5462
class Solution:
5563
def frequencySort(self, nums: List[int]) -> List[int]:
5664
cnt = Counter(nums)
57-
cnt = sorted(cnt.items(), key=lambda x: (x[1], -x[0]))
58-
ans = []
59-
for v, freq in cnt:
60-
ans.extend([v] * freq)
61-
return ans
65+
return sorted(nums, key=lambda x: (cnt[x], -x))
6266
```
6367

6468
### **Java**
@@ -69,22 +73,17 @@ class Solution:
6973
class Solution {
7074
public int[] frequencySort(int[] nums) {
7175
int[] cnt = new int[201];
76+
List<Integer> t = new ArrayList<>();
7277
for (int v : nums) {
73-
++cnt[v + 100];
74-
}
75-
List<int[]> t = new ArrayList<>();
76-
for (int i = 0; i < cnt.length; ++i) {
77-
if (cnt[i] > 0) {
78-
t.add(new int[] {cnt[i], i});
79-
}
78+
v += 100;
79+
++cnt[v];
80+
t.add(v);
8081
}
81-
t.sort((a, b) -> a[0] == b[0] ? b[1] - a[1] : a[0] - b[0]);
82+
t.sort((a, b) -> cnt[a] == cnt[b] ? b - a : cnt[a] - cnt[b]);
8283
int[] ans = new int[nums.length];
8384
int i = 0;
84-
for (int[] e : t) {
85-
for (int j = 0; j < e[0]; ++j) {
86-
ans[i++] = e[1] - 100;
87-
}
85+
for (int v : t) {
86+
ans[i++] = v - 100;
8887
}
8988
return ans;
9089
}
@@ -98,17 +97,14 @@ class Solution {
9897
public:
9998
vector<int> frequencySort(vector<int>& nums) {
10099
vector<int> cnt(201);
101-
for (int& v : nums) ++cnt[v + 100];
102-
vector<vector<int>> t;
103-
for (int i = 0; i < cnt.size(); ++i)
104-
if (cnt[i])
105-
t.push_back({cnt[i], -i});
106-
sort(t.begin(), t.end());
107-
vector<int> ans;
108-
for (auto& e : t)
109-
for (int j = 0; j < e[0]; ++j)
110-
ans.push_back(-e[1] - 100);
111-
return ans;
100+
for (int v : nums) {
101+
++cnt[v + 100];
102+
}
103+
sort(nums.begin(), nums.end(), [&](const int a, const int b) {
104+
if (cnt[a + 100] == cnt[b + 100]) return a > b;
105+
return cnt[a + 100] < cnt[b + 100];
106+
});
107+
return nums;
112108
}
113109
};
114110
```
@@ -121,25 +117,11 @@ func frequencySort(nums []int) []int {
121117
for _, v := range nums {
122118
cnt[v+100]++
123119
}
124-
var t [][]int
125-
for i, v := range cnt {
126-
if v > 0 {
127-
t = append(t, []int{v, i})
128-
}
129-
}
130-
sort.Slice(t, func(i, j int) bool {
131-
if t[i][0] == t[j][0] {
132-
return t[i][1] > t[j][1]
133-
}
134-
return t[i][0] < t[j][0]
120+
sort.Slice(nums, func(i, j int) bool {
121+
a, b := nums[i]+100, nums[j]+100
122+
return cnt[a] < cnt[b] || cnt[a] == cnt[b] && a > b
135123
})
136-
var ans []int
137-
for _, e := range t {
138-
for i := 0; i < e[0]; i++ {
139-
ans = append(ans, e[1]-100)
140-
}
141-
}
142-
return ans
124+
return nums
143125
}
144126
```
145127

solution/1600-1699/1636.Sort Array by Increasing Frequency/README_EN.md

Lines changed: 20 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,7 @@
4949
class Solution:
5050
def frequencySort(self, nums: List[int]) -> List[int]:
5151
cnt = Counter(nums)
52-
cnt = sorted(cnt.items(), key=lambda x: (x[1], -x[0]))
53-
ans = []
54-
for v, freq in cnt:
55-
ans.extend([v] * freq)
56-
return ans
52+
return sorted(nums, key=lambda x: (cnt[x], -x))
5753
```
5854

5955
### **Java**
@@ -62,22 +58,17 @@ class Solution:
6258
class Solution {
6359
public int[] frequencySort(int[] nums) {
6460
int[] cnt = new int[201];
61+
List<Integer> t = new ArrayList<>();
6562
for (int v : nums) {
66-
++cnt[v + 100];
67-
}
68-
List<int[]> t = new ArrayList<>();
69-
for (int i = 0; i < cnt.length; ++i) {
70-
if (cnt[i] > 0) {
71-
t.add(new int[] {cnt[i], i});
72-
}
63+
v += 100;
64+
++cnt[v];
65+
t.add(v);
7366
}
74-
t.sort((a, b) -> a[0] == b[0] ? b[1] - a[1] : a[0] - b[0]);
67+
t.sort((a, b) -> cnt[a] == cnt[b] ? b - a : cnt[a] - cnt[b]);
7568
int[] ans = new int[nums.length];
7669
int i = 0;
77-
for (int[] e : t) {
78-
for (int j = 0; j < e[0]; ++j) {
79-
ans[i++] = e[1] - 100;
80-
}
70+
for (int v : t) {
71+
ans[i++] = v - 100;
8172
}
8273
return ans;
8374
}
@@ -91,17 +82,14 @@ class Solution {
9182
public:
9283
vector<int> frequencySort(vector<int>& nums) {
9384
vector<int> cnt(201);
94-
for (int& v : nums) ++cnt[v + 100];
95-
vector<vector<int>> t;
96-
for (int i = 0; i < cnt.size(); ++i)
97-
if (cnt[i])
98-
t.push_back({cnt[i], -i});
99-
sort(t.begin(), t.end());
100-
vector<int> ans;
101-
for (auto& e : t)
102-
for (int j = 0; j < e[0]; ++j)
103-
ans.push_back(-e[1] - 100);
104-
return ans;
85+
for (int v : nums) {
86+
++cnt[v + 100];
87+
}
88+
sort(nums.begin(), nums.end(), [&](const int a, const int b) {
89+
if (cnt[a + 100] == cnt[b + 100]) return a > b;
90+
return cnt[a + 100] < cnt[b + 100];
91+
});
92+
return nums;
10593
}
10694
};
10795
```
@@ -114,25 +102,11 @@ func frequencySort(nums []int) []int {
114102
for _, v := range nums {
115103
cnt[v+100]++
116104
}
117-
var t [][]int
118-
for i, v := range cnt {
119-
if v > 0 {
120-
t = append(t, []int{v, i})
121-
}
122-
}
123-
sort.Slice(t, func(i, j int) bool {
124-
if t[i][0] == t[j][0] {
125-
return t[i][1] > t[j][1]
126-
}
127-
return t[i][0] < t[j][0]
105+
sort.Slice(nums, func(i, j int) bool {
106+
a, b := nums[i]+100, nums[j]+100
107+
return cnt[a] < cnt[b] || cnt[a] == cnt[b] && a > b
128108
})
129-
var ans []int
130-
for _, e := range t {
131-
for i := 0; i < e[0]; i++ {
132-
ans = append(ans, e[1]-100)
133-
}
134-
}
135-
return ans
109+
return nums
136110
}
137111
```
138112

solution/1600-1699/1636.Sort Array by Increasing Frequency/Solution.cpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,13 @@ class Solution {
22
public:
33
vector<int> frequencySort(vector<int>& nums) {
44
vector<int> cnt(201);
5-
for (int& v : nums) ++cnt[v + 100];
6-
vector<vector<int>> t;
7-
for (int i = 0; i < cnt.size(); ++i)
8-
if (cnt[i])
9-
t.push_back({cnt[i], -i});
10-
sort(t.begin(), t.end());
11-
vector<int> ans;
12-
for (auto& e : t)
13-
for (int j = 0; j < e[0]; ++j)
14-
ans.push_back(-e[1] - 100);
15-
return ans;
5+
for (int v : nums) {
6+
++cnt[v + 100];
7+
}
8+
sort(nums.begin(), nums.end(), [&](const int a, const int b) {
9+
if (cnt[a + 100] == cnt[b + 100]) return a > b;
10+
return cnt[a + 100] < cnt[b + 100];
11+
});
12+
return nums;
1613
}
1714
};

solution/1600-1699/1636.Sort Array by Increasing Frequency/Solution.go

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,9 @@ func frequencySort(nums []int) []int {
33
for _, v := range nums {
44
cnt[v+100]++
55
}
6-
var t [][]int
7-
for i, v := range cnt {
8-
if v > 0 {
9-
t = append(t, []int{v, i})
10-
}
11-
}
12-
sort.Slice(t, func(i, j int) bool {
13-
if t[i][0] == t[j][0] {
14-
return t[i][1] > t[j][1]
15-
}
16-
return t[i][0] < t[j][0]
6+
sort.Slice(nums, func(i, j int) bool {
7+
a, b := nums[i]+100, nums[j]+100
8+
return cnt[a] < cnt[b] || cnt[a] == cnt[b] && a > b
179
})
18-
var ans []int
19-
for _, e := range t {
20-
for i := 0; i < e[0]; i++ {
21-
ans = append(ans, e[1]-100)
22-
}
23-
}
24-
return ans
10+
return nums
2511
}

solution/1600-1699/1636.Sort Array by Increasing Frequency/Solution.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
11
class Solution {
22
public int[] frequencySort(int[] nums) {
33
int[] cnt = new int[201];
4+
List<Integer> t = new ArrayList<>();
45
for (int v : nums) {
5-
++cnt[v + 100];
6+
v += 100;
7+
++cnt[v];
8+
t.add(v);
69
}
7-
List<int[]> t = new ArrayList<>();
8-
for (int i = 0; i < cnt.length; ++i) {
9-
if (cnt[i] > 0) {
10-
t.add(new int[] {cnt[i], i});
11-
}
12-
}
13-
t.sort((a, b) -> a[0] == b[0] ? b[1] - a[1] : a[0] - b[0]);
10+
t.sort((a, b) -> cnt[a] == cnt[b] ? b - a : cnt[a] - cnt[b]);
1411
int[] ans = new int[nums.length];
1512
int i = 0;
16-
for (int[] e : t) {
17-
for (int j = 0; j < e[0]; ++j) {
18-
ans[i++] = e[1] - 100;
19-
}
13+
for (int v : t) {
14+
ans[i++] = v - 100;
2015
}
2116
return ans;
2217
}
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
class Solution:
22
def frequencySort(self, nums: List[int]) -> List[int]:
33
cnt = Counter(nums)
4-
cnt = sorted(cnt.items(), key=lambda x: (x[1], -x[0]))
5-
ans = []
6-
for v, freq in cnt:
7-
ans.extend([v] * freq)
8-
return ans
4+
return sorted(nums, key=lambda x: (cnt[x], -x))

0 commit comments

Comments
 (0)