Skip to content

Commit ccde9af

Browse files
committed
10/12/2020
1 parent a593bab commit ccde9af

File tree

3 files changed

+84
-37
lines changed

3 files changed

+84
-37
lines changed

Daily-challenge/Dec/09/README.md

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,35 @@ Could you implement next() and hasNext() to run in average O(1) time and use O(h
4747

4848
## Code
4949
```python
50-
class Solution(object):
51-
def flipAndInvertImage(self, A):
52-
result = []
53-
for row in A:
54-
result.append(list(map(lambda x: 0 if x == 1 else 1, row[::-1])))
55-
return result
56-
```
57-
<!--
58-
var seq1 = new [] { "jumps", "over", "the", "lazy", "dog" };
59-
var seq2 = new [] { "the", "quick", "brown", "fox" };
60-
var res = seq1.SelectMany(n1 =>
61-
seq2.Where (n2 => n1.Length == n2.Length)
62-
.DefaultIfEmpty ()
63-
.Select (n2 => (n1, n2)))
64-
.Count (); -->
50+
# Definition for a binary tree node.
51+
# class TreeNode:
52+
# def __init__(self, val=0, left=None, right=None):
53+
# self.val = val
54+
# self.left = left
55+
# self.right = right
56+
class BSTIterator:
57+
58+
def __init__(self, root):
59+
self.stack = []
60+
while root:
61+
self.stack.append(root)
62+
root = root.left
63+
64+
def hasNext(self):
65+
return len(self.stack) > 0
66+
67+
def next(self):
68+
node = self.stack.pop()
69+
x = node.right
70+
while x:
71+
self.stack.append(x)
72+
x = x.left
73+
return node.val
74+
75+
76+
77+
# Your BSTIterator object will be instantiated and called as such:
78+
# obj = BSTIterator(root)
79+
# param_1 = obj.next()
80+
# param_2 = obj.hasNext()
81+
```

Daily-challenge/Dec/09/sol.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,31 @@
1-
class Solution(object):
2-
def flipAndInvertImage(self, A):
3-
result = []
4-
for row in A:
5-
result.append(list(map(lambda x: 0 if x == 1 else 1, row[::-1])))
6-
return result
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 BSTIterator:
8+
9+
def __init__(self, root):
10+
self.stack = []
11+
while root:
12+
self.stack.append(root)
13+
root = root.left
14+
15+
def hasNext(self):
16+
return len(self.stack) > 0
17+
18+
def next(self):
19+
node = self.stack.pop()
20+
x = node.right
21+
while x:
22+
self.stack.append(x)
23+
x = x.left
24+
return node.val
25+
26+
27+
28+
# Your BSTIterator object will be instantiated and called as such:
29+
# obj = BSTIterator(root)
30+
# param_1 = obj.next()
31+
# param_2 = obj.hasNext()

Daily-challenge/Dec/10/README.md

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
1-
# Flipping an Image
1+
# Valid Mountain Array
2+
Given an array of integers arr, return true if and only if it is a valid mountain array.
23

3-
Solution
4-
Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resulting image.
4+
Recall that arr is a mountain array if and only if:
55

6-
To flip an image horizontally means that each row of the image is reversed. For example, flipping [1, 1, 0] horizontally results in [0, 1, 1].
6+
arr.length >= 3
7+
There exists some i with 0 < i < arr.length - 1 such that:
8+
arr[0] < arr[1] < ... < arr[i - 1] < A[i]
9+
arr[i] > arr[i + 1] > ... > arr[arr.length - 1]
710

8-
To invert an image means that each 0 is replaced by 1, and each 1 is replaced by 0. For example, inverting [0, 1, 1] results in [1, 0, 0].
11+
912

1013
Example 1:
1114

12-
Input: [[1,1,0],[1,0,1],[0,0,0]]
13-
Output: [[1,0,0],[0,1,0],[1,1,1]]
14-
Explanation: First reverse each row: [[0,1,1],[1,0,1],[0,0,0]].
15-
Then, invert the image: [[1,0,0],[0,1,0],[1,1,1]]
15+
Input: arr = [2,1]
16+
Output: false
1617
Example 2:
1718

18-
Input: [[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]]
19-
Output: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
20-
Explanation: First reverse each row: [[0,0,1,1],[1,0,0,1],[1,1,1,0],[0,1,0,1]].
21-
Then invert the image: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
22-
Notes:
19+
Input: arr = [3,5,5]
20+
Output: false
21+
Example 3:
2322

24-
1 <= A.length = A[0].length <= 20
25-
0 <= A[i][j] <= 1<br>
23+
Input: arr = [0,3,2,1]
24+
Output: true
25+
26+
27+
Constraints:
28+
29+
1 <= arr.length <= 104
30+
0 <= arr[i] <= 104<br>
2631

2732
## Idea
2833

0 commit comments

Comments
 (0)