Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 2670. Find the Distinct Difference Array #66

Merged
merged 1 commit into from
Jan 31, 2024
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
24 changes: 24 additions & 0 deletions leetcode/2670/2670. Find the Distinct Difference Array.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package _670

func distinctDifferenceArray(nums []int) []int {
n := len(nums)
diff := make([]int, n)
prefixSet := make(map[int]bool)
suffixSet := make(map[int]int)

// Initialize suffix set with counts
for _, num := range nums {
suffixSet[num]++
}

for i, num := range nums {
prefixSet[num] = true
suffixSet[num]--
if suffixSet[num] == 0 {
delete(suffixSet, num)
}

diff[i] = len(prefixSet) - len(suffixSet)
}
return diff
}
32 changes: 32 additions & 0 deletions leetcode/2670/2670. Find the Distinct Difference Array_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package _670

import (
"reflect"
"testing"
)

func Test_distinctDifferenceArray(t *testing.T) {
type args struct {
nums []int
}
tests := []struct {
name string
args args
want []int
}{
{
name: "one",
args: args{
nums: []int{1, 2, 3, 4, 5},
},
want: []int{-3, -1, 1, 3, 5},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := distinctDifferenceArray(tt.args.nums); !reflect.DeepEqual(got, tt.want) {
t.Errorf("distinctDifferenceArray() = %v, want %v", got, tt.want)
}
})
}
}
Loading