Skip to content

Instantly share code, notes, and snippets.

View zhuangh's full-sized avatar
🎯
Focusing

Hao Zhuang zhuangh

🎯
Focusing
View GitHub Profile
@zhuangh
zhuangh / bellmanFord.cc
Last active February 7, 2018 07:07
bellmanFord algorithm
using pairType = std::pair<int, int>;
using graphType = std::unordered_map<int, vector<pairType> >;
class Solution {
private:
int bellmanFord(const int & K, const int &N, std::vector<int> &delay,
graphType & graph, const int &TIMEOUT ){
int d = -1;
delay[K] = 0;
@zhuangh
zhuangh / dijkstra.cc
Last active January 29, 2018 08:14
Dijkstra's algorithm
using pairType = std::pair<int, int>;
using graphType = std::unordered_map<int, vector<pairType> >;
using heapType = std::priority_queue<pairType, vector<pairType> >;
class Solution {
private:
int dijkstra(const int & K, const int &N, std::vector<int> &delay,
graphType & graph, const int &TIMEOUT ){
int d = -1;
@zhuangh
zhuangh / dijkstra.py
Created January 29, 2018 08:10
Dijkstra python version
class Solution:
def dijkstra(self, K, N, delay, graph, TIMEOUT):
Q = set(range(N))
delay[K-1] = 0
hp = []
heapq.heappush(hp, (0, K-1))
while len(hp) > 0:
_, u = heapq.heappop(hp)
for v, w in graph[u].items():
if delay[u] + w < delay[v]:
@zhuangh
zhuangh / bellman_ford.py
Created January 29, 2018 08:12
Bellman Ford python version
class Solution:
def bellman_ford(self, K, N, delay, graph, TIMEOUT):
delay[K-1] = 0
for i in range(N):
for u, vs in graph.items():
for v, w in vs.items():
if delay[v] > delay[u] + w:
delay[v] = delay[u] + w
d = max(delay)
@zhuangh
zhuangh / mst_prim.py
Created January 30, 2018 00:01
prim for MST
def prim(nodes, edges):
conn = {}
for u, v, w in edges:
if u not in conn:
conn[u] = [(w,u,v)]
else:
conn[u].append((w, u, v))
if v not in conn:
conn[v] = [(w,v,u)]
else:
@zhuangh
zhuangh / kruskal.py
Created January 30, 2018 00:02
Kruskal method
def union_find(u, arr):
if arr[u] == u:
return u
return union_find(arr[u], arr)
def union(u, v, arr):
arr[union_find(v, arr)] = union_find(u, arr)
def kruskal(nodes, edges):
mst = []
@zhuangh
zhuangh / kruskal.cc
Last active January 31, 2018 00:08
kruskal algorithm in C++
#include <iostream>
template<typename T=int>
class edgeType{
private:
char from;
char to;
T w;
public:
edgeType(char a, char b, int c):from(a),to(b),w(c) {}
@zhuangh
zhuangh / predict_winner.py
Last active February 7, 2018 06:07
advance DP
class Solution:
def PredictTheWinner(self, c):
"""
:type nums: List[int]
:rtype: bool
"""
r = len(c)
t = sum(c)
s = [[0 for i in range(r)] for j in range(r)]
for l in range(r): # l is for the length - 1
@zhuangh
zhuangh / PredictWinner.cc
Created February 7, 2018 06:46
Advance DP
class Solution {
public:
bool PredictTheWinner(vector<int>& nums) {
int length = nums.size();
std::vector< std::vector<int> > s(length, std::vector<int> (length));
for(int l = 0; l < length; l++){
for(int i=0; i < length - l; i++){
if(l==0) s[i][l] = nums[i];
else if (l==1) s[i][l] = max(nums[i], nums[i+1]);
else if (l==2) s[i][l] = max(nums[i] + min(nums[i+1], nums[i+2]),
@zhuangh
zhuangh / openLock.cc
Created February 12, 2018 01:29
openlock BFS
class Solution {
public:
bool check_deadends(unordered_set<string>& deadends, const string & target){
if(deadends.find(target) != deadends.end()) return true;
deadends.insert(target);
return false;
}
bool check_target(const string & a, const string & b){
return (a==b);