Skip to content

Commit fdabedf

Browse files
authored
feat: 2670. Find the Distinct Difference Array (#66)
Signed-off-by: ashing <[email protected]>
1 parent 9b23c56 commit fdabedf

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package _670
2+
3+
func distinctDifferenceArray(nums []int) []int {
4+
n := len(nums)
5+
diff := make([]int, n)
6+
prefixSet := make(map[int]bool)
7+
suffixSet := make(map[int]int)
8+
9+
// Initialize suffix set with counts
10+
for _, num := range nums {
11+
suffixSet[num]++
12+
}
13+
14+
for i, num := range nums {
15+
prefixSet[num] = true
16+
suffixSet[num]--
17+
if suffixSet[num] == 0 {
18+
delete(suffixSet, num)
19+
}
20+
21+
diff[i] = len(prefixSet) - len(suffixSet)
22+
}
23+
return diff
24+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package _670
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func Test_distinctDifferenceArray(t *testing.T) {
9+
type args struct {
10+
nums []int
11+
}
12+
tests := []struct {
13+
name string
14+
args args
15+
want []int
16+
}{
17+
{
18+
name: "one",
19+
args: args{
20+
nums: []int{1, 2, 3, 4, 5},
21+
},
22+
want: []int{-3, -1, 1, 3, 5},
23+
},
24+
}
25+
for _, tt := range tests {
26+
t.Run(tt.name, func(t *testing.T) {
27+
if got := distinctDifferenceArray(tt.args.nums); !reflect.DeepEqual(got, tt.want) {
28+
t.Errorf("distinctDifferenceArray() = %v, want %v", got, tt.want)
29+
}
30+
})
31+
}
32+
}

0 commit comments

Comments
 (0)