Skip to content

Instantly share code, notes, and snippets.

@muxcmux
muxcmux / water-container.rb
Created January 23, 2020 22:36
this is think is O(n2)
# @param {Integer[]} height
# @return {Integer}
def max_area(heights)
max = 0
heights.each_with_index do |y, x|
heights.each_with_index do |y1, x1|
width = (x - x1).abs
area = [y, y1].min * width
max = area if area >= max
end
@muxcmux
muxcmux / max_profit.rb
Created January 23, 2020 22:35
wtf is the complexity for this shit?!
# @param {Integer[]} prices
# @return {Integer}
def max_profit(prices)
mp = 0
prices.each_with_index do |price, i|
for n in (i + 1)..prices.count do
profit = prices[n].to_i - price
mp = profit if profit > mp
end
end
@muxcmux
muxcmux / alien_sorted.rb
Created January 23, 2020 22:23
nikva idea za time/space complexity, no 100% bachka :D
# @param {String[]} words
# @param {String} order
# @return {Boolean}
def is_alien_sorted(words, order)
sorted = words.sort do |a, b|
cmp = [a.length, b.length].max.times do |i|
next if a[i] === b[i]
break i
end
if cmp >= a.length || cmp >= b.length
@muxcmux
muxcmux / min-valid-paren.rb
Last active January 23, 2020 21:44
O(2n), samo che ne bachka, shtoto ne hvashta vryshta "))((" vmesto empty str
# @param {String} s
# @return {String}
def min_remove_to_make_valid(s)
chars = s.split('')
op = chars.reduce(0) { |a, c| a + (c == '(' ? 1 : 0) }
cl = chars.reduce(0) { |a, c| a + (c == ')' ? 1 : 0) }
diff = op - cl
chars.select do |c|
if diff === 0
true
@muxcmux
muxcmux / hack.sh
Created April 6, 2012 15:01 — forked from erikh/hack.sh
OSX For Hackers
#!/usr/bin/env sh
##
# This is script with usefull tips taken from:
# https://github.com/mathiasbynens/dotfiles/blob/master/.osx
#
# install it:
# curl -sL https://raw.github.com/gist/2108403/hack.sh | sh
#
@muxcmux
muxcmux / blog.thor
Created March 11, 2012 15:24
Simple thor task for uploading via LFTP
class Blog < Thor
no_tasks do
def config
{
:u => 'username', # user
:p => 'password', # pass
:h => 'ftp://your.ftpserver.com', # host
:r => 'octopress', # remote dir
:l => File.basename(File.join(__FILE__, '..', 'public')) # local dir
@muxcmux
muxcmux / version.rake
Created February 12, 2012 02:53
Manage your rails app version with this rake task
def valid? version
pattern = /^\d+\.\d+\.\d+(\-(dev|beta|rc\d+))?$/
raise "Tried to set invalid version: #{version}".red unless version =~ pattern
end
def correct_version version
ver, flag = version.split '-'
v = ver.split '.'
(0..2).each do |n|
v[n] = v[n].to_i