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
41 changes: 41 additions & 0 deletions lcp/LCP 08. 剧情触发时间/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,47 @@ class Solution {
}
```

#### Swift

```swift
class Solution {
func getTriggerTime(_ increase: [[Int]], _ requirements: [[Int]]) -> [Int] {
let m = increase.count, n = requirements.count
var s = Array(repeating: [0, 0, 0], count: m + 1)

for i in 0..<m {
for j in 0..<3 {
s[i + 1][j] = s[i][j] + increase[i][j]
}
}

var ans = Array(repeating: -1, count: n)
for i in 0..<n {
var left = 0, right = m + 1
while left < right {
let mid = (left + right) / 2
if check(s[mid], requirements[i]) {
ans[i] = mid
right = mid
} else {
left = mid + 1
}
}
}
return ans
}

private func check(_ a: [Int], _ b: [Int]) -> Bool {
for i in 0..<3 {
if a[i] < b[i] {
return false
}
}
return true
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
36 changes: 36 additions & 0 deletions lcp/LCP 08. 剧情触发时间/Solution.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class Solution {
func getTriggerTime(_ increase: [[Int]], _ requirements: [[Int]]) -> [Int] {
let m = increase.count, n = requirements.count
var s = Array(repeating: [0, 0, 0], count: m + 1)

for i in 0..<m {
for j in 0..<3 {
s[i + 1][j] = s[i][j] + increase[i][j]
}
}

var ans = Array(repeating: -1, count: n)
for i in 0..<n {
var left = 0, right = m + 1
while left < right {
let mid = (left + right) / 2
if check(s[mid], requirements[i]) {
ans[i] = mid
right = mid
} else {
left = mid + 1
}
}
}
return ans
}

private func check(_ a: [Int], _ b: [Int]) -> Bool {
for i in 0..<3 {
if a[i] < b[i] {
return false
}
}
return true
}
}
Loading