Press "Enter" to skip to content

Huahua's Tech Road

花花酱 LeetCode 864. Shortest Path to Get All Keys

Problem

We are given a 2-dimensionalĀ grid.Ā "."Ā is an empty cell,Ā "#"Ā isĀ a wall,Ā "@"Ā is the starting point, ("a",Ā "b", …) are keys, and ("A",Ā "B", …) are locks.

We start at the starting point, and one move consists of walking one space in one of the 4 cardinal directions.Ā  We cannot walk outside the grid, or walk into a wall.Ā  If we walk over a key, we pick it up.Ā  We can’t walk over a lock unless we have the corresponding key.

For someĀ 1 <= K <= 6, there is exactly one lowercase and one uppercase letter of the firstĀ KĀ letters of the English alphabet in the grid.Ā  This means that there is exactly one key for each lock, and one lock for each key; and also that the letters used to represent the keys and locks wereĀ chosen in the same order as the English alphabet.

Return the lowest number of moves to acquire all keys.Ā  IfĀ it’s impossible, returnĀ -1.

Example 1:

Input: ["@.a.#","###.#","b.A.B"]
Output: 8

Example 2:

Input: ["@..aA","..B#.","....b"]
Output: 6

Note:

  1. 1 <= grid.lengthĀ <= 30
  2. 1 <= grid[0].lengthĀ <= 30
  3. grid[i][j]Ā contains onlyĀ '.',Ā '#',Ā '@',Ā 'a'-'f'Ā andĀ 'A'-'F'
  4. The number of keys is inĀ [1, 6].Ā  Each key has a different letter and opens exactly one lock.

Solution: BFS

Time complexity: O(m*n*64)

Space complexity: O(m*n*64)

C++

Python3

Related Problems

 

花花酱 LeetCode 839. Similar String Groups

Problem

Two stringsĀ XĀ andĀ YĀ are similar if we can swap two letters (in different positions) ofĀ X, so thatĀ it equalsĀ Y.

For example,Ā "tars"Ā andĀ "rats"Ā are similar (swapping at positionsĀ 0Ā andĀ 2), andĀ "rats"Ā andĀ "arts"Ā are similar, butĀ "star"Ā is not similar toĀ "tars",Ā "rats", orĀ "arts".

Together, these form two connected groups by similarity:Ā {"tars", "rats", "arts"}Ā andĀ {"star"}.Ā  Notice thatĀ "tars"Ā andĀ "arts"Ā are in the same group even though they are not similar.Ā  Formally, each group is such that a word is in the group if and only if it is similar to at least one other word in the group.

We are given a listĀ AĀ of strings.Ā  Every string inĀ AĀ is an anagram of every other string inĀ A.Ā  How many groups are there?

Example 1:

Input: ["tars","rats","arts","star"]
Output: 2

Note:

  1. A.length <= 2000
  2. A[i].length <= 1000
  3. A.length * A[i].length <= 20000
  4. All words inĀ AĀ consist of lowercase letters only.
  5. All words inĀ AĀ have the same length and are anagrams of each other.
  6. The judging time limit has been increased for this question.

Solution: Brute Force + Union Find

Time Complexity: O(n^2 * L)

Space Complexity: O(n)

C++

 

花花酱 LeetCode 840. Magic Squares In Grid

Problem

A 3 x 3 magic square is a 3 x 3 grid filled with distinct numbersĀ from 1 to 9Ā such that each row, column, and both diagonals all have the same sum.

Given anĀ gridĀ of integers, how many 3 x 3 “magic square” subgrids are there?Ā  (Each subgrid is contiguous).

Example 1:

Input: [[4,3,8,4],
        [9,5,1,9],
        [2,7,6,2]]
Output: 1
Explanation: 
The following subgrid is a 3 x 3 magic square:
438
951
276

while this one is not:
384
519
762

In total, there is only one magic square inside the given grid.

Note:

  1. 1 <= grid.lengthĀ <= 10
  2. 1 <= grid[0].lengthĀ <= 10
  3. 0 <= grid[i][j] <= 15

Solution

Time complexity: O(m*n)

Space complexity: O(1)

C++

 

花花酱 LeetCode 865. Smallest Subtree with all the Deepest Nodes

Problem

Given a binary tree rooted atĀ root, theĀ depthĀ of each node is the shortest distance to the root.

A node isĀ deepestĀ if it has the largest depth possible amongĀ any node in theĀ entire tree.

The subtree of a node is that node, plus the set of all descendants of that node.

Return the node with the largest depth such that it contains all the deepest nodes in it’s subtree.

 

Example 1:

Input: [3,5,1,6,2,0,8,null,null,7,4]
Output: [2,7,4]
Explanation:



We return the node with value 2, colored in yellow in the diagram.
The nodes colored in blue are the deepest nodes of the tree.
The input "[3, 5, 1, 6, 2, 0, 8, null, null, 7, 4]" is a serialization of the given tree.
The output "[2, 7, 4]" is a serialization of the subtree rooted at the node with value 2.
Both the input and output have TreeNode type.

 

Note:

  • The number of nodes in the tree will be between 1 and 500.
  • The values of each node are unique.

 


Solution: Recursion

Time complexity: O(n)

Space complexity: O(n)

C++

v2

Python3

 

花花酱 LeetCode 867. Prime Palindrome

Problem

Find the smallest prime palindrome greater than or equal toĀ N.

Recall that aĀ number isĀ primeĀ if it’s only divisors are 1 and itself, and it is greater than 1.

For example, 2,3,5,7,11 and 13 areĀ primes.

Recall that a number is aĀ palindromeĀ if it reads the same from left to right as it does from right to left.

For example, 12321 is a palindrome.

 

Example 1:

Input: 6
Output: 7

Example 2:

Input: 8
Output: 11

Example 3:

Input: 13
Output: 101

Note:

  • 1 <= N <= 10^8
  • The answer is guaranteed to exist and be less thanĀ 2 * 10^8.

Solution: Math

All odd digits palindromes have a factor 11, they are not prime except 11 itself.

Time complexity: O(n)

Space complexity: O(1)