Skip to content

Instantly share code, notes, and snippets.

View weidagang's full-sized avatar

Dagang Wei weidagang

  • Bay Area, California
View GitHub Profile
@weidagang
weidagang / circular_queue.c
Last active April 27, 2019 22:40
Problem: Implement a circular queue of integers of user-specified size using a simple array. Provide routines to initialize(), enqueue() and dequeue() the queue. Make it thread safe. Please implement this with your own code and do not use any class libraries or library calls.
/*
Problem
Implement a circular queue of integers of user-specified size using a simple array.
Provide routines to initialize(), enqueue() and dequeue() the queue. Make it thread safe.
Please implement this with your own code and do not use any class libraries or library calls.
*/
#include <malloc.h>
@weidagang
weidagang / kth_in_matrix.cpp
Last active December 11, 2015 20:28
Problem: Given K, find the K-th minimum number from a sorted N*M matrix. Sorted matrix means the rows are sorted left to right in ascending order, and columns are sorted top to bottom in ascending order.
#include <cstdio>
#include <cstring>
#include <memory>
#include <algorithm>
#define N 1000
#define OK 0
#define ERROR 1
struct Node {
@weidagang
weidagang / permuation.cpp
Last active December 13, 2015 23:48
Given a string of characters, print its permutations in alphbetic order. Sample input: bbjd; Sample output: bbdj, bbjd, bdbj, ..., jbdb, jdbb
/*
Given a string of characters, print its permutations in alphbetic order.
Sample input: bbjd;
Sample output: bbdj, bbjd, bdbj, ..., jbdb, jdbb
*/
#include <iostream>
#include <string>
#include <algorithm>
@weidagang
weidagang / stl_map.cpp
Last active December 14, 2015 05:40
The example of STL map
#include<map>
#include<string>
#include<iostream>
#include<cassert>
typedef std::map<int, std::string> Map;
int main() {
Map m;
Map::iterator it;
@weidagang
weidagang / trie.cpp
Last active December 14, 2015 07:09
Trie
//============================================================================
// Name : trie.cpp
// Author : dagang.wei
// Version :
// Copyright :
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <algorithm>
#include <string>
/*
http://oj.leetcode.com/problems/largest-rectangle-in-histogram/
Given n non-negative integers representing the histogram's bar height where the width of each bar is 1,
find the area of largest rectangle in the histogram.
*/
class Solution {
private:
struct Record {
@weidagang
weidagang / proc-info.sh
Created March 9, 2013 02:46
Print process info
#! /bin/bash
PROCS=(zygote installd keystore mediaserver netd powerbtnd servicemanager ueventd vold)
OUT=/android/data/system/android_pmap.txt
rm -f $OUT
date >> $OUT
echo cat /sys/fs/cgroup/memory/android-start/memory.limit_in_bytes >> $OUT
@weidagang
weidagang / binary_heap.cpp
Last active December 14, 2015 23:29
Binary heap structure
struct Node {
int m_value;
int m_row;
int m_column;
Node() {
}
Node(int value, int row, int column) {
m_value = value;
@weidagang
weidagang / probability_tree.dot
Created April 2, 2013 12:42
graphviz probability tree
# Generate image by: dot -Tpng -o probability_tree.png probability_tree.dot
digraph probability_tree {
s [label=""]
male [label="男"]
female [label="女"]
y1 [label="穿凉鞋"]
y2 [label="穿凉鞋"]
n1 [label="不穿凉鞋"]
n2 [label="不穿凉鞋"]
@weidagang
weidagang / n_power_m.cpp
Created April 14, 2013 12:46
Calculate n to the power of m with C++ template meta programming
#include <iostream>
template<int N, int M>
struct Pow {
enum { value = N * Pow<N, M-1>::value };
};
template<int N>
struct Pow<N, 0> {
enum { value = 1 };