Skip to content

Commit a81c405

Browse files
committed
Additional work + daily
1 parent a2b69cd commit a81c405

16 files changed

+445
-98
lines changed

Diff for: README.md

+104-98
Large diffs are not rendered by default.

Diff for: my-submissions/e1791 v1 Inefficient Counter.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def findCenter(self, edges: List[List[int]]) -> int:
3+
edgeCounter = defaultdict(int)
4+
5+
for edge in edges :
6+
edgeCounter[edge[0]] += 1
7+
edgeCounter[edge[1]] += 1
8+
9+
return max([x for x in edgeCounter], key=lambda x:edgeCounter.get(x))
10+

Diff for: my-submissions/e1791 v2 oneliner single if.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def findCenter(self, edges: List[List[int]]) -> int:
3+
return edges[0][0] if edges[0][0] in edges[1] else edges[0][1]

Diff for: my-submissions/e1791 v2.c

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#define findCenter(edges, edgeSize, edgesColSize) ((edges[0][0] == edges[1][0] || edges[0][0] == edges[1][1]) ? edges[0][0] : edges[0][1])

Diff for: my-submissions/e1791 v2.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution {
2+
public:
3+
int findCenter(vector<vector<int>>& edges) {
4+
if (edges[0][0] == edges[1][0] || edges[0][0] == edges[1][1])
5+
return edges[0][0];
6+
else
7+
return edges[0][1];
8+
}
9+
};

Diff for: my-submissions/e1791 v2.java

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution {
2+
public int findCenter(int[][] edges) {
3+
return (edges[0][0] == edges[1][0] || edges[0][0] == edges[1][1]) ? edges[0][0] : edges[0][1];
4+
}
5+
}

Diff for: my-submissions/m1660 v1.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def correctBinaryTree(self, root: TreeNode) -> TreeNode:
9+
toVisit = [(root, 0)] # (curr, depth)
10+
depths = {}
11+
12+
while toVisit :
13+
curr, depth = toVisit.pop()
14+
if not curr :
15+
continue
16+
if curr in depths and depths[curr] < depth :
17+
continue
18+
19+
depths[curr] = depth
20+
depth += 1
21+
22+
toVisit.append((curr.left, depth))
23+
toVisit.append((curr.right, depth))
24+
25+
toVisit.append((root))
26+
27+
while toVisit :
28+
parent = toVisit.pop()
29+
30+
if parent.left and \
31+
((parent.left.left and depths[parent.left] == depths[parent.left.left]) or \
32+
(parent.left.right and depths[parent.left] == depths[parent.left.right])) :
33+
parent.left = None
34+
break
35+
36+
if parent.right and \
37+
((parent.right.right and depths[parent.right] == depths[parent.right.right]) or \
38+
(parent.right.left and depths[parent.right] == depths[parent.right.left])) :
39+
parent.right = None
40+
break
41+
42+
if parent.right :
43+
toVisit.append(parent.right)
44+
if parent.left :
45+
toVisit.append(parent.left)
46+
47+
48+
return root
49+

Diff for: my-submissions/m1660 v2 One Pass.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def correctBinaryTree(self, root: TreeNode) -> TreeNode:
9+
toVisit = [(root, None, 0)] # (curr, depth)
10+
depths = {}
11+
12+
while toVisit :
13+
curr, parent, depth = toVisit.pop()
14+
if not curr :
15+
continue
16+
17+
if curr.left in depths or curr.right in depths :
18+
if parent.left == curr :
19+
parent.left = None
20+
else :
21+
parent.right = None
22+
break
23+
24+
depths[curr] = depth
25+
depth += 1
26+
27+
toVisit.append((curr.left, curr, depth))
28+
toVisit.append((curr.right, curr, depth))
29+
30+
return root
31+

Diff for: my-submissions/m1743 v2.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution:
2+
def restoreArray(self, adjacentPairs: List[List[int]]) -> List[int]:
3+
mappings = defaultdict(list)
4+
starters = set()
5+
6+
# Mapping
7+
for pair in adjacentPairs :
8+
mappings[pair[0]].append(pair[1])
9+
mappings[pair[1]].append(pair[0])
10+
11+
# Find starter
12+
starter = -1
13+
for k, v in mappings.items() :
14+
if len(v) == 1 :
15+
starter = k
16+
break
17+
18+
19+
20+
output = [starter]
21+
while len(output) <= len(adjacentPairs) :
22+
options = mappings[output[-1]]
23+
24+
if len(options) <= 1 :
25+
output.append(options[0])
26+
elif output[-2] != options[0] :
27+
output.append(options[0])
28+
else :
29+
output.append(options[1])
30+
31+
return output

