(n, prev)
| 124 | }; |
| 125 | |
| 126 | const 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 | |
| 145 | const multiply = (prev, next) => { |
| 146 | const [rows, cols] = [2, 2]; |
no test coverage detected