Skip to content

Commit 705f27e

Browse files
committed
happy new year
1 parent 318d0b6 commit 705f27e

File tree

4 files changed

+132
-71
lines changed

4 files changed

+132
-71
lines changed

Daily-challenge/Dec/30/README.md

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,39 @@ Just do while loop and convert to decimal
4242
## Code
4343
```python
4444
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:]
45+
def gameOfLife(self, board: List[List[int]]) -> None:
46+
"""
47+
Do not return anything, modify board in-place instead.
48+
"""
49+
m = len(board)
50+
n = len(board[0])
51+
52+
53+
for i in range(m):
54+
for j in range(n):
55+
if board[i][j] == 0 or board[i][j] == 2:
56+
if self.determineLive(board,i,j) == 3:
57+
board[i][j] = 2
58+
else:
59+
if self.determineLive(board,i,j) < 2 or self.determineLive(board,i,j) >3:
60+
board[i][j] = 3
61+
for i in range(m):
62+
for j in range(n):
63+
if board[i][j] == 2: board[i][j] = 1
64+
if board[i][j] == 3: board[i][j] = 0
65+
66+
67+
def determineLive(self, board, i, j):
68+
m,n = len(board), len(board[0])
69+
count = 0
70+
if i-1 >= 0 and j-1 >= 0: count += board[i-1][j-1]%2
71+
if i-1 >= 0: count += board[i-1][j]%2
72+
if i-1 >= 0 and j+1 < n: count += board[i-1][j+1]%2
73+
if j-1 >= 0: count += board[i][j-1]%2
74+
if j+1 < n: count += board[i][j+1]%2
75+
if i+1 < m and j-1 >= 0: count += board[i+1][j-1]%2
76+
if i+1 < m: count += board[i+1][j]%2
77+
if i+1 < m and j+1 < n: count += board[i+1][j+1]%2
78+
return count
5679

5780
```

Daily-challenge/Dec/30/sol.py

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,36 @@
11
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:]
2+
def gameOfLife(self, board: List[List[int]]) -> None:
3+
"""
4+
Do not return anything, modify board in-place instead.
5+
"""
6+
m = len(board)
7+
n = len(board[0])
8+
9+
10+
for i in range(m):
11+
for j in range(n):
12+
if board[i][j] == 0 or board[i][j] == 2:
13+
if self.determineLive(board,i,j) == 3:
14+
board[i][j] = 2
15+
else:
16+
if self.determineLive(board,i,j) < 2 or self.determineLive(board,i,j) >3:
17+
board[i][j] = 3
18+
for i in range(m):
19+
for j in range(n):
20+
if board[i][j] == 2: board[i][j] = 1
21+
if board[i][j] == 3: board[i][j] = 0
22+
23+
24+
def determineLive(self, board, i, j):
25+
m,n = len(board), len(board[0])
26+
count = 0
27+
if i-1 >= 0 and j-1 >= 0: count += board[i-1][j-1]%2
28+
if i-1 >= 0: count += board[i-1][j]%2
29+
if i-1 >= 0 and j+1 < n: count += board[i-1][j+1]%2
30+
if j-1 >= 0: count += board[i][j-1]%2
31+
if j+1 < n: count += board[i][j+1]%2
32+
if i+1 < m and j-1 >= 0: count += board[i+1][j-1]%2
33+
if i+1 < m: count += board[i+1][j]%2
34+
if i+1 < m and j+1 < n: count += board[i+1][j+1]%2
35+
return count
1336

Daily-challenge/Dec/31/README.md

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,56 @@
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.
1+
# Largest Rectangle in Histogram
2+
Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.
113

124

135

14-
Example 1:
15-
166

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:
7+
Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3].
208

21-
22-
Input: board = [[1,1],[1,0]]
23-
Output: [[1,1],[1,1]]
249

2510

26-
Constraints:
2711

28-
m == board.length
29-
n == board[i].length
30-
1 <= m, n <= 25
31-
board[i][j] is 0 or 1.
12+
The largest rectangle is shown in the shaded area, which has area = 10 unit.
13+
3214

3315

34-
Follow up:
16+
Example:
3517

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>
18+
Input: [2,1,5,6,2,3]
19+
Output: 10<br>
3820

3921
## Idea
4022
Just do while loop and convert to decimal
4123

4224
## Code
4325
```python
4426
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:]
27+
def largestRectangleArea(self, height):
28+
n = len(height)
29+
30+
31+
left = [1] * n
32+
right = [1] * n
33+
max_rect = 0
34+
35+
for i in range(0, n):
36+
j = i - 1
37+
while j >= 0:
38+
if height[j] >= height[i]:
39+
left[i] += left[j]
40+
j -= left[j]
41+
else: break
42+
43+
for i in range(n - 1, -1, -1):
44+
j = i + 1
45+
while j < n:
46+
if height[j] >= height[i]:
47+
right[i] += right[j]
48+
j += right[j]
49+
else: break
50+
51+
for i in range(0, n):
52+
max_rect = max(max_rect, height[i] * (left[i] + right[i] - 1))
53+
54+
return max_rect
5655

5756
```

Daily-challenge/Dec/31/sol.py

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
11
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-
2+
def largestRectangleArea(self, height):
3+
n = len(height)
4+
5+
6+
left = [1] * n
7+
right = [1] * n
8+
max_rect = 0
9+
10+
for i in range(0, n):
11+
j = i - 1
12+
while j >= 0:
13+
if height[j] >= height[i]:
14+
left[i] += left[j]
15+
j -= left[j]
16+
else: break
17+
18+
for i in range(n - 1, -1, -1):
19+
j = i + 1
20+
while j < n:
21+
if height[j] >= height[i]:
22+
right[i] += right[j]
23+
j += right[j]
24+
else: break
25+
26+
for i in range(0, n):
27+
max_rect = max(max_rect, height[i] * (left[i] + right[i] - 1))
28+
29+
return max_rect

0 commit comments

Comments
 (0)