-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #784 from vaskoz/day390
Day390
- Loading branch information
Showing
3 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,22 @@ | ||
package day390 | ||
|
||
// MissingThousandInMillion returns the numbers missing | ||
// between 1 and 1 million. | ||
// Runs in O(N) time and space. | ||
func MissingThousandInMillion(nums []int) []int { | ||
seen := make(map[int]struct{}, len(nums)) | ||
|
||
for _, n := range nums { | ||
seen[n] = struct{}{} | ||
} | ||
|
||
ans := make([]int, 0, 1_000_000-len(seen)+1) | ||
|
||
for n := 1; n <= 1_000_000; n++ { | ||
if _, ok := seen[n]; !ok { | ||
ans = append(ans, n) | ||
} | ||
} | ||
|
||
return ans | ||
} |
This file contains 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 @@ | ||
package day390 | ||
|
||
import ( | ||
"math/rand" | ||
"reflect" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestMissingThousandInMillion(t *testing.T) { | ||
t.Parallel() | ||
|
||
missing := []int{10_395, 25_011, 345_335, 565_332, 999_993} | ||
input := setup(missing) | ||
|
||
if res := MissingThousandInMillion(input); !reflect.DeepEqual(res, missing) { | ||
t.Errorf("Expected %v, got %v", missing, res) | ||
} | ||
} | ||
|
||
func BenchmarkMissingThousandInMillion(b *testing.B) { | ||
missing := []int{25_011, 345_335, 565_332, 999_993, 10_395} | ||
input := setup(missing) | ||
|
||
for i := 0; i < b.N; i++ { | ||
MissingThousandInMillion(input) | ||
} | ||
} | ||
|
||
func setup(miss []int) []int { | ||
r := rand.New(rand.NewSource(time.Now().UnixNano())) | ||
ans := make([]int, 1_000_000-len(miss)+1) | ||
missMap := make(map[int]struct{}, len(miss)) | ||
|
||
for _, n := range miss { | ||
missMap[n] = struct{}{} | ||
} | ||
|
||
pos := 0 | ||
|
||
for n := 1; n <= 1_000_000; n++ { | ||
if _, skip := missMap[n]; !skip { | ||
ans[pos] = n | ||
pos++ | ||
} | ||
} | ||
|
||
r.Shuffle(len(ans), func(i, j int) { | ||
ans[i], ans[j] = ans[j], ans[i] | ||
}) | ||
|
||
return ans | ||
} |