Skip to content

Commit b3df5be

Browse files
committedDec 26, 2022
feat: add solutions to lc problem: No.1152
No.1152.Analyze User Website Visit Pattern
1 parent 8811c2f commit b3df5be

File tree

6 files changed

+498
-2
lines changed

6 files changed

+498
-2
lines changed
 

‎solution/1100-1199/1152.Analyze User Website Visit Pattern/README.md

+173-1
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,194 @@
6767

6868
<!-- 这里可写通用的实现逻辑 -->
6969

70+
**方法一:哈希表 + 排序**
71+
72+
我们先用哈希表 $d$ 记录每个用户访问的网站,然后遍历 $d$,对于每个用户,我们枚举其访问的所有三元组,统计去重三元组的出现次数,最后遍历所有三元组,返回出现次数最多的、字典序最小的三元组。
73+
74+
时间复杂度 $O(n^3)$,空间复杂度 $O(n^3)$。其中 $n$ 是 `username` 的长度。
75+
7076
<!-- tabs:start -->
7177

7278
### **Python3**
7379

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

7682
```python
77-
83+
class Solution:
84+
def mostVisitedPattern(self, username: List[str], timestamp: List[int], website: List[str]) -> List[str]:
85+
d = defaultdict(list)
86+
for user, _, site in sorted(zip(username, timestamp, website), key=lambda x: x[1]):
87+
d[user].append(site)
88+
89+
cnt = Counter()
90+
for sites in d.values():
91+
m = len(sites)
92+
s = set()
93+
if m > 2:
94+
for i in range(m - 2):
95+
for j in range(i + 1, m - 1):
96+
for k in range(j + 1, m):
97+
s.add((sites[i], sites[j], sites[k]))
98+
for t in s:
99+
cnt[t] += 1
100+
return sorted(cnt.items(), key=lambda x: (-x[1], x[0]))[0][0]
78101
```
79102

80103
### **Java**
81104

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

84107
```java
108+
class Solution {
109+
public List<String> mostVisitedPattern(String[] username, int[] timestamp, String[] website) {
110+
Map<String, List<Node>> d = new HashMap<>();
111+
int n = username.length;
112+
for (int i = 0; i < n; ++i) {
113+
String user = username[i];
114+
int ts = timestamp[i];
115+
String site = website[i];
116+
d.computeIfAbsent(user, k -> new ArrayList<>()).add(new Node(user, ts, site));
117+
}
118+
Map<String, Integer> cnt = new HashMap<>();
119+
for (var sites : d.values()) {
120+
int m = sites.size();
121+
Set<String> s = new HashSet<>();
122+
if (m > 2) {
123+
Collections.sort(sites, (a, b) -> a.ts - b.ts);
124+
for (int i = 0; i < m - 2; ++i) {
125+
for (int j = i + 1; j < m - 1; ++j) {
126+
for (int k = j + 1; k < m; ++k) {
127+
s.add(sites.get(i).site + "," + sites.get(j).site + "," + sites.get(k).site);
128+
}
129+
}
130+
}
131+
}
132+
for (String t : s) {
133+
cnt.put(t, cnt.getOrDefault(t, 0) + 1);
134+
}
135+
}
136+
int mx = 0;
137+
String t = "";
138+
for (var e : cnt.entrySet()) {
139+
if (mx < e.getValue() || (mx == e.getValue() && e.getKey().compareTo(t) < 0)) {
140+
mx = e.getValue();
141+
t = e.getKey();
142+
}
143+
}
144+
return Arrays.asList(t.split(","));
145+
}
146+
}
147+
148+
class Node {
149+
String user;
150+
int ts;
151+
String site;
152+
153+
Node(String user, int ts, String site) {
154+
this.user = user;
155+
this.ts = ts;
156+
this.site = site;
157+
}
158+
}
159+
```
160+
161+
### **C++**
162+
163+
```cpp
164+
class Solution {
165+
public:
166+
vector<string> mostVisitedPattern(vector<string>& username, vector<int>& timestamp, vector<string>& website) {
167+
unordered_map<string, vector<pair<int, string>>> d;
168+
int n = username.size();
169+
for (int i = 0; i < n; ++i) {
170+
auto user = username[i];
171+
int ts = timestamp[i];
172+
auto site = website[i];
173+
d[user].emplace_back(ts, site);
174+
}
175+
unordered_map<string, int> cnt;
176+
for (auto& [_, sites] : d) {
177+
int m = sites.size();
178+
unordered_set<string> s;
179+
if (m > 2) {
180+
sort(sites.begin(), sites.end());
181+
for (int i = 0; i < m - 2; ++i) {
182+
for (int j = i + 1; j < m - 1; ++j) {
183+
for (int k = j + 1; k < m; ++k) {
184+
s.insert(sites[i].second + "," + sites[j].second + "," + sites[k].second);
185+
}
186+
}
187+
}
188+
}
189+
for (auto& t : s) {
190+
cnt[t]++;
191+
}
192+
}
193+
int mx = 0;
194+
string t;
195+
for (auto& [p, v] : cnt) {
196+
if (mx < v || (mx == v && t > p)) {
197+
mx = v;
198+
t = p;
199+
}
200+
}
201+
return split(t, ',');
202+
}
203+
204+
vector<string> split(string& s, char c) {
205+
vector<string> res;
206+
stringstream ss(s);
207+
string t;
208+
while (getline(ss, t, c)) {
209+
res.push_back(t);
210+
}
211+
return res;
212+
}
213+
};
214+
```
85215

216+
### **Go**
217+
218+
```go
219+
func mostVisitedPattern(username []string, timestamp []int, website []string) []string {
220+
d := map[string][]pair{}
221+
for i, user := range username {
222+
ts := timestamp[i]
223+
site := website[i]
224+
d[user] = append(d[user], pair{ts, site})
225+
}
226+
cnt := map[string]int{}
227+
for _, sites := range d {
228+
m := len(sites)
229+
s := map[string]bool{}
230+
if m > 2 {
231+
sort.Slice(sites, func(i, j int) bool { return sites[i].ts < sites[j].ts })
232+
for i := 0; i < m-2; i++ {
233+
for j := i + 1; j < m-1; j++ {
234+
for k := j + 1; k < m; k++ {
235+
s[sites[i].site+","+sites[j].site+","+sites[k].site] = true
236+
}
237+
}
238+
}
239+
}
240+
for t := range s {
241+
cnt[t]++
242+
}
243+
}
244+
mx, t := 0, ""
245+
for p, v := range cnt {
246+
if mx < v || (mx == v && p < t) {
247+
mx = v
248+
t = p
249+
}
250+
}
251+
return strings.Split(t, ",")
252+
}
253+
254+
type pair struct {
255+
ts int
256+
site string
257+
}
86258
```
87259

88260
### **...**

‎solution/1100-1199/1152.Analyze User Website Visit Pattern/README_EN.md

+167-1
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,179 @@ The pattern (&quot;home&quot;, &quot;home&quot;, &quot;home&quot;) has score 0 (
6767
### **Python3**
6868

6969
```python
70-
70+
class Solution:
71+
def mostVisitedPattern(self, username: List[str], timestamp: List[int], website: List[str]) -> List[str]:
72+
d = defaultdict(list)
73+
for user, _, site in sorted(zip(username, timestamp, website), key=lambda x: x[1]):
74+
d[user].append(site)
75+
76+
cnt = Counter()
77+
for sites in d.values():
78+
m = len(sites)
79+
s = set()
80+
if m > 2:
81+
for i in range(m - 2):
82+
for j in range(i + 1, m - 1):
83+
for k in range(j + 1, m):
84+
s.add((sites[i], sites[j], sites[k]))
85+
for t in s:
86+
cnt[t] += 1
87+
return sorted(cnt.items(), key=lambda x: (-x[1], x[0]))[0][0]
7188
```
7289

7390
### **Java**
7491

7592
```java
93+
class Solution {
94+
public List<String> mostVisitedPattern(String[] username, int[] timestamp, String[] website) {
95+
Map<String, List<Node>> d = new HashMap<>();
96+
int n = username.length;
97+
for (int i = 0; i < n; ++i) {
98+
String user = username[i];
99+
int ts = timestamp[i];
100+
String site = website[i];
101+
d.computeIfAbsent(user, k -> new ArrayList<>()).add(new Node(user, ts, site));
102+
}
103+
Map<String, Integer> cnt = new HashMap<>();
104+
for (var sites : d.values()) {
105+
int m = sites.size();
106+
Set<String> s = new HashSet<>();
107+
if (m > 2) {
108+
Collections.sort(sites, (a, b) -> a.ts - b.ts);
109+
for (int i = 0; i < m - 2; ++i) {
110+
for (int j = i + 1; j < m - 1; ++j) {
111+
for (int k = j + 1; k < m; ++k) {
112+
s.add(sites.get(i).site + "," + sites.get(j).site + "," + sites.get(k).site);
113+
}
114+
}
115+
}
116+
}
117+
for (String t : s) {
118+
cnt.put(t, cnt.getOrDefault(t, 0) + 1);
119+
}
120+
}
121+
int mx = 0;
122+
String t = "";
123+
for (var e : cnt.entrySet()) {
124+
if (mx < e.getValue() || (mx == e.getValue() && e.getKey().compareTo(t) < 0)) {
125+
mx = e.getValue();
126+
t = e.getKey();
127+
}
128+
}
129+
return Arrays.asList(t.split(","));
130+
}
131+
}
132+
133+
class Node {
134+
String user;
135+
int ts;
136+
String site;
137+
138+
Node(String user, int ts, String site) {
139+
this.user = user;
140+
this.ts = ts;
141+
this.site = site;
142+
}
143+
}
144+
```
145+
146+
### **C++**
147+
148+
```cpp
149+
class Solution {
150+
public:
151+
vector<string> mostVisitedPattern(vector<string>& username, vector<int>& timestamp, vector<string>& website) {
152+
unordered_map<string, vector<pair<int, string>>> d;
153+
int n = username.size();
154+
for (int i = 0; i < n; ++i) {
155+
auto user = username[i];
156+
int ts = timestamp[i];
157+
auto site = website[i];
158+
d[user].emplace_back(ts, site);
159+
}
160+
unordered_map<string, int> cnt;
161+
for (auto& [_, sites] : d) {
162+
int m = sites.size();
163+
unordered_set<string> s;
164+
if (m > 2) {
165+
sort(sites.begin(), sites.end());
166+
for (int i = 0; i < m - 2; ++i) {
167+
for (int j = i + 1; j < m - 1; ++j) {
168+
for (int k = j + 1; k < m; ++k) {
169+
s.insert(sites[i].second + "," + sites[j].second + "," + sites[k].second);
170+
}
171+
}
172+
}
173+
}
174+
for (auto& t : s) {
175+
cnt[t]++;
176+
}
177+
}
178+
int mx = 0;
179+
string t;
180+
for (auto& [p, v] : cnt) {
181+
if (mx < v || (mx == v && t > p)) {
182+
mx = v;
183+
t = p;
184+
}
185+
}
186+
return split(t, ',');
187+
}
188+
189+
vector<string> split(string& s, char c) {
190+
vector<string> res;
191+
stringstream ss(s);
192+
string t;
193+
while (getline(ss, t, c)) {
194+
res.push_back(t);
195+
}
196+
return res;
197+
}
198+
};
199+
```
76200

201+
### **Go**
202+
203+
```go
204+
func mostVisitedPattern(username []string, timestamp []int, website []string) []string {
205+
d := map[string][]pair{}
206+
for i, user := range username {
207+
ts := timestamp[i]
208+
site := website[i]
209+
d[user] = append(d[user], pair{ts, site})
210+
}
211+
cnt := map[string]int{}
212+
for _, sites := range d {
213+
m := len(sites)
214+
s := map[string]bool{}
215+
if m > 2 {
216+
sort.Slice(sites, func(i, j int) bool { return sites[i].ts < sites[j].ts })
217+
for i := 0; i < m-2; i++ {
218+
for j := i + 1; j < m-1; j++ {
219+
for k := j + 1; k < m; k++ {
220+
s[sites[i].site+","+sites[j].site+","+sites[k].site] = true
221+
}
222+
}
223+
}
224+
}
225+
for t := range s {
226+
cnt[t]++
227+
}
228+
}
229+
mx, t := 0, ""
230+
for p, v := range cnt {
231+
if mx < v || (mx == v && p < t) {
232+
mx = v
233+
t = p
234+
}
235+
}
236+
return strings.Split(t, ",")
237+
}
238+
239+
type pair struct {
240+
ts int
241+
site string
242+
}
77243
```
78244

79245
### **...**

0 commit comments

Comments
 (0)