Skip to content

Commit b064cfe

Browse files
authored
Added tasks 222-392
1 parent 619d269 commit b064cfe

File tree

34 files changed

+1074
-0
lines changed

34 files changed

+1074
-0
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
222\. Count Complete Tree Nodes
2+
3+
Medium
4+
5+
Given the `root` of a **complete** binary tree, return the number of the nodes in the tree.
6+
7+
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`.
8+
9+
Design an algorithm that runs in less than `O(n)` time complexity.
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2021/01/14/complete.jpg)
14+
15+
**Input:** root = [1,2,3,4,5,6]
16+
17+
**Output:** 6
18+
19+
**Example 2:**
20+
21+
**Input:** root = []
22+
23+
**Output:** 0
24+
25+
**Example 3:**
26+
27+
**Input:** root = [1]
28+
29+
**Output:** 1
30+
31+
**Constraints:**
32+
33+
* The number of nodes in the tree is in the range <code>[0, 5 * 10<sup>4</sup>]</code>.
34+
* <code>0 <= Node.val <= 5 * 10<sup>4</sup></code>
35+
* The tree is guaranteed to be **complete**.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package s0222_count_complete_tree_nodes
2+
3+
// #Easy #Depth_First_Search #Tree #Binary_Search #Binary_Tree #Binary_Search_II_Day_10
4+
// #Top_Interview_150_Binary_Tree_General #2025_05_24_Time_0_ms_(100.00%)_Space_9.18_MB_(74.02%)
5+
6+
/**
7+
* Definition for a binary tree node.
8+
* type TreeNode struct {
9+
* Val int
10+
* Left *TreeNode
11+
* Right *TreeNode
12+
* }
13+
*/
14+
func countNodes(root *TreeNode) int {
15+
if root == nil {
16+
return 0
17+
}
18+
leftHeight := leftHeight(root)
19+
rightHeight := rightHeight(root)
20+
// case 1: When Height(Left sub-tree) = Height(right sub-tree) 2^h - 1
21+
if leftHeight == rightHeight {
22+
return (1 << leftHeight) - 1
23+
} else {
24+
return 1 + countNodes(root.Left) + countNodes(root.Right)
25+
}
26+
}
27+
28+
func leftHeight(root *TreeNode) int {
29+
if root == nil {
30+
return 0
31+
}
32+
return 1 + leftHeight(root.Left)
33+
}
34+
35+
func rightHeight(root *TreeNode) int {
36+
if root == nil {
37+
return 0
38+
}
39+
return 1 + rightHeight(root.Right)
40+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package s0222_count_complete_tree_nodes
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestCountNodes(t *testing.T) {
9+
root := &TreeNode{
10+
Val: 1,
11+
Left: &TreeNode{
12+
Val: 2,
13+
Left: &TreeNode{
14+
Val: 4,
15+
},
16+
Right: &TreeNode{
17+
Val: 5,
18+
},
19+
},
20+
Right: &TreeNode{
21+
Val: 3,
22+
Left: &TreeNode{
23+
Val: 6,
24+
},
25+
},
26+
}
27+
assert.Equal(t, 6, countNodes(root))
28+
}
29+
30+
func TestCountNodes2(t *testing.T) {
31+
root := &TreeNode{
32+
Val: 1,
33+
Left: &TreeNode{
34+
Val: 2,
35+
Left: &TreeNode{
36+
Val: 4,
37+
},
38+
Right: &TreeNode{
39+
Val: 5,
40+
},
41+
},
42+
Right: &TreeNode{
43+
Val: 3,
44+
},
45+
}
46+
assert.Equal(t, 5, countNodes(root))
47+
}
48+
49+
func TestCountNodes3(t *testing.T) {
50+
assert.Equal(t, 0, countNodes(nil))
51+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package s0222_count_complete_tree_nodes
2+
3+
type TreeNode struct {
4+
Val int
5+
Left *TreeNode
6+
Right *TreeNode
7+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
228\. Summary Ranges
2+
3+
Easy
4+
5+
You are given a **sorted unique** integer array `nums`.
6+
7+
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`.
8+
9+
Each range `[a,b]` in the list should be output as:
10+
11+
* `"a->b"` if `a != b`
12+
* `"a"` if `a == b`
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [0,1,2,4,5,7]
17+
18+
**Output:** ["0->2","4->5","7"]
19+
20+
**Explanation:** The ranges are: [0,2] --> "0->2" [4,5] --> "4->5" [7,7] --> "7"
21+
22+
**Example 2:**
23+
24+
**Input:** nums = [0,2,3,4,6,8,9]
25+
26+
**Output:** ["0","2->4","6","8->9"]
27+
28+
**Explanation:** The ranges are: [0,0] --> "0" [2,4] --> "2->4" [6,6] --> "6" [8,9] --> "8->9"
29+
30+
**Example 3:**
31+
32+
**Input:** nums = []
33+
34+
**Output:** []
35+
36+
**Example 4:**
37+
38+
**Input:** nums = [-1]
39+
40+
**Output:** ["-1"]
41+
42+
**Example 5:**
43+
44+
**Input:** nums = [0]
45+
46+
**Output:** ["0"]
47+
48+
**Constraints:**
49+
50+
* `0 <= nums.length <= 20`
51+
* <code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code>
52+
* All the values of `nums` are **unique**.
53+
* `nums` is sorted in ascending order.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package s0228_summary_ranges
2+
3+
// #Easy #Array #Top_Interview_150_Intervals #2025_05_24_Time_0_ms_(100.00%)_Space_3.94_MB_(33.05%)
4+
5+
import (
6+
"fmt"
7+
)
8+
9+
func summaryRanges(nums []int) []string {
10+
ranges := make([]string, 0)
11+
if len(nums) == 0 {
12+
return ranges
13+
}
14+
// size of array
15+
n := len(nums)
16+
// start of range
17+
a := nums[0]
18+
// end of range
19+
b := a
20+
for i := 1; i < n; i++ {
21+
// we need to make a decision if the next element
22+
// will expand the range
23+
// i starts at 1, not 0, because 1 is the next
24+
// candidate for expanding the range
25+
if nums[i] != b+1 {
26+
// only when our next element does not expand the range
27+
// do we add the range a->b to our list of ranges
28+
if a == b {
29+
ranges = append(ranges, fmt.Sprintf("%d", a))
30+
} else {
31+
ranges = append(ranges, fmt.Sprintf("%d->%d", a, b))
32+
}
33+
// since nums[i] is not accounted for by our range a->b
34+
// because nums[i] is not b+1, we need to set a and b
35+
// to this new range start point of bigger than b+1
36+
// maybe it is b+2? b+3? b+4? all we know is it is not b+1
37+
a = nums[i]
38+
b = a
39+
} else {
40+
// if the next element expands our range we do so
41+
b++
42+
}
43+
}
44+
// the only range that is not accounted for at this point is the last range
45+
// if our a and b are not equal then we add the range accordingly
46+
if a == b {
47+
ranges = append(ranges, fmt.Sprintf("%d", a))
48+
} else {
49+
ranges = append(ranges, fmt.Sprintf("%d->%d", a, b))
50+
}
51+
return ranges
52+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package s0228_summary_ranges
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestSummaryRanges(t *testing.T) {
9+
nums := []int{0, 1, 2, 4, 5, 7}
10+
expected := []string{"0->2", "4->5", "7"}
11+
assert.Equal(t, expected, summaryRanges(nums))
12+
}
13+
14+
func TestSummaryRanges2(t *testing.T) {
15+
nums := []int{0, 2, 3, 4, 6, 8, 9}
16+
expected := []string{"0", "2->4", "6", "8->9"}
17+
assert.Equal(t, expected, summaryRanges(nums))
18+
}
19+
20+
func TestSummaryRanges3(t *testing.T) {
21+
nums := []int{}
22+
expected := []string{}
23+
assert.Equal(t, expected, summaryRanges(nums))
24+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
242\. Valid Anagram
2+
3+
Easy
4+
5+
Given two strings `s` and `t`, return `true` _if_ `t` _is an anagram of_ `s`_, and_ `false` _otherwise_.
6+
7+
An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
8+
9+
**Example 1:**
10+
11+
**Input:** s = "anagram", t = "nagaram"
12+
13+
**Output:** true
14+
15+
**Example 2:**
16+
17+
**Input:** s = "rat", t = "car"
18+
19+
**Output:** false
20+
21+
**Constraints:**
22+
23+
* <code>1 <= s.length, t.length <= 5 * 10<sup>4</sup></code>
24+
* `s` and `t` consist of lowercase English letters.
25+
26+
**Follow up:** What if the inputs contain Unicode characters? How would you adapt your solution to such a case?
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package s0242_valid_anagram
2+
3+
// #Easy #String #Hash_Table #Sorting #Data_Structure_I_Day_6_String
4+
// #Programming_Skills_I_Day_11_Containers_and_Libraries #Udemy_Strings #Top_Interview_150_Hashmap
5+
// #2025_05_24_Time_0_ms_(100.00%)_Space_4.81_MB_(36.79%)
6+
7+
func isAnagram(s string, t string) bool {
8+
if len(s) != len(t) {
9+
return false
10+
}
11+
charFreqMap := make([]int, 26)
12+
for _, c := range s {
13+
charFreqMap[c-'a']++
14+
}
15+
for _, c := range t {
16+
if charFreqMap[c-'a'] == 0 {
17+
return false
18+
}
19+
charFreqMap[c-'a']--
20+
}
21+
return true
22+
}

0 commit comments

Comments
 (0)