-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented 'Maximum Count of Positive Integer and Negative Integer' …
…challenge
- Loading branch information
Showing
3 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
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
79 changes: 79 additions & 0 deletions
79
LeetCode/LeetCode/Challenges/MaximumCountOfPositiveIntegerAndNegativeInteger.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,79 @@ | ||
// | ||
// MaximumCountOfPositiveIntegerAndNegativeInteger.swift | ||
// LeetCode | ||
// | ||
// Created by William Boles on 25/02/2024. | ||
// | ||
|
||
import Foundation | ||
|
||
//https://leetcode.com/problems/maximum-count-of-positive-integer-and-negative-integer/ | ||
struct MaximumCountOfPositiveIntegerAndNegativeInteger { | ||
|
||
//Time: O(log n) where n is the number of elements in `nums` | ||
//Space: O(1) | ||
//array | ||
//binary search | ||
// | ||
//Solution Description: | ||
//As `nums` is already sorted we can use two binary searches to find the total number negative and positive elements. First we | ||
//use binary search to find the right-most negative element (if it exists) and return the count of negative elements. Then we | ||
//use binary search to find the left-most positive element (if it exists) and return the count of positive elements. Next we | ||
//take the maximum of those two values and return it. | ||
// | ||
//N.B. 0 does not count as negative or positive. | ||
func maximumCount(_ nums: [Int]) -> Int { | ||
let negativeCount = negativeElementsBinarySearch(nums) | ||
let positiveCount = positiveElementsBinarySearch(nums) | ||
|
||
return max(negativeCount, positiveCount) | ||
} | ||
|
||
private func negativeElementsBinarySearch(_ values: [Int]) -> Int { | ||
let target = -1 | ||
|
||
var left = 0 | ||
var right = values.count - 1 | ||
|
||
var result = -1 | ||
|
||
while left <= right { | ||
let mid = left + (right - left) / 2 //to avoid overflow | ||
|
||
if values[mid] <= target { | ||
left = mid + 1 | ||
result = mid | ||
} else { | ||
right = mid - 1 | ||
} | ||
} | ||
|
||
return result + 1 //return count of negative elements | ||
} | ||
|
||
private func positiveElementsBinarySearch(_ values: [Int]) -> Int { | ||
let target = 1 | ||
|
||
var left = 0 | ||
var right = values.count - 1 | ||
|
||
var result = -1 | ||
|
||
while left <= right { | ||
let mid = left + (right - left) / 2 //to avoid overflow | ||
|
||
if values[mid] >= target { | ||
right = mid - 1 | ||
result = mid | ||
} else { | ||
left = mid + 1 | ||
} | ||
} | ||
|
||
guard result != -1 else { | ||
return 0 | ||
} | ||
|
||
return values.count - result | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
LeetCode/LeetCodeTests/Tests/MaximumCountOfPositiveIntegerAndNegativeIntegerTests.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,39 @@ | ||
// | ||
// MaximumCountOfPositiveIntegerAndNegativeIntegerTests.swift | ||
// LeetCodeTests | ||
// | ||
// Created by William Boles on 25/02/2024. | ||
// | ||
|
||
import XCTest | ||
|
||
@testable import LeetCode | ||
|
||
final class MaximumCountOfPositiveIntegerAndNegativeIntegerTests: XCTestCase { | ||
|
||
//MARK: - Tests | ||
|
||
func test_A() { | ||
let nums = [-2,-1,-1,1,2,3] | ||
|
||
let result = MaximumCountOfPositiveIntegerAndNegativeInteger().maximumCount(nums) | ||
|
||
XCTAssertEqual(result, 3) | ||
} | ||
|
||
func test_B() { | ||
let nums = [-3,-2,-1,0,0,1,2] | ||
|
||
let result = MaximumCountOfPositiveIntegerAndNegativeInteger().maximumCount(nums) | ||
|
||
XCTAssertEqual(result, 3) | ||
} | ||
|
||
func test_C() { | ||
let nums = [5,20,66,1314] | ||
|
||
let result = MaximumCountOfPositiveIntegerAndNegativeInteger().maximumCount(nums) | ||
|
||
XCTAssertEqual(result, 4) | ||
} | ||
} |