-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
278 First Bad Version.swift
45 lines (36 loc) 路 971 Bytes
/
278 First Bad Version.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//
// 278 First Bad Version.swift
// LeetCode-Solutions
//
// Created by Aleksandar Dinic on 30/01/2021.
// Copyright 漏 2021 Aleksandar Dinic. All rights reserved.
//
import Foundation
/// Source: https://leetcode.com/problems/first-bad-version/
class Solution {
/// Finds the first bad version.
///
/// - Parameter n: The number of versions.
/// - Returns: First bad version.
///
/// - Complexity:
/// - time: O(log n), where n is the given `n`.
/// - space: O(1), only constant space is used.
func firstBadVersion(_ n: Int) -> Int {
var start = 1
var end = n
while start < end {
let mid = start + (end - start) / 2
if isBadVersion(mid) {
end = mid
} else {
start = mid + 1
}
}
return start
}
}
/// Provided code
class VersionControl {
func isBadVersion(_ version: Int) -> Bool
}