Skip to content

Instantly share code, notes, and snippets.

@ChunMinChang
ChunMinChang / queue1.cpp
Last active January 31, 2018 07:17
Implement queue by stacks #data-structure #queue #stack
#include <cassert>
#include <iostream>
#include <stack>
template<class T>
class Queue
{
public:
Queue()
{
@ChunMinChang
ChunMinChang / README.md
Last active January 31, 2018 13:59
Sum of all children's node #recursion #tree

Note

The solution is based on postorder. By the implementation in binary tree traversal, we could easily solve this problem by replace Run function from printing node's value to calculating the sum of the children node's value.

@ChunMinChang
ChunMinChang / README.md
Last active September 18, 2020 05:59
Binary Tree Traversal #tree

Binary Tree Traversal

TODO

  • Implement Morris's traversal algorithms.
  • Implement leetcode's solution for postorder.
  • Add comment to explain the implementation by using visited-flag(flag.cpp)

Reference

  • Non-recursive Preorder Traversal of Binary Tree
  • Tushar Roy
@ChunMinChang
ChunMinChang / bruteforce.cpp
Last active February 5, 2018 05:28
Find the max length of the substring in a digit string that its sum of the first half is equal to its second half #DPfCI #dynamic_programming
#include <cassert>
#include <iostream>
#include <string>
bool
IsHalfEqual(std::string s)
{
assert(s.length() % 2 == 0);
unsigned int i = 0;
@ChunMinChang
ChunMinChang / minPrice1.cpp
Last active February 5, 2018 05:28
Calculate the lowest prices between stations #DPfCI #dynamic_programming #recursion
// $ g++ minPrice1.cpp --std=c++11
//
// Ticket Prices:
//
// To
// | 0 | 1 | 2 | 3 | 4 |
// --+-----+-----+-----+-----+-----+
// 0 | 0 | 10 | 75 | 94 | 107 |
// F --+-----+-----+-----+-----+-----+
// r 1 | * | 0 | 35 | 50 | 83 |
@ChunMinChang
ChunMinChang / hanoi.cpp
Last active February 5, 2018 05:27
Tower of Hanoi #DPfCI #dynamic_programming #recursion
// Compile by $ g++ hanoi.cpp --std=c++11
//
// The standard `Tower of Hanoi` has 3 pegs and a number of disks
// of different sizes, which can slide onto any peg.
// The objective of the puzzle is to move entire stack from one peg to
// another peg, obeying the following simple rules:
// 1. The disks need to keep in ascending order of size on the peg any time.
// That is, no disk can be placed on top of a smaller disk.
// 2. Only one disk can be moved at a time.
// 3. Each move consists of taking the upper disk from one of the stacks
@ChunMinChang
ChunMinChang / bubbleSort.cpp
Last active November 8, 2018 05:01
Warm Up for Dynamic Programming Practice #DPfCI #dynamic_programming #recursion
#include <cassert>
#include <iostream>
#undef RECURSION
#undef DYNAMIC_PROGRAMMING
#undef RUN
#define RECURSION 1
#define DYNAMIC_PROGRAMMING 2
#define RUN RECURSION
@ChunMinChang
ChunMinChang / exp.cpp
Last active January 31, 2018 14:17
Exponentiation by squaring #math #recursion
#include "exp.h"
#include <stack>
// Calculate the exponentiation by squaring:
// k ^ n = (k ^ (n / 2)) ^ 2 if n is even
// or k * (k ^ ((n - 1) / 2))^ 2 if n is odd
// or
// k ^ n = (k ^ 2) ^ (n / 2) if n is even
// or k * (k ^ 2) ^ ((n - 1) / 2) if n is odd
//
@ChunMinChang
ChunMinChang / README.md
Last active March 6, 2022 13:23
Implementation of different methods to calculate the _Fibonacci_ numbers by fast doubling #recursion #dynamic_programming #Fibonacci #math

Calculating Fibonacci Numbers by Fast Doubling

Implementation of different methods to calculate the Fibonacci numbers by fast doubling.

Please read my blog post for more detail.

Reference

@ChunMinChang
ChunMinChang / README.md
Last active January 31, 2018 14:19
Reference counting #smartpointer