Skip to content

Commit 318d0b6

Browse files
committed
Passed driver liceniceed test aye
1 parent e4b650f commit 318d0b6

File tree

6 files changed

+175
-59
lines changed

6 files changed

+175
-59
lines changed

Daily-challenge/Dec/27/README.md

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,48 @@
1-
# Partition Equal Subset Sum
2-
Given a non-empty array nums containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.
1+
# Jump Game IV
2+
3+
Solution
4+
Given an array of integers arr, you are initially positioned at the first index of the array.
5+
6+
In one step you can jump from index i to index:
7+
8+
i + 1 where: i + 1 < arr.length.
9+
i - 1 where: i - 1 >= 0.
10+
j where: arr[i] == arr[j] and i != j.
11+
Return the minimum number of steps to reach the last index of the array.
12+
13+
Notice that you can not jump outside of the array at any time.
314

415

516

617
Example 1:
718

8-
Input: nums = [1,5,11,5]
9-
Output: true
10-
Explanation: The array can be partitioned as [1, 5, 5] and [11].
19+
Input: arr = [100,-23,-23,404,100,23,23,23,3,404]
20+
Output: 3
21+
Explanation: You need three jumps from index 0 --> 4 --> 3 --> 9. Note that index 9 is the last index of the array.
1122
Example 2:
1223

13-
Input: nums = [1,2,3,5]
14-
Output: false
15-
Explanation: The array cannot be partitioned into equal sum subsets.
24+
Input: arr = [7]
25+
Output: 0
26+
Explanation: Start index is the last index. You don't need to jump.
27+
Example 3:
28+
29+
Input: arr = [7,6,9,6,9,6,9,7]
30+
Output: 1
31+
Explanation: You can jump directly from index 0 to index 7 which is last index of the array.
32+
Example 4:
33+
34+
Input: arr = [6,1,9]
35+
Output: 2
36+
Example 5:
37+
38+
Input: arr = [11,22,7,7,7,7,7,7,7,22,13]
39+
Output: 3
1640

1741

1842
Constraints:
1943

20-
1 <= nums.length <= 200
21-
1 <= nums[i] <= 100<br>
44+
1 <= arr.length <= 5 * 10^4
45+
-10^8 <= arr[i] <= 10^8<br>
2246

2347
## Idea
2448
Just do while loop and convert to decimal

Daily-challenge/Dec/29/README.md

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,34 @@
1-
# Jump Game III
2-
Given an array of non-negative integers arr, you are initially positioned at start index of the array. When you are at index i, you can jump to i + arr[i] or i - arr[i], check if you can reach to any index with value 0.
1+
# Pseudo-Palindromic Paths in a Binary Tree
2+
Given a binary tree where node values are digits from 1 to 9. A path in the binary tree is said to be pseudo-palindromic if at least one permutation of the node values in the path is a palindrome.
33

4-
Notice that you can not jump outside of the array at any time.
4+
Return the number of pseudo-palindromic paths going from the root node to leaf nodes.
55

66

77

88
Example 1:
99

10-
Input: arr = [4,2,3,0,3,1,2], start = 5
11-
Output: true
12-
Explanation:
13-
All possible ways to reach at index 3 with value 0 are:
14-
index 5 -> index 4 -> index 1 -> index 3
15-
index 5 -> index 6 -> index 4 -> index 1 -> index 3
10+
11+
12+
Input: root = [2,3,1,3,1,null,1]
13+
Output: 2
14+
Explanation: The figure above represents the given binary tree. There are three paths going from the root node to leaf nodes: the red path [2,3,3], the green path [2,1,1], and the path [2,3,1]. Among these paths only red path and green path are pseudo-palindromic paths since the red path [2,3,3] can be rearranged in [3,2,3] (palindrome) and the green path [2,1,1] can be rearranged in [1,2,1] (palindrome).
1615
Example 2:
1716

18-
Input: arr = [4,2,3,0,3,1,2], start = 0
19-
Output: true
20-
Explanation:
21-
One possible way to reach at index 3 with value 0 is:
22-
index 0 -> index 4 -> index 1 -> index 3
17+
18+
19+
Input: root = [2,1,1,1,3,null,null,null,null,null,1]
20+
Output: 1
21+
Explanation: The figure above represents the given binary tree. There are three paths going from the root node to leaf nodes: the green path [2,1,1], the path [2,1,3,1], and the path [2,1]. Among these paths only the green path is pseudo-palindromic since [2,1,1] can be rearranged in [1,2,1] (palindrome).
2322
Example 3:
2423

25-
Input: arr = [3,0,2,1,2], start = 2
26-
Output: false
27-
Explanation: There is no way to reach at index 1 with value 0.
24+
Input: root = [9]
25+
Output: 1
2826

2927

3028
Constraints:
3129

32-
1 <= arr.length <= 5 * 10^4
33-
0 <= arr[i] < arr.length
34-
0 <= start < arr.length<br>
30+
The given binary tree will have between 1 and 10^5 nodes.
31+
Node values are digits from 1 to 9.<br>
3532

3633
## Idea
3734
Just do while loop and convert to decimal

Daily-challenge/Dec/29/sol.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +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
17
class Solution:
2-
def canReach(self, arr: List[int], start: int) -> bool:
3-
n = len(arr)
8+
def dfs(self, root):
9+
if not root: return
410

5-
idx = start
6-
q = [start]
11+
cur, pal = self.dict_freq[root.val], self.Pal
712

8-
while q:
13+
self.Pal = self.Pal - 1 if cur == 1 else self.Pal + 1
14+
self.dict_freq[root.val] = (cur + 1) % 2
15+
16+
if not root.left and not root.right and self.Pal <= 1:
17+
self.Res += 1
18+
19+
self.dfs(root.left)
20+
self.dfs(root.right)
21+
22+
self.Pal, self.dict_freq[root.val] = pal, cur
923

10-
node = q.pop(0)
11-
if arr[node] == 0:
12-
return True
13-
if arr[node] < 0:
14-
continue
15-
16-
for i in [node + arr[node], node - arr[node]]:
17-
if 0 <= i < n:
18-
q.append(i)
19-
20-
arr[node] = -arr[node]
24+
def pseudoPalindromicPaths (self, root):
25+
self.dict_freq = defaultdict(int)
26+
self.Pal, self.Res = 0, 0
27+
self.dfs(root)
28+
return self.Res
2129

2230

2331

Daily-challenge/Dec/30/README.md

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,40 @@
1-
# The Skyline Problem
1+
# Game of Life
2+
According to Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970."
23

3-
Solution
4-
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are given the locations and height of all the buildings as shown on a cityscape photo (Figure A), write a program to output the skyline formed by these buildings collectively (Figure B).
4+
The board is made up of an m x n grid of cells, where each cell has an initial state: live (represented by a 1) or dead (represented by a 0). Each cell interacts with its eight neighbors (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):
55

6-
Buildings Skyline Contour
7-
The geometric information of each building is represented by a triplet of integers [Li, Ri, Hi], where Li and Ri are the x coordinates of the left and right edge of the ith building, respectively, and Hi is its height. It is guaranteed that 0 ≤ Li, Ri ≤ INT_MAX, 0 < Hi ≤ INT_MAX, and Ri - Li > 0. You may assume all buildings are perfect rectangles grounded on an absolutely flat surface at height 0.
6+
Any live cell with fewer than two live neighbors dies as if caused by under-population.
7+
Any live cell with two or three live neighbors lives on to the next generation.
8+
Any live cell with more than three live neighbors dies, as if by over-population.
9+
Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
10+
The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously. Given the current state of the m x n grid board, return the next state.
811

9-
For instance, the dimensions of all buildings in Figure A are recorded as: [ [2 9 10], [3 7 15], [5 12 12], [15 20 10], [19 24 8] ] .
12+
1013

11-
The output is a list of "key points" (red dots in Figure B) in the format of [ [x1,y1], [x2, y2], [x3, y3], ... ] that uniquely defines a skyline. A key point is the left endpoint of a horizontal line segment. Note that the last key point, where the rightmost building ends, is merely used to mark the termination of the skyline, and always has zero height. Also, the ground in between any two adjacent buildings should be considered part of the skyline contour.
14+
Example 1:
1215

13-
For instance, the skyline in Figure B should be represented as:[ [2 10], [3 15], [7 12], [12 0], [15 10], [20 8], [24, 0] ].
1416

