From 5f7b8443ef45febfc2690f9a0a5cbfe3f4ca5040 Mon Sep 17 00:00:00 2001 From: ashing Date: Wed, 31 Jan 2024 12:26:21 +0800 Subject: [PATCH] feat: 2670. Find the Distinct Difference Array Signed-off-by: ashing --- ...670. Find the Distinct Difference Array.go | 24 ++++++++++++++ ...Find the Distinct Difference Array_test.go | 32 +++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 leetcode/2670/2670. Find the Distinct Difference Array.go create mode 100644 leetcode/2670/2670. Find the Distinct Difference Array_test.go diff --git a/leetcode/2670/2670. Find the Distinct Difference Array.go b/leetcode/2670/2670. Find the Distinct Difference Array.go new file mode 100644 index 0000000..f36c8bc --- /dev/null +++ b/leetcode/2670/2670. Find the Distinct Difference Array.go @@ -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 +} diff --git a/leetcode/2670/2670. Find the Distinct Difference Array_test.go b/leetcode/2670/2670. Find the Distinct Difference Array_test.go new file mode 100644 index 0000000..4385979 --- /dev/null +++ b/leetcode/2670/2670. Find the Distinct Difference Array_test.go @@ -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) + } + }) + } +}