Skip to content

Instantly share code, notes, and snippets.

View nma's full-sized avatar

Nick Ma nma

View GitHub Profile
@nma
nma / buy_and_sell_stock_single_txn.py
Created June 22, 2019 19:13
Best time to buy and sell stock single transaction
class Solution:
def maxProfit(self, prices: List[int]) -> int:
if len(prices) < 2:
return 0
low_price = prices[0]
max_profit = 0
for i in range(1, len(prices)):
if prices[i] > low_price:
@nma
nma / permutations_iterative.py
Created April 8, 2019 01:51
Permutations Iterative
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
permutations = [[]]
for choice in nums:
new_permutations = []
for perm in permutations:
perm.append(choice)
for j in range(len(perm)):
@nma
nma / combinations_iterative.py
Created April 8, 2019 01:17
Combinations Iterative
class Solution:
def combine(self, n: int, k: int) -> List[List[int]]:
nums = list(range(1, n + 1))
combinations = [[]]
for depth in range(0, k):
new_combinations = []
for combo in combinations:
start_index = nums.index(combo[-1]) + 1 if len(combo) > 0 else 0
@nma
nma / permutations_backtracking.py
Created April 6, 2019 04:14
Permutations Backtracking
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
result = []
def backtracking(permutations, path, options):
if len(path) == len(options):
permutations.append(path[:])
else:
for option in options:
@nma
nma / combinations_backtracking.py
Last active April 5, 2019 04:37
Combinations Backtracking
class Solution:
def combine(self, n: int, k: int) -> List[List[int]]:
nums = list(range(1, n + 1))
combinations = []
def backtracking(results, path, depth, options):
if depth == 0:
results.append(path[:])
else:
@nma
nma / permutations_recursive.py
Last active August 26, 2020 04:00
Permutations Recusive
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
result = []
def permute_helper(ans: List[int], cur: List[int], options: List[int]):
if len(options) == 0:
ans.append(cur)
else:
for index, choice in enumerate(options):
@nma
nma / combinations_recursive.py
Last active August 26, 2020 03:56
Combinations Recursive
class Solution:
def combine(self, n: int, k: int) -> List[List[int]]:
nums = list(range(1, n + 1))
combinations = []
def combine_helper(ans: List[int], path: List[int], options: List[int], count: int):
if count == 0:
ans.append(path)
else:
@nma
nma / windowsize.js
Created February 22, 2017 21:31
ReactJS get window sizes
constructor(props) {
super(props);
this.state = { height: 512 };
this.updateWindowDimensions = this.updateWindowDimensions.bind(this);
}
componentDidMount() {
this.updateWindowDimensions();
window.addEventListener("resize", this.updateWindowDimensions.bind(this));
}
I have run an nginx container...
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6d67de07731d nginx "nginx -g 'daemon ..." 40 minutes ago Up 40 minutes 80/tcp, 443/tcp epic_goldberg
I want to use Debian for debug:
docker run -it --pid=container:6d67de07731d --net=container:6d67de07731d --cap-add sys_admin debian
I can see the nginx process:
@nma
nma / ifme_rails_c9ide_rake_setup.rb
Created February 9, 2017 01:58
Rake tasks for quickly setting up C9 IDE.
desc 'Automate the Config Setup for New Environments'
task :setup_workspace do
dev_example = Rails.root + 'config/env/development.example.env'
dev_target = Rails.root + 'config/env/development.env'
FileUtils.cp(dev_example, dev_target)
test_example = Rails.root + 'config/env/test.example.env'
test_target = Rails.root + 'config/env/test.env'
FileUtils.cp(test_example, test_target)