Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4c6fd8e

Browse files
committed
Updated Google problem day2
1 parent 97aed5a commit 4c6fd8e

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

google/README.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# leetcode google interview problems
2+
3+
4+
### 1. [Leetcode 939](https://leetcode.com/problems/minimum-area-rectangle/description/) : Minimum Area Rectangle
5+
<pre>
6+
You are given an array of points in the X-Y plane points where points[i] = [xi, yi].
7+
8+
Return the minimum area of a rectangle formed from these points, with sides parallel to the X and Y axes. If there is not any such rectangle, return 0.
9+
10+
Example 1:
11+
12+
Input: points = [[1,1],[1,3],[3,1],[3,3],[2,2]]
13+
Output: 4
14+
Example 2:
15+
16+
Input: points = [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
17+
Output: 2
18+
19+
Constraints:
20+
21+
1 <= points.length <= 500
22+
points[i].length == 2
23+
0 <= xi, yi <= 4 * 104
24+
All the given points are unique.
25+
</pre>
26+
27+
Solution:
28+
29+
```python
30+
class Solution:
31+
def minAreaRect(self, points: List[List[int]]) -> int:
32+
min_area = float('inf')
33+
visited = set()
34+
for x1,y1 in points:
35+
for x2, y2 in visited:
36+
if (x1, y2) in visited and (x2, y1) in visited:
37+
size = abs(x2-x1) * abs(y2-y1)
38+
min_area = min(min_area, size)
39+
visited.add((x1,y1))
40+
return min_area if min_area != float('inf') else 0
41+
```
42+
43+
### 2. [Leetcode 1293](https://leetcode.com/problems/shortest-path-in-a-grid-with-obstacles-elimination/) : Shortest Path in a Grid with Obstacles Elimination
44+
<pre>
45+
You are given an m x n integer matrix grid where each cell is either 0 (empty) or 1 (obstacle). You can move up, down, left, or right from and to an empty cell in one step.
46+
47+
Return the minimum number of steps to walk from the upper left corner (0, 0) to the lower right corner (m - 1, n - 1) given that you can eliminate at most k obstacles. If it is not possible to find such walk return -1.
48+
49+
50+
51+
Example 1:
52+
53+
54+
Input: grid = [[0,0,0],[1,1,0],[0,0,0],[0,1,1],[0,0,0]], k = 1
55+
Output: 6
56+
Explanation:
57+
The shortest path without eliminating any obstacle is 10.
58+
The shortest path with one obstacle elimination at position (3,2) is 6. Such path is (0,0) -> (0,1) -> (0,2) -> (1,2) -> (2,2) -> (3,2) -> (4,2).
59+
Example 2:
60+
61+
62+
Input: grid = [[0,1,1],[1,1,1],[1,0,0]], k = 1
63+
Output: -1
64+
Explanation: We need to eliminate at least two obstacles to find such a walk.
65+
66+
67+
Constraints:
68+
69+
m == grid.length
70+
n == grid[i].length
71+
1 <= m, n <= 40
72+
1 <= k <= m * n
73+
grid[i][j] is either 0 or 1.
74+
grid[0][0] == grid[m - 1][n - 1] == 0
75+
</pre>
76+
77+
Solution:
78+
79+
```python
80+
class Solution:
81+
def shortestPath(self, grid: List[List[int]], k: int) -> int:
82+
rows = len(grid)
83+
cols = len(grid[0])
84+
if rows + cols - 2 <= k:
85+
return rows + cols - 2
86+
queue = deque([(0,(0,0,k))])
87+
directions = [(1, 0), (-1, 0), (0, 1), (0, -1)]
88+
visited = set([(0,0,k)])
89+
while queue:
90+
steps, (r, c, eilimination_remaining) = queue.popleft()
91+
if r == rows-1 and c == cols-1:
92+
return steps
93+
for (r_inc, c_inc) in directions:
94+
new_row, new_col = r + r_inc, c + c_inc
95+
if 0 <= new_row and new_row < rows and 0 <= new_col and new_col < cols:
96+
new_eiliminated = eilimination_remaining - grid[new_row][new_col]
97+
if ((new_row,new_col, new_eiliminated) not in visited) and new_eiliminated >= 0:
98+
visited.add((new_row, new_col, new_eiliminated))
99+
queue.append((steps + 1, (new_row, new_col, new_eiliminated)))
100+
return -1
101+
```

0 commit comments

Comments
 (0)