Skip to content

Instantly share code, notes, and snippets.

@jporcelli
jporcelli / 3sum.java
Created January 21, 2016 05:01
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
public class Solution {
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> R = new ArrayList<List<Integer>>();
int N = nums.length;
for(int i=0; i<N; i++){
int j=0, k=N-1;
while(j>=0 && j<i && k>i && k<N) {
int sum3 = nums[i]+nums[j]+nums[k];
@jporcelli
jporcelli / removeDuplicatesSortedArray2.java
Created January 15, 2016 08:21
Remove Duplicates from Sorted Array II
public class Solution {
public int removeDuplicates(int[] A) {
if(A.length == 1){
return 1;
}
int duplicates = 0, local_duplicates = 1;
for(int i = 0 ; i < A.length - 1;){
if(A[i] == A[i + 1] && i < (A.length - duplicates)){
local_duplicates++;
@jporcelli
jporcelli / mHT.java
Created January 15, 2016 05:22
310. Minimum Height Trees
public class Solution {
public List<Integer> findMinHeightTrees(int n, int[][] edges) {
if(n==1) {
List<Integer> x = new ArrayList<Integer>();
x.add(0);
return x;
}
List<List<Integer>> g = new ArrayList<List<Integer>>(n);
for(int i=0; i<n; i++){
g.add(new ArrayList<Integer>());
@jporcelli
jporcelli / InvertBinaryTree.C
Created July 25, 2015 07:21
https://leetcode.com/problems/invert-binary-tree/ Invert Binary Tree - Trivia: This problem was inspired by this original tweet by Max Howell: Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off.
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
@jporcelli
jporcelli / main.py
Last active August 29, 2015 14:18
han yin python midterm practice
#hello han
Problem 2: Write a function - sub count - that takes two arguments, a
string (s) and a substring (sub) , and returns the number of times sub
occurs in s.
For example:
x=‘you say hello and I say hello and they all say hello’
print(subcount(x,’hello’)) should print 3
print(subcount(x,’goodbye’)) should print 0
(Note: There is a python string function count that does the same
@jporcelli
jporcelli / Main.C
Created March 12, 2015 02:36
Uva 910, TV game
#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
#define PI acos(-1)
#define sqr(x) ((x) * (x))
#define PB push_back
#define MP make_pair
#define F first
@jporcelli
jporcelli / Main.java
Last active August 29, 2015 14:14
Distinct Subsequences, Problem 10069 UVa, A subsequence of a given sequence is just the given sequence with some elements (possibly none) left out. Formally, given a sequence X = x1x2…xm, another sequence Z = z1z2…zk is a subsequence of X if there exists a strictly increasing sequence <i1, i2, …, ik> of indices of X such that for all j = 1, 2, ……
import java.math.BigInteger;
import java.io.*;
import java.util.*;
/*
* Main.java
* java program model for www.programming-challenges.com
*/
class Main implements Runnable {
@jporcelli
jporcelli / D.C
Created January 28, 2015 20:36
CodeForces Round 288 Div 2, Problem D
#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
#define PI acos(-1)
#define sqr(x) ((x) * (x))
#define PB push_back
#define MP make_pair
#define F first
@jporcelli
jporcelli / Main.C
Created January 26, 2015 22:07
UVa, Edit Step Ladders
#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
#define PI acos(-1)
#define sqr(x) ((x) * (x))
#define PB push_back
#define MP make_pair
#define F first