- Notifications
You must be signed in to change notification settings - Fork2
Added tasks 222-289#64
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
14 changes: 14 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
32 changes: 32 additions & 0 deletionssrc/main/python/g0201_0300/s0222_count_complete_tree_nodes/Solution0222.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,32 @@ | ||
# #Easy #Depth_First_Search #Tree #Binary_Search #Binary_Tree #Binary_Search_II_Day_10 | ||
# #Top_Interview_150_Binary_Tree_General #2025_09_17_Time_0_ms_(100.00%)_Space_23.29_MB_(82.07%) | ||
from typing import Optional | ||
class TreeNode: | ||
def __init__(self, val: int = 0, left: Optional["TreeNode"] = None, right: Optional["TreeNode"] = None): | ||
self.val = val | ||
self.left = left | ||
self.right = right | ||
class Solution: | ||
def countNodes(self, root: Optional[TreeNode]) -> int: | ||
if root is None: | ||
return 0 | ||
left_height = self._left_height(root) | ||
right_height = self._right_height(root) | ||
if left_height == right_height: | ||
return (1 << left_height) - 1 | ||
return 1 + self.countNodes(root.left) + self.countNodes(root.right) | ||
def _left_height(self, node: Optional[TreeNode]) -> int: | ||
if node is None: | ||
return 0 | ||
return 1 + self._left_height(node.left) | ||
def _right_height(self, node: Optional[TreeNode]) -> int: | ||
if node is None: | ||
return 0 | ||
return 1 + self._right_height(node.right) | ||
18 changes: 18 additions & 0 deletionssrc/main/python/g0201_0300/s0222_count_complete_tree_nodes/Solution0222_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,18 @@ | ||
import unittest | ||
from Solution0222 import Solution, TreeNode | ||
class SolutionTest(unittest.TestCase): | ||
def test_countNodes(self): | ||
left_left = TreeNode(4) | ||
left_right = TreeNode(5) | ||
left = TreeNode(2, left_left, left_right) | ||
right_left = TreeNode(6) | ||
right = TreeNode(3, right_left, None) | ||
root = TreeNode(1, left, right) | ||
self.assertEqual(Solution().countNodes(root), 6) | ||
def test_countNodes_null(self): | ||
self.assertEqual(Solution().countNodes(None), 0) | ||
def test_countNodes_single(self): | ||
self.assertEqual(Solution().countNodes(TreeNode(1)), 1) |
35 changes: 35 additions & 0 deletionssrc/main/python/g0201_0300/s0222_count_complete_tree_nodes/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,35 @@ | ||
222\. Count Complete Tree Nodes | ||
Medium | ||
Given the `root` of a **complete** binary tree, return the number of the nodes in the tree. | ||
According to **[Wikipedia](http://en.wikipedia.org/wiki/Binary_tree#Types_of_binary_trees)**, every level, except possibly the last, is completely filled in a complete binary tree, and all nodes in the last level are as far left as possible. It can have between `1` and <code>2<sup>h</sup></code> nodes inclusive at the last level `h`. | ||
Design an algorithm that runs in less than `O(n)` time complexity. | ||
**Example 1:** | ||
 | ||
**Input:** root = [1,2,3,4,5,6] | ||
**Output:** 6 | ||
**Example 2:** | ||
**Input:** root = [] | ||
**Output:** 0 | ||
**Example 3:** | ||
**Input:** root = [1] | ||
**Output:** 1 | ||
**Constraints:** | ||
* The number of nodes in the tree is in the range <code>[0, 5 * 10<sup>4</sup>]</code>. | ||
* <code>0 <= Node.val <= 5 * 10<sup>4</sup></code> | ||
* The tree is guaranteed to be **complete**. |
26 changes: 26 additions & 0 deletionssrc/main/python/g0201_0300/s0228_summary_ranges/Solution0228.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 @@ | ||
# #Easy #Array #Top_Interview_150_Intervals #2025_09_17_Time_0_ms_(100.00%)_Space_17.69_MB_(84.69%) | ||
from typing import List | ||
class Solution: | ||
def summaryRanges(self, nums: List[int]) -> List[str]: | ||
ranges: List[str] = [] | ||
if not nums: | ||
return ranges | ||
a = nums[0] | ||
b = a | ||
for i in range(1, len(nums)): | ||
if nums[i] != b + 1: | ||
if a == b: | ||
ranges.append(str(a)) | ||
else: | ||
ranges.append(f"{a}->{b}") | ||
a = nums[i] | ||
b = a | ||
else: | ||
b += 1 | ||
if a == b: | ||
ranges.append(str(a)) | ||
else: | ||
ranges.append(f"{a}->{b}") | ||
return ranges |
15 changes: 15 additions & 0 deletionssrc/main/python/g0201_0300/s0228_summary_ranges/Solution0228_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,15 @@ | ||
import unittest | ||
from Solution0228 import Solution | ||
class SolutionTest(unittest.TestCase): | ||
def test_summaryRanges(self): | ||
self.assertEqual( | ||
Solution().summaryRanges([0, 1, 2, 4, 5, 7]), | ||
["0->2", "4->5", "7"], | ||
) | ||
def test_summaryRanges2(self): | ||
self.assertEqual( | ||
Solution().summaryRanges([0, 2, 3, 4, 6, 8, 9]), | ||
["0", "2->4", "6", "8->9"], | ||
) |
53 changes: 53 additions & 0 deletionssrc/main/python/g0201_0300/s0228_summary_ranges/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,53 @@ | ||
228\. Summary Ranges | ||
Easy | ||
You are given a **sorted unique** integer array `nums`. | ||
Return _the **smallest sorted** list of ranges that **cover all the numbers in the array exactly**_. That is, each element of `nums` is covered by exactly one of the ranges, and there is no integer `x` such that `x` is in one of the ranges but not in `nums`. | ||
Each range `[a,b]` in the list should be output as: | ||
* `"a->b"` if `a != b` | ||
* `"a"` if `a == b` | ||
**Example 1:** | ||
**Input:** nums = [0,1,2,4,5,7] | ||
**Output:** ["0->2","4->5","7"] | ||
**Explanation:** The ranges are: [0,2] --> "0->2" [4,5] --> "4->5" [7,7] --> "7" | ||
**Example 2:** | ||
**Input:** nums = [0,2,3,4,6,8,9] | ||
**Output:** ["0","2->4","6","8->9"] | ||
**Explanation:** The ranges are: [0,0] --> "0" [2,4] --> "2->4" [6,6] --> "6" [8,9] --> "8->9" | ||
**Example 3:** | ||
**Input:** nums = [] | ||
**Output:** [] | ||
**Example 4:** | ||
**Input:** nums = [-1] | ||
**Output:** ["-1"] | ||
**Example 5:** | ||
**Input:** nums = [0] | ||
**Output:** ["0"] | ||
**Constraints:** | ||
* `0 <= nums.length <= 20` | ||
* <code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code> | ||
* All the values of `nums` are **unique**. | ||
* `nums` is sorted in ascending order. |
17 changes: 17 additions & 0 deletionssrc/main/python/g0201_0300/s0242_valid_anagram/Solution0242.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 #String #Hash_Table #Sorting #Data_Structure_I_Day_6_String | ||
# #Programming_Skills_I_Day_11_Containers_and_Libraries #Udemy_Strings #Top_Interview_150_Hashmap | ||
# #2025_09_17_Time_11_ms_(72.46%)_Space_17.66_MB_(97.60%) | ||
class Solution: | ||
def isAnagram(self, s: str, t: str) -> bool: | ||
if len(s) != len(t): | ||
return False | ||
freq = [0] * 26 | ||
for ch in s: | ||
freq[ord(ch) - 97] += 1 | ||
for ch in t: | ||
idx = ord(ch) - 97 | ||
if freq[idx] == 0: | ||
return False | ||
freq[idx] -= 1 | ||
return True |
9 changes: 9 additions & 0 deletionssrc/main/python/g0201_0300/s0242_valid_anagram/Solution0242_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 Solution0242 import Solution | ||
class SolutionTest(unittest.TestCase): | ||
def test_isAnagram(self): | ||
self.assertTrue(Solution().isAnagram("anagram", "nagaram")) | ||
def test_isAnagram2(self): | ||
self.assertFalse(Solution().isAnagram("rat", "car")) |
24 changes: 24 additions & 0 deletionssrc/main/python/g0201_0300/s0242_valid_anagram/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,24 @@ | ||
242\. Valid Anagram | ||
Easy | ||
Given two strings `s` and `t`, return `true` _if_ `t` _is an anagram of_ `s`_, and_ `false` _otherwise_. | ||
**Example 1:** | ||
**Input:** s = "anagram", t = "nagaram" | ||
**Output:** true | ||
**Example 2:** | ||
**Input:** s = "rat", t = "car" | ||
**Output:** false | ||
**Constraints:** | ||
* <code>1 <= s.length, t.length <= 5 * 10<sup>4</sup></code> | ||
* `s` and `t` consist of lowercase English letters. | ||
**Follow up:** What if the inputs contain Unicode characters? How would you adapt your solution to such a case? |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
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.