Given a square grid of integers arr, a falling path with non-zero shifts is a choice of exactly one element from each row of arr, such that no two elements chosen in adjacent rows are in the same column.

Return the minimum sum of a falling path with non-zero shifts.

Example 1:

Input: arr = [[1,2,3],[4,5,6],[7,8,9]] Output: 13 Explanation: The possible falling paths are: [1,5,9], [1,5,7], [1,6,7], [1,6,8], [2,4,8], [2,4,9], [2,6,7], [2,6,8], [3,4,8], [3,4,9], [3,5,7], [3,5,9] The falling path with the smallest sum is [1,5,7], so the answer is 13.

Given a m * n grid, where each cell is either 0 (empty) or 1 (obstacle). In one step, you can move up, down, left or right from and to an empty cell.

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.

Example 1:

Input: grid = [[0,0,0], [1,1,0], [0,0,0], [0,1,1], [0,0,0]], k = 1 Output: 6 Explanation: The shortest path without eliminating any obstacle is 10. 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).

Example 2:

Input: grid = [[0,1,1], [1,1,1], [1,0,0]], k = 1 Output: -1 Explanation: We need to eliminate at least two obstacles to find such a walk.

Constraints:

grid.length == m grid[0].length == n 1 <= m, n <= 40 1 <= k <= m*n grid[i][j] == 0 or 1 grid[0][0] == grid[m-1][n-1] == 0

classSolution { public: structState { int x, y; int remain; State(int _x, int _y, int _remain) { x = _x; y = _y; remain = _remain; } }; intshortestPath(vector<vector<int>>& grid, int k){ int row = grid.size(), col = grid[0].size(); queue<State> q; q.push(State(0, 0, k)); int step = 0; vector<vector<int>> dirs = {{0,-1},{-1,0}, {0,1}, {1,0}}; vector<vector<vector<bool>>> seen(row, vector<vector<bool>>(col, vector<bool>(k+1, false))); while (!q.empty()) { int size = q.size(); for (int i = 0; i < size; i++) { State s = q.front(); q.pop(); if (s.x == row-1 && s.y == col-1) return step; for (auto &dir : dirs) { int x = s.x + dir[0]; int y = s.y + dir[1]; if (x < 0 || x >= row || y < 0 || y >= col) continue; int r = s.remain - (grid[x][y] == 1); if (r < 0 || seen[x][y][r]) continue; seen[x][y][r] = true; q.push(State(x, y, r)); } } step++; } return-1; } };

Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where:

status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it.

Return the maximum number of candies you can get following the rules above.

Example 1:

Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don’t have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2:

Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3:

Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4:

Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5:

classSolution { public: intmaxCandies(vector<int>& status, vector<int>& candies, vector<vector<int>>& keys, vector<vector<int>>& containedBoxes, vector<int>& initialBoxes){ queue<int> q; unordered_set<int> all_keys; vector<int> all_boxes; for (int i : initialBoxes) q.push(i); while (!q.empty()) { int i = q.front(); q.pop(); all_boxes.push_back(i); for (int b : containedBoxes[i]) q.push(b); for (int k : keys[i]) all_keys.insert(k); } int total = 0; for (int i : all_boxes) { if (status[i] == 1 || all_keys.count(i)) total += candies[i]; } return total; } };

You are given a square board of characters. You can move on the board starting at the bottom right square marked with the character ‘S’.

You need to reach the top left square marked with the character ‘E’. The rest of the squares are labeled either with a numeric character 1, 2, …, 9 or with an obstacle ‘X’. In one move you can go up, left or up-left (diagonally) only if there is no obstacle there.

Return a list of two integers: the first integer is the maximum sum of numeric characters you can collect, and the second is the number of such paths that you can take to get that maximum sum, taken modulo 10^9 + 7.

In case there is no path, return [0, 0].

Example 1:

Input: board = [“E23”,”2X2”,”12S”] Output: [7,1] Example 2:

Input: board = [“E12”,”1X1”,”21S”] Output: [4,2] Example 3:

Given an equation, represented by words on left side and the result on right side.

You need to check if the equation is solvable under the following rules:

Each character is decoded as one digit (0 - 9). Every pair of different characters they must map to different digits. Each words[i] and result are decoded as one number without leading zeros. Sum of numbers on left side (words) will equal to the number on right side (result). Return True if the equation is solvable otherwise return False.

Example 1:

Input: words = [“SEND”,”MORE”], result = “MONEY” Output: true Explanation: Map ‘S’-> 9, ‘E’->5, ‘N’->6, ‘D’->7, ‘M’->1, ‘O’->0, ‘R’->8, ‘Y’->’2’ Such that: “SEND” + “MORE” = “MONEY” , 9567 + 1085 = 10652 Example 2:

Input: words = [“SIX”,”SEVEN”,”SEVEN”], result = “TWENTY” Output: true Explanation: Map ‘S’-> 6, ‘I’->5, ‘X’->0, ‘E’->8, ‘V’->7, ‘N’->2, ‘T’->1, ‘W’->’3’, ‘Y’->4 Such that: “SIX” + “SEVEN” + “SEVEN” = “TWENTY” , 650 + 68782 + 68782 = 138214 Example 3:

Input: words = [“THIS”,”IS”,”TOO”], result = “FUNNY” Output: true Example 4:

Input: words = [“LEET”,”CODE”], result = “POINT” Output: false

Constraints:

