MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / power

Function power

javascript/0070-climbing-stairs.js:126–143  ·  view source on GitHub ↗
(n, prev)

Source from the content-addressed store, hash-verified

124};
125
126const power = (n, prev) => {
127 let next = [
128 [1, 0],
129 [0, 1],
130 ];
131
132 const isEmpty = () => n === 0;
133 while (!isEmpty()) {
134 /* Time O(log(N)) */
135 const isBit = (n & 1) === 1;
136 if (isBit) next = multiply(next, prev); /* Time O(1) | Space O(1) */
137
138 n >>= 1;
139 prev = multiply(prev, prev); /* Time O(1) | Space O(1) */
140 }
141
142 return next;
143};
144
145const multiply = (prev, next) => {
146 const [rows, cols] = [2, 2];

Callers 1

climbStairsFunction · 0.85

Calls 2

isEmptyFunction · 0.70
multiplyFunction · 0.70

Tested by

no test coverage detected