Skip to content

Commit f50e6f5

Browse files
committed
feat: Refactor square root calculation to use efficient binary search algorithm
1 parent 9254515 commit f50e6f5

File tree

1 file changed

+2
-8
lines changed

1 file changed

+2
-8
lines changed

easy/sqrtx.test.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
// Problem: https://leetcode.com/problems/sqrtx/description/
2-
// Doc: https://leetcode.com/problems/sqrtx/solutions/5491801/efficient-binary-search-for-square-root-calculation/
2+
// Doc: https://leetcode.com/problems/sqrtx/solutions/5491842/efficient-binary-search-for-calculating-square-root/
33
const mySqrt = (x: number): number => {
4-
let [l, r] = [0, 1];
5-
// We are not allowed to use Math.pow
6-
// So we initialize r using this loop :v
7-
for (let i = 1; i <= 31; i++) {
8-
r *= i;
9-
}
10-
r--;
4+
let [l, r] = [0, x];
115

126
let ans = 0;
137
while (l <= r) {

0 commit comments

Comments
 (0)