Skip to content

Commit 5c6a798

Browse files
committed
01/12/2020
1 parent b10444d commit 5c6a798

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+2015
-0
lines changed

Daily-challenge/Dec/01/README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Maximum Depth of Binary Tree
2+
Given the root of a binary tree, return its maximum depth.
3+
4+
A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
5+
6+
7+
8+
Example 1:
9+
10+
11+
Input: root = [3,9,20,null,null,15,7]
12+
Output: 3
13+
Example 2:
14+
15+
Input: root = [1,null,2]
16+
Output: 2
17+
Example 3:
18+
19+
Input: root = []
20+
Output: 0
21+
Example 4:
22+
23+
Input: root = [0]
24+
Output: 1
25+
26+
27+
Constraints:
28+
29+
The number of nodes in the tree is in the range [0, 104].
30+
-100 <= Node.val <= 100<br>
31+
32+
## Idea
33+
Just do while loop and convert to decimal
34+
35+
## Code
36+
```python
37+
# Definition for singly-linked list.
38+
# class ListNode:
39+
# def __init__(self, val=0, next=None):
40+
# self.val = val
41+
# self.next = next
42+
class Solution:
43+
def getDecimalValue(self, head: ListNode) -> int:
44+
string = ""
45+
while head:
46+
string += str(head.val)
47+
head = head.next
48+
# print(string)
49+
return int(string, 2)
50+
```

Daily-challenge/Dec/01/sol.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
6+
class Solution:
7+
def getDecimalValue(self, head: ListNode) -> int:
8+
ans = ''
9+
while head:
10+
ans += str(head.val)
11+
head = head.next
12+
return int(ans, 2)

Daily-challenge/Dec/02/README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Insertion Sort List
2+
3+
4+
Sort a linked list using insertion sort.
5+
6+
7+
A graphical example of insertion sort. The partial sorted list (black) initially contains only the first element in the list.
8+
With each iteration one element (red) is removed from the input data and inserted in-place into the sorted list
9+
10+
11+
Algorithm of Insertion Sort:
12+
13+
Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list.
14+
At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there.
15+
It repeats until no input elements remain.
16+
17+
Example 1:
18+
19+
Input: 4->2->1->3
20+
Output: 1->2->3->4
21+
Example 2:
22+
23+
Input: -1->5->3->4->0
24+
Output: -1->0->3->4->5 <br>
25+
26+
## Idea
27+
two ways, one is to use a list and cheat, or we can use standard while loop and extra space for the dummy nodes and etc.
28+
## Code
29+
```python
30+
# Definition for singly-linked list.
31+
# class ListNode:
32+
# def __init__(self, val=0, next=None):
33+
# self.val = val
34+
# self.next = next
35+
class Solution:
36+
def insertionSortList(self, head: ListNode) -> ListNode:
37+
dum = ListNode(0)
38+
prev = dum
39+
40+
while head:
41+
temp = head.next
42+
if prev.val >= head.val:
43+
prev = dum
44+
while prev.next and prev.next.val < head.val:
45+
prev = prev.next
46+
47+
head.next = prev.next
48+
prev.next = head
49+
head = temp
50+
return dum.next
51+
52+
```

Daily-challenge/Dec/02/sol.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
6+
class Solution:
7+
def insertionSortList(self, head: ListNode) -> ListNode:
8+
mylist=[]
9+
ptr=head
10+
while ptr:
11+
mylist.append(ptr.val)
12+
ptr=ptr.next
13+
14+
mylist.sort()
15+
16+
ptr=head
17+
for val in mylist:
18+
ptr.val=val
19+
ptr=ptr.next
20+
21+
return head
22+
23+
# Definition for singly-linked list.
24+
class ListNode:
25+
def __init__(self, val=0, next=None):
26+
self.val = val
27+
self.next = next
28+
class Solution:
29+
def insertionSortList(self, head: ListNode) -> ListNode:
30+
dum = ListNode(0)
31+
prev = dum
32+
33+
while head:
34+
temp = head.next
35+
if prev.val >= head.val:
36+
prev = dum
37+
while prev.next and prev.next.val < head.val:
38+
prev = prev.next
39+
40+
head.next = prev.next
41+
prev.next = head
42+
head = temp
43+
return dum.next
44+

Daily-challenge/Dec/03/README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Consecutive Characters
2+
3+
Solution
4+
Given a string s, the power of the string is the maximum length of a non-empty substring that contains only one unique character.
5+
6+
Return the power of the string.
7+
8+
9+
10+
Example 1:
11+
12+
Input: s = "leetcode"
13+
Output: 2
14+
Explanation: The substring "ee" is of length 2 with the character 'e' only.
15+
Example 2:
16+
17+
Input: s = "abbcccddddeeeeedcba"
18+
Output: 5
19+
Explanation: The substring "eeeee" is of length 5 with the character 'e' only.
20+
Example 3:
21+
22+
Input: s = "triplepillooooow"
23+
Output: 5
24+
Example 4:
25+
26+
Input: s = "hooraaaaaaaaaaay"
27+
Output: 11
28+
Example 5:
29+
30+
Input: s = "tourist"
31+
Output: 1
32+
33+
34+
Constraints:
35+
36+
1 <= s.length <= 500
37+
s contains only lowercase English letters. <br>
38+
39+
## Idea
40+
Just a while loop or for loop is fine
41+
42+
## Code
43+
44+
```python
45+
class Solution:
46+
def maxPower(self, s: str) -> int:
47+
i = 1
48+
n = len(s)
49+
count = 1
50+
res = 1
51+
while i < n:
52+
if s[i] == s[i-1]:
53+
count += 1
54+
res = max(res, count)
55+
else:
56+
# res = max(res, count)
57+
count = 1
58+
i += 1
59+
return res
60+
```

