Skip to content

Instantly share code, notes, and snippets.

View weidagang's full-sized avatar

Dagang Wei weidagang

  • Bay Area, California
View GitHub Profile
#include <deque>
#include <algorithm>
#include <cstdio>
const int MAX_N = 1000 * 1000;
int n, w;
int a[MAX_N];
int input() {
@weidagang
weidagang / parlindrome_partitioning.cpp
Last active December 16, 2015 20:49
Parlindrome Partitioning. Given a string s, partition s such that every substring of the partition is a palindrome.Return all possible palindrome partitioning of s.For example, given s = "aab", Return [ ["aa","b"], ["a","a","b"] ]. http://leetcode.com/onlinejudge
class Solution {
public:
vector<vector<string>> partition(string s) {
vector<vector<string>> results;
if (0 == s.size()) {
return results;
}
@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;
@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 / 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 / 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 / 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 / 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 / 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_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,