Skip to content

Instantly share code, notes, and snippets.

@cangoal
cangoal / ShortestWordDistanceIII.java
Created April 19, 2016 02:39
LeetCode - Shortest Word Distance III
This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as word2.
// Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list.
// word1 and word2 may be the same and they represent two individual words in the list.
// For example,
// Assume that words = ["practice", "makes", "perfect", "coding", "makes"].
// Given word1 = “makes”, word2 = “coding”, return 1.
@cangoal
cangoal / ShortestWordDistanceII.java
Created April 19, 2016 02:14
LeetCode - Shortest Word Distance II
// This is a follow up of Shortest Word Distance. The only difference is now you are given the list of words and your method will be called repeatedly many times with different parameters. How would you optimize it?
// Design a class which receives a list of words in the constructor, and implements a method that takes two words word1 and word2 and return the shortest distance between these two words in the list.
// For example,
// Assume that words = ["practice", "makes", "perfect", "coding", "makes"].
// Given word1 = “coding”, word2 = “practice”, return 3.
// Given word1 = "makes", word2 = "coding", return 1.
@cangoal
cangoal / ShortestWordDistance.java
Last active April 19, 2016 02:23
LeetCode - Shortest Word Distance
// Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list.
// For example,
// Assume that words = ["practice", "makes", "perfect", "coding", "makes"].
// Given word1 = “coding”, word2 = “practice”, return 3.
// Given word1 = "makes", word2 = "coding", return 1.
// Note:
// You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.
@cangoal
cangoal / BestMeetingPoint.java
Created April 19, 2016 00:49
LeetCode - Best Meeting Point
// A group of two or more people wants to meet and minimize the total travel distance. You are given a 2D grid of values 0 or 1, where each 1 marks the home of someone in the group. The distance is calculated using Manhattan Distance, where distance(p1, p2) = |p2.x - p1.x| + |p2.y - p1.y|.
// For example, given three people living at (0,0), (0,4), and (2,2):
// 1 - 0 - 0 - 0 - 1
// | | | | |
// 0 - 0 - 0 - 0 - 0
// | | | | |
// 0 - 0 - 1 - 0 - 0
// The point (0,2) is an ideal meeting point, as the total travel distance of 2+2+2=6 is minimal. So return 6.
@cangoal
cangoal / SparseMatrixMultiplication.java
Created April 18, 2016 20:58
LeetCode - Sparse Matrix Multiplication
// Given two sparse matrices A and B, return the result of AB.
// You may assume that A's column number is equal to B's row number.
// Example:
// A = [
// [ 1, 0, 0],
// [-1, 0, 3]
// ]
@cangoal
cangoal / NestedListWeightSum.java
Created April 18, 2016 04:42
LeetCode - Nested List Weight Sum
// Given a nested list of integers, return the sum of all integers in the list weighted by their depth.
// 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]], return 10. (four 1's at depth 2, one 2 at depth 1)
// Example 2:
// Given the list [1,[4,[6]]], return 27. (one 1 at depth 1, one 4 at depth 2, and one 6 at depth 3; 1 + 4*2 + 6*3 = 27)
@cangoal
cangoal / TheSkylineProblem.java
Created April 18, 2016 02:05
LeetCode - The Skyline Problem
public List<int[]> getSkyline(int[][] buildings) {
List<int[]> res = new ArrayList<int[]>();
List<int[]> heights = new ArrayList<int[]>();
for(int[] building : buildings){
heights.add(new int[]{building[0], -building[2]});
heights.add(new int[]{building[1], building[2]});
}
Collections.sort(heights, (a, b) ->{
@cangoal
cangoal / PalindromePairs.java
Created April 17, 2016 05:51
LeetCode - Palindrome Pairs
// Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that the concatenation of the two words, i.e. words[i] + words[j] is a palindrome.
// Example 1:
// Given words = ["bat", "tab", "cat"]
// Return [[0, 1], [1, 0]]
// The palindromes are ["battab", "tabbat"]
// Example 2:
// Given words = ["abcd", "dcba", "lls", "s", "sssll"]
// Return [[0, 1], [1, 0], [3, 2], [2, 4]]
// The palindromes are ["dcbaabcd", "abcddcba", "slls", "llssssll"]
@cangoal
cangoal / CreateMaximumNumber.java
Created April 17, 2016 04:19
LeetCode - Create Maximum Number
// Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum number of length k <= m + n from digits of the two. The relative order of the digits from the same array must be preserved. Return an array of the k digits. You should try to optimize your time and space complexity.
// Example 1:
// nums1 = [3, 4, 6, 5]
// nums2 = [9, 1, 2, 5, 8, 3]
// k = 5
// return [9, 8, 6, 5, 3]
// Example 2:
// nums1 = [6, 7]
@cangoal
cangoal / BinaryTreeVerticalOrderTraversal.java
Last active April 17, 2016 02:52
LeetCode - Binary Tree Vertical Order Traversal
// Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bottom, column by column).
// If two nodes are in the same row and column, the order should be from left to right.
// Examples:
// Given binary tree [3,9,20,null,null,15,7],
// 3
// /\
// / \