Skip to content

Instantly share code, notes, and snippets.

@jporcelli
jporcelli / FavSeq.java
Last active August 29, 2015 14:10
In this problem you are given N sequences of distinct numbers. The task is to construct the shortest sequence S, such that all N sequences are (not necessarily continuous) subsequences of S. If there are many such sequences, then you have to construct the lexicographically smallest one. It is guaranteed that such a sequence exists. Sequence A[1……
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
@jporcelli
jporcelli / PathSum2.java
Created October 23, 2014 23:58
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> paths = new ArrayList<List<Integer>>();
if(root == null){
return paths;
}else{
List<Integer> path = new ArrayList<Integer>();
getpaths(root, path, paths, 0, sum);
return paths;
@jporcelli
jporcelli / FindMidLL.java
Created October 22, 2014 20:42
Find the mid point in a linked list
private static ListNode findmid(ListNode cur){
ListNode s = cur;
ListNode f = cur;
while(f != null){
f = f.next;
if(f != null && f.next != null){
f = f.next;
s = s.next;
}
@jporcelli
jporcelli / ReverseLL.java
Created October 22, 2014 20:41
Reverse a linked list
private static ListNode reverse(ListNode head) {
ListNode cur = head;
ListNode t = cur.next;
ListNode n = null;
if(t != null){
n = t.next;
t.next = cur;
}
@jporcelli
jporcelli / IterativePostOrder_wStack.java
Created October 21, 2014 13:55
Given a binary tree, return the postorder traversal of its nodes' values.
public List<Integer> postorderTraversal(TreeNode root) {
Stack<TreeNode> s = new Stack<TreeNode>();
List<Integer> l = new ArrayList<Integer>();
if(root == null){
return l;
}
s.push(root);
TreeNode cur = null;
@jporcelli
jporcelli / MinPathSum.java
Created October 20, 2014 22:16
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time.
public int minPathSum(int[][] grid) {
int m = grid.length;
int n = grid[0].length;
for(int i = 1; i < m; i++){
grid[i][0] = grid[i-1][0] + grid[i][0];
}
for(int i = 1; i < n; i++){
@jporcelli
jporcelli / LongestConsecutive.java
Created October 19, 2014 03:02
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
public static int longestConsecutive(int[] num) {
//need support for concurrent modifications of the backing table
//@todo find a different way of removing entries from the table during iteration
Map<String, Set<Integer>> h = new ConcurrentHashMap<String, Set<Integer>>();
int max = Integer.MIN_VALUE;
//init the table
for(int v : num){
@jporcelli
jporcelli / MinStack.java
Created October 12, 2014 22:44
A min stack implementation where pop, push, and min, are all constant time operations O(1), and requires only constant additional memory on top of the underlying stack memory requirements, O(N)
private final Stack<Integer> s;
private Integer min;
public MinStack() {
this.s = new Stack<Integer>();
this.min = null;
}
public Integer push(Integer e) {
if (min == null) {
@jporcelli
jporcelli / ReorderList.java
Created October 11, 2014 21:16
Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do this in-place without altering the nodes' values.
public static void reorderList(ListNode head) {
if(head == null || head.next == null){
return;
}
ListNode cur = head;
ListNode sh = findmid(cur);
ListNode shr = reverse(sh.next);
sh.next = null;
@jporcelli
jporcelli / MaximumContrigousProduct.java
Created October 10, 2014 21:26
Find the contiguous subarray within an array (containing at least one number) which has the largest product.
public int maxProduct(int[] A) {
int n = A.length;
int max = Integer.MIN_VALUE;
int np = 1;
int pp = 1;
for(int i = 0; i < n; i++){
if(A[i] == 0){