Skip to content

Instantly share code, notes, and snippets.

View weidagang's full-sized avatar

Dagang Wei weidagang

  • Bay Area, California
View GitHub Profile
/*
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".
Note:
If there is no such window in S that covers all characters in T, return the emtpy string "".
@weidagang
weidagang / functional_stack.js
Created July 28, 2013 07:09
Functional stack
#!/usr/bin/env node
function make_stack() {
return null
}
function push(stack, x) {
return {
top : function() { return x },
pop : function() { return stack }
@weidagang
weidagang / binary_tree_iterator_lazy.js
Created July 28, 2013 07:09
Binary tree iterator implemented with lazy list
#!/usr/bin/env node
function empty() {
return null
}
function singleton(e) {
return function() {
return {
first : e,
@weidagang
weidagang / binary_tree_iterator.js
Last active December 19, 2015 23:49
Functional binary tree iterator
#!/usr/bin/env node
function empty() {
return null
}
function singleton(e) {
return {
first : function() { return e },
rest : function() { return null }
@weidagang
weidagang / binary_tree_iterator.cpp
Created June 22, 2013 15:36
Binary tree iterator
#include <stack>
#ifndef BINARY_TREE_ITERATOR
#define BINARY_TREE_ITERATOR
/*
class Node {
public:
Node* left();
Node* right();
@weidagang
weidagang / poj_2549_sumsets.cpp
Created June 11, 2013 10:55
POJ 2549 Sumsets. Given S, a set of integers, find the largest d such that a + b + c = d where a, b, c, and d are distinct elements of S. http://poj.org/problem?id=2549
#include <cstdio>
#include <algorithm>
const int MAX_N = 1000;
int n;
int a[MAX_N];
int input() {
scanf("%d", &n);
@weidagang
weidagang / kth_element.cpp
Last active December 17, 2015 06:58
find the kth element
#include <iostream>
#include <vector>
#include <algorithm>
int nums[] = { 1, 5, 1, 2, 4, 5, 3, 6, 7, 9, 8 };
const int N = sizeof(nums) / sizeof(nums[0]);
// partition the array[fromIdx, toIdx] into 2 parts,
// where the left is < nums[pivotIdx], the right is >= nums[pivotIdx],
// return the final index of the pivot item
@weidagang
weidagang / largest_rectangle_in_histogram.cpp
Created May 13, 2013 05:43
Find the largest rectangle in histogram. POJ 2559.
#include <algorithm>
#include <cstdio>
#include <deque>
#define MAX_N 100000
int n;
int h[MAX_N + 1];
struct Node {
@weidagang
weidagang / cpp_producer_consumer.cpp
Last active April 4, 2022 03:21
Implement producer/consumer (multiple producers, multiple consumers) problem with buffer size = 10.
/**
C++ Producer Consumer using C++11 thread facilities
To compile: g++ -std=c++11 <program name> -pthread -lpthread -o pc
*/
#include <iostream>
#include <sstream>
#include <vector>
#include <stack>
#include <thread>
#include <mutex>
@weidagang
weidagang / stl_sort.cpp
Last active December 17, 2015 05:49
STL sort usage
#include <algorithm>
#include <string>
#include <iostream>
#include <iterator>
#include <vector>
#include <functional>
struct person {
std::string m_name;
int m_age;