This file contains hidden or 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 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: |
This file contains hidden or 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
// 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]] |
This file contains hidden or 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 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: |
This file contains hidden or 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
// 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", |
This file contains hidden or 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
// 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 |
This file contains hidden or 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
// 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 |
This file contains hidden or 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
// 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. |
This file contains hidden or 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 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) { |
This file contains hidden or 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 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: |
This file contains hidden or 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
// Implement an iterator to flatten a 2d vector. | |
// For example, | |
// Given 2d vector = | |
// [ | |
// [1,2], | |
// [3], | |
// [4,5,6] | |
// ] |