2 <= words.length <= 5 1 <= words[i].length, result.length <= 7 words[i], result contains only upper case English letters. Number of different characters used on the expression is at most 10.

classSolution { public: int char_to_int[256]; int int_to_char[10]; boolisSolvable(vector<string>& words, string result){ for (int i = 0; i < 256; i++) char_to_int[i] = -1; for (int i = 0; i < 10; i++) int_to_char[i] = -1; string word; return backtrace(words, 0, 0, 0, 0, result); } boolbacktrace(vector<string> &words, int i, int k, int num, int sum, string &result){ if (words[i].size() == k) { sum += num; num = 0; k = 0; i++; } if (i == words.size()) { return check(sum, result); } int c = words[i][k]; for (int n = 0; n <= 9; n++) { if (n == 0 && k == 0) continue; if (int_to_char[n] != -1 && int_to_char[n] != c) continue; if (char_to_int[c] != -1 && char_to_int[c] != n) continue; int old_c = int_to_char[n]; int old_n = char_to_int[c]; int_to_char[n] = c; char_to_int[c] = n; if (backtrace(words, i, k+1, num * 10 + n, sum, result)) returntrue; int_to_char[n] = old_c; char_to_int[c] = old_n; } returnfalse; } boolcheck(int sum, string &result){ if (result.empty()) returnfalse; int i = result.size() - 1; do { if (i < 0) returnfalse; int n = sum % 10; sum = sum / 10; int c = result[i]; if (char_to_int[c] != -1 && char_to_int[c] != n) returnfalse; if (int_to_char[n] != -1 && int_to_char[n] != c) returnfalse; i--; } while (sum); return i < 0; } };

You have a keyboard layout as shown above in the XY plane, where each English uppercase letter is located at some coordinate, for example, the letter A is located at coordinate (0,0), the letter B is located at coordinate (0,1), the letter P is located at coordinate (2,3) and the letter Z is located at coordinate (4,1).

Given the string word, return the minimum total distance to type such string using only two fingers. The distance between coordinates (x1,y1) and (x2,y2) is |x1 - x2| + |y1 - y2|.

Note that the initial positions of your two fingers are considered free so don’t count towards your total distance, also your two fingers do not have to start at the first letter or the first two letters.

Example 1:

Input: word = “CAKE” Output: 3 Explanation: Using two fingers, one optimal way to type “CAKE” is: Finger 1 on letter ‘C’ -> cost = 0 Finger 1 on letter ‘A’ -> cost = Distance from letter ‘C’ to letter ‘A’ = 2 Finger 2 on letter ‘K’ -> cost = 0 Finger 2 on letter ‘E’ -> cost = Distance from letter ‘K’ to letter ‘E’ = 1 Total distance = 3 Example 2:

Input: word = “HAPPY” Output: 6 Explanation: Using two fingers, one optimal way to type “HAPPY” is: Finger 1 on letter ‘H’ -> cost = 0 Finger 1 on letter ‘A’ -> cost = Distance from letter ‘H’ to letter ‘A’ = 2 Finger 2 on letter ‘P’ -> cost = 0 Finger 2 on letter ‘P’ -> cost = Distance from letter ‘P’ to letter ‘P’ = 0 Finger 1 on letter ‘Y’ -> cost = Distance from letter ‘A’ to letter ‘Y’ = 4 Total distance = 6 Example 3:

Input: word = “NEW” Output: 3 Example 4:

Input: word = “YEAR” Output: 7

Constraints:

2 <= word.length <= 300 Each word[i] is an English uppercase letter.

Given a time represented in the format “HH:MM”, form the next closest time by reusing the current digits. There is no limit on how many times a digit can be reused.

You may assume the given input string is always valid. For example, “01:34”, “12:09” are all valid. “1:34”, “12:9” are all invalid.

Example 1:

Input: “19:34” Output: “19:39” Explanation: The next closest time choosing from digits 1, 9, 3, 4, is 19:39, which occurs 5 minutes later. It is not 19:33, because this occurs 23 hours and 59 minutes later. Example 2:

Input: “23:59” Output: “22:22” Explanation: The next closest time choosing from digits 2, 3, 5, 9, is 22:22. It may be assumed that the returned time is next day’s time since it is smaller than the input time numerically.

Given a node from a Circular Linked List which is sorted in ascending order, write a function to insert a value insertVal into the list such that it remains a sorted circular list. The given node can be a reference to any single node in the list, and may not be necessarily the smallest value in the circular list.

If there are multiple suitable places for insertion, you may choose any place to insert the new value. After the insertion, the circular list should remain sorted.

If the list is empty (i.e., given node is null), you should create a new single circular list and return the reference to that single node. Otherwise, you should return the original given node.

Example 1:

Input: head = [3,4,1], insertVal = 2 Output: [3,4,1,2] Explanation: In the figure above, there is a sorted circular list of three elements. You are given a reference to the node with value 3, and we need to insert 2 into the list. The new node should be inserted between node 1 and node 3. After the insertion, the list should look like this, and we should still return node 3.

Example 2:

Input: head = [], insertVal = 1 Output: [1] Explanation: The list is empty (given head is null). We create a new single circular list and return the reference to that single node. Example 3:

Input: head = [1], insertVal = 0 Output: [1,0]

Constraints:

0 <= Number of Nodes <= 5 * 10^4 -10^6 <= Node.val <= 10^6 -10^6 <= insertVal <= 10^6