Skip to content

Instantly share code, notes, and snippets.

@pdu
pdu / ZombieMarch.cpp
Created December 9, 2012 15:30
interview street: zombie march, page rank
/*
* https://www.interviewstreet.com/challenges/dashboard/#problem/4ff1484963063 Zombie March
* http://www.17sotv.com/ 17sotv电影天堂
*/
#include <stdio.h>
#include <string.h>
#include <vector>
#include <algorithm>
using namespace std;
@pdu
pdu / EvenTree.py
Created December 10, 2012 13:16
interview street: even tree, greedy algorithm
#!/usr/bin/python
"""
greedy algorithm
http://www.17sotv.com/ 17sotv电影天堂
"""
import sys
def solve(used, edges, k):
/*
* http://www.17sotv.com/ 17sotv电影天堂
*/
#include <vector>
using namespace std;
#include <stdint.h>
class MyStack
@pdu
pdu / MyRWLock.cpp
Last active May 19, 2016 02:06
algos: use mutex to implement read-write lock
extern "C"
{
#include <pthread.h>
}
class MyRWLock
{
public:
MyRWLock();
~MyRWLock();
@pdu
pdu / QuickSort.py
Created December 22, 2012 07:08
quick sort implemented in python
#!/usr/bin/python
import random
def quick_sort_(buf, left, right):
ll, rr = left, right
pivot = random.choice(buf[left : right + 1])
while left <= right:
while buf[left] < pivot:
left += 1
@pdu
pdu / NthNum.py
Last active December 10, 2015 01:49
give 2 ascending sorted arrays with same length n, find the nth number between these 2 arrays
#!/usr/bin/python
def get_nth_num(a, b):
if len(a) != len(b) or len(a) == 0:
return None
id_a, id_b = 0, 0
for _ in xrange(len(a)):
if a[id_a] <= b[id_b]:
tmp = a[id_a]
id_a += 1
@pdu
pdu / PerfectShuffle.py
Created December 23, 2012 14:37
find perfect shuffle of an array, for example: 1 2 3 4 5 6 7 8 9 10 --> 1 6 2 7 3 8 4 9 5 10
#!/usr/bin/python
def perfect_shuffle_(buf, left, right):
len_ = right - left + 1
if len_ == 2:
return
left_beg = left + len_ / 4
right_beg = left + len_ / 2
shuffle_len = len_ / 4
if len_ % 4 == 0:
@pdu
pdu / leetcode_3sum.cpp
Created December 28, 2012 19:26
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. http://www.leetcode.com/onlinejudge
#include <tr1/unordered_map>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
class Solution {
public:
vector<vector<int> > threeSum(vector<int> &num) {
vector< vector<int> > ret;
@pdu
pdu / leetcode_3sum_closest.cpp
Created December 28, 2012 19:54
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. http://www.leetcode.com/onlinejudge
#include <algorithm>
#include <vector>
using namespace std;
class Solution {
public:
void update(int& closest, int& ret, int sum, int target) {
int diff = abs(sum - target);
if (diff < closest) {
closest = diff;
@pdu
pdu / leetcode_4sum.cpp
Created December 28, 2012 20:25
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. http://www.leetcode.com/onlinejudge
#include <algorithm>
#include <vector>
#include <string>
#include <tr1/unordered_map>
using namespace std;
class Solution {
public:
vector<vector<int> > fourSum(vector<int> &num, int target) {
vector< vector<int> > ret;