This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
history | sed -E 's/(^( )+[0-9]+ [0-9\:T\-]+) //' |
NewerOlder