Skip to content

Commit ba9725d

Browse files
committed
feat: add solutions to lc problem: No.1636
No.1636.Sort Array by Increasing Frequency
1 parent e29005a commit ba9725d

File tree

6 files changed

+237
-2
lines changed

6 files changed

+237
-2
lines changed

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

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,96 @@
5151
<!-- 这里可写当前语言的特殊实现逻辑 -->
5252

5353
```python
54-
54+
class Solution:
55+
def frequencySort(self, nums: List[int]) -> List[int]:
56+
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
5562
```
5663

5764
### **Java**
5865

5966
<!-- 这里可写当前语言的特殊实现逻辑 -->
6067

6168
```java
69+
class Solution {
70+
public int[] frequencySort(int[] nums) {
71+
int[] cnt = new int[201];
72+
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+
}
80+
}
81+
t.sort((a, b) -> a[0] == b[0] ? b[1] - a[1] : a[0] - b[0]);
82+
int[] ans = new int[nums.length];
83+
int i = 0;
84+
for (int[] e : t) {
85+
for (int j = 0; j < e[0]; ++j) {
86+
ans[i++] = e[1] - 100;
87+
}
88+
}
89+
return ans;
90+
}
91+
}
92+
```
93+
94+
### **C++**
95+
96+
```cpp
97+
class Solution {
98+
public:
99+
vector<int> frequencySort(vector<int>& nums) {
100+
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;
112+
}
113+
};
114+
```
62115
116+
### **Go**
117+
118+
```go
119+
func frequencySort(nums []int) []int {
120+
cnt := make([]int, 201)
121+
for _, v := range nums {
122+
cnt[v+100]++
123+
}
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]
135+
})
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
143+
}
63144
```
64145

65146
### **...**

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

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,94 @@
4646
### **Python3**
4747

4848
```python
49-
49+
class Solution:
50+
def frequencySort(self, nums: List[int]) -> List[int]:
51+
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
5057
```
5158

5259
### **Java**
5360

5461
```java
62+
class Solution {
63+
public int[] frequencySort(int[] nums) {
64+
int[] cnt = new int[201];
65+
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+
}
73+
}
74+
t.sort((a, b) -> a[0] == b[0] ? b[1] - a[1] : a[0] - b[0]);
75+
int[] ans = new int[nums.length];
76+
int i = 0;
77+
for (int[] e : t) {
78+
for (int j = 0; j < e[0]; ++j) {
79+
ans[i++] = e[1] - 100;
80+
}
81+
}
82+
return ans;
83+
}
84+
}
85+
```
86+
87+
### **C++**
88+
89+
```cpp
90+
class Solution {
91+
public:
92+
vector<int> frequencySort(vector<int>& nums) {
93+
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;
105+
}
106+
};
107+
```
55108
109+
### **Go**
110+
111+
```go
112+
func frequencySort(nums []int) []int {
113+
cnt := make([]int, 201)
114+
for _, v := range nums {
115+
cnt[v+100]++
116+
}
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]
128+
})
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
136+
}
56137
```
57138

58139
### **...**
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
vector<int> frequencySort(vector<int>& nums) {
4+
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;
16+
}
17+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
func frequencySort(nums []int) []int {
2+
cnt := make([]int, 201)
3+
for _, v := range nums {
4+
cnt[v+100]++
5+
}
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]
17+
})
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
25+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public int[] frequencySort(int[] nums) {
3+
int[] cnt = new int[201];
4+
for (int v : nums) {
5+
++cnt[v + 100];
6+
}
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]);
14+
int[] ans = new int[nums.length];
15+
int i = 0;
16+
for (int[] e : t) {
17+
for (int j = 0; j < e[0]; ++j) {
18+
ans[i++] = e[1] - 100;
19+
}
20+
}
21+
return ans;
22+
}
23+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def frequencySort(self, nums: List[int]) -> List[int]:
3+
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

0 commit comments

Comments
 (0)