Description
Given two integers n and k, return all possible combinations of k numbers out of 1 … n.
Example:
1 | Input: n = 4, k = 2 |
Thinking outside the box
Given n non-negative integers representing the histogram’s bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.
Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3]
.
The largest rectangle is shown in the shaded area, which has area = 10
unit.
Example:
1 | Input: [2,1,5,6,2,3] |
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).
Example:
1 | Input: S = "ADOBECODEBANC", T = "ABC" |
Note:
""
.Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.
Below is one possible representation of s1 = "great"
:
1 | great |
To scramble the string, we may choose any non-leaf node and swap its two children.
For example, if we choose the node "gr"
and swap its two children, it produces a scrambled string "rgeat"
.
1 | rgeat |
We say that "rgeat"
is a scrambled string of "great"
.
Similarly, if we continue to swap the children of nodes "eat"
and "at"
, it produces a scrambled string "rgtae"
.
1 | rgtae |
We say that "rgtae"
is a scrambled string of "great"
.
Given two strings s1 and s2 of the same length, determine if s2 is a scrambled string of s1.
Example 1:
1 | Input: s1 = "great", s2 = "rgeat" |
Example 2:
1 | Input: s1 = "abcde", s2 = "caebd" |
Two elements of a binary search tree (BST) are swapped by mistake.
Recover the tree without changing its structure.
Example 1:
1 | Input: [1,3,null,null,2] |
Example 2:
1 | Input: [3,1,4,null,null,2] |
Follow up:
Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.
You should preserve the original relative order of the nodes in each of the two partitions.
Example:
1 | Input: head = 1->4->3->2->5->2, x = 3 |
Given a binary tree, determine if it is a valid binary search tree (BST).
Assume a BST is defined as follows:
Example 1:
1 | Input: |
Example 2:
1 | 5 |
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue.
Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
Note: You are not suppose to use the library’s sort function for this problem.
Example:
1 | Input: [2,0,2,1,1,0] |
Follow up: