Skip to content

Instantly share code, notes, and snippets.

@cangoal
cangoal / FindtheCelebrity.java
Created May 12, 2016 20:01
LeetCode - Find the Celebrity
// Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist one celebrity. The definition of a celebrity is that all the other n - 1 people know him/her but he/she does not know any of them.
// Now you want to find out who the celebrity is or verify that there is not one. The only thing you are allowed to do is to ask questions like: "Hi, A. Do you know B?" to get information of whether A knows B. You need to find out the celebrity (or verify there is not one) by asking as few questions as possible (in the asymptotic sense).
// You are given a helper function bool knows(a, b) which tells you whether A knows B. Implement a function int findCelebrity(n), your function should minimize the number of calls to knows.
// Note: There will be exactly one celebrity if he/she is in the party. Return the celebrity's label if there is a celebrity in the party. If there is no celebrity, return -1.
public int findCelebrity(int n) {
if(n <= 1) return n - 1;
@cangoal
cangoal / BinaryTreeUpsideDown.java
Last active May 12, 2016 19:51
LeetCode - Binary Tree Upside Down
// Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tree where the original right nodes turned into left leaf nodes. Return the new root.
// For example:
// Given a binary tree {1,2,3,4,5},
// 1
// / \
// 2 3
// / \
// 4 5
// return the root of the binary tree [4,5,2,#,#,3,1].
@cangoal
cangoal / WordPatternII.java
Created May 11, 2016 20:08
LeetCode - Word Pattern II
// Given a pattern and a string str, find if str follows the same pattern.
// Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty substring in str.
// Examples:
// pattern = "abab", str = "redblueredblue" should return true.
// pattern = "aaaa", str = "asdasdasdasd" should return true.
// pattern = "aabb", str = "xyzabcxzyabc" should return false.
// Notes:
// You may assume both pattern and str contains only lowercase letters.
@cangoal
cangoal / WordPattern.java
Created May 11, 2016 18:49
LeetCode - Word Patttern
// Given a pattern and a string str, find if str follows the same pattern.
// Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.
// Examples:
// pattern = "abba", str = "dog cat cat dog" should return true.
// pattern = "abba", str = "dog cat cat fish" should return false.
// pattern = "aaaa", str = "dog cat cat dog" should return false.
// pattern = "abba", str = "dog dog dog dog" should return false.
// Notes:
@cangoal
cangoal / MovingAveragefromDataStream.java
Created May 11, 2016 03:31
LeetCode - Moving Average from Data Stream
// Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window.
// For example,
// MovingAverage m = new MovingAverage(3);
// m.next(1) = 1
// m.next(10) = (1 + 10) / 2
// m.next(3) = (1 + 10 + 3) / 3
// m.next(5) = (10 + 3 + 5) / 3
public class MovingAverage {
@cangoal
cangoal / VerifyPreorderSequenceinBinarySearchTree.java
Created April 21, 2016 04:44
LeetCode - Verify Preorder Sequence in Binary Search Tree
// Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary search tree.
// You may assume each number in the sequence is unique.
// Follow up:
// Could you do it using only constant space complexity?
// use stack
public boolean verifyPreorder(int[] preorder) {
Stack<Integer> stack = new Stack<Integer>();
@cangoal
cangoal / PreviousPermutation.java
Last active April 21, 2016 01:53
LintCode - Previous Permutation
public ArrayList<Integer> previousPermuation(ArrayList<Integer> nums) {
// write your code
if(nums == null || nums.size() < 2) return nums;
int start = -1;
// find the fisrt "large" number
for(int i=nums.size()-2; i>=0; i--){
if(nums.get(i) > nums.get(i+1)){
start = i;
break;
}
@cangoal
cangoal / LargestBSTSubtree.java
Created April 21, 2016 00:32
LeetCode - Largest BST Subtree
// Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest means subtree with largest number of nodes in it.
// Note:
// A subtree must include all of its descendants.
// Here's an example:
// 10
// / \
// 5 15
// / \ \
// 1 8 7
@cangoal
cangoal / StrobogrammaticNumberIII.java
Created April 20, 2016 19:56
LeetCode - Strobogrammatic Number III
// A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).
// Write a function to count the total strobogrammatic numbers that exist in the range of low <= num <= high.
// For example,
// Given low = "50", high = "100", return 3. Because 69, 88, and 96 are three strobogrammatic numbers.
// Note:
// Because the range might be a large number, the low and high numbers are represented as string.
@cangoal
cangoal / MaximumSizeSubarraySumEqualsk.java
Created April 19, 2016 03:28
LeetCode - Maximum Size Subarray Sum Equals k
// Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If there isn't one, return 0 instead.
// Example 1:
// Given nums = [1, -1, 5, -2, 3], k = 3,
// return 4. (because the subarray [1, -1, 5, -2] sums to 3 and is the longest)
// Example 2:
// Given nums = [-2, -1, 2, 1], k = 1,
// return 2. (because the subarray [-1, 2] sums to 1 and is the longest)