Skip to content

Instantly share code, notes, and snippets.

View changhengliou's full-sized avatar
💬
JUST FOR FUN

ChangHeng changhengliou

💬
JUST FOR FUN
  • New York, NY
  • 03:39 (UTC -04:00)
View GitHub Profile
@changhengliou
changhengliou / 0_serve.go
Last active August 11, 2018 14:13
Golang net/http package source code
func (c *conn) serve(ctx context.Context)
@changhengliou
changhengliou / disjoint_set.cc
Created December 25, 2018 07:49
Disjoint set / Union find data structure
#include <vector>
using namespace std;
class Disjoint_set {
private:
// the max depth of the tree depth
vector<int> _rank;
// the parent of the each node
vector<int> _parent;
public:
@changhengliou
changhengliou / bellman_ford.cc
Created December 25, 2018 09:41
Bellman ford's shortest path algorithm for finding shortest path
#include <vector>
#include <queue>
#include <iostream>
#include <stdexcept>
using namespace std;
// bellman ford's algorithm is an algorithm that finds shortest path
// unlike dijkstra's algorithm, it also works while the cost is negative.
// the time complexity is O(V·E)
@changhengliou
changhengliou / dijkstra.cc
Last active August 20, 2019 07:50
Dijkstra's algorithm
#include <vector>
#include <iostream>
using namespace std;
vector<int> dijkstra(vector<vector<pair<int, int>>>& graph) {
vector<int> record(graph.size(), INT_MAX);
vector<bool> visited(graph.size(), false);
int curr = 0;
record[curr] = 0;
for (int i = 0; i < graph.size() - 1; i++) {
@changhengliou
changhengliou / pattern.cs
Created December 25, 2018 12:07
design pattern
// ----------- adapter pattern -------------------
public class TargetA
{
public void Start() { ... }
public void End() { ... }
}
public class TargetB
{
public void Begin() { ... }
@changhengliou
changhengliou / Controller.js
Last active May 17, 2019 10:12
AOP implementation with ES6 Proxy
class BaseController {
constructor () {
this.ctx = {
status: 200,
body: {}
};
const before = this.before || (() => {
console.log(`Before run`);
return true;
});
@changhengliou
changhengliou / segmentTree.cc
Created July 10, 2019 07:54
segmentTree.cc
#include <iostream>
using namespace std;
struct TreeNode {
int start;
int end;
int sum; // or max/min
TreeNode *left;
TreeNode *right;
@changhengliou
changhengliou / rbtree.cc
Created July 14, 2019 13:10
Red black tree
#include <iostream>
#include <queue>
using namespace std;
enum Color { RED, BLACK };
struct Node {
char val;
Color color;
@changhengliou
changhengliou / heap.cc
Created July 22, 2019 06:27
binary heap
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
bool default_predicate(int a, int b) { return a > b; }
struct Heap {
private:
@changhengliou
changhengliou / SegmentTree.cc
Created May 27, 2020 09:54
SegmentTree (array)
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
class SegmentTree {
private:
// number of elements
int _N;