Skip to content

Instantly share code, notes, and snippets.

View themichaelyang's full-sized avatar
🌱
‾ʖ̫‾

Michael Yang themichaelyang

🌱
‾ʖ̫‾
View GitHub Profile
def unique_paths(m, n)
grid = Array.new(m)
m.times.with_index do |y|
grid[y] = Array.new(n)
grid[y][0] = 1
n.times.with_index do |x|
from_left = if x - 1 >= 0 then grid[y][x - 1] else 0 end
from_above = if y - 1 >= 0 then grid[y - 1][x] else 0 end
def rob(nums)
rob_impl(nums, 0)
end
# https://nithinbekal.com/posts/ruby-memoization/
# https://stackoverflow.com/questions/35595047/how-to-generically-memoize-any-function-in-ruby
# We could use a Class instance to hold the memory
def memoize(method_name)
memory = {}
original = method(method_name)
# For some reason, only iterative DP passes Leetcode
def coin_change(coins, amount)
result = coin_change_iterative(coins, amount)
if result.infinite?
-1
else
result
end
end
class NumMatrix
# (0, 0) is the top left
def initialize(matrix)
@corner_areas = matrix
@corner_areas.each.with_index do |row, y|
row.each.with_index do |value, x|
upper_sum = positive_dig(@corner_areas, y - 1, x)
left_sum = positive_dig(@corner_areas, y, x - 1)
@corner_areas[y][x] = value + upper_sum + left_sum - positive_dig(@corner_areas, y - 1, x - 1)
# Here is my solution, ignoring the sparse optimization, so we iterate over k. Originally, I wanted to only keep track
# of the locations with fruit.
def max_total_fruits(fruits, start_pos, k)
steps = []
fruits.each do |pos, amount|
steps[pos] = amount
end
right_sum = 0
def max_score(card_points, k)
left_sums = cumulative_sum(card_points[0...k]).prepend(0)
right_sums = cumulative_sum(card_points[-k..-1].reverse_each).reverse.append(0)
left_sums.zip(right_sums).map(&:sum).max
end
def cumulative_sum(iter)
sum = 0
iter.map {|x| sum += x}
class NumArray
def initialize(nums)
sum = 0
@prefix_sums = nums.map do |x|
sum += x
end
@prefix_sums.prepend(0)
end
def sum_range(left, right)
@themichaelyang
themichaelyang / userChrome.css
Last active June 10, 2020 23:37
my firefox userChrome css
@namespace url(http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul);
/* Adapted from: https://www.reddit.com/r/FirefoxCSS/comments/bm77hx/modern_firefox/ */
/* the little line under the title bar separating it from content */
#navigator-toolbox::after {
height: 0px !important;
}
#personal-bookmarks #PlacesToolbarItems {
-moz-box-pack: center;
@themichaelyang
themichaelyang / 1c.py
Last active October 26, 2021 03:16
MIT 6.0001 Problem Set 1C Solutions
# MIT OCW 6.0001
# PSET 1C
# SOLUTION
# salary
starting_annual_salary = int(input("Starting salary? "))
# starting_annual_salary = 10000
semi_annual_raise = 0.07
# investments
history | sed -E 's/(^( )+[0-9]+ [0-9\:T\-]+) //'