Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save caglarorhan/87f61be83f72d4f0d9cbf599e460ba5a to your computer and use it in GitHub Desktop.
Save caglarorhan/87f61be83f72d4f0d9cbf599e460ba5a to your computer and use it in GitHub Desktop.
findMaximumSustainableClusterSize is a sliding window example
function findMaximumSustainableClusterSize(bootingPower,processingPower, powerMax){
let maxClusterSize=0;
let firstClusterIndex=0;
let lastClusterIndex=0;
while(firstClusterIndex<processingPower.length && lastClusterIndex<processingPower.length){
console.log(firstClusterIndex,":",lastClusterIndex)
let totalPowerConsumeOfChosenClusterWindow = 0;
let windowBootingPowers = bootingPower.filter((power,index)=>index>=firstClusterIndex&&index<=lastClusterIndex);
let windowProcessingPowers = processingPower.filter((power,index)=>index>=firstClusterIndex&&index<=lastClusterIndex);
let totalBootingPowerInWindow = windowBootingPowers.reduce((prev,current)=>prev+current, 0);
let totalProcessingPowerInWindow = windowProcessingPowers.reduce((prev,current)=>prev+current, 0);
totalPowerConsumeOfChosenClusterWindow = totalBootingPowerInWindow + totalProcessingPowerInWindow;
console.log(totalPowerConsumeOfChosenClusterWindow);
if(totalPowerConsumeOfChosenClusterWindow>powerMax){
if(lastClusterIndex===firstClusterIndex){
lastClusterIndex++;
firstClusterIndex++;
}else{
firstClusterIndex++;
}
}else{
if(maxClusterSize<(lastClusterIndex-firstClusterIndex+1)){
maxClusterSize=lastClusterIndex-firstClusterIndex+1;
}
lastClusterIndex++;
}
console.log('Max ClusterSize:',maxClusterSize)
}
return maxClusterSize;
}
console.log(findMaximumSustainableClusterSize([3,6,1,3,4],[2,1,3,4,5],25))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment