This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
A message containing letters from A-Z is being encoded to numbers using the following mapping: | |
'A' -> 1 | |
'B' -> 2 | |
... | |
'Z' -> 26 | |
Given an encoded message containing digits, determine the total number of ways to decode it. | |
For example, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) | |
You have the following 3 operations permitted on a word: | |
a) Insert a character | |
b) Delete a character | |
c) Replace a character | |
*/ | |
public int editDistance(String w1, String w2){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. | |
For example, given array S = {-1 2 1 -4}, and target = 1. | |
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2). | |
Time Complexity : O(N^2) | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Given a 2D board and a word, find if the word exists in the grid. | |
The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once. | |
For example, | |
Given board = | |
[ | |
["ABCE"], |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public boolean search(int[] A, int target) { | |
int low = 0, high = A.length-1; | |
while (low <= high){ | |
while (low < high && A[low] == A[high]) | |
high--; | |
int mid = low + (high - low)/2; | |
if (A[mid] == target) | |
return true; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int search(int[] A, int target) { | |
int low = 0, high = A.length-1; | |
while (low <= high){ | |
int mid = low + (high - low)/2; | |
if (A[mid] == target) | |
return mid; | |
if (A[low] <= A[mid]){ | |
if (A[low] <= target && target < A[mid]) | |
high = mid-1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Bar{ | |
int height, startIdx; | |
Bar(int h, int i){ this.height = h; this.startIdx = i; } | |
} | |
public int largestRectangleArea(int[] height) { | |
int maxArea = 0; | |
Stack<Bar> stack = new Stack<Bar>(); | |
stack.push(new Bar(-1, 0)); | |
for (int i=0; i<=height.length; i++){ | |
int h = i < height.length ? height[i] : 0; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void merge(int A[], int m, int B[], int n) { | |
int k = n + m - 1; | |
while (m > 0 && n > 0){ | |
if (A[m-1] > B[n-1]) | |
A[k--] = A[--m]; | |
else | |
A[k--] = B[--n]; | |
} | |
while (n > 0) | |
A[k--] = B[--n]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static int[] firstKSmallestElements(int[] A, int k){ | |
int n = A.length; | |
if (n == 0 || k == 0 || n < k) return null; | |
int idx = selectK(A, 0, n-1, k); | |
// int idx = selectKIterative(A, 0, n-1, k); | |
return Arrays.copyOf(A, k); | |
} | |
static int selectK(int[] A, int low, int high, int k){ | |
int pivot = partition(A, low, high); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static int findLeftMostElement(int[] A, int key){ | |
if (A.length == 0) return -1; | |
int low = 0, high = A.length-1; | |
while (low < high-1){ | |
int mid = low + (high - low)/2; | |
if (A[mid] < key) | |
low = mid; | |
else | |
high = mid; | |
} |