-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Submit 1744_CanYouEatYourFavoriteCandyonYourFavoriteDay
- Loading branch information
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
...nd/Pages/1744_CanYouEatYourFavoriteCandyonYourFavoriteDay.xcplaygroundpage/Contents.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//: [Previous](@previous) | ||
|
||
import Foundation | ||
|
||
class Solution { | ||
func canEat(_ candiesCount: [Int], _ queries: [[Int]]) -> [Bool] { | ||
// 前缀和 | ||
let sums = candiesCount.reduce(into: [Int]()) { result, count in | ||
guard let lastSum = result.last else { | ||
result.append(count) | ||
return | ||
} | ||
result.append(lastSum + count) | ||
} | ||
|
||
let result = queries.map { query -> Bool in | ||
let (favoriteType, favoriteDay, dailyCap) = (query[0], query[1], query[2]) | ||
let x1 = favoriteDay + 1 | ||
let y1 = (favoriteDay + 1) * dailyCap | ||
let x2 = favoriteType == 0 ? 1 : sums[favoriteType - 1] + 1 | ||
let y2 = sums[favoriteType] | ||
|
||
return !(x1 > y2 || y1 < x2) | ||
} | ||
|
||
return result | ||
} | ||
} | ||
|
||
// Tests | ||
let solution = Solution() | ||
solution.canEat([7,4,5,3,8], [[0,2,2],[4,2,4],[2,13,1000000000]]) | ||
|
||
//: [Next](@next) |