15-
Notes:
17+
Input: board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]]
18+
Output: [[0,0,0],[1,0,1],[0,1,1],[0,1,0]]
19+
Example 2:
1620

17-
The number of buildings in any input list is guaranteed to be in the range [0, 10000].
18-
The input list is already sorted in ascending order by the left x position Li.
19-
The output list must be sorted by the x position.
20-
There must be no consecutive horizontal lines of equal height in the output skyline. For instance, [...[2 3], [4 5], [7 5], [11 5], [12 7]...] is not acceptable; the three lines of height 5 should be merged into one in the final output as such: [...[2 3], [4 5], [12 7], ...]<br>
21+
22+
Input: board = [[1,1],[1,0]]
23+
Output: [[1,1],[1,1]]
24+
25+
26+
Constraints:
27+
28+
m == board.length
29+
n == board[i].length
30+
1 <= m, n <= 25
31+
board[i][j] is 0 or 1.
32+
33+
34+
Follow up:
35+
36+
Could you solve it in-place? Remember that the board needs to be updated simultaneously: You cannot update some cells first and then use their updated values to update other cells.
37+
In this question, we represent the board using a 2D array. In principle, the board is infinite, which would cause problems when the active area encroaches upon the border of the array (i.e., live cells reach the border). How would you address these problems?<br>
2138

2239
## Idea
2340
Just do while loop and convert to decimal

Daily-challenge/Dec/31/README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Game of Life
2+
According to Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970."
3+
4+
The board is made up of an m x n grid of cells, where each cell has an initial state: live (represented by a 1) or dead (represented by a 0). Each cell interacts with its eight neighbors (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):
5+
6+
Any live cell with fewer than two live neighbors dies as if caused by under-population.
7+
Any live cell with two or three live neighbors lives on to the next generation.
8+
Any live cell with more than three live neighbors dies, as if by over-population.
9+
Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
10+
The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously. Given the current state of the m x n grid board, return the next state.
11+
12+
13+
14+
Example 1:
15+
16+
17+
Input: board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]]
18+
Output: [[0,0,0],[1,0,1],[0,1,1],[0,1,0]]
19+
Example 2:
20+
21+
22+
Input: board = [[1,1],[1,0]]
23+
Output: [[1,1],[1,1]]
24+
25+
26+
Constraints:
27+
28+
m == board.length
29+
n == board[i].length
30+
1 <= m, n <= 25
31+
board[i][j] is 0 or 1.
32+
33+
34+
Follow up:
35+
36+
Could you solve it in-place? Remember that the board needs to be updated simultaneously: You cannot update some cells first and then use their updated values to update other cells.
37+
In this question, we represent the board using a 2D array. In principle, the board is infinite, which would cause problems when the active area encroaches upon the border of the array (i.e., live cells reach the border). How would you address these problems?<br>
38+
39+
## Idea
40+
Just do while loop and convert to decimal
41+
42+
## Code
43+
```python
44+
class Solution:
45+
def getSkyline(self, buildings):
46+
events = sorted([(L, -H, R) for L, R, H in buildings] + list({(R, 0, None) for _, R, _ in buildings}))
47+
res, hp = [[0, 0]], [(0, float("inf"))]
48+
for x, negH, R in events:
49+
while x >= hp[0][1]:
50+
heapq.heappop(hp)
51+
if negH:
52+
heapq.heappush(hp, (negH, R))
53+
if res[-1][1] + hp[0][0]:
54+
res += [x, -hp[0][0]],
55+
return res[1:]
56+
57+
```

Daily-challenge/Dec/31/sol.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def getSkyline(self, buildings):
3+
events = sorted([(L, -H, R) for L, R, H in buildings] + list({(R, 0, None) for _, R, _ in buildings}))
4+
res, hp = [[0, 0]], [(0, float("inf"))]
5+
for x, negH, R in events:
6+
while x >= hp[0][1]:
7+
heapq.heappop(hp)
8+
if negH:
9+
heapq.heappush(hp, (negH, R))
10+
if res[-1][1] + hp[0][0]:
11+
res += [x, -hp[0][0]],
12+
return res[1:]
13+

0 commit comments

Comments
 (0)