Leetcode 2021-06-25
🟡 752.open-the-lock
🏷️ Tags
#breadth_first_search #array #hash_table #string
Description
You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots:
The lock initially starts at
You are given a list of
Given a
Example
🟡 752.open-the-lock
🏷️ Tags
#breadth_first_search #array #hash_table #string
Description
You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots:
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'. The wheels can rotate freely and wrap around: for example we can turn '9' to be '0', or '0' to be '9'. Each move consists of turning one wheel one slot.The lock initially starts at
'0000', a string representing the state of the 4 wheels.You are given a list of
deadends dead ends, meaning if the lock displays any of these codes, the wheels of the lock will stop turning and you will be unable to open it.Given a
target representing the value of the wheels that will unlock the lock, return the minimum total number of turns required to open the lock, or -1 if it is impossible.Example
Input: deadends = ["0201","0101","0102","1212","2002"], target = "0202"
Output: 6
Explanation:
A sequence of valid moves would be "0000" -> "1000" -> "1100" -> "1200" -> "1201" -> "1202" -> "0202".
Note that a sequence like "0000" -> "0001" -> "0002" -> "0102" -> "0202" would be invalid,
because the wheels of the lock become stuck after the display becomes the dead end "0102".
Leetcode 2021-06-26
🔴 773.sliding-puzzle
🏷️ Tags
#breadth_first_search #array #matrix
Description
On a 2x3
A move consists of choosing
The state of the board is solved if and only if the
Given a puzzle board, return the least number of moves required so that the state of the board is solved. If it is impossible for the state of the board to be solved, return -1.
Example
🔴 773.sliding-puzzle
🏷️ Tags
#breadth_first_search #array #matrix
Description
On a 2x3
board, there are 5 tiles represented by the integers 1 through 5, and an empty square represented by 0.A move consists of choosing
0 and a 4-directionally adjacent number and swapping it.The state of the board is solved if and only if the
board is [[1,2,3],[4,5,0]].Given a puzzle board, return the least number of moves required so that the state of the board is solved. If it is impossible for the state of the board to be solved, return -1.
Example
Input: board = [[1,2,3],[4,0,5]]
Output: 1
Explanation: Swap the 0 and the 5 in one move.
Leetcode 2021-06-27
🟡 909.snakes-and-ladders
🏷️ Tags
#breadth_first_search #array #matrix
Description
On an N x N
You start on square
You choose a destination square
(This choice simulates the result of a standard 6-sided die roll: ie., there are always at most 6 destinations, regardless of the size of the board.)
If
A board square on row
Note that you only take a snake or ladder at most once per move: if the destination to a snake or ladder is the start of another snake or ladder, you do not continue moving. (For example, if the board is `[[4,-1],[-1,3]]`, and on the first move your destination square is `2`, then you finish your first move at `3`, because you do not continue moving to `4`.)
Return the least number of moves required to reach square N*N. If it is not possible, return
Example
🟡 909.snakes-and-ladders
🏷️ Tags
#breadth_first_search #array #matrix
Description
On an N x N
board, the numbers from 1 to N*N are written boustrophedonically starting from the bottom left of the board, and alternating direction each row. For example, for a 6 x 6 board, the numbers are written as follows:You start on square
1 of the board (which is always in the last row and first column). Each move, starting from square x, consists of the following:You choose a destination square
S with number x+1, x+2, x+3, x+4, x+5, or x+6, provided this number is <= N*N.(This choice simulates the result of a standard 6-sided die roll: ie., there are always at most 6 destinations, regardless of the size of the board.)
If
S has a snake or ladder, you move to the destination of that snake or ladder. Otherwise, you move to S.A board square on row
r and column c has a "snake or ladder" if board[r][c] != -1. The destination of that snake or ladder is board[r][c].Note that you only take a snake or ladder at most once per move: if the destination to a snake or ladder is the start of another snake or ladder, you do not continue moving. (For example, if the board is `[[4,-1],[-1,3]]`, and on the first move your destination square is `2`, then you finish your first move at `3`, because you do not continue moving to `4`.)
Return the least number of moves required to reach square N*N. If it is not possible, return
-1.Example
Input: [
[-1,-1,-1,-1,-1,-1],
[-1,-1,-1,-1,-1,-1],
[-1,-1,-1,-1,-1,-1],
[-1,35,-1,-1,13,-1],
[-1,-1,-1,-1,-1,-1],
[-1,15,-1,-1,-1,-1]]
Output: 4
Explanation:
At the beginning, you start at square 1 [at row 5, column 0].
You decide to move to square 2, and must take the ladder to square 15.
You then decide to move to square 17 (row 3, column 5), and must take the snake to square 13.
You then decide to move to square 14, and must take the ladder to square 35.
You then decide to move to square 36, ending the game.
It can be shown that you need at least 4 moves to reach the N*N-th square, so the answer is 4.
Leetcode 2021-06-28
🔴 815.bus-routes
🏷️ Tags
#breadth_first_search #array #hash_table
Description
You are given an array
For example, if
You will start at the bus stop
Return the least number of buses you must take to travel from
Example
🔴 815.bus-routes
🏷️ Tags
#breadth_first_search #array #hash_table
Description
You are given an array
routes representing bus routes where routes[i] is a bus route that the ith bus repeats forever.For example, if
routes[0] = [1, 5, 7], this means that the 0th bus travels in the sequence 1 -> 5 -> 7 -> 1 -> 5 -> 7 -> 1 -> ... forever.You will start at the bus stop
source (You are not on any bus initially), and you want to go to the bus stop target. You can travel between bus stops by buses only.Return the least number of buses you must take to travel from
source to target. Return -1 if it is not possible.Example
Input: routes = [[1,2,7],[3,6,7]], source = 1, target = 6
Output: 2
Explanation: The best strategy is take the first bus to the bus stop 7, then take the second bus to the bus stop 6.
Leetcode 2021-06-30
🔴 剑指 Offer 37.xu-lie-hua-er-cha-shu-lcof
🏷️ Tags
#tree #depth_first_search #breadth_first_search #design #string #binary_tree
undefinedundefined
🔴 剑指 Offer 37.xu-lie-hua-er-cha-shu-lcof
🏷️ Tags
#tree #depth_first_search #breadth_first_search #design #string #binary_tree
undefinedundefined
Leetcode 2021-07-01
🟢 LCP 07.chuan-di-xin-xi
🏷️ Tags
#depth_first_search #breadth_first_search #graph #dynamic_programming
undefinedundefined
🟢 LCP 07.chuan-di-xin-xi
🏷️ Tags
#depth_first_search #breadth_first_search #graph #dynamic_programming
undefinedundefined
Leetcode 2021-07-02
🟡 1833.maximum-ice-cream-bars
🏷️ Tags
#greedy #array #sorting
Description
It is a sweltering summer day, and a boy wants to buy some ice cream bars.
At the store, there are
Return the maximum number of ice cream bars the boy can buy with
Note: The boy can buy the ice cream bars in any order.
Example
🟡 1833.maximum-ice-cream-bars
🏷️ Tags
#greedy #array #sorting
Description
It is a sweltering summer day, and a boy wants to buy some ice cream bars.
At the store, there are
n ice cream bars. You are given an array costs of length n, where costs[i] is the price of the ith ice cream bar in coins. The boy initially has coins coins to spend, and he wants to buy as many ice cream bars as possible. Return the maximum number of ice cream bars the boy can buy with
coins coins.Note: The boy can buy the ice cream bars in any order.
Example
Input: costs = [1,3,2,4,1], coins = 7
Output: 4
Explanation: The boy can buy ice cream bars at indices 0,1,2,4 for a total price of 1 + 3 + 2 + 1 = 7.
Leetcode 2021-07-03
🟡 451.sort-characters-by-frequency
🏷️ Tags
#hash_table #string #bucket_sort #counting #sorting #heap_priority_queue
Description
Given a string
Example
🟡 451.sort-characters-by-frequency
🏷️ Tags
#hash_table #string #bucket_sort #counting #sorting #heap_priority_queue
Description
Given a string
s, sort it in decreasing order based on the frequency of characters, and return the sorted string.Example
Input: s = "tree"
Output: "eert"
Explanation: 'e' appears twice while 'r' and 't' both appear once.
So 'e' must appear before both 'r' and 't'. Therefore "eetr" is also a valid answer.
Leetcode-cn.com 2021-07-03
🟡 451.sort-characters-by-frequency
🏷️ Tags
#hash_table #string #bucket_sort #counting #sorting #heap_priority_queue
Description
给定一个字符串,请将字符串里的字符按照出现的频率降序排列。
Example
🟡 451.sort-characters-by-frequency
🏷️ Tags
#hash_table #string #bucket_sort #counting #sorting #heap_priority_queue
Description
给定一个字符串,请将字符串里的字符按照出现的频率降序排列。
Example
输入:
"tree"
输出:
"eert"
解释:
'e'出现两次,'r'和't'都只出现一次。
因此'e'必须出现在'r'和't'之前。此外,"eetr"也是一个有效的答案。
Leetcode-cn.com 2021-07-04
🟢 645.set-mismatch
🏷️ Tags
#bit_manipulation #array #hash_table #sorting
Description
集合
给定一个数组
请你找出重复出现的整数,再找到丢失的整数,将它们以数组的形式返回。
Example
🟢 645.set-mismatch
🏷️ Tags
#bit_manipulation #array #hash_table #sorting
Description
集合
s 包含从 1 到 n 的整数。不幸的是,因为数据错误,导致集合里面某一个数字复制了成了集合里面的另外一个数字的值,导致集合 丢失了一个数字 并且 有一个数字重复 。给定一个数组
nums 代表了集合 S 发生错误后的结果。请你找出重复出现的整数,再找到丢失的整数,将它们以数组的形式返回。
Example
输入:nums = [1,2,2,4]
输出:[2,3]
Leetcode.com 2021-07-03
🔴 363.max-sum-of-rectangle-no-larger-than-k
🏷️ Tags
#array #binary_search #dynamic_programming #matrix #ordered_set
Description
Given an
It is guaranteed that there will be a rectangle with a sum no larger than
Example
🔴 363.max-sum-of-rectangle-no-larger-than-k
🏷️ Tags
#array #binary_search #dynamic_programming #matrix #ordered_set
Description
Given an
m x n matrix matrix and an integer k, return the max sum of a rectangle in the matrix such that its sum is no larger than k.It is guaranteed that there will be a rectangle with a sum no larger than
k.Example
Input: matrix = [[1,0,1],[0,-2,3]], k = 2
Output: 2
Explanation: Because the sum of the blue rectangle [[0, 1], [-2, 3]] is 2, and 2 is the max number no larger than k (k = 2).
Leetcode-cn.com 2021-07-05
🔴 726.number-of-atoms
🏷️ Tags
#stack #hash_table #string
Description
给定一个化学式
原子总是以一个大写字母开始,接着跟随0个或任意个小写字母,表示原子的名字。
如果数量大于 1,原子后会跟着数字表示原子的数量。如果数量等于 1 则不会跟数字。例如,H2O 和 H2O2 是可行的,但 H1O2 这个表达是不可行的。
两个化学式连在一起是新的化学式。例如 H2O2He3Mg4 也是化学式。
一个括号中的化学式和数字(可选择性添加)也是化学式。例如 (H2O2) 和 (H2O2)3 是化学式。
给定一个化学式,输出所有原子的数量。格式为:第一个(按字典序)原子的名子,跟着它的数量(如果数量大于 1),然后是第二个原子的名字(按字典序),跟着它的数量(如果数量大于 1),以此类推。
Example
🔴 726.number-of-atoms
🏷️ Tags
#stack #hash_table #string
Description
给定一个化学式
formula(作为字符串),返回每种原子的数量。原子总是以一个大写字母开始,接着跟随0个或任意个小写字母,表示原子的名字。
如果数量大于 1,原子后会跟着数字表示原子的数量。如果数量等于 1 则不会跟数字。例如,H2O 和 H2O2 是可行的,但 H1O2 这个表达是不可行的。
两个化学式连在一起是新的化学式。例如 H2O2He3Mg4 也是化学式。
一个括号中的化学式和数字(可选择性添加)也是化学式。例如 (H2O2) 和 (H2O2)3 是化学式。
给定一个化学式,输出所有原子的数量。格式为:第一个(按字典序)原子的名子,跟着它的数量(如果数量大于 1),然后是第二个原子的名字(按字典序),跟着它的数量(如果数量大于 1),以此类推。
Example
输入:
formula = "H2O"
输出: "H2O"
解释:
原子的数量是 {'H': 2, 'O': 1}。
Leetcode.com 2021-07-04
🔴 1220.count-vowels-permutation
🏷️ Tags
#dynamic_programming
Description
Given an integer
Each character is a lower case vowel (
Each vowel
Each vowel
Each vowel
Each vowel
Each vowel
Since the answer may be too large, return it modulo
Example
🔴 1220.count-vowels-permutation
🏷️ Tags
#dynamic_programming
Description
Given an integer
n, your task is to count how many strings of length n can be formed under the following rules:Each character is a lower case vowel (
'a', 'e', 'i', 'o', 'u')Each vowel
'a' may only be followed by an 'e'.Each vowel
'e' may only be followed by an 'a' or an 'i'.Each vowel
'i' may not be followed by another 'i'.Each vowel
'o' may only be followed by an 'i' or a 'u'.Each vowel
'u' may only be followed by an 'a'.Since the answer may be too large, return it modulo
10^9 + 7.Example
Input: n = 1
Output: 5
Explanation: All possible strings are: "a", "e", "i" , "o" and "u".
Leetcode-cn.com 2021-07-06
🟡 1418.display-table-of-food-orders-in-a-restaurant
🏷️ Tags
#array #hash_table #string #ordered_set #sorting
Description
给你一个数组
请你返回该餐厅的 点菜展示表 。在这张表中,表中第一行为标题,其第一列为餐桌桌号 “Table” ,后面每一列都是按字母顺序排列的餐品名称。接下来每一行中的项则表示每张餐桌订购的相应餐品数量,第一列应当填对应的桌号,后面依次填写下单的餐品数量。
注意:客户姓名不是点菜展示表的一部分。此外,表中的数据行应该按餐桌桌号升序排列。
Example
🟡 1418.display-table-of-food-orders-in-a-restaurant
🏷️ Tags
#array #hash_table #string #ordered_set #sorting
Description
给你一个数组
orders,表示客户在餐厅中完成的订单,确切地说, orders[i]=[customerNamei,tableNumberi,foodItemi] ,其中 customerNamei 是客户的姓名,tableNumberi 是客户所在餐桌的桌号,而 foodItemi 是客户点的餐品名称。请你返回该餐厅的 点菜展示表 。在这张表中,表中第一行为标题,其第一列为餐桌桌号 “Table” ,后面每一列都是按字母顺序排列的餐品名称。接下来每一行中的项则表示每张餐桌订购的相应餐品数量,第一列应当填对应的桌号,后面依次填写下单的餐品数量。
注意:客户姓名不是点菜展示表的一部分。此外,表中的数据行应该按餐桌桌号升序排列。
Example
输入:orders = [["David","3","Ceviche"],["Corina","10","Beef Burrito"],["David","3","Fried Chicken"],["Carla","5","Water"],["Carla","5","Ceviche"],["Rous","3","Ceviche"]]
输出:[["Table","Beef Burrito","Ceviche","Fried Chicken","Water"],["3","0","2","1","0"],["5","0","1","0","1"],["10","1","0","0","0"]]
解释:
点菜展示表如下所示:
Table,Beef Burrito,Ceviche,Fried Chicken,Water
3 ,0 ,2 ,1 ,0
5 ,0 ,1 ,0 ,1
10 ,1 ,0 ,0 ,0
对于餐桌 3:David 点了 "Ceviche" 和 "Fried Chicken",而 Rous 点了 "Ceviche"
而餐桌 5:Carla 点了 "Water" 和 "Ceviche"
餐桌 10:Corina 点了 "Beef Burrito"
Leetcode.com 2021-07-05
🟢 566.reshape-the-matrix
🏷️ Tags
#array #matrix #simulation
Description
In MATLAB, there is a handy function called
You are given an
The reshaped matrix should be filled with all the elements of the original matrix in the same row-traversing order as they were.
If the
Example
🟢 566.reshape-the-matrix
🏷️ Tags
#array #matrix #simulation
Description
In MATLAB, there is a handy function called
reshape which can reshape an m x n matrix into a new one with a different size r x c keeping its original data.You are given an
m x n matrix mat and two integers r and c representing the row number and column number of the wanted reshaped matrix.The reshaped matrix should be filled with all the elements of the original matrix in the same row-traversing order as they were.
If the
reshape operation with given parameters is possible and legal, output the new reshaped matrix; Otherwise, output the original matrix.Example
Input: mat = [[1,2],[3,4]], r = 1, c = 4
Output: [[1,2,3,4]]
Leetcode-cn.com 2021-07-07
🟡 1711.count-good-meals
🏷️ Tags
#array #hash_table
Description
大餐 是指 恰好包含两道不同餐品 的一餐,其美味程度之和等于 2 的幂。
你可以搭配 任意 两道餐品做一顿大餐。
给你一个整数数组
注意,只要餐品下标不同,就可以认为是不同的餐品,即便它们的美味程度相同。
Example
🟡 1711.count-good-meals
🏷️ Tags
#array #hash_table
Description
大餐 是指 恰好包含两道不同餐品 的一餐,其美味程度之和等于 2 的幂。
你可以搭配 任意 两道餐品做一顿大餐。
给你一个整数数组
deliciousness ,其中 deliciousness[i] 是第 i 道餐品的美味程度,返回你可以用数组中的餐品做出的不同 大餐 的数量。结果需要对 109 + 7 取余。注意,只要餐品下标不同,就可以认为是不同的餐品,即便它们的美味程度相同。
Example
输入:deliciousness = [1,3,5,7,9]
输出:4
解释:大餐的美味程度组合为 (1,3) 、(1,7) 、(3,5) 和 (7,9) 。
它们各自的美味程度之和分别为 4 、8 、8 和 16 ,都是 2 的幂。
Leetcode.com 2021-07-06
🟡 1338.reduce-array-size-to-the-half
🏷️ Tags
#greedy #array #hash_table #sorting #heap_priority_queue
Description
Given an array
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example
🟡 1338.reduce-array-size-to-the-half
🏷️ Tags
#greedy #array #hash_table #sorting #heap_priority_queue
Description
Given an array
arr. You can choose a set of integers and remove all the occurrences of these integers in the array.Return the minimum size of the set so that at least half of the integers of the array are removed.
Example
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
Leetcode-cn.com 2021-07-08
🟡 930.binary-subarrays-with-sum
🏷️ Tags
#array #hash_table #prefix_sum #sliding_window
Description
给你一个二元数组
子数组 是数组的一段连续部分。
Example
🟡 930.binary-subarrays-with-sum
🏷️ Tags
#array #hash_table #prefix_sum #sliding_window
Description
给你一个二元数组
nums ,和一个整数 goal ,请你统计并返回有多少个和为 goal 的 非空 子数组。子数组 是数组的一段连续部分。
Example
输入:nums = [1,0,1,0,1], goal = 2
输出:4
解释:
如下面黑体所示,有 4 个满足题目要求的子数组:
[1,0,1,0,1]
[1,0,1,0,1]
[1,0,1,0,1]
[1,0,1,0,1]
Leetcode.com 2021-07-07
🟡 378.kth-smallest-element-in-a-sorted-matrix
🏷️ Tags
#array #binary_search #matrix #sorting #heap_priority_queue
Description
Given an
Note that it is the
Example
🟡 378.kth-smallest-element-in-a-sorted-matrix
🏷️ Tags
#array #binary_search #matrix #sorting #heap_priority_queue
Description
Given an
n x n matrix where each of the rows and columns are sorted in ascending order, return the kth smallest element in the matrix.Note that it is the
kth smallest element in the sorted order, not the kth distinct element.Example
Input: matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
Output: 13
Explanation: The elements in the matrix are [1,5,9,10,11,12,13,13,15], and the 8th smallest number is 13
Leetcode-cn.com 2021-07-09
🟢 面试题 17.10.find-majority-element-lcci
🏷️ Tags
#array #counting
Description
数组中占比超过一半的元素称之为主要元素。给你一个 整数 数组,找出其中的主要元素。若没有,返回
Example
🟢 面试题 17.10.find-majority-element-lcci
🏷️ Tags
#array #counting
Description
数组中占比超过一半的元素称之为主要元素。给你一个 整数 数组,找出其中的主要元素。若没有,返回
-1 。请设计时间复杂度为 O(N) 、空间复杂度为 O(1) 的解决方案。Example
输入:[1,2,5,9,5,9,5,5,5]
输出:5