This is one of those problems where the clean idea matters more than the amount of code. For 69. Sqrt(x), the solution in this repository is mainly a binary search solution.
Guide
What?
The first job is to translate the English prompt into state, transition, and stopping conditions. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: binary search, two pointers, sliding window.
Guide
When?
This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are mySqrtRange, mySqrt.
Guide
Why?
The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime Error for x = 2147395599
02class Solution {
03public:
04 long long mySqrtRange(long long x, long long low, long long high){
05 long long mid = (low + high)/2;
06 if((mid * mid <= x) && (mid+1) * (mid+1) > x){
07 return mid;
08 }else if(mid * mid < x){
09 return mySqrtRange(x, low+1, high);
10 }
11 return mySqrtRange(x, low, high-1);
12 }
13
14 int mySqrt(int x) {
15 return (int)mySqrtRange((long long)x, (long long)0, (long long)(x/2));
16 }
17};
18
19//binary search
20//https://leetcode.com/problems/sqrtx/discuss/25047/A-Binary-Search-Solution
21//Runtime: 4 ms, faster than 100.00% of C++ online submissions for Sqrt(x).
22//Memory Usage: 8.2 MB, less than 99.48% of C++ online submissions for Sqrt(x).
23//NOTE1: use (mid < x/mid) to replace (mid * mid < x)
24//NOTE2: use (int mid = left + (right - left)/2;) to replace (int mid = (left + right)/2;)
25class Solution {
26public:
27 int mySqrt(int x) {
28 if(x == 0) return 0;
29 int left = 1, right = INT_MAX;
30 while(true){
31 //runtime error: signed integer overflow: 1 + 2147483647 cannot be represented in type 'int'
32 // int mid = (left + right)/2;
33 int mid = left + (right - left)/2;
34 if(mid <= x/mid && (mid+1) > x/(mid+1)){
35 return mid;
36 }else if(mid < x/mid){
37 left = mid+1;
38 }else{
39 //mid > x/mid
40 right = mid-1;
41 }
42 }
43 }
44};
Cost