Skip to content

Commit 0424dce

Browse files
authored
add the regular numbers problem (#116)
1 parent bd1953a commit 0424dce

File tree

4 files changed

+69
-4
lines changed

4 files changed

+69
-4
lines changed

README.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
Welcome to **Data Structures and Algorithms in Go**! 🎉 This project is designed as a dynamic, hands-on resource for learning and practicing data structures and algorithms in the Go programming language.
1010

11-
* At least six problems and solutions for each of the fourteen topics
12-
* Completely free, community-driven, and continuously evolving
13-
* Executes and comes with 100% test coverage, ensuring a high level of quality
14-
* Ability to use in your favorite IDE, editor, or web browser
11+
* More than one hundred rehearsal problems, at least six problems for each of the fifteen topics
12+
* Executable and comes with 100% test coverage, ensuring correctness and quality
13+
* Completely free, community-editable, and continuously evolving
14+
* Ability to study and practice in your favorite IDE, editor, or web browser
1515
* Pure Go, no third-party libraries, Guaranteed forward compatibility
1616

1717
## 📚 Table of Contents
@@ -75,6 +75,7 @@ Welcome to **Data Structures and Algorithms in Go**! 🎉 This project is design
7575
* [Kth Largest Element](./heap/kth_largest_element_test.go)
7676
* [Merge Sorted Lists](./heap/merge_sorted_list_test.go)
7777
* [Median in a Stream](./heap/median_in_a_stream_test.go)
78+
* [Regular Numbers](./heap/regular_numbers_test.go)
7879
* [Kth Closest Points to the Center](./heap/k_closest_points_to_origin_test.go)
7980
* [Sliding Maximum](./heap/sliding_maximum_test.go)
8081
* [Heap Sort](./heap/heap_sort_test.go)

heap/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,6 @@ Priority queues implemented as heaps are commonly used in job scheduling, for ex
9797
* [Kth Largest Element](./kth_largest_element_test.go), [Solution](./kth_largest_element.go)
9898
* [Merge Sorted Lists](./merge_sorted_list_test.go), [Solution](./merge_sorted_list.go)
9999
* [Median in a Stream](./median_in_a_stream_test.go), [Solution](./median_in_a_stream_test.go)
100+
* [Regular Numbers](./regular_numbers_test.go), [Solution](./regular_numbers_test.go)
100101
* [Kth Closest Points to the Center](./k_closest_points_to_origin_test.go), [Solution](./k_closest_points_to_origin.go)
101102
* [Sliding Maximum](./sliding_maximum_test.go), [Solution](./sliding_maximum.go)

heap/regular_numbers.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package heap
2+
3+
import "container/heap"
4+
5+
// RegularNumbers solves the problem in O(n*log n) time and O(n) space.
6+
func RegularNumbers(n int) []int {
7+
output := []int{}
8+
minHeap := &minHeap{}
9+
heap.Push(minHeap, 1)
10+
11+
last, count := 0, 0
12+
13+
for count < n {
14+
current := heap.Pop(minHeap).(int)
15+
if current > last {
16+
output = append(output, current)
17+
last = current
18+
count++
19+
heap.Push(minHeap, current*2)
20+
heap.Push(minHeap, current*3)
21+
heap.Push(minHeap, current*5)
22+
}
23+
}
24+
25+
return output
26+
}

heap/regular_numbers_test.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package heap
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
/*
9+
TestRegularNumbers tests solution(s) with the following signature and problem description:
10+
11+
func RegularNumbers(n int) []int
12+
13+
Regular numbers are numbers whose only prime divisors are 2, 3, and 5.
14+
For example 24 is a regular number because it can be factored into 2^3 * 3^1 * 5^0.
15+
Given a positive integer n, write a function that returns the first n regular numbers.
16+
*/
17+
func TestRegularNumbers(t *testing.T) {
18+
tests := []struct {
19+
n int
20+
regularNumbers []int
21+
}{
22+
{0, []int{}},
23+
{1, []int{1}},
24+
{2, []int{1, 2}},
25+
{3, []int{1, 2, 3}},
26+
{4, []int{1, 2, 3, 4}},
27+
{5, []int{1, 2, 3, 4, 5}},
28+
{10, []int{1, 2, 3, 4, 5, 6, 8, 9, 10, 12}},
29+
{15, []int{1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24}},
30+
}
31+
32+
for i, test := range tests {
33+
if got := RegularNumbers(test.n); !reflect.DeepEqual(got, test.regularNumbers) {
34+
t.Fatalf("Failed test case #%d. Want %v got %d", i, test.regularNumbers, got)
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)