Skip to content

Commit 9099c53

Browse files
committed
17/12/2020
1 parent 28f6ed6 commit 9099c53

File tree

2 files changed

+50
-68
lines changed

2 files changed

+50
-68
lines changed

Daily-challenge/Dec/16/README.md

Lines changed: 34 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,57 @@
1-
# Longest Mountain in Array
2-
Let's call any (contiguous) subarray B (of A) a mountain if the following properties hold:
1+
# Validate Binary Search Tree
32

4-
B.length >= 3
5-
There exists some 0 < i < B.length - 1 such that B[0] < B[1] < ... B[i-1] < B[i] > B[i+1] > ... > B[B.length - 1]
6-
(Note that B could be any subarray of A, including the entire array A.)
3+
Solution
4+
Given the root of a binary tree, determine if it is a valid binary search tree (BST).
75

8-
Given an array A of integers, return the length of the longest mountain.
6+
A valid BST is defined as follows:
97

10-
Return 0 if there is no mountain.
8+
The left subtree of a node contains only nodes with keys less than the node's key.
9+
The right subtree of a node contains only nodes with keys greater than the node's key.
10+
Both the left and right subtrees must also be binary search trees.
11+
1112

1213
Example 1:
1314

14-
Input: [2,1,4,7,3,2,5]
15-
Output: 5
16-
Explanation: The largest mountain is [1,4,7,3,2] which has length 5.
15+
16+
Input: root = [2,1,3]
17+
Output: true
1718
Example 2:
1819

19-
Input: [2,2,2]
20-
Output: 0
21-
Explanation: There is no mountain.
22-
Note:
2320

24-
0 <= A.length <= 10000
25-
0 <= A[i] <= 10000
26-
Follow up:
21+
Input: root = [5,1,4,null,null,3,6]
22+
Output: false
23+
Explanation: The root node's value is 5 but its right child's value is 4.
24+
25+
26+
Constraints:
2727

28-
Can you solve it using only one pass?
29-
Can you solve it in O(1) space?<br>
28+
The number of nodes in the tree is in the range [1, 104].
29+
-231 <= Node.val <= 231 - 1<br>
3030

3131
## Idea
3232

3333
## Code
3434
```python
35+
# Definition for a binary tree node.
36+
# class TreeNode:
37+
# def __init__(self, val=0, left=None, right=None):
38+
# self.val = val
39+
# self.left = left
40+
# self.right = right
3541
class Solution:
36-
def longestMountain(self, A: List[int]) -> int:
37-
total = 0
38-
39-
length = len(A)
40-
if length <= 2:
41-
return 0
42+
def isValidBST(self, root: TreeNode) -> bool:
4243

43-
pointer = 0
4444

45-
while pointer <= length-2:
46-
base = pointer
47-
while pointer + 1 < length and A[pointer] < A[pointer+1]:
48-
pointer += 1
45+
def preorder(root, left, right):
46+
if not root:
47+
return True
48+
if not left < root.val < right:
49+
return False
4950

50-
if pointer == base:
51-
pointer += 1
52-
continue
53-
54-
peak = pointer
55-
while pointer + 1 < length and A[pointer] > A[pointer+1]:
56-
pointer += 1
57-
58-
if peak == pointer:
59-
continue
60-
61-
total = max(total, pointer - base + 1)
51+
return (preorder(root.left, left, root.val) and preorder(root.right, root.val, right))
52+
53+
return preorder(root, float("-inf"), float("inf"))
6254

63-
return total
6455

6556
```
6657

Daily-challenge/Dec/16/sol.py

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,20 @@
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
17
class Solution:
2-
def longestMountain(self, A: List[int]) -> int:
3-
total = 0
8+
def isValidBST(self, root: TreeNode) -> bool:
49

5-
length = len(A)
6-
if length <= 2:
7-
return 0
810

9-
pointer = 0
10-
11-
while pointer <= length-2:
12-
base = pointer
13-
while pointer + 1 < length and A[pointer] < A[pointer+1]:
14-
pointer += 1
15-
16-
if pointer == base:
17-
pointer += 1
18-
continue
19-
20-
peak = pointer
21-
while pointer + 1 < length and A[pointer] > A[pointer+1]:
22-
pointer += 1
11+
def preorder(root, left, right):
12+
if not root:
13+
return True
14+
if not left < root.val < right:
15+
return False
2316

24-
if peak == pointer:
25-
continue
26-
27-
total = max(total, pointer - base + 1)
28-
29-
return total
17+
return (preorder(root.left, left, root.val) and preorder(root.right, root.val, right))
18+
19+
return preorder(root, float("-inf"), float("inf"))
20+

0 commit comments

Comments
 (0)