Skip to content

Commit

Permalink
feat: Refactor square root calculation to use efficient binary search…
Browse files Browse the repository at this point in the history
… algorithm
  • Loading branch information
pragusga25 committed Jul 17, 2024
1 parent 9254515 commit f50e6f5
Showing 1 changed file with 2 additions and 8 deletions.
10 changes: 2 additions & 8 deletions easy/sqrtx.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
// Problem: https://leetcode.com/problems/sqrtx/description/
// Doc: https://leetcode.com/problems/sqrtx/solutions/5491801/efficient-binary-search-for-square-root-calculation/
// Doc: https://leetcode.com/problems/sqrtx/solutions/5491842/efficient-binary-search-for-calculating-square-root/
const mySqrt = (x: number): number => {
let [l, r] = [0, 1];
// We are not allowed to use Math.pow
// So we initialize r using this loop :v
for (let i = 1; i <= 31; i++) {
r *= i;
}
r--;
let [l, r] = [0, x];

let ans = 0;
while (l <= r) {
Expand Down

0 comments on commit f50e6f5

Please sign in to comment.