Skip to content

Instantly share code, notes, and snippets.

View shailrshah's full-sized avatar
🖥️
Available

Shail Shah shailrshah

🖥️
Available
View GitHub Profile
private static final String[] LESS_THAN_20 = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
private static final String[] TENS = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
private static final String[] THOUSANDS = {"", "Thousand", "Million", "Billion"};
public String numberToWords(int n) {
if(n == 0) return "Zero"
StringBuilder sb = new StringBuilder();
for(int i = 0; n > 0; n /= 1000, i++) {
int modThousand = n % 1000;
@shailrshah
shailrshah / VerticalBinaryTree.java
Created November 8, 2017 20:01
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.
/*
3
/\
/ \
9 8
/\ /\
/ \/ \
4 01 7
/\
/ \
@shailrshah
shailrshah / GenerateAllValidParenthesis.java
Created November 8, 2017 15:55
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
public List<String> generateParenthesis(int n) {
List<String> list = new ArrayList<>();
backtrack(list, new StringBuilder(), 0, 0, n);
return list;
}
private static void backtrack(List<String> list, StringBuilder sb, int open, int close, int n) {
if(sb.length() == 2*n){
list.add(sb.toString());
return;
@shailrshah
shailrshah / FindCeleb.java
Created November 8, 2017 03:47
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 i…
// if A knows B -> A is definately not a celeb, B is a candidate
// 1. Finalize on a candidate
// 2. Confirm that everyone knows him and candidate knows no one
public int findCelebrity(int n) {
int candidate = 0;
for(int i = 0; i < n; i++)
candidate = knows(candidate, i) ? i : candidate;
@shailrshah
shailrshah / MaxSubArrayLen.java
Created November 8, 2017 03:01
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.
/*
To find: A range i..j such that nums[i] + nums[i+1] + ... + nums[j] = sum(i, j) = k
sum(i, j) = sum(0, j) - sum(0, i-1) since for [1 2 3 4], sum(1, 3) = sum(0, 3) - sum(0, 0);
sum(0, j) - sum(0, i-1) = k, or sum(0, i-1) = sum(0, j) - k
For each sum(0, j), check if there was a sum(0, i-1) such that it equals sum(0, j) - k.
*/
public int maxSubArrayLen(int[] nums, int k) {
int sum = 0, max = 0;
Map<Integer, Integer> map = new HashMap<>();
@shailrshah
shailrshah / MinWindowSubString.java
Created November 8, 2017 01:29
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n). For example, S = "ADOBECODEBANC" T = "ABC" Minimum window is "BANC". If there is no such window in S that covers all characters in T, return the empty string "". If there are multiple such windows, you are guaranteed that …
public String minWindow(String s, String t) {
Map<Character, Integer> map = new HashMap<>();
for(Character ch : t.toCharArray())
map.put(ch, map.getOrDefault(ch, 0) + 1);
int start = 0, end = 0, toBeFound = t.length();
int minStart = 0, minLen = Integer.MAX_VALUE;
while(end < s.length()) {
@shailrshah
shailrshah / MinMeetingRooms.java
Created November 7, 2017 04:15
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), find the minimum number of conference rooms required. For example, Given [[0, 30],[5, 10],[15, 20]], return 2.
public int minMeetingRooms(Interval[] intervals) {
if(intervals == null || intervals.length == 0)
return 0;
Arrays.sort(intervals, (i1, i2) -> i1.start - i2.start);
PriorityQueue<Interval> queue = new PriorityQueue<>(intervals.length, (i1, i2) -> i1.end-i2.end);
queue.offer(intervals[0]); // start with a room
for(int i = 1; i < intervals.length; i++) {
@shailrshah
shailrshah / MergeIntervals.java
Created November 7, 2017 02:05
Given a collection of intervals, merge all overlapping intervals.
// [1,3],[2,6],[8,10],[15,18] -> [1,6],[8,10],[15,18]
public List<Interval> merge(List<Interval> intervals) {
Collections.sort(intervals, (i1, i2) -> i1.start - i2.start);
List<Interval> mergedIntervals = new ArrayList<>();
for(int i = 0; i < intervals.size(); i++) {
int start = intervals.get(i).start;
int end = intervals.get(i).end;
@shailrshah
shailrshah / Swap.java
Last active November 6, 2017 17:27
Swap two numbers A and B, without using a temporary variable.
public static void swap(int x, int y) {
if(x == y) return;
x = x ^ y; // x = A ^ B, y = B
y = x ^ y; // x = A ^ B, y = A ^ (B ^ B) = A ^ 0 = A
x = x ^ y; // y = A, x = A ^ B ^ A = B ^ (A ^ A) = B ^ 0 = B
}
@shailrshah
shailrshah / LongestConsecutive.java
Created November 6, 2017 14:40
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given [100, 4, 200, 1, 3, 2], The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4.
public int longestConsecutive(int[] nums) {
Map<Integer, Integer> map = new HashMap<>();
int max = 0;
for(int i = 0; i < nums.length; i++) {
if(map.containsKey(nums[i]))
continue;
int left = map.getOrDefault(nums[i]-1, 0);
int right = map.getOrDefault(nums[i]+1, 0);