On a 2×3 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.
Examples:
1
2
3
Input:board=[[1,2,3],[4,0,5]]
Output:1
Explanation:Swap the0andthe5inone move.
1
2
3
Input:board=[[1,2,3],[5,4,0]]
Output:-1
Explanation:No number of moves will make the board solved.
1
2
3
4
5
6
7
8
9
10
Input:board=[[4,1,2],[5,0,3]]
Output:5
Explanation:5isthe smallest number of moves that solves the board.
Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU, find the exclusive time of these functions.
Each function has a unique id, start from 0 to n-1. A function may be called recursively or by another function.
A log is a string has this format : function_id:start_or_end:timestamp. For example, "0:start:0" means function 0 starts from the very beginning of time 0. "0:end:0" means function 0 ends to the very end of time 0.
Exclusive time of a function is defined as the time spent within this function, the time spent by calling other functions should not be considered as this function’s exclusive time. You should return the exclusive time of each function sorted by their function id.
Example 1:
1
2
3
4
5
6
7
8
9
10
11
12
13
Input:
n=2
logs=
["0:start:0",
"1:start:2",
"1:end:5",
"0:end:6"]
Output:[3,4]
Explanation:
Function0starts at time0,thenit executes2units of time andreaches the endof time1.
Now function0calls function1,function1starts at time2,executes4units of time andendat time5.
Function0isrunning again at time6,andalso endat the time6,thus executes1unit of time.
So function0totally execute2+1=3units of time,andfunction1totally execute4units of time.
Note:
Input logs will be sorted by timestamp, NOT log id.
Your output should be sorted by function id, which means the 0th element of your output corresponds to the exclusive time of function 0.
Two functions won’t start or end at the same time.
Functions could be called recursively, and will always end.
In the “100 game,” two players take turns adding, to a running total, any integer from 1..10. The player who first causes the running total to reach or exceed 100 wins.
What if we change the game so that players cannot re-use integers?
For example, two players might take turns drawing from a common pool of numbers of 1..15 without replacement until they reach a total >= 100.
Given an integer maxChoosableInteger and another integer desiredTotal, determine if the first player to move can force a win, assuming both players play optimally.
You can always assume that maxChoosableInteger will not be larger than 20 and desiredTotal will not be larger than 300.
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
Input:
maxChoosableInteger=10
desiredTotal=11
Output:
false
Explanation:
No matter which integerthe first player choose,the first player will lose.
The first player can choose an integerfrom1up to10.
Ifthe first player choose1,the second player can only choose integers from2up to10.
The second player will win by choosing10andgetatotal=11,which is>=desiredTotal.
Same with other integers chosen by the first player,the second player will always win.
Solution: Recursion with memoization
Time complexity: O(2^M)
Space complexity: O(2^M)
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Author: Huahua
// Running time: 19 ms (<98.70%)
classSolution{
public:
boolcanIWin(intM,intT){
constintsum=M*(M+1)/2;
if(sum<T)returnfalse;
if(T<=0)returntrue;
m_=vector<char>(1<<M,0);
returncanIWin(M,T,0);
}
private:
vector<char>m_;// 0: unknown, 1: won, -1: lost
boolcanIWin(intM,intT,intstate){
if(T<=0)returnfalse;
if(m_[state])returnm_[state]==1;
for(inti=0;i<M;++i){
if(state&(1<<i))continue;// number i used
// The next player can not win, current player wins