Skip to content

Instantly share code, notes, and snippets.

@pdu
pdu / leetcode_binary_tree_maximum_path_sum.cpp
Created December 29, 2012 13:57
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. http://www.leetcode.com/onlinejudge
#include <algorithm>
using namespace std;
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
@pdu
pdu / leetcode_binary_tree_zigzag_level_order_traversal.cpp
Created December 29, 2012 14:06
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). http://www.leetcode.com/onlinejudge
#include <algorithm>
using namespace std;
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
class Solution {
public:
int climbStairs(int n) {
int f[3] = {1, 0, 0};
int cur = 0;
for (int i = 1; i <= n; ++i) {
cur = i % 3;
f[cur] = f[0] + f[1] + f[2] - f[cur];
}
return f[cur];
@pdu
pdu / leetcode_combination_sum.cpp
Created December 30, 2012 06:32
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. http://www.leetcode.com/onlinejudge
#include <algorithm>
using namespace std;
class Solution {
public:
void dfs(vector<vector<int> >& ret, vector<int>& tmp, vector<int>& cands, int left, int target) {
if (target == 0) {
ret.push_back(tmp);
return;
}
@pdu
pdu / leetcode_combination_sum2.cpp
Created December 30, 2012 06:39
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. http://www.leetcode.com/onlinejudge
#include <algorithm>
using namespace std;
class Solution {
public:
void dfs(vector<vector<int> >& ret, vector<int>& tmp, vector<int>& cands, int left, int target) {
if (target == 0) {
ret.push_back(tmp);
return;
}
@pdu
pdu / leetcode_combinations.cpp
Created December 30, 2012 06:47
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. http://www.leetcode.com/onlinejudge
class Solution {
public:
void dfs(vector<vector<int> >& ret, vector<int>& tmp, int n, int left, int k) {
if (k == 0) {
ret.push_back(tmp);
return;
}
for (int i = left; i <= n; ++i) {
tmp.push_back(i);
dfs(ret, tmp, n, i + 1, k - 1);
@pdu
pdu / leetcode_construct_binary_tre_from_inorder_and_postorder_traversal.cpp
Created December 30, 2012 15:16
Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. http://www.leetcode.com/onlinejudge
#include <tr1::unordered_map>
using namespace std;
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
@pdu
pdu / leetcode_construct_binary_tree_from_preorder_and_inorder_traversal.cpp
Created December 30, 2012 15:30
Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. http://www.leetcode.com/onlinejudge
#include <tr1::unordered_map>
using namespace std;
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
@pdu
pdu / glassdoor_removephrase.py
Created January 2, 2013 07:40
giving n sentences, remove common phrases in each sentence, a phrase is defined by 3 or more consecutive words glassdoor: imo.im
#!/usr/bin/python
def remove(buf):
dict = {}
wordslist = []
for line in buf:
words = line.split()
wordslist.append(words)
for i in xrange( len(words) - 2 ):
phrase = ' '.join(words[i:i+3])
@pdu
pdu / glassdoor_cactus.py
Created January 2, 2013 14:09
find the number of spanning trees in a cactus graph
#!/usr/bin/python
def dfs(buf, flag, nodes, start, cur):
if flag[cur]:
if cur == start and len(nodes) != 2 and min(nodes) == start:
return len(nodes)
else:
return -1
flag[cur] = True
nodes.append(cur)