Skip to content

Instantly share code, notes, and snippets.

@cangoal
cangoal / WiggleSortII.java
Last active April 15, 2016 21:01
LeetCode - Wiggle Sort II
// Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]....
// Example:
// (1) Given nums = [1, 5, 1, 1, 6, 4], one possible answer is [1, 4, 1, 5, 1, 6].
// (2) Given nums = [1, 3, 2, 2, 3, 1], one possible answer is [2, 3, 1, 3, 1, 2].
// Note:
// You may assume all input has valid answer.
// Follow Up:
@cangoal
cangoal / MinimumHeightTrees.java
Created April 15, 2016 15:32
LeetCode - Minimum Height Trees
// For a undirected graph with tree characteristics, we can choose any node as the root. The result graph is then a rooted tree. Among all possible rooted trees, those with minimum height are called minimum height trees (MHTs). Given such a graph, write a function to find all the MHTs and return a list of their root labels.
// Format
// The graph contains n nodes which are labeled from 0 to n - 1. You will be given the number n and a list of undirected edges (each edge is a pair of labels).
// You can assume that no duplicate edges will appear in edges. Since all edges are undirected, [0, 1] is the same as [1, 0] and thus will not appear together in edges.
// Example 1:
// Given n = 4, edges = [[1, 0], [1, 2], [1, 3]]
@cangoal
cangoal / BurstBalloons.java
Created April 15, 2016 14:17
LeetCode - Burst Balloons
// Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by array nums. You are asked to burst all the balloons. If the you burst balloon i you will get nums[left] * nums[i] * nums[right] coins. Here left and right are adjacent indices of i. After the burst, the left and right then becomes adjacent.
// Find the maximum coins you can collect by bursting the balloons wisely.
// Note:
// (1) You may imagine nums[-1] = nums[n] = 1. They are not real therefore you can not burst them.
// (2) 0 ≤ n ≤ 500, 0 ≤ nums[i] ≤ 100
// Example:
@cangoal
cangoal / AlienDictionary.java
Created April 15, 2016 05:47
LeetCode - Alien Dictionary
// There is a new alien language which uses the latin alphabet. However, the order among letters are unknown to you. You receive a list of words from the dictionary, where words are sorted lexicographically by the rules of this new language. Derive the order of letters in this language.
// For example,
// Given the following words in dictionary,
// [
// "wrt",
// "wrf",
// "er",
// "ett",
@cangoal
cangoal / UniqueWordAbbreviation.java
Last active April 15, 2016 03:00
LeetCode - Unique Word Abbreviation
// An abbreviation of a word follows the form <first letter><number><last letter>. Below are some examples of word abbreviations:
// a) it --> it (no abbreviation)
// 1
// b) d|o|g --> d1g
// 1 1 1
// 1---5----0----5--8
// c) i|nternationalizatio|n --> i18n
@cangoal
cangoal / GeneralizedAbbreviation.java
Last active August 28, 2018 00:35
LeetCode - Generalized Abbreviation
// Write a function to generate the generalized abbreviations of a word.
// Example:
// Given word = "word", return the following list (order does not matter):
// ["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "w1r1", "1o2", "2r1", "3d", "w3", "4"]
// Show Company Tags
// Show Tags
// Show Similar Problems
// my solution
@cangoal
cangoal / CountofSmallerNumbersAfterSelf.java
Last active April 18, 2016 04:14
LeetCode - Count of Smaller Numbers After Self
// You are given an integer array nums and you have to return a new counts array. The counts array has the property where counts[i] is the number of smaller elements to the right of nums[i].
// Example:
// Given nums = [5, 2, 6, 1]
// To the right of 5 there are 2 smaller elements (2 and 1).
// To the right of 2 there is only 1 smaller element (1).
// To the right of 6 there is 1 smaller element (1).
// To the right of 1 there is 0 smaller element.
@cangoal
cangoal / ClosestBinarySearchTreeValueII.java
Created April 14, 2016 14:24
LeetCode - Closest Binary Search Tree Value II
// Given a non-empty binary search tree and a target value, find k values in the BST that are closest to the target.
// Note:
// Given target value is a floating point.
// You may assume k is always valid, that is: k ≤ total nodes.
// You are guaranteed to have only one unique set of k values in the BST that are closest to the target.
// Follow up:
// Assume that the BST is balanced, could you solve it in less than O(n) runtime (where n = total nodes)?
public List<Integer> closestKValues(TreeNode root, double target, int k) {
@cangoal
cangoal / FlattenNestedListIterator.java
Created April 14, 2016 06:01
LeetCode - Flatten Nested List Iterator
// Given a nested list of integers, implement an iterator to flatten it.
// Each element is either an integer, or a list -- whose elements may also be integers or other lists.
// Example 1:
// Given the list [[1,1],2,[1,1]],
// By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,1,2,1,1].
// Example 2:
@cangoal
cangoal / Flatten2DVector.java
Created April 14, 2016 02:31
LeetCode - Flatten 2D Vector
// Implement an iterator to flatten a 2d vector.
// For example,
// Given 2d vector =
// [
// [1,2],
// [3],
// [4,5,6]
// ]