publicint[][] updateMatrix(int[][] mat) { intm= mat.length, n = mat[0].length; int[][] res = newint[m][n]; for (inti=0; i < m; i++) { Arrays.fill(res[i], Integer.MAX_VALUE); } for (inti=0; i < m; i++) { for (intj=0; j < n; j++) { if (mat[i][j] == 0) bfs(mat, res, i, j, m, n); } } return res; }
privatevoidbfs(int[][] mat, int[][] res, int i, int j, int m, int n) { res[i][j] = 0; Queue<Node> q = newLinkedList<>(); q.offer(newNode(i, j, 0)); while (!q.isEmpty()) { Nodenode= q.poll(); for (int[] dir: dirs) { intx= node.x+dir[0], y = node.y+dir[1], d = node.dist+1; if (x < 0 || x >= m || y < 0 || y >= n || d >= res[x][y]) continue; res[x][y] = d; q.offer(newNode(x, y, d)); } } } }
publicint[][] updateMatrix(int[][] mat) { intm= mat.length, n = mat[0].length; int[][] res = newint[m][n]; boolean[][] visited = newboolean[m][n]; Queue<Node> q = newLinkedList<>(); for (inti=0; i < m; i++) { for (intj=0; j < n; j++) { if (mat[i][j] == 0) q.offer(newNode(i, j)); // add all } } intdist=1; while (!q.isEmpty()) { intsize= q.size(); while (size-- > 0) { Nodenode= q.poll(); for (int[] dir: dirs) { intx= node.x + dir[0], y = node.y + dir[1]; if (x < 0 || x >= m || y < 0 || y >= n || visited[x][y]) continue; if (mat[x][y] == 1) res[x][y] = dist; // set value at the first time, must be the shortest distance visited[x][y] = true; q.offer(newNode(x, y)); } } ++dist; // increase distance for each circle } return res; } }
934. Shortest Bridge
You are given an n x n binary matrix grid where 1 represents land and 0 represents water. An island is a 4-directionally connected group of 1's not connected to any other 1's. There are exactly two islands in grid. You may change 0's to 1's to connect the two islands to form one island. Return the smallest number of 0's you must flip to connect the two islands.
publicintshortestBridge(int[][] grid) { intm= grid.length, n = grid[0].length; Queue<Node> q = newLinkedList<>(); boolean[][] visited = newboolean[m][n]; for (inti=0; i < m; i++) { for (intj=0; j < n; j++) { if (grid[i][j] == 0) continue; dfs(grid, visited, q, i, j, m, n); i = m; j = n; } } intres=0; while (!q.isEmpty()) { intsize= q.size(); while (size-- > 0) { Nodenode= q.poll(); for (int[] dir: dirs) { intx= node.x + dir[0], y = node.y + dir[1]; if (x < 0 || x >= m || y < 0 || y >= n || visited[x][y]) continue; if (grid[x][y] == 1) return res; visited[x][y] = true; q.offer(newNode(x, y)); } } res++; } return -1; }
privatevoiddfs(int[][] grid, boolean[][] visited, Queue<Node> q, int i, int j, int m, int n) { if (i < 0 || i >= m || j < 0 || j >= n || visited[i][j] || grid[i][j] == 0) return; q.offer(newNode(i, j)); visited[i][j] = true; for (int[] dir: dirs) { dfs(grid, visited, q, i+dir[0], j+dir[1], m, n); } } }
127. Word Ladder
A transformation sequence from word beginWord to word endWord using a dictionary wordList is a sequence of words $\text{beginWord} \rightarrow s_1 \rightarrow s_2 \rightarrow \cdots \rightarrow s_k$ such that:
Every adjacent pair of words differs by a single letter.
Every $s_i$ for $1 \le i \le k$ is in wordList. Note that beginWord does not need to be in wordList.
$s_k == \text{endWord}$
Given two words, beginWord and endWord, and a dictionary wordList, return the number of words in the shortest transformation sequence from beginWord to endWord, or 0 if no such sequence exists.
Example 1:
Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"] Output: 5 Explanation: One shortest transformation sequence is "hit" -> "hot" -> "dot" -> "dog" -> cog", which is 5 words long.
Example 2:
Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log"] Output: 0 Explanation: The endWord "cog" is not in wordList, therefore there is no valid transformation sequence.
// get index of node/edge in this.edges privateintgetWordIdx(String word) { if (!map.containsKey(word)) { intres= idx; map.put(word, idx++); edges.add(newArrayList<>()); return res; } return map.get(word); } }
854. K-Similar Strings
Strings s1 and s2 are k-similar(for some non-negative integer k) if we can swap the positions of two letters in s1 exactly k times so that the resulting string equals s2.
Given two anagrams s1 and s2, return the smallest k for which s1 and s2 are k-similar.
Example 1:
Input: s1 = "ab", s2 = "ba" Output: 1 Explanation: The two string are 1-similar because we can use one swap to change s1 to s2: "ab" --> "ba".
Example 2:
Input: s1 = "abc", s2 = "bca" Output: 2 Explanation: The two strings are 2-similar because we can use two swaps to change s1 to s2: "abc" --> "bac" --> "bca".