Leetcode 2021-06-12
π΄ 1449.form-largest-integer-with-digits-that-add-up-to-target
π·οΈ Tags
#string #dynamic_programming
Description
Given an array of integers
The cost of painting a digit (i+1) is given by
The total cost used must be equal to
Integer does not have digits 0.
Since the answer may be too large, return it as string.
If there is no way to paint any integer given the condition, return "0".
Example
π΄ 1449.form-largest-integer-with-digits-that-add-up-to-target
π·οΈ Tags
#string #dynamic_programming
Description
Given an array of integers
cost and an integer target. Return the maximum integer you can paint under the following rules:The cost of painting a digit (i+1) is given by
cost[i] (0 indexed).The total cost used must be equal to
target.Integer does not have digits 0.
Since the answer may be too large, return it as string.
If there is no way to paint any integer given the condition, return "0".
Example
Input: cost = [4,3,2,5,6,7,2,5,5], target = 9
Output: "7772"
Explanation: The cost to paint the digit '7' is 2, and the digit '2' is 3. Then cost("7772") = 2*3+ 3*1 = 9. You could also paint "977", but "7772" is the largest number.
Digit cost
1 -> 4
2 -> 3
3 -> 2
4 -> 5
5 -> 6
6 -> 7
7 -> 2
8 -> 5
9 -> 5
Leetcode 2021-06-13
π’ 278.first-bad-version
π·οΈ Tags
#binary_search
Description
You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.
Suppose you have
You are given an API
Example
π’ 278.first-bad-version
π·οΈ Tags
#binary_search
Description
You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.
Suppose you have
n versions [1, 2, ..., n] and you want to find out the first bad one, which causes all the following ones to be bad.You are given an API
bool isBadVersion(version) which returns whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.Example
Input: n = 5, bad = 4
Output: 4
Explanation:
call isBadVersion(3) -> false
call isBadVersion(5) -> true
call isBadVersion(4) -> true
Then 4 is the first bad version.
Leetcode 2021-06-14
π’ 374.guess-number-higher-or-lower
π·οΈ Tags
#binary_search
Description
We are playing the Guess Game. The game is as follows:
I pick a number from
Every time you guess wrong, I will tell you whether the number I picked is higher or lower than your guess.
You call a pre-defined API
Return the number that I picked.
Example
π’ 374.guess-number-higher-or-lower
π·οΈ Tags
#binary_search
Description
We are playing the Guess Game. The game is as follows:
I pick a number from
1 to n. You have to guess which number I picked.Every time you guess wrong, I will tell you whether the number I picked is higher or lower than your guess.
You call a pre-defined API
int guess(int num), which returns 3 possible results:-1: The number I picked is lower than your guess (i.e. pick < num).1: The number I picked is higher than your guess (i.e. pick > num).0: The number I picked is equal to your guess (i.e. pick == num).Return the number that I picked.
Example
Input: n = 10, pick = 6
Output: 6
Leetcode 2021-06-15
π’ 852.peak-index-in-a-mountain-array
π·οΈ Tags
#binary_search
Description
Let's call an array
There exists some
Given an integer array
Example
π’ 852.peak-index-in-a-mountain-array
π·οΈ Tags
#binary_search
Description
Let's call an array
arr a mountain if the following properties hold:arr.length >= 3There exists some
i with 0 < i < arr.length - 1 such that:arr[0] < arr[1] < ... arr[i-1] < arr[i] arr[i] > arr[i+1] > ... > arr[arr.length - 1]Given an integer array
arr that is guaranteed to be a mountain, return any i such that arr[0] < arr[1] < ... arr[i - 1] < arr[i] > arr[i + 1] > ... > arr[arr.length - 1].Example
Input: arr = [0,1,0]
Output: 1
Leetcode 2021-06-16
π‘ 877.stone-game
π·οΈ Tags
#minimax #math #dynamic_programming
Description
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return
Example
π‘ 877.stone-game
π·οΈ Tags
#minimax #math #dynamic_programming
Description
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones
piles[i].The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return
True if and only if Alex wins the game.Example
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Leetcode 2021-06-17
π΄ 65.valid-number
π·οΈ Tags
#math #string
Description
A valid number can be split up into these components (in order):
A decimal number or an integer.
(Optional) An
A decimal number can be split up into these components (in order):
(Optional) A sign character (either
One of the following formats:
One or more digits, followed by a dot
One or more digits, followed by a dot
A dot
An integer can be split up into these components (in order):
(Optional) A sign character (either
One or more digits.
For example, all the following are valid numbers:
Given a string
Example
π΄ 65.valid-number
π·οΈ Tags
#math #string
Description
A valid number can be split up into these components (in order):
A decimal number or an integer.
(Optional) An
'e' or 'E', followed by an integer.A decimal number can be split up into these components (in order):
(Optional) A sign character (either
'+' or '-').One of the following formats:
One or more digits, followed by a dot
'.'.One or more digits, followed by a dot
'.', followed by one or more digits.A dot
'.', followed by one or more digits.An integer can be split up into these components (in order):
(Optional) A sign character (either
'+' or '-').One or more digits.
For example, all the following are valid numbers:
["2", "0089", "-0.1", "+3.14", "4.", "-.9", "2e10", "-90E3", "3e+7", "+6e-1", "53.5e93", "-123.456e789"], while the following are not valid numbers: ["abc", "1a", "1e", "e3", "99e2.5", "--6", "-+3", "95a54e53"].Given a string
s, return true if s is a valid number.Example
Input: s = "0"
Output: true
Leetcode 2021-06-18
π΄ 483.smallest-good-base
π·οΈ Tags
#math #binary_search
Description
Given an integer
We call
Example
π΄ 483.smallest-good-base
π·οΈ Tags
#math #binary_search
Description
Given an integer
n represented as a string, return the smallest good base of n.We call
k >= 2 a good base of n, if all digits of n base k are 1's.Example
Input: n = "13"
Output: "3"
Explanation: 13 base 3 is 111.
Leetcode 2021-06-19
π‘ 1239.maximum-length-of-a-concatenated-string-with-unique-characters
π·οΈ Tags
#bit_manipulation #backtracking
Description
Given an array of strings
Return the maximum possible length of
Example
π‘ 1239.maximum-length-of-a-concatenated-string-with-unique-characters
π·οΈ Tags
#bit_manipulation #backtracking
Description
Given an array of strings
arr. String s is a concatenation of a sub-sequence of arr which have unique characters.Return the maximum possible length of
s.Example
Input: arr = ["un","iq","ue"]
Output: 4
Explanation: All possible concatenations are "","un","iq","ue","uniq" and "ique".
Maximum length is 4.
Leetcode 2021-06-20
π‘ 1600.throne-inheritance
π·οΈ Tags
#tree #design
Description
A kingdom consists of a king, his children, his grandchildren, and so on. Every once in a while, someone in the family dies or a child is born.
The kingdom has a well-defined order of inheritance that consists of the king as the first member. Let's define the recursive function
For example, assume we have a kingdom that consists of the king, his children Alice and Bob (Alice is older than Bob), and finally Alice's son Jack.
In the beginning,
Calling
Calling
Calling
Calling
Using the above function, we can always obtain a unique order of inheritance.
Implement the
Example
π‘ 1600.throne-inheritance
π·οΈ Tags
#tree #design
Description
A kingdom consists of a king, his children, his grandchildren, and so on. Every once in a while, someone in the family dies or a child is born.
The kingdom has a well-defined order of inheritance that consists of the king as the first member. Let's define the recursive function
Successor(x, curOrder), which given a person x and the inheritance order so far, returns who should be the next person after x in the order of inheritance.
Successor(x, curOrder):
if x has no children or all of x's children are in curOrder:
if x is the king return null
else return Successor(x's parent, curOrder)
else return x's oldest child who's not in curOrder
For example, assume we have a kingdom that consists of the king, his children Alice and Bob (Alice is older than Bob), and finally Alice's son Jack.
In the beginning,
curOrder will be ["king"].Calling
Successor(king, curOrder) will return Alice, so we append to curOrder to get ["king", "Alice"].Calling
Successor(Alice, curOrder) will return Jack, so we append to curOrder to get ["king", "Alice", "Jack"].Calling
Successor(Jack, curOrder) will return Bob, so we append to curOrder to get ["king", "Alice", "Jack", "Bob"].Calling
Successor(Bob, curOrder) will return null. Thus the order of inheritance will be ["king", "Alice", "Jack", "Bob"].Using the above function, we can always obtain a unique order of inheritance.
Implement the
ThroneInheritance class:ThroneInheritance(string kingName) Initializes an object of the ThroneInheritance class. The name of the king is given as part of the constructor.void birth(string parentName, string childName) Indicates that parentName gave birth to childName.void death(string name) Indicates the death of name. The death of the person doesn't affect the Successor function nor the current inheritance order. You can treat it as just marking the person as dead.string[] getInheritanceOrder() Returns a list representing the current order of inheritance excluding dead people.Example
Input
["ThroneInheritance", "birth", "birth", "birth", "birth", "birth", "birth", "getInheritanceOrder", "death", "getInheritanceOrder"]
[["king"], ["king", "andy"], ["king", "bob"], ["king", "catherine"], ["andy", "matthew"], ["bob", "alex"], ["bob", "asha"], [null], ["bob"], [null]]
Output
[null, null, null, null, null, null, null, ["king", "andy", "matthew", "bob", "alex", "asha", "catherine"], null, ["king", "andy", "matthew", "alex", "asha", "catherine"]]
Explanation
ThroneInheritance t= new ThroneInheritance("king"); // order: king
t.birth("king", "andy"); // order: king > andy
t.birth("king", "bob"); // order: king > andy > bob
t.birth("king", "catherine"); // order: king > andy > bob > catherine
t.birth("andy", "matthew"); // order: king > andy > matthew > bob > catherine
t.birth("bob", "alex"); // order: king > andy > matthew > bob > alex > catherine
t.birth("bob", "asha"); // order: king > andy > matthew > bob > alex > asha > catherine
t.getInheritanceOrder(); // return ["king", "andy", "matthew", "bob", "alex", "asha", "catherine"]
t.death("bob"); // order: king > andy > matthew > bob > alex > asha > catherine
t.getInheritanceOrder(); // return ["king", "andy", "matthew", "alex", "asha", "catherine"]
Leetcode 2021-06-21
π’ 401.binary-watch
π·οΈ Tags
#bit_manipulation #backtracking
Description
A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom represent the minutes (0-59). Each LED represents a zero or one, with the least significant bit on the right.
For example, the below binary watch reads
Given an integer
The hour must not contain a leading zero.
For example,
The minute must be consist of two digits and may contain a leading zero.
For example,
Example
π’ 401.binary-watch
π·οΈ Tags
#bit_manipulation #backtracking
Description
A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom represent the minutes (0-59). Each LED represents a zero or one, with the least significant bit on the right.
For example, the below binary watch reads
"4:51".Given an integer
turnedOn which represents the number of LEDs that are currently on, return all possible times the watch could represent. You may return the answer in any order.The hour must not contain a leading zero.
For example,
"01:00" is not valid. It should be "1:00".The minute must be consist of two digits and may contain a leading zero.
For example,
"10:2" is not valid. It should be "10:02".Example
Input: turnedOn = 1
Output: ["0:01","0:02","0:04","0:08","0:16","0:32","1:00","2:00","4:00","8:00"]
Leetcode 2021-06-22
π‘ εζ Offer 38.zi-fu-chuan-de-pai-lie-lcof
π·οΈ Tags
#backtracking
undefinedundefined
π‘ εζ Offer 38.zi-fu-chuan-de-pai-lie-lcof
π·οΈ Tags
#backtracking
undefinedundefined
Leetcode 2021-06-23
π’ εζ Offer 15.er-jin-zhi-zhong-1de-ge-shu-lcof
π·οΈ Tags
#bit_manipulation
undefinedundefined
π’ εζ Offer 15.er-jin-zhi-zhong-1de-ge-shu-lcof
π·οΈ Tags
#bit_manipulation
undefinedundefined
Leetcode 2021-06-24
π΄ 149.max-points-on-a-line
π·οΈ Tags
#hash_table #math
Description
Given an array of
Example
π΄ 149.max-points-on-a-line
π·οΈ Tags
#hash_table #math
Description
Given an array of
points where points[i] = [xi, yi] represents a point on the X-Y plane, return the maximum number of points that lie on the same straight line.Example
Input: points = [[1,1],[2,2],[3,3]]
Output: 3
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