Skip to content

Commit 8548bba

Browse files
authored
Added tasks 50, 52, 69, 149
1 parent 72074c0 commit 8548bba

File tree

13 files changed

+300
-2
lines changed

13 files changed

+300
-2
lines changed

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,7 @@ Go-based LeetCode algorithm problem solutions, regularly updated.
810810
| 0007 |[Reverse Integer](src/main/go/g0001_0100/s0007_reverse_integer/solution.go)| Medium | Top_Interview_Questions, Math | 0 | 100.00
811811
| 0009 |[Palindrome Number](src/main/go/g0001_0100/s0009_palindrome_number/solution.go)| Easy | Math | 0 | 100.00
812812
| 0172 |[Factorial Trailing Zeroes](src/main/go/g0101_0200/s0172_factorial_trailing_zeroes/solution.go)| Medium | Top_Interview_Questions, Math | 0 | 100.00
813+
| 0050 |[Pow(x, n)](src/main/go/g0001_0100/s0050_powx_n/solution.go)| Medium | Top_Interview_Questions, Math, Recursion | 0 | 100.00
813814

814815
#### Udemy Strings
815816

@@ -1146,6 +1147,7 @@ Go-based LeetCode algorithm problem solutions, regularly updated.
11461147
| 0077 |[Combinations](src/main/go/g0001_0100/s0077_combinations/solution.go)| Medium | Backtracking | 36 | 77.01
11471148
| 0046 |[Permutations](src/main/go/g0001_0100/s0046_permutations/solution.go)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Backtracking, Big_O_Time_O(n\*n!)_Space_O(n+n!) | 0 | 100.00
11481149
| 0039 |[Combination Sum](src/main/go/g0001_0100/s0039_combination_sum/solution.go)| Medium | Top_100_Liked_Questions, Array, Backtracking, Big_O_Time_O(2^n)_Space_O(n+2^n) | 0 | 100.00
1150+
| 0052 |[N-Queens II](src/main/go/g0001_0100/s0052_n_queens_ii/solution.go)| Hard | Backtracking | 0 | 100.00
11491151
| 0022 |[Generate Parentheses](src/main/go/g0001_0100/s0022_generate_parentheses/solution.go)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Backtracking, Big_O_Time_O(2^n)_Space_O(n) | 0 | 100.00
11501152
| 0079 |[Word Search](src/main/go/g0001_0100/s0079_word_search/solution.go)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Matrix, Backtracking, Big_O_Time_O(4^(m\*n))_Space_O(m\*n) | 99 | 79.91
11511153

@@ -1182,8 +1184,8 @@ Go-based LeetCode algorithm problem solutions, regularly updated.
11821184
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11831185
|-|-|-|-|-|-
11841186
| 0215 |[Kth Largest Element in an Array](src/main/go/g0201_0300/s0215_kth_largest_element_in_an_array/solution.go)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Heap_Priority_Queue, Divide_and_Conquer, Quickselect, LeetCode_75_Heap/Priority_Queue, Big_O_Time_O(n\*log(n))_Space_O(log(n)) | 17 | 83.49
1185-
| 0373 |[Find K Pairs with Smallest Sums](src/main/go/g0301_0400/s0373_find_k_pairs_with_smallest_sums/solution.go)| Medium | Array, Heap_Priority_Queue | 21 | 90.40
11861187
| 0502 |[IPO](src/main/go/g0501_0600/s0502_ipo/solution.go)| Hard | Array, Sorting, Greedy, Heap_Priority_Queue | 62 | 91.95
1188+
| 0373 |[Find K Pairs with Smallest Sums](src/main/go/g0301_0400/s0373_find_k_pairs_with_smallest_sums/priorrity_queue.go)| |||
11871189
| 0295 |[Find Median from Data Stream](src/main/go/g0201_0300/s0295_find_median_from_data_stream/medianfinder.go)| Hard | Top_100_Liked_Questions, Sorting, Two_Pointers, Design, Heap_Priority_Queue, Data_Stream, Big_O_Time_O(n\*log_n)_Space_O(n) | 59 | 96.70
11881190

11891191
#### Top Interview 150 Bit Manipulation
@@ -1204,6 +1206,9 @@ Go-based LeetCode algorithm problem solutions, regularly updated.
12041206
| 0009 |[Palindrome Number](src/main/go/g0001_0100/s0009_palindrome_number/solution.go)| Easy | Math | 0 | 100.00
12051207
| 0066 |[Plus One](src/main/go/g0001_0100/s0066_plus_one/solution.go)| Easy | Top_Interview_Questions, Array, Math | 0 | 100.00
12061208
| 0172 |[Factorial Trailing Zeroes](src/main/go/g0101_0200/s0172_factorial_trailing_zeroes/solution.go)| Medium | Top_Interview_Questions, Math | 0 | 100.00
1209+
| 0069 |[Sqrt(x)](src/main/go/g0001_0100/s0069_sqrtx/solution.go)| Easy | Top_Interview_Questions, Math, Binary_Search | 0 | 100.00
1210+
| 0050 |[Pow(x, n)](src/main/go/g0001_0100/s0050_powx_n/solution.go)| Medium | Top_Interview_Questions, Math, Recursion | 0 | 100.00
1211+
| 0149 |[Max Points on a Line](src/main/go/g0101_0200/s0149_max_points_on_a_line/solution.go)| Hard | Top_Interview_Questions, Array, Hash_Table, Math, Geometry | 4 | 100.00
12071212

12081213
#### Top Interview 150 1D DP
12091214

@@ -1679,6 +1684,7 @@ Go-based LeetCode algorithm problem solutions, regularly updated.
16791684
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
16801685
|-|-|-|-|-|-
16811686
| 0202 |[Happy Number](src/main/go/g0201_0300/s0202_happy_number/solution.go)| Easy | Top_Interview_Questions, Hash_Table, Math, Two_Pointers | 0 | 100.00
1687+
| 0149 |[Max Points on a Line](src/main/go/g0101_0200/s0149_max_points_on_a_line/solution.go)| Hard | Top_Interview_Questions, Array, Hash_Table, Math, Geometry | 4 | 100.00
16821688

16831689
### Binary Search I
16841690

@@ -1702,6 +1708,7 @@ Go-based LeetCode algorithm problem solutions, regularly updated.
17021708

17031709
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
17041710
|-|-|-|-|-|-
1711+
| 0069 |[Sqrt(x)](src/main/go/g0001_0100/s0069_sqrtx/solution.go)| Easy | Top_Interview_Questions, Math, Binary_Search | 0 | 100.00
17051712

17061713
#### Day 5
17071714

@@ -1775,7 +1782,7 @@ Go-based LeetCode algorithm problem solutions, regularly updated.
17751782
| 0392 |[Is Subsequence](src/main/go/g0301_0400/s0392_is_subsequence/solution.go)| Easy | String, Dynamic_Programming, Two_Pointers, LeetCode_75_Two_Pointers, Dynamic_Programming_I_Day_19, Level_1_Day_2_String, Udemy_Two_Pointers, Top_Interview_150_Two_Pointers | 0 | 100.00
17761783
| 0383 |[Ransom Note](src/main/go/g0301_0400/s0383_ransom_note/solution.go)| Easy | String, Hash_Table, Counting, Data_Structure_I_Day_6_String, Top_Interview_150_Hashmap | 0 | 100.00
17771784
| 0380 |[Insert Delete GetRandom O(1)](src/main/go/g0301_0400/s0380_insert_delete_getrandom_o1/randomizedset.go)| Medium | Array, Hash_Table, Math, Design, Randomized, Programming_Skills_II_Day_20, Top_Interview_150_Array/String | 47 | 87.65
1778-
| 0373 |[Find K Pairs with Smallest Sums](src/main/go/g0301_0400/s0373_find_k_pairs_with_smallest_sums/solution.go)| Medium | Array, Heap_Priority_Queue, Top_Interview_150_Heap | 21 | 90.40
1785+
| 0373 |[Find K Pairs with Smallest Sums](src/main/go/g0301_0400/s0373_find_k_pairs_with_smallest_sums/priorrity_queue.go)| |||
17791786
| 0347 |[Top K Frequent Elements](src/main/go/g0301_0400/s0347_top_k_frequent_elements/solution.go)| Medium | Top_100_Liked_Questions, Array, Hash_Table, Sorting, Heap_Priority_Queue, Counting, Divide_and_Conquer, Quickselect, Bucket_Sort, Data_Structure_II_Day_20_Heap_Priority_Queue, Big_O_Time_O(n\*log(n))_Space_O(k) | 0 | 100.00
17801787
| 0338 |[Counting Bits](src/main/go/g0301_0400/s0338_counting_bits/solution.go)| Easy | Dynamic_Programming, Bit_Manipulation, LeetCode_75_Bit_Manipulation, Udemy_Bit_Manipulation, Big_O_Time_O(num)_Space_O(num) | 0 | 100.00
17811788
| 0322 |[Coin Change](src/main/go/g0301_0400/s0322_coin_change/solution.go)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Breadth_First_Search, Algorithm_II_Day_18_Dynamic_Programming, Dynamic_Programming_I_Day_20, Level_2_Day_12_Dynamic_Programming, Top_Interview_150_1D_DP, Big_O_Time_O(m\*n)_Space_O(amount) | 10 | 79.59
@@ -1828,6 +1835,7 @@ Go-based LeetCode algorithm problem solutions, regularly updated.
18281835
| 0152 |[Maximum Product Subarray](src/main/go/g0101_0200/s0152_maximum_product_subarray/solution.go)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Dynamic_Programming_I_Day_6, Level_2_Day_13_Dynamic_Programming, Udemy_Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 0 | 100.00
18291836
| 0151 |[Reverse Words in a String](src/main/go/g0101_0200/s0151_reverse_words_in_a_string/solution.go)| Medium | String, Two_Pointers, LeetCode_75_Array/String, Udemy_Strings, Top_Interview_150_Array/String | 0 | 100.00
18301837
| 0150 |[Evaluate Reverse Polish Notation](src/main/go/g0101_0200/s0150_evaluate_reverse_polish_notation/solution.go)| Medium | Top_Interview_Questions, Array, Math, Stack, Programming_Skills_II_Day_3, Top_Interview_150_Stack | 0 | 100.00
1838+
| 0149 |[Max Points on a Line](src/main/go/g0101_0200/s0149_max_points_on_a_line/solution.go)| Hard | Top_Interview_Questions, Array, Hash_Table, Math, Geometry, Algorithm_II_Day_21_Others, Top_Interview_150_Math | 4 | 100.00
18311839
| 0148 |[Sort List](src/main/go/g0101_0200/s0148_sort_list/solution.go)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Sorting, Two_Pointers, Linked_List, Divide_and_Conquer, Merge_Sort, Level_2_Day_4_Linked_List, Top_Interview_150_Divide_and_Conquer, Big_O_Time_O(log(N))_Space_O(log(N)) | 7 | 87.91
18321840
| 0146 |[LRU Cache](src/main/go/g0101_0200/s0146_lru_cache/lrucache.go)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Design, Linked_List, Doubly_Linked_List, Udemy_Linked_List, Top_Interview_150_Linked_List, Big_O_Time_O(1)_Space_O(capacity) | 75 | 84.31
18331841
| 0142 |[Linked List Cycle II](src/main/go/g0101_0200/s0142_linked_list_cycle_ii/solution.go)| Medium | Top_100_Liked_Questions, Hash_Table, Two_Pointers, Linked_List, Data_Structure_II_Day_10_Linked_List, Level_1_Day_4_Linked_List, Udemy_Linked_List, Big_O_Time_O(N)_Space_O(1) | 0 | 100.00
@@ -1881,6 +1889,7 @@ Go-based LeetCode algorithm problem solutions, regularly updated.
18811889
| 0072 |[Edit Distance](src/main/go/g0001_0100/s0072_edit_distance/solution.go)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, LeetCode_75_DP/Multidimensional, Algorithm_II_Day_18_Dynamic_Programming, Dynamic_Programming_I_Day_19, Udemy_Dynamic_Programming, Top_Interview_150_Multidimensional_DP, Big_O_Time_O(n^2)_Space_O(n2) | 0 | 100.00
18821890
| 0071 |[Simplify Path](src/main/go/g0001_0100/s0071_simplify_path/solution.go)| Medium | String, Stack, Top_Interview_150_Stack | 0 | 100.00
18831891
| 0070 |[Climbing Stairs](src/main/go/g0001_0100/s0070_climbing_stairs/solution.go)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization, Algorithm_I_Day_12_Dynamic_Programming, Dynamic_Programming_I_Day_2, Level_1_Day_10_Dynamic_Programming, Udemy_Dynamic_Programming, Top_Interview_150_1D_DP, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
1892+
| 0069 |[Sqrt(x)](src/main/go/g0001_0100/s0069_sqrtx/solution.go)| Easy | Top_Interview_Questions, Math, Binary_Search, Binary_Search_I_Day_4, Top_Interview_150_Math | 0 | 100.00
18841893
| 0068 |[Text Justification](src/main/go/g0001_0100/s0068_text_justification/solution.go)| Hard | Array, String, Simulation, Top_Interview_150_Array/String | 0 | 100.00
18851894
| 0067 |[Add Binary](src/main/go/g0001_0100/s0067_add_binary/solution.go)| Easy | String, Math, Bit_Manipulation, Simulation, Programming_Skills_II_Day_5, Top_Interview_150_Bit_Manipulation | 0 | 100.00
18861895
| 0066 |[Plus One](src/main/go/g0001_0100/s0066_plus_one/solution.go)| Easy | Top_Interview_Questions, Array, Math, Programming_Skills_II_Day_3, Udemy_Arrays, Top_Interview_150_Math | 0 | 100.00
@@ -1894,7 +1903,9 @@ Go-based LeetCode algorithm problem solutions, regularly updated.
18941903
| 0055 |[Jump Game](src/main/go/g0001_0100/s0055_jump_game/solution.go)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Greedy, Algorithm_II_Day_12_Dynamic_Programming, Dynamic_Programming_I_Day_4, Udemy_Arrays, Top_Interview_150_Array/String, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
18951904
| 0054 |[Spiral Matrix](src/main/go/g0001_0100/s0054_spiral_matrix/solution.go)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Matrix, Simulation, Programming_Skills_II_Day_8, Level_2_Day_1_Implementation/Simulation, Udemy_2D_Arrays/Matrix, Top_Interview_150_Matrix | 0 | 100.00
18961905
| 0053 |[Maximum Subarray](src/main/go/g0001_0100/s0053_maximum_subarray/solution.go)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Divide_and_Conquer, Data_Structure_I_Day_1_Array, Dynamic_Programming_I_Day_5, Udemy_Famous_Algorithm, Top_Interview_150_Kadane's_Algorithm, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
1906+
| 0052 |[N-Queens II](src/main/go/g0001_0100/s0052_n_queens_ii/solution.go)| Hard | Backtracking, Top_Interview_150_Backtracking | 0 | 100.00
18971907
| 0051 |[N-Queens](src/main/go/g0001_0100/s0051_n_queens/solution.go)| Hard | Top_100_Liked_Questions, Array, Backtracking, Big_O_Time_O(N!)_Space_O(N) | 0 | 100.00
1908+
| 0050 |[Pow(x, n)](src/main/go/g0001_0100/s0050_powx_n/solution.go)| Medium | Top_Interview_Questions, Math, Recursion, Udemy_Integers, Top_Interview_150_Math | 0 | 100.00
18981909
| 0049 |[Group Anagrams](src/main/go/g0001_0100/s0049_group_anagrams/solution.go)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, String, Hash_Table, Sorting, Data_Structure_II_Day_8_String, Programming_Skills_II_Day_11, Udemy_Strings, Top_Interview_150_Hashmap, Big_O_Time_O(n\*k_log_k)_Space_O(n) | 4 | 96.55
18991910
| 0048 |[Rotate Image](src/main/go/g0001_0100/s0048_rotate_image/solution.go)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Matrix, Data_Structure_II_Day_3_Array, Programming_Skills_II_Day_7, Udemy_2D_Arrays/Matrix, Top_Interview_150_Matrix, Big_O_Time_O(n^2)_Space_O(1) | 0 | 100.00
19001911
| 0046 |[Permutations](src/main/go/g0001_0100/s0046_permutations/solution.go)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Backtracking, Algorithm_I_Day_11_Recursion_Backtracking, Level_2_Day_20_Brute_Force/Backtracking, Udemy_Backtracking/Recursion, Top_Interview_150_Backtracking, Big_O_Time_O(n\*n!)_Space_O(n+n!) | 0 | 100.00
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
50\. Pow(x, n)
2+
3+
Medium
4+
5+
Implement [pow(x, n)](http://www.cplusplus.com/reference/valarray/pow/), which calculates `x` raised to the power `n` (i.e., <code>x<sup>n</sup></code>).
6+
7+
**Example 1:**
8+
9+
**Input:** x = 2.00000, n = 10
10+
11+
**Output:** 1024.00000
12+
13+
**Example 2:**
14+
15+
**Input:** x = 2.10000, n = 3
16+
17+
**Output:** 9.26100
18+
19+
**Example 3:**
20+
21+
**Input:** x = 2.00000, n = -2
22+
23+
**Output:** 0.25000
24+
25+
**Explanation:** 2<sup>\-2</sup> = 1/2<sup>2</sup> = 1/4 = 0.25
26+
27+
**Constraints:**
28+
29+
* `-100.0 < x < 100.0`
30+
* <code>-2<sup>31</sup> <= n <= 2<sup>31</sup>-1</code>
31+
* `n` is an integer.
32+
* <code>-10<sup>4</sup> <= x<sup>n</sup> <= 10<sup>4</sup></code>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package s0050_powx_n
2+
3+
// #Medium #Top_Interview_Questions #Math #Recursion #Udemy_Integers #Top_Interview_150_Math
4+
// #2025_05_24_Time_0_ms_(100.00%)_Space_3.88_MB_(100.00%)
5+
6+
func myPow(x float64, n int) float64 {
7+
nn := int64(n)
8+
res := 1.0
9+
if n < 0 {
10+
nn = -nn
11+
}
12+
for nn > 0 {
13+
if nn%2 == 1 {
14+
nn--
15+
res *= x
16+
} else {
17+
x *= x
18+
nn /= 2
19+
}
20+
}
21+
if n < 0 {
22+
return 1.0 / res
23+
}
24+
return res
25+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package s0050_powx_n
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestMyPow(t *testing.T) {
9+
assert.InDelta(t, 1024.00000, myPow(2.00000, 10), 1e-9)
10+
}
11+
12+
func TestMyPow2(t *testing.T) {
13+
assert.InDelta(t, 9.261000000000001, myPow(2.10000, 3), 1e-9)
14+
}
15+
16+
func TestMyPow3(t *testing.T) {
17+
assert.InDelta(t, 0.25000, myPow(2.00000, -2), 1e-9)
18+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
52\. N-Queens II
2+
3+
Hard
4+
5+
The **n-queens** puzzle is the problem of placing `n` queens on an `n x n` chessboard such that no two queens attack each other.
6+
7+
Given an integer `n`, return _the number of distinct solutions to the **n-queens puzzle**_.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2020/11/13/queens.jpg)
12+
13+
**Input:** n = 4
14+
15+
**Output:** 2
16+
17+
**Explanation:** There are two distinct solutions to the 4-queens puzzle as shown.
18+
19+
**Example 2:**
20+
21+
**Input:** n = 1
22+
23+
**Output:** 1
24+
25+
**Constraints:**
26+
27+
* `1 <= n <= 9`
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package s0052_n_queens_ii
2+
3+
// #Hard #Backtracking #Top_Interview_150_Backtracking
4+
// #2025_05_24_Time_0_ms_(100.00%)_Space_3.86_MB_(84.00%)
5+
6+
func totalNQueens(n int) int {
7+
row := make([]bool, n)
8+
col := make([]bool, n)
9+
diagonal := make([]bool, 2*n-1)
10+
antiDiagonal := make([]bool, 2*n-1)
11+
return totalNQueensHelper(n, 0, row, col, diagonal, antiDiagonal)
12+
}
13+
14+
func totalNQueensHelper(
15+
n, r int,
16+
row, col, diagonal, antiDiagonal []bool,
17+
) int {
18+
if r == n {
19+
return 1
20+
}
21+
count := 0
22+
for c := 0; c < n; c++ {
23+
if !row[r] && !col[c] && !diagonal[r+c] && !antiDiagonal[r-c+n-1] {
24+
row[r], col[c], diagonal[r+c], antiDiagonal[r-c+n-1] = true, true, true, true
25+
count += totalNQueensHelper(n, r+1, row, col, diagonal, antiDiagonal)
26+
row[r], col[c], diagonal[r+c], antiDiagonal[r-c+n-1] = false, false, false, false
27+
}
28+
}
29+
return count
30+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package s0052_n_queens_ii
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestTotalNQueens(t *testing.T) {
9+
assert.Equal(t, 2, totalNQueens(4))
10+
}
11+
12+
func TestTotalNQueens2(t *testing.T) {
13+
assert.Equal(t, 1, totalNQueens(1))
14+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
69\. Sqrt(x)
2+
3+
Easy
4+
5+
Given a non-negative integer `x`, compute and return _the square root of_ `x`.
6+
7+
Since the return type is an integer, the decimal digits are **truncated**, and only **the integer part** of the result is returned.
8+
9+
**Note:** You are not allowed to use any built-in exponent function or operator, such as `pow(x, 0.5)` or <code>x ** 0.5</code>.
10+
11+
**Example 1:**
12+
13+
**Input:** x = 4
14+
15+
**Output:** 2
16+
17+
**Example 2:**
18+
19+
**Input:** x = 8
20+
21+
**Output:** 2
22+
23+
**Explanation:** The square root of 8 is 2.82842..., and since the decimal part is truncated, 2 is returned.
24+
25+
**Constraints:**
26+
27+
* <code>0 <= x <= 2<sup>31</sup> - 1</code>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package s0069_sqrtx
2+
3+
// #Easy #Top_Interview_Questions #Math #Binary_Search #Binary_Search_I_Day_4
4+
// #Top_Interview_150_Math #2025_05_24_Time_0_ms_(100.00%)_Space_4.02_MB_(77.28%)
5+
6+
func mySqrt(x int) int {
7+
if x < 2 { // Handles x==0 and x==1 correctly
8+
return x
9+
}
10+
start := 1
11+
end := x / 2
12+
var sqrt int
13+
for start <= end {
14+
sqrt = start + (end-start)/2
15+
if sqrt == x/sqrt {
16+
return sqrt
17+
} else if sqrt > x/sqrt {
18+
end = sqrt - 1
19+
} else {
20+
start = sqrt + 1
21+
}
22+
}
23+
// sqrt will still contain the last value tried
24+
if sqrt > x/sqrt {
25+
return sqrt - 1
26+
}
27+
return sqrt
28+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package s0069_sqrtx
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestMySqrt(t *testing.T) {
9+
assert.Equal(t, 2, mySqrt(4))
10+
}
11+
12+
func TestMySqrt2(t *testing.T) {
13+
assert.Equal(t, 2, mySqrt(8))
14+
}

0 commit comments

Comments
 (0)