Skip to content

Commit 2d287d5

Browse files
committed
feat: add solutions to lc problem: No.1772
No.1772.Sort Features by Popularity
1 parent 4eb2baa commit 2d287d5

File tree

6 files changed

+266
-88
lines changed

6 files changed

+266
-88
lines changed

solution/1700-1799/1772.Sort Features by Popularity/README.md

+96-30
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,13 @@
4949

5050
<!-- 这里可写通用的实现逻辑 -->
5151

52-
“哈希表 + 计数器”实现。
52+
**方法一:哈希表 + 自定义排序**
53+
54+
我们遍历 `responses`,对于 `responses[i]` 中的每个单词,我们用一个哈希表 `ws` 暂存。接下来将 `ws` 中的单词记录到哈希表 `cnt` 中,记录每个单词出现的次数。
55+
56+
接下来,采用自定义排序,将 `features` 中的单词按照出现次数从大到小排序,如果出现次数相同,则按照出现的下标从小到大排序。
57+
58+
时间复杂度 $O(n \times \log n)$,其中 $n$ 为 `features` 的长度。
5359

5460
<!-- tabs:start -->
5561

@@ -60,14 +66,12 @@
6066
```python
6167
class Solution:
6268
def sortFeatures(self, features: List[str], responses: List[str]) -> List[str]:
63-
feature_set = set(features)
64-
counter = Counter()
65-
for resp in responses:
66-
for feat in set(resp.split(' ')):
67-
if feat in feature_set:
68-
counter[feat] += 1
69-
order = {feat: i for i, feat in enumerate(features)}
70-
return sorted(features, key=lambda feat: (-counter[feat], order[feat]))
69+
cnt = Counter()
70+
for r in responses:
71+
ws = set(r.split())
72+
for s in ws:
73+
cnt[s] += 1
74+
return sorted(features, key=lambda x: -cnt[x])
7175
```
7276

7377
### **Java**
@@ -77,34 +81,96 @@ class Solution:
7781
```java
7882
class Solution {
7983
public String[] sortFeatures(String[] features, String[] responses) {
80-
Set<String> featureSet = new HashSet<>();
81-
Map<String, Integer> order = new HashMap<>();
82-
for (int i = 0; i < features.length; ++i) {
83-
featureSet.add(features[i]);
84-
order.put(features[i], i);
84+
Map<String, Integer> cnt = new HashMap<>();
85+
for (String r : responses) {
86+
Set<String> ws = new HashSet<>();
87+
for (String w : r.split(" ")) {
88+
ws.add(w);
89+
}
90+
for (String w : ws) {
91+
cnt.put(w, cnt.getOrDefault(w, 0) + 1);
92+
}
93+
}
94+
int n = features.length;
95+
Integer[] idx = new Integer[n];
96+
for (int i = 0; i < n; ++i) {
97+
idx[i] = i;
98+
}
99+
Arrays.sort(idx, (i, j) -> {
100+
int d = cnt.getOrDefault(features[j], 0) - cnt.getOrDefault(features[i], 0);
101+
return d == 0 ? i - j : d;
102+
});
103+
String[] ans = new String[n];
104+
for (int i = 0; i < n; ++i) {
105+
ans[i] = features[idx[i]];
85106
}
107+
return ans;
108+
}
109+
}
110+
```
111+
112+
### **C++**
86113

