Skip to content

Instantly share code, notes, and snippets.

@richzw
richzw / mongoose group operation
Created July 19, 2012 07:13
The way of mongoose group operation
command = {
'group' : { //mongodb group command
'ns' : 'pings', //the collection to query
'cond' : {'active.end' : { $gt: new Date() }}, //active.end must be in the future
'initial': {'count': 0}, //initialize any count object properties
'$reduce' : 'function(doc, out){ out.count++ }',
'key' : {'url': 1} //fields to group by
}
}
@richzw
richzw / api easy test
Created July 21, 2012 16:27
redirect test for api easy
var APIeasy = require('api-easy'),
assert = require('assert');
var suite = APIeasy.describe('/api');
var urlRedir;
suite.discuss('Test Redirection API')
.use('localhost', 3000)
.setHeader('Content-Type', 'application/json')
.followRedirect(false)
@richzw
richzw / Mongoose GridFS
Created July 31, 2012 10:10
API for mongoose GridFS
mongoose = require('mongoose');
var GridStore = mongoose.mongo.GridStore,
Grid = mongoose.mongo.Grid,
ObjectID = mongoose.mongo.BSONPure.ObjectID;
exports.getGridFile = function(id, fn) {
var db = mongoose.connection.db,
id = new ObjectID(id),
@richzw
richzw / max_subtree.cc
Created September 20, 2012 03:24 — forked from luangong/maximum_subtree.cc
求二叉树的最大子树
//a binary tree, each node is positive or negative integer, how to find a sub-tree, all the nodes sum is largest.
#include <iostream>
#include <limits>
using namespace std;
struct Node {
long data;
Node *lchild;
@richzw
richzw / Find the median.c
Created September 25, 2012 07:18
Let X[1..n] and Y[1..n] be two arrays, each containing n numbers already in sorted order. Give an O(lgn)-time algorithm to find the median of all 2n elements in arrays X and Y.
//
//from 'introduce to algorithm'
//Precondition: the length of seq1 is equal to seq2, both of them are n.
7 int findMed(int seq1[], int seq2[], int n, int low, int high){
8 if (low > high)
9 return -1;
10 int k = (high + low)/2;
11 if (k == n-1 && seq1[n-1]<=seq2[0])
12 return seq1[n-1];
@richzw
richzw / Find one number from descending order array
Created December 11, 2012 02:56
Find the number from descending order array with left rotate N element
5 /**
6 * @func: the binary search fur descending order array.
7 */
8 int binary_search(int arr[], int left, int right, int val){
9 int ret = -1;
10 if (NULL == arr || left >= right)
11 return ret;
12
13 while(left <= right){
14 int middle = (left & right) + ((left ^ right) >> 1);
@richzw
richzw / monotonous queue
Last active October 14, 2015 00:58
monotonous queue implementation
8 const int LEN = 3;
9 struct node{
10 int val;
11 int seq;
12 }monoto_Q[LEN];
13
14 void findAscend_MonotonousQueue(int arr[], int len, int qSize){
15 if (arr == NULL || len <= LEN)
16 return;
17 int index = 0, tail = 0, head = 1;
@richzw
richzw / sort an sorted array
Last active December 9, 2015 20:48
An array with n elements which is K most sorted, namely the distance between the current index and sorted index is less than K. It should be faster than O(n*lgn).
//build an Kth min heap, time k*lgk + (n-k)lgk
1 #include <iostream>
2 #include <iterator>
3 #include <algorithm>
4 using namespace std;
5
6 void shiftdown(int arr[], int root, int right){
7 int child = 2*root + 1;
8
@richzw
richzw / segment tree.c
Last active December 10, 2015 22:09
Segment Tree
给定一个整数N,比如N=100,有如下的初始有序序列位于[0,N-1]之间 [0 1] [4 5 6] [9 10 11] [20] [98 99]。设计一个数据结构保存这个初始序列,然后写一个函数,接受一个参数x,满足0<=x<=N-1,1)若x在该结构中不存在,出错;2)若x存在,返回x之后第一个不存在的数,并把该数写入结构中
@richzw
richzw / Find the min absolute value
Last active December 11, 2015 22:28
一个有序数组(从小到大排列),数组中的数据有正有负,求这个数组中的最小绝对值
9 /*
10 * binary search
11 */
12 int findMin_binary(int arr[], int left, int right){
13 if (NULL == arr)
14 return 0;
15
16 while(left < right){
17 if (arr[left] > 0)
18 return arr[left];