There are n buildings in a line. You are given an integer array heights of size n that represents the heights of the buildings in the line.
The ocean is to the right of the buildings. A building has an ocean view if the building can see the ocean without obstructions. Formally, a building has an ocean view if all the buildings to its right have a smaller height.
Return a list of indices (0-indexed) of buildings that have an ocean view, sorted in increasing order.
Example 1:
<pre>Input: heights = [4,2,3,1]
Output: [0,2,3]
Explanation: Building 1 (0-indexed) does not have an ocean view because building 2 is taller.
</pre>
Example 2:
<pre>Input: heights = [4,3,2,1]
Output: [0,1,2,3]
Explanation: All the buildings have an ocean view.
</pre>
Example 3:
<pre>Input: heights = [1,3,2,4]
Output: [3]
Explanation: Only building 3 has an ocean view.
</pre>
Constraints:
1 <= heights.length <= 10<sup>5</sup>
1 <= heights[i] <= 10<sup>9</sup>
</div>
题目大意:
大海在右边,求看到大海的大厦的下标
解题思路:
数组元素之间大小关系且保持顺序,用stack
解题步骤:
N/A
注意事项:
Python代码:
1 2 3 4 5 6 7
deffindBuildings(self, heights: List[int]) -> List[int]: stack = [] for i inrange(len(heights)): while stack and heights[i] >= heights[stack[-1]]: stack.pop() stack.append(i) # 4 3 1 return stack
The demons had captured the princess and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of m x n rooms laid out in a 2D grid. Our valiant knight was initially positioned in the top-left room and must fight his way through dungeon to rescue the princess.
The knight has an initial health point represented by a positive integer. If at any point his health point drops to 0 or below, he dies immediately.
Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).
To reach the princess as quickly as possible, the knight decides to move only rightward or downward in each step.
Return the knight's minimum initial health so that he can rescue the princess.
Note that any room can contain threats or power-ups, even the first room the knight enters and the bottom-right room where the princess is imprisoned.
Example 1:
<pre>Input: dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
Output: 7
Explanation: The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.
</pre>
A certain bug's home is on the x-axis at position x. Help them get there from position 0.
The bug jumps according to the following rules:
It can jump exactly a positions forward (to the right).
It can jump exactly b positions backward (to the left).
It cannot jump backward twice in a row.
It cannot jump to any forbidden positions.
The bug may jump forward beyond its home, but it cannot jump to positions numbered with negative integers.
Given an array of integers forbidden, where forbidden[i] means that the bug cannot jump to the position forbidden[i], and integers a, b, and x, return the minimum number of jumps needed for the bug to reach its home. If there is no possible sequence of jumps that lands the bug on position x, return -1.
Example 1:
<pre>Input: forbidden = [14,4,18,1,15], a = 3, b = 15, x = 9
Output: 3
Explanation: 3 jumps forward (0 -> 3 -> 6 -> 9) will get the bug home.
</pre>
Example 2:
<pre>Input: forbidden = [8,3,16,6,12,20], a = 15, b = 13, x = 11
Output: -1
</pre>
Example 3:
<pre>Input: forbidden = [1,6,2,14,5,17,4], a = 16, b = 9, x = 7
Output: 2
Explanation: One jump forward (0 -> 16) then one jump backward (16 -> 7) will get the bug home.
</pre>
用BFS模板,但此题到了某个位置可以有两个状态:向前跳和向后跳。所以visited不能只含位置,必须包含方向,(position, is_backward). if (neighbor, neighbor_is_backward) in visited也记得包含方向,否则LTE,因为Python不会检查是否tuple
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
OFFSETS = [(1, 0), (-1, 0), (0, 1), (0, -1)] classSolution: defnumIslands(self, grid: List[List[str]]) -> int: ifnot grid: return0 count = 0 for i inrange(len(grid)): for j inrange(len(grid[0])): if grid[i][j] == '1': self.bfs(grid, i, j) count += 1 return count
defbfs(self, nums, i, j): queue = deque([(i, j)]) nums[i][j] = 'X' while queue: island = queue.popleft() for dx, dy in OFFSETS: x, y = island[0] + dx, island[1] + dy if x < 0or x >= len(nums) or y < 0or y >= len(nums[0]) or nums[x][y] in ['0', 'X']: continue queue.append((x, y)) nums[x][y] = 'X'
publicvoiddfs(char[][] grid, int a, int b){ if(!isValid(grid,a,b)) return; grid[a][b] = 'x'; dfs(grid, a+1, b); dfs(grid, a-1, b); dfs(grid, a, b+1); dfs(grid, a, b-1); }
publicbooleanisValid(char[][] grid, int x, int y){ if(x<0||x>=grid.length||y<0||y>=grid[0].length||grid[x][y]!='1') returnfalse; returntrue; }