π One-stop destination for all your technical interview Preparation π
maxLength = max(maxLength, r-l);
We only need to handle four cases:
for(i=0,n-1)
for(j=i+1,n-1)
for(k=j+1,n-1)
a+b+c==0, cnt++
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
k
such that nums[k] < nums[k + 1]
. If no such index exists, just reverse nums
and done.
2.From back find the largest index l > k
such that nums[k] < nums[l]
. 3.Swap
nums[k]
and nums[l]
. 4.Reverse
the sub-array nums[k + 1:]
.next_permutation(a.being(),a.end())
function in c++.Solve function algorithm:
if current cell is already marked, move to the next cell.
isValid function algorithm:
int iindex = newRow + i, jindex = newCol + j;`min(maxLeft(i),maxRight(i))-a[i]
Step 1: DO
Step 2: RECUR
Step 3: UNDO
Make sure to use base conditions.
clockwise rotate
first reverse up to down, then swap the symmetry
1 2 3 7 8 9 7 4 1
4 5 6 => 4 5 6 => 8 5 2
7 8 9 1 2 3 9 6 3
Anticlockwise rotate
first swap the symmetry, then reverse up to down
1 2 3 1 4 7 3 6 9
4 5 6 => 2 5 8 => 2 5 8
7 8 9 3 6 9 1 4 7
0
, return 1.0
ans = x
ans
with x
.x
and finally return 1/result
.n
is integer
and range of integers is -2,147,483,648 to 2,147,483,647 so if we convert -2,147,483,648 to positive, it will overflow.2^5 = 2*(2^4) = 2*(4^2) = 2*16 = 32
Main Algorithm - we iterate over each row in the board, i.e. once we reach the last row of the board, we should have explored all the possible solutions. - At each iteration (we are located at certain row), we then further iterate over each column of the board, along the current row. At this second iteration, we then can explore the possibility of placing a queen on a particular cell. - Before, we place a queen on the cell with index of (row, col)
, we need to check if this cell is under the attacking zone of the queens that have been placed on the board before. We can do it with isSafe()
function. - Once the check passes, we then can proceed to place a queen. Along with the placement, one should also mark out the attacking zone of this newly-placed queen. We achieve this with placeQueen()
function. - As an important behavior of backtracking, we should be able to abandon our previous decision at the moment we decide to move on to the next candidate. We achieve this with removeQueen()
function.
The time complexity of nQueens
problem is dependent of how we implement isSafe()
function.
isSafe()
function:
All the algorithm is same but we can optimize the isSafe()
function by using extra space.
isSafe()
function:
colSet
vector to store the column index of the queens that are placed on the board.posDig
and negDig
vectors to store the positive and negative diagonal indices of the queens that are placed on the board.col + row
col - row + n - 1
Again same algorithm but we just used 1 vector instead of 3 vectors.
isSafe()
function:
flag[0] to flag[n - 1]
to indicate if the column had a queen before.flag[n] to flag[3 * n - 2]
to indicate if the 45Β° diagonal had a queen before.flag[3 * n - 1] to flag[5 * n - 3]
to indicate if the 135Β° diagonal had a queen before.flag(5 * n - 3)
vector to store the information of the board.n + col + row
4 * n - 2 + col - row
For explanation refer previous question nQueens-1
max(temp[1],x[1])
.INT_MAX
, because we are taking minimum of right and down.grid[m-1][n-1]
, because it Donβt have any other choice.SC: O(m*n)
col
variable and set it true
initially. while traversing the array if we got any 0 in first column so we change col = false
.col==false
we set it to 0.sort()
function.Step 1: DO
Step 2: RECUR
Step 3: UNDO
Make sure to use base conditions.
way1+way2
because we want to find all the possible ways to decode the string.Soonβ¦
There are a few ways to solve this problem. I have explained all the solutions and approach to optimize it from Brute-Force to Dynamic Programming to solving using Catalan formula in: β [ Simple Solution w/ Explanation | Optimization from Brute-Force to DP](https://leetcode.com/problems/unique-binary-search-trees/discuss/1565543/Simple-Solution-w-Explanation-or-Optimization-from-Brute-Force-to-DP) |
( Time: O(3^N), Space: O(N) )
( Time: O(N^2), Space: O(N) )
( Time: O(N^2), Space: O(N) )
( Time: O(N), Space: O(1) )
( Time: O(N), Space: O(1) )
[C++ Easy & Clean Solution | Fastest | All (4+) Methods | Detailed Explanation & Comments](https://leetcode.com/problems/unique-binary-search-trees/discuss/1565544/C%2B%2B-Easy-and-Clean-Solution-or-Fastest-or-All-(3%2B)-Methods-or-Detailed-Explanation-and-Comments) |
dp[0] = dp[1] = 1.
i
from 2...n.
i
nodes. we can consider each of the node j
from 1...i
as the root node.i
nodes, the result is summation for all j
from 1...i
of dp[j-1] * dp[i-j]
. (Comparing to above solution dp[j-1] = numTrees(j-1) and dp[i-j]=numTrees(i-j)
)