Diff for: my-submissions/m1743.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution:
2+
def restoreArray(self, adjacentPairs: List[List[int]]) -> List[int]:
3+
mappings = defaultdict(List[int])
4+
starters = set()
5+
6+
for pair in adjacentPairs :
7+
if pair[0] in mappings :
8+
mappings[pair[0]].append(pair[1])
9+
else :
10+
mappings[pair[0]] = [pair[1]]
11+
12+
if pair[1] in mappings :
13+
mappings[pair[1]].append(pair[0])
14+
else :
15+
mappings[pair[1]] = [pair[0]]
16+
17+
if pair[0] in starters :
18+
starters.remove(pair[0])
19+
else :
20+
starters.add(pair[0])
21+
22+
if pair[1] in starters :
23+
starters.remove(pair[1])
24+
else :
25+
starters.add(pair[1])
26+
27+
output = [list(starters)[0]]
28+
while len(output) <= len(adjacentPairs) :
29+
options = mappings[output[-1]]
30+
31+
if len(options) <= 1 :
32+
output.append(options[0])
33+
elif output[-2] != options[0] :
34+
output.append(options[0])
35+
else :
36+
output.append(options[1])
37+
38+
return output

Diff for: my-submissions/m1980 v1 trie.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution:
2+
def findDifferentBinaryString(self, nums: List[str]) -> str:
3+
trie = {}
4+
5+
for num in nums :
6+
curr = trie
7+
for c in num :
8+
curr[-1] = curr.get(-1, 0) + 1 # Count cases
9+
if c not in curr :
10+
curr[c] = {}
11+
curr = curr[c]
12+
13+
output = []
14+
15+
while len(output) < len(nums[0]) :
16+
if not trie :
17+
output.append('0')
18+
elif len(trie) == 3 :
19+
zero = trie['0'].get(-1, 0)
20+
one = trie['1'].get(-1, 0)
21+
22+
if zero > one :
23+
output.append('0')
24+
trie = trie.get('0')
25+
else :
26+
output.append('1')
27+
trie = trie.get('1')
28+
elif '0' in trie :
29+
output.append('1')
30+
trie = None
31+
else :
32+
output.append('0')
33+
trie = None
34+
35+
36+
return ''.join(output)
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def findDifferentBinaryString(self, nums: List[str]) -> str:
3+
output = []
4+
5+
for i in range(len(nums[0])) :
6+
output.append('0' if nums[i][i] == '1' else '1')
7+
8+
9+
return ''.join(output)

Diff for: my-submissions/m1980 v2.c

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
char* findDifferentBinaryString(char** nums, int numsSize) {
2+
char* output = (char*) malloc(sizeof(char) * (numsSize + 1));
3+
4+
for (int i = 0; i < numsSize; i++) {
5+
output[i] = (nums[i][i] == '1') ? '0' : '1';
6+
}
7+
output[numsSize] = 0;
8+
9+
return output;
10+
}

Diff for: my-submissions/m2352 brute force.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def equalPairs(self, grid: List[List[int]]) -> int:
3+
horizontal = {}
4+
vertical = {}
5+
6+
for i, row in enumerate(grid) :
7+
horizontal[i] = tuple(row)
8+
9+
for j in range(len(grid[0])) :
10+
vertical[j] = tuple([grid[i][j] for i in range(len(grid))])
11+
12+
output = 0
13+
for i in range(len(grid)) :
14+
for j in range(len(grid[0])) :
15+
if horizontal[i] == vertical[j] :
16+
output += 1
17+
18+
return output

Diff for: my-submissions/m430.cpp

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
// Definition for a Node.
3+
class Node {
4+
public:
5+
int val;
6+
Node* prev;
7+
Node* next;
8+
Node* child;
9+
};
10+
*/
11+
12+
class Solution {
13+
public:
14+
Node* flatten(Node* head) {
15+
if (!head) {
16+
return NULL;
17+
}
18+
19+
if (!head->child) {
20+
head->next = flatten(head->next);
21+
return head;
22+
}
23+
24+
if (!head->next) {
25+
head->next = flatten(head->child);
26+
head->child = NULL;
27+
head->next->prev = head;
28+
return head;
29+
}
30+
31+
Node* holder = head->next;
32+
head->next = flatten(head->child);
33+
head->next->prev = head;
34+
head->child = NULL;
35+
36+
Node* output = head;
37+
38+
while (head->next) {
39+
head = head->next;
40+
}
41+
42+
head->next = flatten(holder);
43+
head->next->prev = head;
44+
45+
return output;
46+
}
47+
};

Diff for: my-submissions/m430.java

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
// Definition for a Node.
3+
class Node {
4+
public int val;
5+
public Node prev;
6+
public Node next;
7+
public Node child;
8+
};
9+
*/
10+
11+
class Solution {
12+
public Node flatten(Node head) {
13+
if (head == null) {
14+
return null;
15+
}
16+
17+
if (head.child == null) {
18+
head.next = flatten(head.next);
19+
return head;
20+
}
21+
22+
if (head.next == null) {
23+
head.next = flatten(head.child);
24+
head.child = null;
25+
head.next.prev = head;
26+
27+
return head;
28+
}
29+
30+
Node holder = head.next;
31+
head.next = flatten(head.child);
32+
head.next.prev = head;
33+
head.child = null;
34+
35+
Node output = head;
36+
while (head.next != null) {
37+
head = head.next;
38+
}
39+
head.next = flatten(holder);
40+
head.next.prev = head;
41+
42+
return output;
43+
}
44+
}

0 commit comments

Comments
 (0)