-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathMaximumCount.swift
More file actions
42 lines (39 loc) Β· 1.19 KB
/
Copy pathMaximumCount.swift
File metadata and controls
42 lines (39 loc) Β· 1.19 KB
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
//
// MaximumCount.swift
// Algorithm Solutions In Swift
//
// Created by littlebanana on 17/03/26.
//
func maximumCount(_ nums: [Int]) -> Int {
if nums[0] > 0 || nums[nums.count - 1] < 0 { return nums.count }
// get right most index for negative element
var negativeRightIndex = -1
var left = 0, right = nums.count - 1
while left <= right {
let mid = (left + right) / 2
if nums[mid] < 0 {
negativeRightIndex = mid
left = mid + 1
} else {
right = mid - 1
}
}
print(negativeRightIndex)
// get left most index for positive element
var positiveRightIndex = -1
left = negativeRightIndex + 1
right = nums.count - 1
while left < nums.count, left <= right {
let mid = (left + right) / 2
if nums[mid] > 0 {
positiveRightIndex = mid
right = mid - 1
} else {
left = mid + 1
}
}
print(positiveRightIndex)
let negativeElements = negativeRightIndex == -1 ? 0 : negativeRightIndex - 0 + 1
let positiveElements = positiveRightIndex == -1 ? 0 : nums.count - positiveRightIndex
return max(negativeElements, positiveElements)
}