Skip to content

Instantly share code, notes, and snippets.

View daifu's full-sized avatar

Daifu Richard Ye daifu

View GitHub Profile
@daifu
daifu / dijkstra_algorithm.rb
Last active January 29, 2022 14:16
Dijkstra Algorithm in Ruby
=begin
Implement dijkstra's algorithm
reference
https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
V is the number of vertices
N is the maximum number of edges attached to a single node.
Time complexity: O( V * N * log V)
@daifu
daifu / mergesort.rb
Created July 21, 2015 18:51
Mergesort in Ruby
=begin
Merge sort in ruby
Referrence
https://en.wikipedia.org/wiki/Merge_sort
Big O complexity
Time complexity (Average, Best, Worest cases): O(n log(n))
Space complexity: O(n)
=end
@daifu
daifu / heapsort.rb
Last active August 29, 2015 14:25
Heapsort in ruby
=begin
implement heapsort in rails
https://en.wikipedia.org/wiki/Heapsort
Big(O) complexity
Time complexity (Average, Best, Worest cases): O(n log(n))
Space complexity: O(1)
test example
@daifu
daifu / flatten_array
Last active August 29, 2015 14:06
Flatten An Array
def flatten(ary)
ret = []
ary.each do |sub_ary|
if sub_ary.is_a?(Array)
ret += flatten(sub_ary)
else
ret << sub_ary
end
end
ret
@daifu
daifu / canJump.java
Last active December 20, 2015 08:09
Jump Game - Leetcode
/*
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Determine if you are able to reach the last index.
For example:
A = [2,3,1,1,4], return true.
@daifu
daifu / trap.java
Created July 28, 2013 00:33
Trapping Rain Water - Leetcode
/*
Given n non-negative integers representing an elevation map where
the width of each bar is 1, compute how much water it is able to trap after raining.
For example,
Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.
Algorithm:
1. construct 2 arrays that store the max from left to right and max from right to left
2. At position i, we now know the max from its left and right, then find the max trapped water.
@daifu
daifu / firstMissingPositive.java
Last active December 20, 2015 01:29
First Missing Positive - Leetcode
/*
Given an unsorted integer array, find the first missing positive integer.
For example,
Given [1,2,0] return 3,
and [3,4,-1,1] return 2.
Your algorithm should run in O(n) time and uses constant space.
*/
@daifu
daifu / combinationSum.java
Last active April 24, 2018 12:47
Combination Sum - Leetcode
/*
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.
Note:
All numbers (including target) will be positive integers.
Elements in a combination (a1, a2, ..., ak) must be in non-descending order. (ie, a1 ? a2 ? ... ? ak).
@daifu
daifu / longestValidParentheses.java
Created June 22, 2013 06:21
Longest Valid Parentheses - leetcode
/*
Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.
For "(()", the longest valid parentheses substring is "()", which has length = 2.
Another example is ")()())", where the longest valid parentheses substring is "()()", which has length = 4.
Algorithm time complexity is O(n), and space complexity is O(n)
Basic algrithm is to keep track of the longest matched paramthesis.
But we need to separate the valid substring, which have 2 special cases: ())()() and ()()(((), and others(whole string