Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions internal/leetcode/problems_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ var problemStatus = map[int]bool{
237: true,
242: true,
258: true,
263: true,
264: true,
268: true,
283: true,
290: true,
Expand Down
18 changes: 18 additions & 0 deletions problems/ugly-number-ii/ugly_number_ii.go
Original file line number Diff line number Diff line change
@@ -1 +1,19 @@
package problem264

func nthUglyNumber(n int) int {
ans, idx := make([]int, n), [3]int{}
ans[0] = 1
for i := 1; i < n; i++ {
for j, n := range [...]int{2, 3, 5} {
if ans[idx[j]]*n <= ans[i-1] {
idx[j]++
}
if num := ans[idx[j]] * n; j == 0 {
ans[i] = num
} else if ans[i] > num {
ans[i] = num
}
}
}
return ans[n-1]
}
38 changes: 38 additions & 0 deletions problems/ugly-number-ii/ugly_number_ii_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,39 @@
package problem264

import "testing"

type caseType struct {
input int
expected int
}

func TestNthUglyNumber(t *testing.T) {
tests := [...]caseType{
{
input: 10,
expected: 12,
},
{
input: 9,
expected: 10,
},
{
input: 60,
expected: 384,
},
{
input: 90,
expected: 1152,
},
{
input: 1,
expected: 1,
},
}
for _, tc := range tests {
output := nthUglyNumber(tc.input)
if output != tc.expected {
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
}
}
}