Daily-challenge/Dec/03/sol.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def maxPower(self, s: str) -> int:
3+
i = 1
4+
n = len(s)
5+
count = 1
6+
res = 1
7+
while i < n:
8+
if s[i] == s[i-1]:
9+
count += 1
10+
res = max(res, count)
11+
else:
12+
# res = max(res, count)
13+
count = 1
14+
i += 1
15+
return res

Daily-challenge/Dec/04/README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Minimum Height Trees
2+
3+
Solution
4+
A tree is an undirected graph in which any two vertices are connected by exactly one path. In other words, any connected graph without simple cycles is a tree.
5+
6+
Given a tree of n nodes labelled from 0 to n - 1, and an array of n - 1 edges where edges[i] = [ai, bi] indicates that there is an undirected edge between the two nodes ai and bi in the tree, you can choose any node of the tree as the root. When you select a node x as the root, the result tree has height h. Among all possible rooted trees, those with minimum height (i.e. min(h)) are called minimum height trees (MHTs).
7+
8+
Return a list of all MHTs' root labels. You can return the answer in any order.
9+
10+
The height of a rooted tree is the number of edges on the longest downward path between the root and a leaf.
11+
12+
13+
14+
Example 1:
15+
16+
17+
Input: n = 4, edges = [[1,0],[1,2],[1,3]]
18+
Output: [1]
19+
Explanation: As shown, the height of the tree is 1 when the root is the node with label 1 which is the only MHT.
20+
Example 2:
21+
22+
23+
Input: n = 6, edges = [[3,0],[3,1],[3,2],[3,4],[5,4]]
24+
Output: [3,4]
25+
Example 3:
26+
27+
Input: n = 1, edges = []
28+
Output: [0]
29+
Example 4:
30+
31+
Input: n = 2, edges = [[0,1]]
32+
Output: [0,1]
33+
34+
35+
Constraints:
36+
37+
1 <= n <= 2 * 104
38+
edges.length == n - 1
39+
0 <= ai, bi < n
40+
ai != bi
41+
All the pairs (ai, bi) are distinct.
42+
The given input is guaranteed to be a tree and there will be no repeated edges. <br>
43+
44+
## Idea
45+
It is a really really hard one for me
46+
47+
## Code
48+
```python
49+
class Solution:
50+
def findMinHeightTrees(self, n, edges):
51+
neighbors = collections.defaultdict(set)
52+
for v, w in edges:
53+
neighbors[v].add(w)
54+
neighbors[w].add(v)
55+
def maxpath(v, visited):
56+
visited.add(v)
57+
paths = [maxpath(w, visited) for w in neighbors[v] if w not in visited]
58+
path = max(paths or [[]], key=len)
59+
path.append(v)
60+
return path
61+
path = maxpath(0, set())
62+
path = maxpath(path[0], set())
63+
m = len(path)
64+
return path[(m-1)//2:m//2+1]
65+
```

Daily-challenge/Dec/04/sol.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def findMinHeightTrees(self, n, edges):
3+
neighbors = collections.defaultdict(set)
4+
for v, w in edges:
5+
neighbors[v].add(w)
6+
neighbors[w].add(v)
7+
def maxpath(v, visited):
8+
visited.add(v)
9+
paths = [maxpath(w, visited) for w in neighbors[v] if w not in visited]
10+
path = max(paths or [[]], key=len)
11+
path.append(v)
12+
return path
13+
path = maxpath(0, set())
14+
path = maxpath(path[0], set())
15+
m = len(path)
16+
return path[(m-1)//2:m//2+1]

Daily-challenge/Dec/05/README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Minimum Cost to Move Chips to The Same Position
2+
We have n chips, where the position of the ith chip is position[i].
3+
4+
We need to move all the chips to the same position. In one step, we can change the position of the ith chip from position[i] to:
5+
6+
position[i] + 2 or position[i] - 2 with cost = 0.
7+
position[i] + 1 or position[i] - 1 with cost = 1.
8+
Return the minimum cost needed to move all the chips to the same position.
9+
10+
11+
12+
Example 1:
13+
14+
15+
Input: position = [1,2,3]
16+
Output: 1
17+
Explanation: First step: Move the chip at position 3 to position 1 with cost = 0.
18+
Second step: Move the chip at position 2 to position 1 with cost = 1.
19+
Total cost is 1.
20+
Example 2:
21+
22+
23+
Input: position = [2,2,2,3,3]
24+
Output: 2
25+
Explanation: We can move the two chips at poistion 3 to position 2. Each move has cost = 1. The total cost = 2.
26+
Example 3:
27+
28+
Input: position = [1,1000000000]
29+
Output: 1
30+
31+
32+
Constraints:
33+
34+
1 <= position.length <= 100
35+
1 <= position[i] <= 10^9<br>
36+
## Idea
37+
It is really intestring, it is not a good question. THere is a very very simple solution but u need to be really abstract
38+
## Code
39+
```python
40+
class Solution:
41+
def minCostToMoveChips(self, position: List[int]) -> int:
42+
eve = 0
43+
odd = 0
44+
for i in position:
45+
if i % 2 == 0:
46+
eve += 1
47+
else:
48+
odd += 1
49+
50+
return min(eve, odd)
51+
```

Daily-challenge/Dec/05/sol.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def minCostToMoveChips(self, position: List[int]) -> int:
3+
eve = 0
4+
odd = 0
5+
for i in position:
6+
if i % 2 == 0:
7+
eve += 1
8+
else:
9+
odd += 1
10+
11+
return min(eve, odd)

0 commit comments

Comments
 (0)