Skip to content

Commit 3ad5799

Browse files
committed
MILESTONE 300 questions on day 31!
1 parent c910889 commit 3ad5799

12 files changed

+325
-4
lines changed

README.md

+11-4
Large diffs are not rendered by default.

my-submissions/m1166.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class FileSystem:
2+
3+
def __init__(self):
4+
self.trie = {}
5+
6+
def createPath(self, path: str, value: int) -> bool:
7+
pathChunks = path.split('/')[1:]
8+
9+
curr = self.trie
10+
for i in range(len(pathChunks) - 1) :
11+
if pathChunks[i] not in curr :
12+
return False
13+
curr = curr[pathChunks[i]]
14+
15+
if pathChunks[-1] in curr and 'value' in curr[pathChunks[-1]] :
16+
return False
17+
18+
curr[pathChunks[-1]] = {}
19+
curr[pathChunks[-1]]['value'] = value
20+
return True
21+
22+
def get(self, path: str) -> int:
23+
pathChunks = path.split('/')[1:]
24+
25+
curr = self.trie
26+
for chunk in pathChunks :
27+
if chunk not in curr :
28+
return -1
29+
curr = curr[chunk]
30+
31+
if 'value' in curr :
32+
return curr['value']
33+
return -1
34+
35+
36+
37+
38+
# Your FileSystem object will be instantiated and called as such:
39+
# obj = FileSystem()
40+
# param_1 = obj.createPath(path,value)
41+
# param_2 = obj.get(path)

my-submissions/m1233.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
def removeSubfolders(self, folder: List[str]) -> List[str]:
3+
trie = {}
4+
folder.sort()
5+
output = []
6+
7+
8+
for f in folder :
9+
path = f.split('/')[1:]
10+
curr = trie
11+
add = False
12+
for c in path :
13+
if c not in curr :
14+
curr[c] = {}
15+
add = True
16+
if False in curr :
17+
break
18+
19+
curr = curr[c]
20+
21+
if add and False not in curr:
22+
output.append(f)
23+
curr[False] = True
24+
25+
return output

my-submissions/m1282.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:
3+
people = sorted([(groupSizes[i], i) for i in range(len(groupSizes))], reverse=True)
4+
outputs = []
5+
6+
while people :
7+
currSize = people[-1][0]
8+
outputs.append([people.pop()[1] for _ in range(currSize)])
9+
10+
return outputs

my-submissions/m2285 Daily.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def maximumImportance(self, n: int, roads: List[List[int]]) -> int:
3+
cnt = Counter([roads[x // 2][x % 2] for x in range(len(roads) * 2)])
4+
validVals = sorted(list(cnt.values()))
5+
6+
i = n
7+
output = 0
8+
9+
while validVals :
10+
output += i * validVals.pop()
11+
i -= 1
12+
13+
return output

my-submissions/m2285 v2.c

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
int compare(const void* a, const void* b) {
2+
return *((int*) a) - *((int*) b);
3+
}
4+
5+
long long maximumImportance(int n, int** roads, int roadsSize, int* roadsColSize) {
6+
int cnt[n];
7+
memset(&cnt, 0, n * sizeof(int));
8+
9+
for (int i = 0; i < roadsSize; i++) {
10+
cnt[roads[i][0]]++;
11+
cnt[roads[i][1]]++;
12+
}
13+
14+
qsort(cnt, n, sizeof(int), compare);
15+
16+
long long output = 0;
17+
for (int i = n; i > 0 && cnt[i - 1] > 0; i--) {
18+
output += (long long) i * (long long) cnt[i - 1];
19+
}
20+
return output;
21+
}

my-submissions/m2285 v2.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
long long maximumImportance(int n, vector<vector<int>>& roads) {
4+
int cnt[n];
5+
memset(&cnt, 0, n * sizeof(int));
6+
7+
for (int i = 0; i < roads.size(); i++) {
8+
cnt[roads[i][0]]++;
9+
cnt[roads[i][1]]++;
10+
}
11+
sort(cnt, cnt + n);
12+
13+
long long output = 0;
14+
for (int i = n; i > 0 && cnt[i - 1] > 0; i--) {
15+
output += (long long) i * (long long) cnt[i - 1];
16+
}
17+
return output;
18+
}
19+
};

my-submissions/m2285 v2.java

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public long maximumImportance(int n, int[][] roads) {
3+
int[] cnt = new int[n];
4+
5+
for (int[] road : roads) {
6+
cnt[road[0]]++;
7+
cnt[road[1]]++;
8+
}
9+
Arrays.sort(cnt);
10+
11+
long output = 0;
12+
for (int i = n; i > 0 && cnt[i - 1] > 0; i--) {
13+
output += ((long) i * (long) cnt[i - 1]);
14+
}
15+
return output;
16+
}
17+
}

my-submissions/m2285 v2.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def maximumImportance(self, n: int, roads: List[List[int]]) -> int:
3+
cnt = [0] * n
4+
for road in roads :
5+
cnt[road[0]] += 1
6+
cnt[road[1]] += 1
7+
cnt.sort()
8+
9+
i = n
10+
output = 0
11+
12+
while cnt and cnt[-1] > 0 :
13+
output += i * cnt.pop()
14+
i -= 1
15+
16+
return output

my-submissions/m3043.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution:
2+
def longestCommonPrefix(self, arr1: List[int], arr2: List[int]) -> int:
3+
trie = {}
4+
5+
for s in arr1 :
6+
curr = trie
7+
for c in str(s) :
8+
if c not in curr :
9+
curr[c] = {}
10+
curr = curr[c]
11+
curr[False] = True
12+
13+
maxx = 0
14+
for s in arr2 :
15+
curr = trie
16+
counter = 0
17+
for c in str(s) :
18+
if c not in curr :
19+
break
20+
counter += 1
21+
curr = curr[c]
22+
maxx = max(maxx, counter)
23+
24+
return maxx

my-submissions/m616.py

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Identical to 758
2+
3+
class Solution:
4+
def addBoldTag(self, s: str, words: List[str]) -> str:
5+
trie = {}
6+
7+
for word in words :
8+
curr = trie
9+
for c in word :
10+
if c not in curr :
11+
curr[c] = {}
12+
curr = curr[c]
13+
14+
curr[False] = True
15+
16+
# Find intervals that need to be bolded
17+
intervals = []
18+
curr = {}
19+
for i, c in enumerate(s) :
20+
curr[i] = trie
21+
keys = list(curr.keys())
22+
for key in keys :
23+
if False in curr[key] :
24+
intervals.append((key, i))
25+
26+
if c not in curr[key] :
27+
curr.pop(key)
28+
continue
29+
30+
curr[key] = curr[key][c]
31+
32+
for i, branch in curr.items() :
33+
if False in branch :
34+
intervals.append((i, len(s)))
35+
36+
intervals.sort(key=lambda x: x[0])
37+
38+
# merging intervals
39+
output = []
40+
if intervals :
41+
output.append(s[:intervals[0][0]])
42+
43+
indx = 0
44+
while indx < len(intervals) :
45+
left, right = intervals[indx]
46+
indx += 1
47+
48+
while indx < len(intervals) and intervals[indx][0] <= right :
49+
if intervals[indx][1] > right :
50+
right = intervals[indx][1]
51+
indx += 1
52+
53+
output.append('<b>')
54+
output.append(s[left:right])
55+
output.append('</b>')
56+
57+
if indx < len(intervals) :
58+
output.append(s[right:intervals[indx][0]])
59+
if intervals :
60+
output.append(s[right:])
61+
else :
62+
output.append(s)
63+
64+
return ''.join(output)

my-submissions/m758.py

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Identical to 758
2+
3+
class Solution:
4+
def boldWords(self, words: List[str], s: str) -> str:
5+
trie = {}
6+
7+
for word in words :
8+
curr = trie
9+
for c in word :
10+
if c not in curr :
11+
curr[c] = {}
12+
curr = curr[c]
13+
14+
curr[False] = True
15+
16+
# Find intervals that need to be bolded
17+
intervals = []
18+
curr = {}
19+
for i, c in enumerate(s) :
20+
curr[i] = trie
21+
keys = list(curr.keys())
22+
for key in keys :
23+
if False in curr[key] :
24+
intervals.append((key, i))
25+
26+
if c not in curr[key] :
27+
curr.pop(key)
28+
continue
29+
30+
curr[key] = curr[key][c]
31+
32+
for i, branch in curr.items() :
33+
if False in branch :
34+
intervals.append((i, len(s)))
35+
36+
intervals.sort(key=lambda x: x[0])
37+
38+
# merging intervals
39+
output = []
40+
if intervals :
41+
output.append(s[:intervals[0][0]])
42+
43+
indx = 0
44+
while indx < len(intervals) :
45+
left, right = intervals[indx]
46+
indx += 1
47+
48+
while indx < len(intervals) and intervals[indx][0] <= right :
49+
if intervals[indx][1] > right :
50+
right = intervals[indx][1]
51+
indx += 1
52+
53+
output.append('<b>')
54+
output.append(s[left:right])
55+
output.append('</b>')
56+
57+
if indx < len(intervals) :
58+
output.append(s[right:intervals[indx][0]])
59+
if intervals :
60+
output.append(s[right:])
61+
else :
62+
output.append(s)
63+
64+
return ''.join(output)

0 commit comments

Comments
 (0)