-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3416-SubsequencesWithAUniqueMiddleModeII.go
More file actions
135 lines (121 loc) · 4.84 KB
/
3416-SubsequencesWithAUniqueMiddleModeII.go
File metadata and controls
135 lines (121 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
// 3416. Subsequences with a Unique Middle Mode II
// Given an integer array nums, find the number of subsequences of size 5 of nums with a unique middle mode.
// Since the answer may be very large, return it modulo 10^9 + 7.
// A mode of a sequence of numbers is defined as the element that appears the maximum number of times in the sequence.
// A sequence of numbers contains a unique mode if it has only one mode.
// A sequence of numbers seq of size 5 contains a unique middle mode if the middle element (seq[2]) is a unique mode.
// Example 1:
// Input: nums = [1,1,1,1,1,1]
// Output: 6
// Explanation:
// [1, 1, 1, 1, 1] is the only subsequence of size 5 that can be formed from this list, and it has a unique middle mode of 1.
// Example 2:
// Input: nums = [1,2,2,3,3,4]
// Output: 4
// Explanation:
// [1, 2, 2, 3, 4] and [1, 2, 3, 3, 4] have unique middle modes because the number at index 2 has the greatest frequency in the subsequence. [1, 2, 2, 3, 3] does not have a unique middle mode because 2 and 3 both appear twice in the subsequence.
// Example 3:
// Input: nums = [0,1,2,3,4,5,6,7,8]
// Output: 0
// Explanation:
// There does not exist a subsequence of length 5 with a unique middle mode.
// Constraints:
// 5 <= nums.length <= 10^5
// -10^9 <= nums[i] <= 10^9
import "fmt"
// // 解答错误 834 / 843
// func subsequencesWithMiddleMode(nums []int) int {
// n, mod := len(nums), 1_000_000_007
// res := n * (n - 1) * (n - 2) * (n - 3) * (n - 4) / 120 // 所有方案数
// suf := map[int]int{}
// for _, num := range nums {
// suf[num]++
// }
// comb2 := func (num int) int { return num * (num - 1) / 2 }
// pre := make(map[int]int, len(suf)) // 预分配空间
// var cp, cs, ps, p2s, ps2 int
// for _, c := range suf {
// cs += comb2(c)
// }
// // 枚举 x,作为子序列正中间的数
// for left, x := range nums[:n-2] {
// suf[x]--
// px, sx := pre[x], suf[x]
// cs -= sx
// ps -= px
// p2s -= px * px
// ps2 -= (sx*2 + 1) * px
// right := n - 1 - left
// res -= (comb2(left-px) * comb2(right-sx)) % mod
// res -= ((cp - comb2(px)) * sx * (right - sx)) % mod
// res -= ((cs - comb2(sx)) * px * (left - px)) % mod
// res -= (((ps-px*sx)*(right-sx) - (ps2 - px*sx*sx)) * px) % mod
// res -= (((ps-px*sx)*(left-px) - (p2s - px*px*sx)) * sx) % mod
// cp += px
// ps += sx
// ps2 += sx * sx
// p2s += (px*2 + 1) * sx
// pre[x]++
// }
// return res % mod
// }
func subsequencesWithMiddleMode(nums []int) int {
n, mod := len(nums), 1_000_000_007
res := n * (n - 1) * (n - 2) % mod * (n - 3) % mod * (n - 4) % mod * 808333339 % mod
suf := make(map[int]int)
for _, num := range nums {
suf[num]++
}
comb2 := func(num int) int { return num * (num - 1) / 2 % mod }
pre := make(map[int]int, len(suf)) // 预分配空间
var cp, cs, ps, p2s, ps2 int
for _, c := range suf {
cs += comb2(c)
}
cs %= mod
// 枚举 x,作为子序列正中间的数
for left, x := range nums[:n-2] {
suf[x]--
px, sx := pre[x], suf[x]
cs -= sx
ps -= px
p2s -= px * px
ps2 -= (sx*2 + 1) * px
right := n - 1 - left
res -= comb2(left-px) * comb2(right-sx)
res -= (cp - comb2(px)) * sx % mod * (right - sx)
res -= (cs - comb2(sx)) * px % mod * (left - px)
res -= ((ps-px*sx)*(right-sx) - (ps2 - px*sx*sx)) % mod * px
res -= ((ps-px*sx)*(left-px) - (p2s - px*px*sx)) % mod * sx
res %= mod
cp += px
ps += sx
ps2 += sx * sx
p2s += (px*2 + 1) * sx
pre[x]++
}
return (res + mod) % mod
}
func main() {
// Example 1:
// Input: nums = [1,1,1,1,1,1]
// Output: 6
// Explanation:
// [1, 1, 1, 1, 1] is the only subsequence of size 5 that can be formed from this list, and it has a unique middle mode of 1.
fmt.Println(subsequencesWithMiddleMode([]int{1,1,1,1,1,1})) // 6
// Example 2:
// Input: nums = [1,2,2,3,3,4]
// Output: 4
// Explanation:
// [1, 2, 2, 3, 4] and [1, 2, 3, 3, 4] have unique middle modes because the number at index 2 has the greatest frequency in the subsequence. [1, 2, 2, 3, 3] does not have a unique middle mode because 2 and 3 both appear twice in the subsequence.
fmt.Println(subsequencesWithMiddleMode([]int{1,2,2,3,3,4})) // 4
// Example 3:
// Input: nums = [0,1,2,3,4,5,6,7,8]
// Output: 0
// Explanation:
// There does not exist a subsequence of length 5 with a unique middle mode.
fmt.Println(subsequencesWithMiddleMode([]int{0,1,2,3,4,5,6,7,8})) // 0
fmt.Println(subsequencesWithMiddleMode([]int{1,2,3,4,5,6,7,8,9})) // 0
fmt.Println(subsequencesWithMiddleMode([]int{9,8,7,6,5,4,3,2,1})) // 0
}