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:
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.
Example 2:
Input: forbidden = [8,3,16,6,12,20], a = 15, b = 13, x = 11 Output: -1
Example 3:
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.
Constraints:
1 <= forbidden.length <= 10001 <= a, b, forbidden[i] <= 2000 0 <= x <= 2000 All the elements in forbidden are distinct. * Position x is not forbidden.
用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; }
You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.
Return the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Given a string s containing an out-of-order English representation of digits 0-9, return the digits in ascending order. Example 1:
Input: s = “owoztneoer” Output: “012”
Example 2:
Input: s = “fviefuro” Output: “45”
Constraints:1 <= s.length <= 10<sup>5</sup>s[i] is one of the characters ["e","g","f","i","h","o","n","s","r","u","t","w","v","x","z"]. s is *guaranteed to be valid.