Last active
March 27, 2022 06:25
-
-
Save M-Quadra/f55f85a10a48e93119c3d79b6e893498 to your computer and use it in GitHub Desktop.
SourceKitService Bug
This file contains hidden or 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
| import Foundation | |
| class Solution { | |
| func numSubarrayBoundedMax(_ nums: [Int], _ left: Int, _ right: Int) -> Int { | |
| let arr = nums.map { v -> (Int, Int) in | |
| switch v { | |
| case ..<left: return (0, 0) | |
| case left...right: return (1, 0) | |
| default: return (0, 1) | |
| } | |
| }.reduce(into: [(0, 0)]) { arr, it in | |
| arr.append((it.0 + arr.last!.0, it.1 + arr.last!.1)) | |
| } | |
| return (1..<arr.count).reduce(0) { opt, i in | |
| opt + (arr[i...].lastIndex(where: { it in | |
| it.0>arr[i-1].0 && it.1==arr[i-1].1 | |
| }) ?? (i-1)) - i+1 | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment