Skip to content

Commit 525065c

Browse files
authored
Sri Hari: Batch-6/Neetcode-All/Added-articles (#4061)
* Batch-6/Neetcode-ALL/Added-articles * Batch-6/Neetcode-ALL/Added-articles
1 parent 214cbc5 commit 525065c

5 files changed

+1008
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
## 1. Hash Map
2+
3+
::tabs-start
4+
5+
```python
6+
class Solution:
7+
def mostVisitedPattern(self, username: List[str], timestamp: List[int], website: List[str]) -> List[str]:
8+
arr = list(zip(timestamp, username, website))
9+
arr.sort()
10+
11+
mp = defaultdict(list)
12+
for time, user, site in arr:
13+
mp[user].append(site)
14+
15+
count = defaultdict(int)
16+
for user in mp:
17+
patterns = set()
18+
cur = mp[user]
19+
for i in range(len(cur)):
20+
for j in range(i + 1, len(cur)):
21+
for k in range(j + 1, len(cur)):
22+
patterns.add((cur[i], cur[j], cur[k]))
23+
for p in patterns:
24+
count[p] += 1
25+
26+
max_count = 0
27+
res = tuple()
28+
for pattern in count:
29+
if count[pattern] > max_count or (count[pattern] == max_count and pattern < res):
30+
max_count = count[pattern]
31+
res = pattern
32+
33+
return list(res)
34+
```
35+
36+
```java
37+
public class Solution {
38+
public List<String> mostVisitedPattern(String[] username, int[] timestamp, String[] website) {
39+
int n = timestamp.length;
40+
List<int[]> arr = new ArrayList<>();
41+
for (int i = 0; i < n; i++) arr.add(new int[]{timestamp[i], i});
42+
arr.sort((a, b) -> Integer.compare(a[0], b[0]));
43+
44+
Map<String, List<String>> mp = new HashMap<>();
45+
for (int[] p : arr) {
46+
int idx = p[1];
47+
mp.computeIfAbsent(username[idx], k -> new ArrayList<>()).add(website[idx]);
48+
}
49+
50+
Map<String, Integer> count = new HashMap<>();
51+
for (String user : mp.keySet()) {
52+
List<String> cur = mp.get(user);
53+
Set<String> patterns = new HashSet<>();
54+
for (int i = 0; i < cur.size(); i++)
55+
for (int j = i + 1; j < cur.size(); j++)
56+
for (int k = j + 1; k < cur.size(); k++)
57+
patterns.add(cur.get(i) + "#" + cur.get(j) + "#" + cur.get(k));
58+
for (String p : patterns)
59+
count.put(p, count.getOrDefault(p, 0) + 1);
60+
}
61+
62+
String res = "";
63+
int max_count = 0;
64+
for (String p : count.keySet()) {
65+
int c = count.get(p);
66+
if (c > max_count || (c == max_count && p.compareTo(res) < 0)) {
67+
max_count = c;
68+
res = p;
69+
}
70+
}
71+
return Arrays.asList(res.split("#"));
72+
}
73+
}
74+
```
75+
76+
```cpp
77+
class Solution {
78+
public:
79+
vector<string> mostVisitedPattern(vector<string>& username, vector<int>& timestamp, vector<string>& website) {
80+
int n = timestamp.size();
81+
vector<pair<int,int>> arr;
82+
for (int i = 0; i < n; ++i) arr.push_back({timestamp[i], i});
83+
sort(arr.begin(), arr.end(),
84+
[](auto& a, auto& b){ return a.first < b.first; });
85+
86+
unordered_map<string, vector<string>> mp;
87+
for (auto& p : arr) mp[username[p.second]].push_back(website[p.second]);
88+
89+
unordered_map<string,int> count;
90+
for (auto& kv : mp) {
91+
auto& cur = kv.second;
92+
unordered_set<string> patterns;
93+
for (int i = 0; i < (int)cur.size(); ++i)
94+
for (int j = i + 1; j < (int)cur.size(); ++j)
95+
for (int k = j + 1; k < (int)cur.size(); ++k)
96+
patterns.insert(cur[i] + "#" + cur[j] + "#" + cur[k]);
97+
for (auto& p : patterns) ++count[p];
98+
}
99+
100+
int maxCnt = 0;
101+
string res;
102+
for (auto& kv : count)
103+
if (kv.second > maxCnt ||
104+
(kv.second == maxCnt && (res.empty() || kv.first < res))) {
105+
maxCnt = kv.second;
106+
res = kv.first;
107+
}
108+
109+
vector<string> ans;
110+
string tmp;
111+
for (char ch : res) {
112+
if (ch == '#') {
113+
ans.push_back(tmp);
114+
tmp.clear();
115+
} else {
116+
tmp += ch;
117+
}
118+
}
119+
ans.push_back(tmp);
120+
return ans;
121+
}
122+
};
123+
```
124+
125+
```javascript
126+
class Solution {
127+
/**
128+
* @param {string[]} username
129+
* @param {number[]} timestamp
130+
* @param {string[]} website
131+
* @return {string[]}
132+
*/
133+
mostVisitedPattern(username, timestamp, website) {
134+
const n = timestamp.length;
135+
const arr = [];
136+
for (let i = 0; i < n; i++) arr.push([timestamp[i], i]);
137+
arr.sort((a, b) => a[0] - b[0]);
138+
139+
const mp = new Map();
140+
for (const [, idx] of arr) {
141+
const user = username[idx], site = website[idx];
142+
if (!mp.has(user)) mp.set(user, []);
143+
mp.get(user).push(site);
144+
}
145+
146+
const count = new Map();
147+
for (const user of mp.keys()) {
148+
const cur = mp.get(user);
149+
const patterns = new Set();
150+
for (let i = 0; i < cur.length; i++) {
151+
for (let j = i + 1; j < cur.length; j++) {
152+
for (let k = j + 1; k < cur.length; k++) {
153+
patterns.add(`${cur[i]}#${cur[j]}#${cur[k]}`);
154+
}
155+
}
156+
}
157+
for (const p of patterns) {
158+
count.set(p, (count.get(p) || 0) + 1);
159+
}
160+
}
161+
162+
let maxCnt = 0, res = "";
163+
for (const [pat, c] of count.entries()) {
164+
if (c > maxCnt || (c === maxCnt && (res === "" || pat < res))) {
165+
maxCnt = c;
166+
res = pat;
167+
}
168+
}
169+
return res.split("#");
170+
}
171+
}
172+
```
173+
174+
```csharp
175+
public class Solution {
176+
public List<string> MostVisitedPattern(string[] username, int[] timestamp, string[] website) {
177+
int n = timestamp.Length;
178+
var arr = new List<(int t, int i)>();
179+
for (int i = 0; i < n; i++) arr.Add((timestamp[i], i));
180+
arr.Sort((a, b) => a.t.CompareTo(b.t));
181+
182+
var mp = new Dictionary<string, List<string>>();
183+
foreach (var (t, idx) in arr) {
184+
string user = username[idx], site = website[idx];
185+
if (!mp.ContainsKey(user)) mp[user] = new List<string>();
186+
mp[user].Add(site);
187+
}
188+
189+
var count = new Dictionary<string,int>();
190+
foreach (var kv in mp) {
191+
var cur = kv.Value;
192+
var patterns = new HashSet<string>();
193+
for (int i = 0; i < cur.Count; i++)
194+
for (int j = i + 1; j < cur.Count; j++)
195+
for (int k = j + 1; k < cur.Count; k++)
196+
patterns.Add($"{cur[i]}#{cur[j]}#{cur[k]}");
197+
foreach (var p in patterns) {
198+
count[p] = count.ContainsKey(p) ? count[p] + 1 : 1;
199+
}
200+
}
201+
202+
int maxCnt = 0;
203+
string res = "";
204+
foreach (var kv in count) {
205+
if (kv.Value > maxCnt ||
206+
(kv.Value == maxCnt &&
207+
(res == "" || string.Compare(kv.Key, res, StringComparison.Ordinal) < 0))) {
208+
maxCnt = kv.Value;
209+
res = kv.Key;
210+
}
211+
}
212+
return new List<string>(res.Split('#'));
213+
}
214+
}
215+
```
216+
217+
::tabs-end
218+
219+
### Time & Space Complexity
220+
221+
* Time complexity: $O(n \log n + n * u + n ^ 3 * w)$
222+
* Space complexity: $O(n * u + n ^ 3 * w)$
223+
224+
> Where $n$ is the size of the array $timestamp$, $u$ is the maximum length of any string in the array $username$, and $w$ is the maximum length of any string in the array $website$.

articles/binary-tree-vertical-order-traversal.md

+11-11
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ class Solution {
151151
* }
152152
*/
153153
public class Solution {
154-
public IList<IList<int>> VerticalOrder(TreeNode root) {
155-
if (root == null) return new List<IList<int>>();
154+
public List<List<int>> VerticalOrder(TreeNode root) {
155+
if (root == null) return new List<List<int>>();
156156

157157
var cols = new SortedDictionary<int, List<int>>();
158158
var queue = new Queue<(TreeNode node, int pos)>();
@@ -169,7 +169,7 @@ public class Solution {
169169
if (node.right != null) queue.Enqueue((node.right, pos + 1));
170170
}
171171

172-
return cols.Values.ToList<IList<int>>();
172+
return cols.Values.ToList<List<int>>();
173173
}
174174
}
175175
```
@@ -354,10 +354,10 @@ class Solution {
354354
public class Solution {
355355
private SortedDictionary<int, List<(int, int)>> cols = new();
356356

357-
public IList<IList<int>> VerticalOrder(TreeNode root) {
357+
public List<List<int>> VerticalOrder(TreeNode root) {
358358
DFS(root, 0, 0);
359359

360-
List<IList<int>> res = new();
360+
List<List<int>> res = new();
361361
foreach (var entry in cols) {
362362
var list = entry.Value.OrderBy(x => x.Item1).Select(x => x.Item2).ToList();
363363
res.Add(list);
@@ -565,8 +565,8 @@ class Solution {
565565
* }
566566
*/
567567
public class Solution {
568-
public IList<IList<int>> VerticalOrder(TreeNode root) {
569-
if (root == null) return new List<IList<int>>();
568+
public List<List<int>> VerticalOrder(TreeNode root) {
569+
if (root == null) return new List<List<int>>();
570570

571571
Dictionary<int, List<int>> cols = new();
572572
Queue<(TreeNode node, int col)> queue = new();
@@ -585,7 +585,7 @@ public class Solution {
585585
if (node.right != null) queue.Enqueue((node.right, col + 1));
586586
}
587587

588-
var res = new List<IList<int>>();
588+
var res = new List<List<int>>();
589589
for (int c = minCol; c <= maxCol; c++) {
590590
res.Add(cols[c]);
591591
}
@@ -801,10 +801,10 @@ public class Solution {
801801
private Dictionary<int, List<(int, int)>> cols = new();
802802
private int minCol = 0, maxCol = 0;
803803

804-
public IList<IList<int>> VerticalOrder(TreeNode root) {
805-
if (root == null) return new List<IList<int>>();
804+
public List<List<int>> VerticalOrder(TreeNode root) {
805+
if (root == null) return new List<List<int>>();
806806
DFS(root, 0, 0);
807-
var res = new List<IList<int>>();
807+
var res = new List<List<int>>();
808808

809809
for (int c = minCol; c <= maxCol; c++) {
810810
var list = cols.ContainsKey(c) ? cols[c] : new List<(int, int)>();

0 commit comments

Comments
 (0)