Skip to content

Instantly share code, notes, and snippets.

View wayetan's full-sized avatar

Wei Tan wayetan

  • Microsoft
  • United States
View GitHub Profile
@wayetan
wayetan / WordSearch.java
Last active August 29, 2015 13:56
Word Search
/**
* 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"],
["SFCS"],
["ADEE"]
@wayetan
wayetan / LargestRectangleinHistogram.java
Created March 1, 2014 09:14
Largest Rectangle in Histogram
/**
* 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.
* For example,
* Given height = [2,1,5,6,2,3],
* return 10.
*/
public class Solution {
public int largestRectangleArea(int[] height) {
@wayetan
wayetan / MaximalRectangle.java
Last active August 29, 2015 13:56
Maximal Rectangle
/**
* Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.
*/
public class Solution {
public int maximalRectangle(char[][] matrix) {
int m = matrix.length;
int n = m == 0 ? 0 : matrix[0].length;
int[][] height = new int[m][n];
int maxArea = 0;
for(int i = 0; i < m; i++) {
@wayetan
wayetan / WordLadder.java
Last active October 14, 2015 08:01
Word Ladder
/**
* Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that:
* 1. Only one letter can be changed at a time
* 2. Each intermediate word must exist in the dictionary
* For example,
* Given:
* start = "hit"
* end = "cog"
* dict = ["hot","dot","dog","lot","log"]
* As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
@wayetan
wayetan / ScrambleString.java
Created February 28, 2014 06:26
Scramble String
/**
* 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":
* great
/ \
gr eat
/ \ / \
g r e at
/ \
a t
@wayetan
wayetan / ValidNumber.java
Last active May 5, 2017 20:05
Valid Number
/**
* Validate if a given string is numeric.
* Some examples:
* "0" => true
* " 0.1 " => true
* "abc" => false
* "1 a" => false
* "2e10" => true
* Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.
*/
@wayetan
wayetan / Decodeways.java
Last active April 4, 2017 18:38
Decode Ways
/**
* 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,
* Given encoded message "12", it could be decoded as "AB" (1 2) or "L" (12).
* The number of ways decoding "12" is 2.
@wayetan
wayetan / RestoreIPAddress.java
Created February 21, 2014 04:07
Restore IP Address
/**
* Given a string containing only digits, restore it by returning all possible valid IP address combinations.
* For example:
* Given "25525511135",
* return ["255.255.11.135", "255.255.111.35"]. (Order does not matter)
*/
public class Solution {
public ArrayList<String> restoreIpAddresses(String s) {
ArrayList<String> res = new ArrayList<String>();
@wayetan
wayetan / InterleavingString.java
Created February 21, 2014 03:47
Interleaving String
/**
* Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.
* For example,
* Given:
* s1 = "aabcc",
* s2 = "dbbca",
* When s3 = "aadbbcbcac", return true.
* When s3 = "aadbbbaccc", return false.
*/
public class Solution {
@wayetan
wayetan / BinaryTreeMaximumPathSum.java
Created February 20, 2014 00:18
Binary Tree Maximum Path Sum
/**
* Given a binary tree, find the maximum path sum.
* The path may start and end at any node in the tree.
* For example:
* For example:
* 1
/ \
2 3
* Return 6.
*/