Created
July 7, 2022 18:57
-
-
Save bertolo1988/8b3e53eaea65ca68749407b22429aab3 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
function memoize(func) { | |
var memo = {}; | |
var slice = Array.prototype.slice; | |
return function () { | |
var args = slice.call(arguments); | |
if (args in memo) return memo[args]; | |
else return (memo[args] = func.apply(this, args)); | |
}; | |
} | |
function sumArray(arr) { | |
let sum = 0; | |
for (let entry of arr) { | |
sum = sum + entry; | |
} | |
return sum; | |
} | |
function minArray(arr) { | |
let min = arr[0]; | |
for (let entry of arr) { | |
if (entry < arr[0]) min = entry; | |
} | |
return min; | |
} | |
function calculatePower(l, r) { | |
const slicedPower = myPower.slice(l, r + 1); | |
const min = minArray(slicedPower); | |
const sum = sumArray(slicedPower); | |
return min * sum; | |
} | |
const memoizedCalculatePower = memoize(calculatePower); | |
var myPower; | |
function findTotalPower(power) { | |
myPower = power; | |
let totalSum = 0n; | |
for (let l = 0; l < myPower.length; l++) { | |
for (let r = l; r < myPower.length; r++) { | |
const powerResult = memoizedCalculatePower(l, r); | |
totalSum = totalSum + BigInt(powerResult); | |
} | |
} | |
return (totalSum % 1000000007n).toString(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment