Skip to content

Instantly share code, notes, and snippets.

#include <iostream>
#include <stack>
using namespace std;
struct TreeNode {
int val;
TreeNode *left, *right;
TreeNode(int v): val(v), left(nullptr), right(nullptr) {}
};
#include <iostream>
using namespace std;
// Counts numbers from 0 to n-1.
struct Counter {
int i = 0;
int n;
bool first = true; // Tracks if this is the first invocation of the next() function
Counter(int _n): n(_n) {}
int* next() {
#include <iostream>
#include <stack>
using namespace std;
// Counts numbers from 0 to n-1.
struct Counter {
int i = 0;
int n;
bool first = true; // Tracks if this is the first invocation of the next() function
Counter(int _n): n(_n) {}
@dhruvbird
dhruvbird / range_union.py
Last active August 29, 2015 14:19
Range (Interval) Intersection
def unionRanges(intervals):
intervals = sorted(intervals) # Sort by elem[0]
ranges = [ ]
if len(intervals) == 0:
return [ ]
start = intervals[0][0]
end = intervals[0][1]
for i in range(1, len(intervals)):
if intervals[i][0] < end:
end = max(end, intervals[i][1])
@dhruvbird
dhruvbird / InPlaceMergeNsqrtN.cpp
Last active August 29, 2015 14:17
In-Place merge for 2 sorted arrays of size N and sqrt(N) each.
/* Compile and run:
*
* g++ -std=c++0x InPlaceMergeNsqrtN.cpp && ./a.out 23
*/
/* Merges 2 sorted arrays of size N and sqrt(N) in Theta(N) time
*
*/
#include <iostream>
#include <vector>
@dhruvbird
dhruvbird / data.txt
Last active August 29, 2015 14:15
All the code to compare regular binary search with 2-level binary search
# Columns:
# log(N) N Time/Iter(usec)[std::lower_bound] N Time/Iter(usec)[l2search]
23 8388608 0.460176 8388608 0.269376
24 16777216 0.621821 16777216 0.435599
25 33554432 0.735565 33554432 0.615605
26 67108864 0.856156 67108864 0.598811
27 134217728 0.959821 134217728 0.636344
28 268435456 1.552199 268435456 0.771024
29 536870912 1.158228 536870912 0.818436
30 1073741824 1.936374 1073741824 0.905859
@dhruvbird
dhruvbird / smwc.py
Created February 11, 2015 06:53
Smallest multiple with constraints
"""
Problem statement:
------------------
Given a number 'n', design an algorithm that will output the smallest
integer number 'X' which contains only the digits {0, 1} such that
X mod n = 0 and X > 0
(1 <= n <= 100000)
"""
def solve(n):
if n < 2:
def merge_sort(msg, m, depth=0):
print " " * depth, msg, m
result=[]
#Exit condition
if len(m) < 2:
return m
mid = int(len(m)/2)
left = m[:mid]
@dhruvbird
dhruvbird / README.md
Created July 24, 2012 16:30
README.md for node-xmpp-bosh
@dhruvbird
dhruvbird / Makefile
Created July 16, 2012 23:02 — forked from utaal/Makefile
webserver using libuv
webserver: webserver.c libuv/uv.a http-parser/http_parser.o
gcc -I libuv/include \
-lrt -lm -lpthread -o \
webserver webserver.c \
libuv/uv.a http-parser/http_parser.o
libuv/uv.a:
$(MAKE) -C libuv
http-parser/http_parser.o: