Skip to content

Commit 343c4a8

Browse files
committed
12/12/2020
1 parent 8d73e6a commit 343c4a8

File tree

3 files changed

+70
-32
lines changed

3 files changed

+70
-32
lines changed

Daily-challenge/Dec/11/README.md

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,27 @@ nums is sorted in ascending order.<br>
4444
## Code
4545
```python
4646
class Solution:
47-
def validSquare(self, p1, p2, p3, p4):
48-
if p1==p2==p3==p4:return False
49-
def dist(x,y):
50-
return (x[0]-y[0])**2+(x[1]-y[1])**2
51-
ls=[dist(p1,p2),dist(p1,p3),dist(p1,p4),dist(p2,p3),dist(p2,p4),dist(p3,p4)]
52-
ls.sort()
53-
if ls[0]==ls[1]==ls[2]==ls[3]:
54-
if ls[4]==ls[5]:
55-
return True
56-
return False
47+
def removeDuplicates(self, nums: List[int]) -> int:
48+
if len(nums) <= 1:
49+
return len(nums)
50+
idx = 0
51+
52+
count = 0
53+
54+
while idx < len(nums)-1:
55+
# print(idx)
56+
if nums[idx] == nums[idx+1]:
57+
count += 1
58+
if nums[idx] != nums[idx+1]:
59+
count = 0
60+
61+
62+
if count >= 2:
63+
del nums[idx]
64+
else:
65+
idx += 1
66+
return len(nums)
67+
68+
5769
```
5870

Daily-challenge/Dec/11/sol.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
class Solution:
2-
def validSquare(self, p1, p2, p3, p4):
3-
if p1==p2==p3==p4:return False
4-
def dist(x,y):
5-
return (x[0]-y[0])**2+(x[1]-y[1])**2
6-
ls=[dist(p1,p2),dist(p1,p3),dist(p1,p4),dist(p2,p3),dist(p2,p4),dist(p3,p4)]
7-
ls.sort()
8-
if ls[0]==ls[1]==ls[2]==ls[3]:
9-
if ls[4]==ls[5]:
10-
return True
11-
return False
2+
def removeDuplicates(self, nums: List[int]) -> int:
3+
if len(nums) <= 1:
4+
return len(nums)
5+
idx = 0
6+
7+
count = 0
8+
9+
while idx < len(nums)-1:
10+
# print(idx)
11+
if nums[idx] == nums[idx+1]:
12+
count += 1
13+
if nums[idx] != nums[idx+1]:
14+
count = 0
15+
16+
17+
if count >= 2:
18+
del nums[idx]
19+
else:
20+
idx += 1
21+
return len(nums)
22+
23+

Daily-challenge/Dec/12/README.md

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,41 @@
1-
# Permutations II
1+
# Smallest Subtree with all the Deepest Nodes
2+
Given the root of a binary tree, the depth of each node is the shortest distance to the root.
23

3-
Solution
4-
Given a collection of numbers, nums, that might contain duplicates, return all possible unique permutations in any order.
4+
Return the smallest subtree such that it contains all the deepest nodes in the original tree.
5+
6+
A node is called the deepest if it has the largest depth possible among any node in the entire tree.
7+
8+
The subtree of a node is tree consisting of that node, plus the set of all descendants of that node.
9+
10+
Note: This question is the same as 1123: https://leetcode.com/problems/lowest-common-ancestor-of-deepest-leaves/
511

612

713

814
Example 1:
915

10-
Input: nums = [1,1,2]
11-
Output:
12-
[[1,1,2],
13-
[1,2,1],
14-
[2,1,1]]
16+
17+
Input: root = [3,5,1,6,2,0,8,null,null,7,4]
18+
Output: [2,7,4]
19+
Explanation: We return the node with value 2, colored in yellow in the diagram.
20+
The nodes coloured in blue are the deepest nodes of the tree.
21+
Notice that nodes 5, 3 and 2 contain the deepest nodes in the tree but node 2 is the smallest subtree among them, so we return it.
1522
Example 2:
1623

17-
Input: nums = [1,2,3]
18-
Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
24+
Input: root = [1]
25+
Output: [1]
26+
Explanation: The root is the deepest node in the tree.
27+
Example 3:
28+
29+
Input: root = [0,1,3,null,2]
30+
Output: [2]
31+
Explanation: The deepest node in the tree is 2, the valid subtrees are the subtrees of nodes 2, 1 and 0 but the subtree of node 2 is the smallest.
1932

2033

2134
Constraints:
2235

23-
1 <= nums.length <= 8
24-
-10 <= nums[i] <= 10 <br>
36+
The number of nodes in the tree will be in the range [1, 500].
37+
0 <= Node.val <= 500
38+
The values of the nodes in the tree are unique. <br>
2539

2640
## Idea
2741

0 commit comments

Comments
 (0)