Skip to content

Commit 3818e9e

Browse files
committed
Add 56. Merge Intervals
1 parent 9633de4 commit 3818e9e

File tree

3 files changed

+84
-6
lines changed

3 files changed

+84
-6
lines changed

README.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@ This repository contains my solutions to LeetCode problems in [Go](https://go.de
66
<img src="https://raw.githubusercontent.com/gist/brudnak/6c21505423e4ff089ab704ec79b5a096/raw/b2d3dec32474b2121b179920734b259323a7c250/go.gif" alt="Go" width="400"/>
77
</div>
88

9-
## Folder Structure 🗂️
10-
119
- `easy/`: Easy level problems.
1210
- `medium/`: Medium level problems.
1311
- `hard/`: Hard level problems.
1412

15-
## Unit Testing 🧪
13+
## Testing 🧪
1614

1715
In your root direction, run:
1816

@@ -32,7 +30,7 @@ Clean test cache
3230
go clean -testcache
3331
```
3432

35-
## Solutions (Continue Updating...)
33+
<!-- ## Solutions (Continue Updating...)
3634
3735
| Leetcode ID | Title & Solution | Coefficient Of Difficulty | Remarks | Approach |
3836
| :-----------------------------------------------------------------------------------------------------------------------------------: | :-------------------------------------------------------------------------------------------------------------------------- | :-----------------------: | :----------------------------------------------------------------------------------------------: | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: |
@@ -60,7 +58,7 @@ go clean -testcache
6058
| [680](https://leetcode.com/problems/valid-palindrome-ii/description/) | [Valid Palindrome II](/easy/680_valid_palindrome_II) | easy | _`Two Pointers`_ | Have a helper function which has the same structure as the main function. Use 2 pointers to check the +1 or -1 element whether they are equal or not |
6159
| [344](https://leetcode.com/problems/reverse-string/description/) | [Reverse String](/easy/344_reverse_string) | easy | _`Two Pointers`_ | |
6260
| [2843](https://leetcode.com/problems/count-symmetric-integers/description/) | [Count Symmetric Integers](/easy/2843_count_symmetric_integers) | easy | | |
63-
| [18](https://leetcode.com/problems/4sum/description/) | [4Sum](/medium/18_4sum) | medium | _`Two pointers`_ | Do Two Sum II and 3Sum first since 4Sum is the combination of those problems. There is an extra for loop. Make sure to check duplicates. |
61+
| [18](https://leetcode.com/problems/4sum/description/) | [4Sum](/medium/18_4sum) | medium | _`Two pointers`_ | Do Two Sum II and 3Sum first since 4Sum is the combination of those problems. There is an extra for loop. Make sure to check duplicates. | -->
6462

6563
## Workflow 🌊
6664

@@ -77,7 +75,7 @@ go clean -testcache
7775
- Update `README.md` with current stats
7876
- Automaticall commits and pushes the changes
7977

80-
## Statistics 📊
78+
## Stats
8179

8280
- Easy: 8 solutions
8381
- Medium: 17 solutions
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package merge_intervals
2+
3+
import (
4+
"sort"
5+
)
6+
7+
func merge(intervals [][]int) [][]int {
8+
n := len(intervals)
9+
res := [][]int{}
10+
11+
// Sort by starting index in each element
12+
sort.Slice(intervals, func(i, j int ) bool {
13+
return intervals[i][0] < intervals[j][0]
14+
})
15+
16+
i := 0
17+
for i < n {
18+
start := intervals[i][0]
19+
end := intervals[i][1]
20+
j := i + 1
21+
22+
for j < n && intervals[j][0] <= end {
23+
if end < intervals[j][1] {
24+
end = intervals[j][1]
25+
}
26+
j ++
27+
}
28+
29+
res = append(res, []int{start, end})
30+
i = j
31+
}
32+
33+
return res
34+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package merge_intervals
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
9+
func TestMerge(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
intervals [][]int
13+
expectedResult [][]int
14+
} {
15+
{
16+
name: "Test Case 1",
17+
intervals: [][]int{{1,3},{1,5},{6,7}},
18+
expectedResult: [][]int{{1,5},{6,7}},
19+
},
20+
{
21+
name: "Test Case 2",
22+
intervals: [][]int{{1,3},{2,6},{8,10},{15,18}},
23+
expectedResult: [][]int{{1,6},{8,10},{15,18}},
24+
},
25+
{
26+
name: "Test Case 3",
27+
intervals: [][]int{{1,4},{4,5}},
28+
expectedResult: [][]int{{1,5}},
29+
},
30+
{
31+
name: "Test Case 4 (edge case)",
32+
intervals: [][]int{},
33+
expectedResult: [][]int{},
34+
},
35+
}
36+
37+
for _, test := range tests {
38+
t.Run(test.name, func(t *testing.T) {
39+
got := merge(test.intervals)
40+
if !reflect.DeepEqual(got, test.expectedResult) {
41+
t.Errorf("Merge(%v) = %v; want = %v", test.intervals, got, test.expectedResult)
42+
}
43+
})
44+
45+
}
46+
}

0 commit comments

Comments
 (0)