Skip to content

Instantly share code, notes, and snippets.

@mac01021
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save mac01021/495d24a4649438b87c49 to your computer and use it in GitHub Desktop.

Select an option

Save mac01021/495d24a4649438b87c49 to your computer and use it in GitHub Desktop.
#from data:stack import *
def Box box(st Int, hgt Int, wdt Int)
def [st Int, hgt Int] .box [pos Int] Box { closed(st, hgt, pos - o:st) }
def area[box Box] Int { box:hgt * box:wdt }
def [a Box] > [b Box] Bool { area[a] > area[b] }
def bestBox[slots Array[Int]] Box {
let stack Stack[Int, Int] = newStack[]
let biggest = box(0,0,0)
forEach[slots] {[pos, slot]
let h = stack.top:hgt
if stack.isEmpty || slot > h {
stack.push[pos, slot]
} else {
while {h > slot} {
let o = stack.pop
set biggest = max[biggest, o.box[pos]]
}
}
}
biggest
}
let input Array[Int] = arrayOf[2, 3, 0, 5, 1, 1, 2, 1,
2, 2, 1, 4, 3, 2, 1, 1,
6, 4, 4, 4, 5, 5, 4, 4,
2, 2, 2, 0, 2, 3, 0, 0]
assert[bestBox[input] = box(16, 4, 8)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment