|
44 | 44 |
|
45 | 45 | <!-- 这里可写通用的实现逻辑 -->
|
46 | 46 |
|
| 47 | +**方法一:双指针** |
| 48 | + |
| 49 | +我们用双指针 $j$ 和 $i$ 分别指向当前窗口的左右端点,用数组或哈希表 $d$ 记录当前窗口内的元素以及出现的次数。 |
| 50 | + |
| 51 | +遍历数组 $flowers$,每一次我们将 $flowers[i]$ 加入到窗口中,即 $d[flowers[i]]++$,然后判断 $d[flowers[i]]$ 是否大于 $cnt$,如果大于 $cnt$,则我们需要将 $flowers[j]$ 从窗口中移除,即 $d[flowers[j]]--$,并将 $j$ 右移,直到 $d[flowers[i]] \leq cnt$。此时窗口内的元素都不超过 $cnt$ 个,因此我们可以将 $i - j + 1$ 加到答案中。 |
| 52 | + |
| 53 | +最后返回答案即可。 |
| 54 | + |
| 55 | +时间复杂度 $O(n)$,空间复杂度 $O(m)$。其中 $n$ 和 $m$ 分别为数组 $flowers$ 的长度以及数组 $flowers$ 中的最大值。 |
| 56 | + |
47 | 57 | <!-- tabs:start -->
|
48 | 58 |
|
49 | 59 | ### **Python3**
|
50 | 60 |
|
51 | 61 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
52 | 62 |
|
53 | 63 | ```python
|
54 |
| - |
| 64 | +class Solution: |
| 65 | + def beautifulBouquet(self, flowers: List[int], cnt: int) -> int: |
| 66 | + mod = 10**9 + 7 |
| 67 | + d = Counter() |
| 68 | + ans = j = 0 |
| 69 | + for i, x in enumerate(flowers): |
| 70 | + d[x] += 1 |
| 71 | + while d[x] > cnt: |
| 72 | + d[flowers[j]] -= 1 |
| 73 | + j += 1 |
| 74 | + ans = (ans + i - j + 1) % mod |
| 75 | + return ans |
55 | 76 | ```
|
56 | 77 |
|
57 | 78 | ### **Java**
|
58 | 79 |
|
59 | 80 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
60 | 81 |
|
61 | 82 | ```java
|
| 83 | +class Solution { |
| 84 | + public int beautifulBouquet(int[] flowers, int cnt) { |
| 85 | + int mx = 0; |
| 86 | + for (int x : flowers) { |
| 87 | + mx = Math.max(mx, x); |
| 88 | + } |
| 89 | + int[] d = new int[mx + 1]; |
| 90 | + long ans = 0; |
| 91 | + final int mod = (int) 1e9 + 7; |
| 92 | + for (int i = 0, j = 0; i < flowers.length; ++i) { |
| 93 | + ++d[flowers[i]]; |
| 94 | + while (d[flowers[i]] > cnt) { |
| 95 | + --d[flowers[j++]]; |
| 96 | + } |
| 97 | + ans = (ans + i - j + 1) % mod; |
| 98 | + } |
| 99 | + return (int) ans; |
| 100 | + } |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +### **C++** |
| 105 | + |
| 106 | +```cpp |
| 107 | +class Solution { |
| 108 | +public: |
| 109 | + int beautifulBouquet(vector<int>& flowers, int cnt) { |
| 110 | + int mx = *max_element(flowers.begin(), flowers.end()); |
| 111 | + int d[mx + 1]; |
| 112 | + memset(d, 0, sizeof(d)); |
| 113 | + long long ans = 0; |
| 114 | + const int mod = 1e9 + 7; |
| 115 | + for (int i = 0, j = 0; i < flowers.size(); ++i) { |
| 116 | + ++d[flowers[i]]; |
| 117 | + while (d[flowers[i]] > cnt) { |
| 118 | + --d[flowers[j++]]; |
| 119 | + } |
| 120 | + ans = (ans + i - j + 1) % mod; |
| 121 | + } |
| 122 | + return ans; |
| 123 | + } |
| 124 | +}; |
| 125 | +``` |
62 | 126 |
|
| 127 | +### **Go** |
| 128 | +
|
| 129 | +```go |
| 130 | +func beautifulBouquet(flowers []int, cnt int) (ans int) { |
| 131 | + mx := 0 |
| 132 | + for _, x := range flowers { |
| 133 | + mx = max(mx, x) |
| 134 | + } |
| 135 | + d := make([]int, mx+1) |
| 136 | + j := 0 |
| 137 | + const mod = 1e9 + 7 |
| 138 | + for i, x := range flowers { |
| 139 | + d[x]++ |
| 140 | + for d[x] > cnt { |
| 141 | + d[flowers[j]]-- |
| 142 | + j++ |
| 143 | + } |
| 144 | + ans = (ans + i - j + 1) % mod |
| 145 | + } |
| 146 | + return |
| 147 | +} |
| 148 | +
|
| 149 | +func max(a, b int) int { |
| 150 | + if a > b { |
| 151 | + return a |
| 152 | + } |
| 153 | + return b |
| 154 | +} |
63 | 155 | ```
|
64 | 156 |
|
65 | 157 | ### **...**
|
|
0 commit comments