This file contains 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
// 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; |
This file contains 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 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]. |
This file contains 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 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. |
This file contains 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 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: |
This file contains 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 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 { |
This file contains 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 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>(); |
This file contains 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 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; | |
} |
This file contains 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 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 |
This file contains 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 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. |
This file contains 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 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) |
NewerOlder