Given binary tree
{1,#,2,3}
,1 \ 2 / 3
[1,3,2]
.{1,#,2,3}
,1 \ 2 / 3
[1,3,2]
."((()))", "(()())", "(())()", "()(())", "()()()"
{1,#,2,3}
,1 \ 2 / 3
[3,2,1]
.[1,2,3]
have the following permutations:[1,2,3]
, [1,3,2]
, [2,1,3]
, [2,3,1]
, [3,1,2]
, and [3,2,1]
.
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.
Note: You can only move either down or right at any point in time.
Apply DFS first failed on time limitation, then adopted DP with 2 dimension matrix passed OJ, finally refector code to use 1 dimension rotational array
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
For example,
Consider the following matrix:
[
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
For an O (rows * cols) solution is too simple to meet the interviewer’s expectation. So what is the better solution should be? Binary search should be a good tool when I face search problem, however, how BS could be apply in matrix? if we regard 0 as start point, total_cols*total_rows-1 as the end point to look the matrix as a array then we can apply BS. However, how could we convert the position to row_NO and col_NO? We know row_NO * total_cols+col_NO=position, So in programming row_NO=postion/total_cols, col_NO=postion%total_cols. then we can adapt this feature in our program to apply binary search to get O(log(cols+rows)) search
[1,1,1,2,2,3]
,5
, and A is now [1,1,2,2,3]
.3
,[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ]
[1,3,3,1]
.sum = 22
,5 / \ 4 8 / / \ 11 13 4 / \ \ 7 2 1
5->4->11->2
which sum is 22.[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ]
Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5.
1 / \ 2 3 / \ \ 4 5 7
1 -> NULL / \ 2 -> 3 -> NULL / \ \ 4-> 5 -> 7 -> NULL
Solution : The main idea actually is tree level traversal, we can just use one node called current represent current level (start from root) and two other nodes which called nextLevelHead and nextLevelEnd to record next level's left child and right child and when current node is null then exchange current and nextLevelHead until nextLevelHead is also null
0-9
only, each root-to-leaf path could represent a number.1->2->3
which represents the number 123
.1 / \ 2 3
1->2
represents the number 12
.1->3
represents the number 13
.25
.0 1 2 4 5 6 7
might become 4 5 6 7 0 1 2
).[0,1,0,2,1,0,1,3,2,1,2,1]
, return 6
.'('
, ')'
, '{'
, '}'
, '['
and ']'
, determine if the input string is valid."()"
and "()[]{}"
are all valid but "(]"
and "([)]"
are not.'.'
.sum = 22
,5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1
[ [5,4,11,2], [5,8,4,5] ]
[1,2,3]
, a solution is:[ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ]