-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
65b9c73
commit 0fe452a
Showing
20 changed files
with
480 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// 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 | ||
// The Largest BST Subtree in this case is the highlighted one. | ||
// The return value is the subtree's size, which is 3. | ||
// | ||
// | ||
// | ||
// Hint: | ||
// | ||
// You can recursively use algorithm similar to 98. | ||
// Validate Binary Search Tree at each node of the tree, which will result in O(nlogn) time complexity. | ||
// Follow up: | ||
// Can you figure out ways to solve it with O(n) time complexity? | ||
public class Solution{ | ||
public int largestBSTSubtree(TreeNode root) { | ||
|
||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array. | ||
// | ||
// Formally the function should: | ||
// Return true if there exists i, j, k | ||
// such that arr[i] < arr[j] < arr[k] given 0 ≤ i < j < k ≤ n-1 else return false. | ||
// Your algorithm should run in O(n) time complexity and O(1) space complexity. | ||
// | ||
// Examples: | ||
// Given [1, 2, 3, 4, 5], | ||
// return true. | ||
// | ||
// Given [5, 4, 3, 2, 1], | ||
// return false. | ||
|
||
public class Solution { | ||
public boolean increasingTriplet(int[] nums) { | ||
|
||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// You are given an array x of n positive numbers. You start at point (0,0) and moves x[0] metres to the north, then x[1] metres to the west, x[2] metres to the south, x[3] metres to the east and so on. In other words, after each move your direction changes counter-clockwise. | ||
// | ||
// Write a one-pass algorithm with O(1) extra space to determine, if your path crosses itself, or not. | ||
// | ||
// Example 1: | ||
// Given x = [2, 1, 1, 2], | ||
// ┌───┐ | ||
// │ │ | ||
// └───┼──> | ||
// │ | ||
// | ||
// Return true (self crossing) | ||
// Example 2: | ||
// Given x = [1, 2, 3, 4], | ||
// ┌──────┐ | ||
// │ │ | ||
// │ | ||
// │ | ||
// └────────────> | ||
// | ||
// Return false (not self crossing) | ||
// Example 3: | ||
// Given x = [1, 1, 1, 1], | ||
// ┌───┐ | ||
// │ │ | ||
// └───┼> | ||
// | ||
// Return true (self crossing) | ||
|
||
public class Solution { | ||
public boolean isSelfCrossing(int[] x) { | ||
|
||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// 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"] | ||
|
||
public class Solution { | ||
public List<List<Integer>> palindromePairs(String[] words) { | ||
|
||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that "all houses in this place forms a binary tree". It will automatically contact the police if two directly-linked houses were broken into on the same night. | ||
// | ||
// Determine the maximum amount of money the thief can rob tonight without alerting the police. | ||
// | ||
// Example 1: | ||
// 3 | ||
// / \ | ||
// 2 3 | ||
// \ \ | ||
// 3 1 | ||
// Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. | ||
// Example 2: | ||
// 3 | ||
// / \ | ||
// 4 5 | ||
// / \ \ | ||
// 1 3 1 | ||
// Maximum amount of money the thief can rob = 4 + 5 = 9. | ||
|
||
/** | ||
* Definition for a binary tree node. | ||
* public class TreeNode { | ||
* int val; | ||
* TreeNode left; | ||
* TreeNode right; | ||
* TreeNode(int x) { val = x; } | ||
* } | ||
*/ | ||
public class Solution { | ||
public int rob(TreeNode root) { | ||
|
||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num | ||
// calculate the number of 1's in their binary representation and return them as an array. | ||
// | ||
// Example: | ||
// For num = 5 you should return [0,1,1,2,1,2]. | ||
// | ||
// Follow up: | ||
// | ||
// It is very easy to come up with a solution with run time O(n*sizeof(integer)). But can you do it in linear time O(n) /possibly in a single pass? | ||
// Space complexity should be O(n). | ||
// Can you do it like a boss? Do it without using any builtin function like __builtin_popcount in c++ or in any other language. | ||
// Hint: | ||
// | ||
// You should make use of what you have produced already. | ||
// Divide the numbers in ranges like [2-3], [4-7], [8-15] and so on. And try to generate new range from previous. | ||
// Or does the odd/even status of the number help you in calculating the number of 1s? | ||
public class Solution { | ||
public int[] countBits(int num) { | ||
|
||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// 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) | ||
public class Solution{ | ||
public int depthSum(List<NestedInteger> nestedList) { | ||
|
||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Given a string, find the length of the longest substring T that contains at most k distinct characters. | ||
// | ||
// For example, Given s = “eceba” and k = 2, | ||
// | ||
// T is "ece" which its length is 3. | ||
public class Solution{ | ||
public int lengthOfLongestSubstringKDistinct(String s, int k) { | ||
|
||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// 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: | ||
// Given the list [1,[4,[6]]], | ||
// | ||
// By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,4,6]. | ||
|
||
/** | ||
* // This is the interface that allows for creating nested lists. | ||
* // You should not implement it, or speculate about its implementation | ||
* public interface NestedInteger { | ||
* | ||
* // @return true if this NestedInteger holds a single integer, rather than a nested list. | ||
* public boolean isInteger(); | ||
* | ||
* // @return the single integer that this NestedInteger holds, if it holds a single integer | ||
* // Return null if this NestedInteger holds a nested list | ||
* public Integer getInteger(); | ||
* | ||
* // @return the nested list that this NestedInteger holds, if it holds a nested list | ||
* // Return null if this NestedInteger holds a single integer | ||
* public List<NestedInteger> getList(); | ||
* } | ||
*/ | ||
public class NestedIterator implements Iterator<Integer> { | ||
|
||
public NestedIterator(List<NestedInteger> nestedList) { | ||
|
||
} | ||
|
||
@Override | ||
public Integer next() { | ||
|
||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
|
||
} | ||
} | ||
|
||
/** | ||
* Your NestedIterator object will be instantiated and called as such: | ||
* NestedIterator i = new NestedIterator(nestedList); | ||
* while (i.hasNext()) v[f()] = i.next(); | ||
*/ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Given an integer (signed 32 bits), write a function to check whether it is a power of 4. | ||
// | ||
// Example: | ||
// Given num = 16, return true. Given num = 5, return false. | ||
// | ||
// Follow up: Could you solve it without loops/recursion? | ||
public class Solution { | ||
public boolean isPowerOfFour(int num) { | ||
|
||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get. | ||
// | ||
// For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 + 3 + 4). | ||
// | ||
// Note: you may assume that n is not less than 2. | ||
// | ||
// Hint: | ||
// | ||
// There is a simple O(n) solution to this problem. | ||
// You may check the breaking results of n ranging from 7 to 10 to discover the regularities. | ||
public class Solution { | ||
public int integerBreak(int n) { | ||
|
||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Write a function that takes a string as input and returns the string reversed. | ||
// | ||
// Example: | ||
// Given s = "hello", return "olleh". | ||
public class Solution { | ||
public String reverseString(String s) { | ||
|
||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Write a function that takes a string as input and reverse only the vowels of a string. | ||
// | ||
// Example 1: | ||
// Given s = "hello", return "holle". | ||
// | ||
// Example 2: | ||
// Given s = "leetcode", return "leotcede". | ||
public class Solution { | ||
public String reverseVowels(String s) { | ||
|
||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// 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 { | ||
public MovingAverage(int size) { | ||
} | ||
|
||
public double next(int val) { | ||
|
||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Given a non-empty array of integers, return the k most frequent elements. | ||
// | ||
// For example, | ||
// Given [1,1,1,2,2,3] and k = 2, return [1,2]. | ||
// | ||
// Note: | ||
// You may assume k is always valid, 1 ≤ k ≤ number of unique elements. | ||
// Your algorithm's time complexity must be better than O(n log n), where n is the array's size. | ||
public class Solution { | ||
public List<Integer> topKFrequent(int[] nums, int k) { | ||
|
||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Design a Tic-tac-toe game that is played between two players on a n x n grid. | ||
// | ||
// You may assume the following rules: | ||
// | ||
// A move is guaranteed to be valid and is placed on an empty block. | ||
// Once a winning condition is reached, no more moves is allowed. | ||
// A player who succeeds in placing n of their marks in a horizontal, vertical, or diagonal row wins the game. | ||
// Example: | ||
// Given n = 3, assume that player 1 is "X" and player 2 is "O" in the board. | ||
// | ||
// TicTacToe toe = new TicTacToe(3); | ||
// | ||
// toe.move(0, 0, 1); -> Returns 0 (no one wins) | ||
// |X| | | | ||
// | | | | // Player 1 makes a move at (0, 0). | ||
// | | | | | ||
// | ||
// toe.move(0, 2, 2); -> Returns 0 (no one wins) | ||
// |X| |O| | ||
// | | | | // Player 2 makes a move at (0, 2). | ||
// | | | | | ||
// | ||
// toe.move(2, 2, 1); -> Returns 0 (no one wins) | ||
// |X| |O| | ||
// | | | | // Player 1 makes a move at (2, 2). | ||
// | | |X| | ||
// | ||
// toe.move(1, 1, 2); -> Returns 0 (no one wins) | ||
// |X| |O| | ||
// | |O| | // Player 2 makes a move at (1, 1). | ||
// | | |X| | ||
// | ||
// toe.move(2, 0, 1); -> Returns 0 (no one wins) | ||
// |X| |O| | ||
// | |O| | // Player 1 makes a move at (2, 0). | ||
// |X| |X| | ||
// | ||
// toe.move(1, 0, 2); -> Returns 0 (no one wins) | ||
// |X| |O| | ||
// |O|O| | // Player 2 makes a move at (1, 0). | ||
// |X| |X| | ||
// | ||
// toe.move(2, 1, 1); -> Returns 1 (player 1 wins) | ||
// |X| |O| | ||
// |O|O| | // Player 1 makes a move at (2, 1). | ||
// |X|X|X| | ||
// Follow up: | ||
// Could you do better than O(n2) per move() operation? | ||
// | ||
// Hint: | ||
// | ||
// Could you trade extra space such that move() operation can be done in O(1)? | ||
// You need two arrays: int rows[n], int cols[n], plus two variables: diagonal, anti_diagonal. | ||
|
||
class TicTacToe { | ||
public TicTacToe(int n) { | ||
|
||
} | ||
public int move(int row, int col, int player) { | ||
|
||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Given two arrays, write a function to compute their intersection. | ||
// | ||
// Example: | ||
// Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2]. | ||
// | ||
// Note: | ||
// Each element in the result must be unique. | ||
// The result can be in any order. | ||
public class Solution { | ||
public int[] intersection(int[] nums1, int[] nums2) { | ||
|
||
} | ||
} |
Oops, something went wrong.