87-
Map<String, Integer> counter = new HashMap<>();
88-
for (String resp : responses) {
89-
Set<String> s = new HashSet<>();
90-
String[] words = resp.split(" ");
91-
for (String word : words) {
92-
s.add(word);
114+
```cpp
115+
class Solution {
116+
public:
117+
vector<string> sortFeatures(vector<string>& features, vector<string>& responses) {
118+
unordered_map<string, int> cnt;
119+
for (auto& r : responses) {
120+
stringstream ss(r);
121+
string t;
122+
unordered_set<string> ws;
123+
while (ss >> t) {
124+
ws.insert(t);
93125
}
94-
for (String word : s) {
95-
if (featureSet.contains(word)) {
96-
counter.put(word, counter.getOrDefault(word, 0) + 1);
97-
}
126+
for (auto& w : ws) {
127+
cnt[w]++;
98128
}
99129
}
100-
String[] copyFeatures = Arrays.copyOf(features, features.length);
101-
Arrays.sort(copyFeatures, (a, b) -> {
102-
// 自定义排序比较器,先按照词频大小从高到低排,若词频相等,再根据features顺序从小到大排
103-
int diff = counter.getOrDefault(b, 0) - counter.getOrDefault(a, 0);
104-
return diff == 0 ? order.get(a) - order.get(b) : diff;
130+
int n = features.size();
131+
vector<int> idx(n);
132+
iota(idx.begin(), idx.end(), 0);
133+
sort(idx.begin(), idx.end(), [&](int i, int j) -> bool {
134+
int d = cnt[features[i]] - cnt[features[j]];
135+
return d > 0 || (d == 0 && i < j);
105136
});
106-
return copyFeatures;
137+
vector<string> ans(n);
138+
for (int i = 0; i < n; ++i) {
139+
ans[i] = features[idx[i]];
140+
}
141+
return ans;
107142
}
143+
};
144+
```
145+
146+
### **Go**
147+
148+
```go
149+
func sortFeatures(features []string, responses []string) []string {
150+
cnt := map[string]int{}
151+
for _, r := range responses {
152+
ws := map[string]bool{}
153+
for _, s := range strings.Split(r, " ") {
154+
ws[s] = true
155+
}
156+
for w := range ws {
157+
cnt[w]++
158+
}
159+
}
160+
n := len(features)
161+
idx := make([]int, n)
162+
for i := range idx {
163+
idx[i] = i
164+
}
165+
sort.Slice(idx, func(i, j int) bool {
166+
d := cnt[features[idx[i]]] - cnt[features[idx[j]]]
167+
return d > 0 || (d == 0 && idx[i] < idx[j])
168+
})
169+
ans := make([]string, n)
170+
for i := range ans {
171+
ans[i] = features[idx[i]]
172+
}
173+
return ans
108174
}
109175
```
110176

solution/1700-1799/1772.Sort Features by Popularity/README_EN.md

+89-28
Original file line numberDiff line numberDiff line change
@@ -50,48 +50,109 @@
5050
```python
5151
class Solution:
5252
def sortFeatures(self, features: List[str], responses: List[str]) -> List[str]:
53-
feature_set = set(features)
54-
counter = Counter()
55-
for resp in responses:
56-
for feat in set(resp.split(' ')):
57-
if feat in feature_set:
58-
counter[feat] += 1
59-
order = {feat: i for i, feat in enumerate(features)}
60-
return sorted(features, key=lambda feat: (-counter[feat], order[feat]))
53+
cnt = Counter()
54+
for r in responses:
55+
ws = set(r.split())
56+
for s in ws:
57+
cnt[s] += 1
58+
return sorted(features, key=lambda x: -cnt[x])
6159
```
6260

6361
### **Java**
6462

6563
```java
6664
class Solution {
6765
public String[] sortFeatures(String[] features, String[] responses) {
68-
Set<String> featureSet = new HashSet<>();
69-
Map<String, Integer> order = new HashMap<>();
70-
for (int i = 0; i < features.length; ++i) {
71-
featureSet.add(features[i]);
72-
order.put(features[i], i);
66+
Map<String, Integer> cnt = new HashMap<>();
67+
for (String r : responses) {
68+
Set<String> ws = new HashSet<>();
69+
for (String w : r.split(" ")) {
70+
ws.add(w);
71+
}
72+
for (String w : ws) {
73+
cnt.put(w, cnt.getOrDefault(w, 0) + 1);
74+
}
7375
}
76+
int n = features.length;
77+
Integer[] idx = new Integer[n];
78+
for (int i = 0; i < n; ++i) {
79+
idx[i] = i;
80+
}
81+
Arrays.sort(idx, (i, j) -> {
82+
int d = cnt.getOrDefault(features[j], 0) - cnt.getOrDefault(features[i], 0);
83+
return d == 0 ? i - j : d;
84+
});
85+
String[] ans = new String[n];
86+
for (int i = 0; i < n; ++i) {
87+
ans[i] = features[idx[i]];
88+
}
89+
return ans;
90+
}
91+
}
92+
```
93+
94+
### **C++**
7495

75-
Map<String, Integer> counter = new HashMap<>();
76-
for (String resp : responses) {
77-
Set<String> s = new HashSet<>();
78-
String[] words = resp.split(" ");
79-
for (String word : words) {
80-
s.add(word);
96+
```cpp
97+
class Solution {
98+
public:
99+
vector<string> sortFeatures(vector<string>& features, vector<string>& responses) {
100+
unordered_map<string, int> cnt;
101+
for (auto& r : responses) {
102+
stringstream ss(r);
103+
string t;
104+
unordered_set<string> ws;
105+
while (ss >> t) {
106+
ws.insert(t);
81107
}
82-
for (String word : s) {
83-
if (featureSet.contains(word)) {
84-
counter.put(word, counter.getOrDefault(word, 0) + 1);
85-
}
108+
for (auto& w : ws) {
109+
cnt[w]++;
86110
}
87111
}
88-
String[] copyFeatures = Arrays.copyOf(features, features.length);
89-
Arrays.sort(copyFeatures, (a, b) -> {
90-
int diff = counter.getOrDefault(b, 0) - counter.getOrDefault(a, 0);
91-
return diff == 0 ? order.get(a) - order.get(b) : diff;
112+
int n = features.size();
113+
vector<int> idx(n);
114+
iota(idx.begin(), idx.end(), 0);
115+
sort(idx.begin(), idx.end(), [&](int i, int j) -> bool {
116+
int d = cnt[features[i]] - cnt[features[j]];
117+
return d > 0 || (d == 0 && i < j);
92118
});
93-
return copyFeatures;
119+
vector<string> ans(n);
120+
for (int i = 0; i < n; ++i) {
121+
ans[i] = features[idx[i]];
122+
}
123+
return ans;
94124
}
125+
};
126+
```
127+
128+
### **Go**
129+
130+
```go
131+
func sortFeatures(features []string, responses []string) []string {
132+
cnt := map[string]int{}
133+
for _, r := range responses {
134+
ws := map[string]bool{}
135+
for _, s := range strings.Split(r, " ") {
136+
ws[s] = true
137+
}
138+
for w := range ws {
139+
cnt[w]++
140+
}
141+
}
142+
n := len(features)
143+
idx := make([]int, n)
144+
for i := range idx {
145+
idx[i] = i
146+
}
147+
sort.Slice(idx, func(i, j int) bool {
148+
d := cnt[features[idx[i]]] - cnt[features[idx[j]]]
149+
return d > 0 || (d == 0 && idx[i] < idx[j])
150+
})
151+
ans := make([]string, n)
152+
for i := range ans {
153+
ans[i] = features[idx[i]]
154+
}
155+
return ans
95156
}
96157
```
97158

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
vector<string> sortFeatures(vector<string>& features, vector<string>& responses) {
4+
unordered_map<string, int> cnt;
5+
for (auto& r : responses) {
6+
stringstream ss(r);
7+
string t;
8+
unordered_set<string> ws;
9+
while (ss >> t) {
10+
ws.insert(t);
11+
}
12+
for (auto& w : ws) {
13+
cnt[w]++;
14+
}
15+
}
16+
int n = features.size();
17+
vector<int> idx(n);
18+
iota(idx.begin(), idx.end(), 0);
19+
sort(idx.begin(), idx.end(), [&](int i, int j) -> bool {
20+
int d = cnt[features[i]] - cnt[features[j]];
21+
return d > 0 || (d == 0 && i < j);
22+
});
23+
vector<string> ans(n);
24+
for (int i = 0; i < n; ++i) {
25+
ans[i] = features[idx[i]];
26+
}
27+
return ans;
28+
}
29+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
func sortFeatures(features []string, responses []string) []string {
2+
cnt := map[string]int{}
3+
for _, r := range responses {
4+
ws := map[string]bool{}
5+
for _, s := range strings.Split(r, " ") {
6+
ws[s] = true
7+
}
8+
for w := range ws {
9+
cnt[w]++
10+
}
11+
}
12+
n := len(features)
13+
idx := make([]int, n)
14+
for i := range idx {
15+
idx[i] = i
16+
}
17+
sort.Slice(idx, func(i, j int) bool {
18+
d := cnt[features[idx[i]]] - cnt[features[idx[j]]]
19+
return d > 0 || (d == 0 && idx[i] < idx[j])
20+
})
21+
ans := make([]string, n)
22+
for i := range ans {
23+
ans[i] = features[idx[i]]
24+
}
25+
return ans
26+
}

0 commit comments

Comments
 (0)