This file contains hidden or 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
function solution(A,B,C) { | |
return nailsBinarySearch(A,B,C); | |
} | |
function nailsBinarySearch(boardStarts, boardEnds, nailPositions) { | |
var boards = boardStarts.map((cur, curIndex) => [cur, boardEnds[curIndex]],[]); | |
var lowerBorderNailsNumber = 1; | |
var upperBorderNailsNumber = nailPositions.length; | |
var minimalNailsNumber = -1; |
This file contains hidden or 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
function boardsBinarySearch(A, numBoards) { | |
var n = A.length; | |
var minBoardSize = 1; | |
var maxBoardSize = n; | |
var result = -1; | |
while (minBoardSize <= maxBoardSize) { | |
var tentativeBoardSize = Math.floor((minBoardSize + maxBoardSize) / 2); | |
if (neededBoards(A, tentativeBoardSize) <= numBoards) { | |
result = tentativeBoardSize; | |
maxBoardSize = tentativeBoardSize - 1; |
This file contains hidden or 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
function minNumOfCoins(coinages, amount) { | |
if (amount === 0) return 0; | |
if (coinages.length === 0 && amount > 0) return Infinity; | |
var maxCoinage = Math.max.apply(null, coinages); | |
var otherCoinages = coinages.filter(coinage => coinage !== maxCoinage) | |
if (amount < maxCoinage) return minNumOfCoins(otherCoinages, amount); | |
var coinsWithMaxCoinage = minNumOfCoins(coinages, amount - maxCoinage) + 1; | |
var coinsWithoutMaxCoinage = minNumOfCoins(otherCoinages, amount); |
This file contains hidden or 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
function betterMinNumOfCoins(coinages, amount) { | |
// sort coinages | |
coinages = coinages.sort((a,b)=>a-b); | |
var minCoinage = Math.min.apply(null, coinages); | |
var dp = new Array(amount + 1).fill(Infinity); | |
dp[0] = 0; | |
for (var i = 1; i <= amount; i++) { | |
for (var j = 0; j < coinages.length; j++) { | |
var coinage = coinages[j]; | |
if (coinage > i) break; |
This file contains hidden or 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
function knapsack01(maxWeight, items, returnAllKnapsacks) { | |
items = items.sort((a,b)=>a.weight-b.weight); | |
var knapsacks = new Array(maxWeight + 1); | |
for (var i = 0; i <= maxWeight; i++) { | |
knapsacks[i] = new Array(items.length); | |
for (var j = 0; j < items.length; j++) { | |
knapsacks[i][j] = 0; | |
}; | |
}; |
This file contains hidden or 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
function knapsackContents(items, maxWeight) { | |
items = items.sort((a,b)=>a.weight-b.weight); | |
var knapsacks = knapsack01(maxWeight, items, true); | |
var result = []; | |
return contentsHelper(knapsacks, items); | |
function contentsHelper(matrix, items) { | |
var column = matrix[matrix.length-1] | |
var numItems = items.length; | |
if (numItems === 0) return []; |
This file contains hidden or 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
function numberSolitaire(arra) { | |
var dp = new Array(arra.length).fill(0); | |
dp[0] = arra[0]; | |
for (var i = 1; i < arra.length; i++) { | |
var max = -Infinity; | |
for (var j = 1; j <= 6; j++) { | |
if (i - j < 0) break; | |
max = Math.max(max, dp[i-j] + arra[i]); | |
} | |
dp[i] = max; |
This file contains hidden or 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
function genomicRangeQuery(nucleotides, mins, maxs) { | |
var result = new Array(mins.length).fill(-1); | |
var distances = distancesFromLastSeen(nucleotides); | |
for (var i = 0; i < mins.length; i++) { | |
var segmentWidth = maxs[i] - mins[i] + 1; | |
if (distances['A'][maxs[i]] < segmentWidth) { | |
result[i] = 1; | |
continue; | |
} | |
if (distances['C'][maxs[i]] < segmentWidth) { |
This file contains hidden or 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
function primeFactorsOf(n) { | |
var lpfs = leastPrimeFactorsOf(n); | |
return helper(lpfs, n, []); | |
function helper(lpfs, n, arra) { | |
if (lpfs[n] == 0) return arra.concat([n]); | |
return helper(lpfs, n/lpfs[n], arra.concat([lpfs[n]])); | |
} | |
} | |
function leastPrimeFactorsOf(n) { |
This file contains hidden or 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
function stackBasedLeader(A) { | |
var n = A.length; | |
var occurrences = {}; | |
var stackSize = 0; | |
var stackElem = -1; | |
for (let a of A) { | |
if (!occurrences[a]) occurrences[a] = 0; | |
occurrences[a]++; |