Created
May 6, 2015 10:48
-
-
Save scottoasis/1d87083c908c24856895 to your computer and use it in GitHub Desktop.
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
/* | |
* test cases | |
* | |
* case 1. | |
* | |
* #============# | |
* #===#====#===# | |
* #==##==####### | |
* #=############ | |
* #=############ | |
* --------------- | |
* [5,0,2,3,4,2,2,3,3,4,3,3,3,5] => 12 + 10 + 4 + 1 + 1 = 28 | |
* | |
* | |
* case 2. | |
* | |
* #=======# | |
* ##=======#===# | |
* ##=##==#=##### | |
* ########=##### | |
* ############## | |
* --------------- | |
* [4,5,2,3,3,2,2,3,1,5,3,3,3,4] => 7 + 10 + 4 + 1 = 22 | |
* | |
*/ | |
function min (a, b) { | |
if (a > b) { | |
return b; | |
} | |
else { | |
return a; | |
} | |
} | |
function fillout (blocks, from, to) { | |
from = from || 0; | |
to = to || blocks.length - 1; | |
if (to < from + 2) { | |
return 0; | |
} | |
var nblocks = 0;; | |
var bFrom = blocks[from] | |
, bTo = blocks[to] | |
, bSurf = min(bFrom, bTo); | |
for (var i = 1; i < (to - from + 1); i++) { | |
if (blocks[from + i] > bSurf) { | |
nblocks = fillout(blocks, from, from + i) + fillout(blocks, from + i, to); | |
break; | |
} | |
else { | |
nblocks += (bSurf - blocks[from + i]); | |
} | |
} | |
return nblocks; | |
} | |
var assert = require('assert'); | |
assert(0 === fillout([1, 3])); | |
assert(1 === fillout([1, 0, 1])); | |
assert(2 === fillout([3, 0, 2])); | |
assert(28 === fillout([5,0,2,3,4,2,2,3,3,4,3,3,3,5])); | |
assert(22 === fillout([4,5,2,3,3,2,2,3,1,5,3,3,3,4])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment