- Notifications
You must be signed in to change notification settings - Fork2
Added tasks 50, 52, 69, 149#66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
11 changes: 11 additions & 0 deletionsREADME.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletionssrc/main/python/g0001_0100/s0050_powx_n/Solution0050.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# #Medium #Top_Interview_Questions #Math #Recursion #Udemy_Integers #Top_Interview_150_Math | ||
# #2025_09_20_Time_0_ms_(100.00%)_Space_17.80_MB_(50.14%) | ||
class Solution: | ||
def myPow(self, x: float, n: int) -> float: | ||
nn = n | ||
res = 1.0 | ||
if n < 0: | ||
nn = -nn | ||
while nn > 0: | ||
if nn % 2 == 1: | ||
nn -= 1 | ||
res *= x | ||
else: | ||
x *= x | ||
nn //= 2 | ||
if n < 0: | ||
return 1.0 / res | ||
return res |
12 changes: 12 additions & 0 deletionssrc/main/python/g0001_0100/s0050_powx_n/Solution0050_test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import unittest | ||
from Solution0050 import Solution | ||
class SolutionTest(unittest.TestCase): | ||
def test_myPow(self): | ||
self.assertEqual(Solution().myPow(2.00000, 10), 1024.00000) | ||
def test_myPow2(self): | ||
self.assertEqual(Solution().myPow(2.10000, 3), 9.261000000000001) | ||
def test_myPow3(self): | ||
self.assertEqual(Solution().myPow(2.00000, -2), 0.25000) |
31 changes: 31 additions & 0 deletionssrc/main/python/g0001_0100/s0050_powx_n/readme.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
50\. Pow(x, n) | ||
Medium | ||
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>). | ||
**Example 1:** | ||
**Input:** x = 2.00000, n = 10 | ||
**Output:** 1024.00000 | ||
**Example 2:** | ||
**Input:** x = 2.10000, n = 3 | ||
**Output:** 9.26100 | ||
**Example 3:** | ||
**Input:** x = 2.00000, n = -2 | ||
**Output:** 0.25000 | ||
**Explanation:** 2<sup>\-2</sup> = 1/2<sup>2</sup> = 1/4 = 0.25 | ||
**Constraints:** | ||
* `-100.0 < x < 100.0` | ||
* <code>-2<sup>31</sup> <= n <= 2<sup>31</sup>-1</code> | ||
* <code>-10<sup>4</sup> <= x<sup>n</sup> <= 10<sup>4</sup></code> |
21 changes: 21 additions & 0 deletionssrc/main/python/g0001_0100/s0052_n_queens_ii/Solution0052.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# #Hard #Backtracking #Top_Interview_150_Backtracking | ||
# #2025_09_20_Time_3_ms_(96.02%)_Space_17.86_MB_(43.80%) | ||
class Solution: | ||
def totalNQueens(self, n: int) -> int: | ||
row = [False] * n | ||
col = [False] * n | ||
diagonal = [False] * (2 * n - 1) | ||
antiDiagonal = [False] * (2 * n - 1) | ||
return self._totalNQueens(n, 0, row, col, diagonal, antiDiagonal) | ||
def _totalNQueens(self, n, r, row, col, diagonal, antiDiagonal): | ||
if r == n: | ||
return 1 | ||
count = 0 | ||
for c in range(n): | ||
if not row[r] and not col[c] and not diagonal[r + c] and not antiDiagonal[r - c + n - 1]: | ||
row[r] = col[c] = diagonal[r + c] = antiDiagonal[r - c + n - 1] = True | ||
count += self._totalNQueens(n, r + 1, row, col, diagonal, antiDiagonal) | ||
row[r] = col[c] = diagonal[r + c] = antiDiagonal[r - c + n - 1] = False | ||
return count |
9 changes: 9 additions & 0 deletionssrc/main/python/g0001_0100/s0052_n_queens_ii/Solution0052_test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import unittest | ||
from Solution0052 import Solution | ||
class SolutionTest(unittest.TestCase): | ||
def test_totalNQueens(self): | ||
self.assertEqual(Solution().totalNQueens(4), 2) | ||
def test_totalNQueens2(self): | ||
self.assertEqual(Solution().totalNQueens(1), 1) |
27 changes: 27 additions & 0 deletionssrc/main/python/g0001_0100/s0052_n_queens_ii/readme.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
52\. N-Queens II | ||
Hard | ||
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. | ||
Given an integer `n`, return _the number of distinct solutions to the **n-queens puzzle**_. | ||
**Example 1:** | ||
 | ||
**Input:** n = 4 | ||
**Output:** 2 | ||
**Explanation:** There are two distinct solutions to the 4-queens puzzle as shown. | ||
**Example 2:** | ||
**Input:** n = 1 | ||
**Output:** 1 | ||
**Constraints:** | ||
* `1 <= n <= 9` |
17 changes: 17 additions & 0 deletionssrc/main/python/g0001_0100/s0069_sqrtx/Solution0069.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# #Easy #Top_Interview_Questions #Math #Binary_Search #Binary_Search_I_Day_4 | ||
# #Top_Interview_150_Math #2025_09_20_Time_0_ms_(100.00%)_Space_17.75_MB_(51.52%) | ||
class Solution: | ||
def mySqrt(self, x: int) -> int: | ||
if x == 0: | ||
return 0 | ||
left, right = 1, x | ||
while left <= right: | ||
mid = (left + right) // 2 | ||
if mid * mid == x: | ||
return mid | ||
elif mid * mid < x: | ||
left = mid + 1 | ||
else: | ||
right = mid - 1 | ||
return right |
9 changes: 9 additions & 0 deletionssrc/main/python/g0001_0100/s0069_sqrtx/Solution0069_test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import unittest | ||
from Solution0069 import Solution | ||
class SolutionTest(unittest.TestCase): | ||
def test_mySqrt(self): | ||
self.assertEqual(Solution().mySqrt(4), 2) | ||
def test_mySqrt2(self): | ||
self.assertEqual(Solution().mySqrt(8), 2) |
27 changes: 27 additions & 0 deletionssrc/main/python/g0001_0100/s0069_sqrtx/readme.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
69\. Sqrt(x) | ||
Easy | ||
Given a non-negative integer `x`, compute and return _the square root of_ `x`. | ||
Since the return type is an integer, the decimal digits are **truncated**, and only **the integer part** of the result is returned. | ||
**Note:** You are not allowed to use any built-in exponent function or operator, such as `pow(x, 0.5)` or `x ** 0.5`. | ||
**Example 1:** | ||
**Input:** x = 4 | ||
**Output:** 2 | ||
**Example 2:** | ||
**Input:** x = 8 | ||
**Output:** 2 | ||
**Explanation:** The square root of 8 is 2.82842..., and since the decimal part is truncated, 2 is returned. | ||
**Constraints:** | ||
* <code>0 <= x <= 2<sup>31</sup> - 1</code> |
26 changes: 26 additions & 0 deletionssrc/main/python/g0101_0200/s0149_max_points_on_a_line/Solution0149.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# #Hard #Top_Interview_Questions #Array #Hash_Table #Math #Geometry #Algorithm_II_Day_21_Others | ||
# #Top_Interview_150_Math #2025_09_20_Time_59_ms_(34.87%)_Space_17.88_MB_(73.72%) | ||
from typing import List | ||
class Solution: | ||
def maxPoints(self, points: List[List[int]]) -> int: | ||
result = min(len(points), 2) | ||
n = len(points) | ||
for i in range(n): | ||
hashmap = {} | ||
for j in range(n): | ||
x1, y1 = points[i] | ||
x2, y2 = points[j] | ||
if x2 - x1 == 0: | ||
m = float("inf") | ||
else: | ||
m = (y2 - y1) / (x2 - x1) | ||
if m in hashmap: | ||
hashmap[m].add(i) | ||
hashmap[m].add(j) | ||
else: | ||
hashmap[m] = set([i, j]) | ||
result = max(len(hashmap[m]), result) | ||
return result |
11 changes: 11 additions & 0 deletionssrc/main/python/g0101_0200/s0149_max_points_on_a_line/Solution0149_test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import unittest | ||
from Solution0149 import Solution | ||
class SolutionTest(unittest.TestCase): | ||
def test_maxPoints(self): | ||
input_points = [[1, 1], [2, 2], [3, 3]] | ||
self.assertEqual(Solution().maxPoints(input_points), 3) | ||
def test_maxPoints2(self): | ||
input_points = [[1, 1], [3, 2], [5, 3], [4, 1], [2, 3], [1, 4]] | ||
self.assertEqual(Solution().maxPoints(input_points), 4) |
28 changes: 28 additions & 0 deletionssrc/main/python/g0101_0200/s0149_max_points_on_a_line/readme.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
149\. Max Points on a Line | ||
Hard | ||
Given an array of `points` where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the **X-Y** plane, return _the maximum number of points that lie on the same straight line_. | ||
**Example 1:** | ||
 | ||
**Input:** points = [[1,1],[2,2],[3,3]] | ||
**Output:** 3 | ||
**Example 2:** | ||
 | ||
**Input:** points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]] | ||
**Output:** 4 | ||
**Constraints:** | ||
* `1 <= points.length <= 300` | ||
* `points[i].length == 2` | ||
* <code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code> | ||
* All the `points` are **unique**. |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.