Skip to content
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
31 changes: 31 additions & 0 deletions lcp/LCP 68. 美观的花束/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,37 @@ func beautifulBouquet(flowers []int, cnt int) (ans int) {
}
```

#### Swift

```swift
class Solution {
func beautifulBouquet(_ flowers: [Int], _ cnt: Int) -> Int {
let mod = Int(1e9 + 7)
var maxFlower = 0
for flower in flowers {
maxFlower = max(maxFlower, flower)
}

var flowerCount = [Int](repeating: 0, count: maxFlower + 1)
var ans = 0
var j = 0

for i in 0..<flowers.count {
flowerCount[flowers[i]] += 1

while flowerCount[flowers[i]] > cnt {
flowerCount[flowers[j]] -= 1
j += 1
}

ans = (ans + (i - j + 1)) % mod
}

return ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
26 changes: 26 additions & 0 deletions lcp/LCP 68. 美观的花束/Solution.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution {
func beautifulBouquet(_ flowers: [Int], _ cnt: Int) -> Int {
let mod = Int(1e9 + 7)
var maxFlower = 0
for flower in flowers {
maxFlower = max(maxFlower, flower)
}

var flowerCount = [Int](repeating: 0, count: maxFlower + 1)
var ans = 0
var j = 0

for i in 0..<flowers.count {
flowerCount[flowers[i]] += 1

while flowerCount[flowers[i]] > cnt {
flowerCount[flowers[j]] -= 1
j += 1
}

ans = (ans + (i - j + 1)) % mod
}

return ans
}
}