Skip to content

Commit

Permalink
Implemented 'Maximum Count of Positive Integer and Negative Integer' …
Browse files Browse the repository at this point in the history
…challenge
  • Loading branch information
wibosco committed Feb 25, 2024
1 parent ec08638 commit 2b4239e
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
8 changes: 8 additions & 0 deletions LeetCode/LeetCode.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,8 @@
43B737E82B8BDA4800C7F1FB /* TheKWeakestRowsInAMatrixTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43B737E72B8BDA4800C7F1FB /* TheKWeakestRowsInAMatrixTests.swift */; };
43B737EA2B8BE24800C7F1FB /* LongestSubsequenceWithLimitedSum.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43B737E92B8BE24800C7F1FB /* LongestSubsequenceWithLimitedSum.swift */; };
43B737EC2B8BE34B00C7F1FB /* LongestSubsequenceWithLimitedSumTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43B737EB2B8BE34B00C7F1FB /* LongestSubsequenceWithLimitedSumTests.swift */; };
43B737EE2B8BEB1C00C7F1FB /* MaximumCountOfPositiveIntegerAndNegativeInteger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43B737ED2B8BEB1C00C7F1FB /* MaximumCountOfPositiveIntegerAndNegativeInteger.swift */; };
43B737F02B8BEB5500C7F1FB /* MaximumCountOfPositiveIntegerAndNegativeIntegerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43B737EF2B8BEB5500C7F1FB /* MaximumCountOfPositiveIntegerAndNegativeIntegerTests.swift */; };
43BD457E2AFCCFAA006216F0 /* SumRootToLeafNumbers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43BD457D2AFCCFAA006216F0 /* SumRootToLeafNumbers.swift */; };
43BD45802AFCCFE2006216F0 /* SumRootToLeafNumbersTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43BD457F2AFCCFE1006216F0 /* SumRootToLeafNumbersTests.swift */; };
43BD45822AFD1C4B006216F0 /* FindModeInBinarySearchTree.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43BD45812AFD1C4B006216F0 /* FindModeInBinarySearchTree.swift */; };
Expand Down Expand Up @@ -1787,6 +1789,8 @@
43B737E72B8BDA4800C7F1FB /* TheKWeakestRowsInAMatrixTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TheKWeakestRowsInAMatrixTests.swift; sourceTree = "<group>"; };
43B737E92B8BE24800C7F1FB /* LongestSubsequenceWithLimitedSum.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LongestSubsequenceWithLimitedSum.swift; sourceTree = "<group>"; };
43B737EB2B8BE34B00C7F1FB /* LongestSubsequenceWithLimitedSumTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LongestSubsequenceWithLimitedSumTests.swift; sourceTree = "<group>"; };
43B737ED2B8BEB1C00C7F1FB /* MaximumCountOfPositiveIntegerAndNegativeInteger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MaximumCountOfPositiveIntegerAndNegativeInteger.swift; sourceTree = "<group>"; };
43B737EF2B8BEB5500C7F1FB /* MaximumCountOfPositiveIntegerAndNegativeIntegerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MaximumCountOfPositiveIntegerAndNegativeIntegerTests.swift; sourceTree = "<group>"; };
43BD457D2AFCCFAA006216F0 /* SumRootToLeafNumbers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SumRootToLeafNumbers.swift; sourceTree = "<group>"; };
43BD457F2AFCCFE1006216F0 /* SumRootToLeafNumbersTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SumRootToLeafNumbersTests.swift; sourceTree = "<group>"; };
43BD45812AFD1C4B006216F0 /* FindModeInBinarySearchTree.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FindModeInBinarySearchTree.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2166,6 +2170,7 @@
3D5C910627A7F7630035C399 /* Matrix01.swift */,
435F019A2A0BE2BC00F6B50F /* MaxAreaOfIsland.swift */,
43495F6A2B221D6A0037FEA7 /* MaximumAverageSubtree.swift */,
43B737ED2B8BEB1C00C7F1FB /* MaximumCountOfPositiveIntegerAndNegativeInteger.swift */,
3D5C90BA27A7F7620035C399 /* MaximumDepthBinaryTree.swift */,
435B70AF2B051FB7003D51CA /* MaximumDifferenceBetweenNodeAndAncestor.swift */,
431B84CD2A9AA3F600C647FD /* MaximumLengthOfPairChain.swift */,
Expand Down Expand Up @@ -2627,6 +2632,7 @@
3D5C921027A7F76B0035C399 /* Matrix01Tests.swift */,
435F019C2A0BE2F100F6B50F /* MaxAreaOfIslandTests.swift */,
43495F6C2B221F120037FEA7 /* MaximumAverageSubtreeTests.swift */,
43B737EF2B8BEB5500C7F1FB /* MaximumCountOfPositiveIntegerAndNegativeIntegerTests.swift */,
3D5C921E27A7F76B0035C399 /* MaximumDepthBinaryTreeTests.swift */,
435B70B12B0528F9003D51CA /* MaximumDifferenceBetweenNodeAndAncestorTests.swift */,
431B84CF2A9AA44E00C647FD /* MaximumLengthOfPairChainTests.swift */,
Expand Down Expand Up @@ -3159,6 +3165,7 @@
3D5C914927A7F7630035C399 /* ListNode.swift in Sources */,
437F39442AEDB4B9002275CF /* IslandPerimeter.swift in Sources */,
3D5C91AF27A7F7630035C399 /* ConvertSortedArrayToBinarySearchTree.swift in Sources */,
43B737EE2B8BEB1C00C7F1FB /* MaximumCountOfPositiveIntegerAndNegativeInteger.swift in Sources */,
3D5C91C827A7F7630035C399 /* ContinuousSubarraySum.swift in Sources */,
430094CE2B0283B000650F1E /* BinarySearchTreeToGreaterSumTree.swift in Sources */,
3D42A47527F62FEA00687F75 /* ProductOfArrayExceptSelf.swift in Sources */,
Expand Down Expand Up @@ -3449,6 +3456,7 @@
3D3C1ECF27FE357E004FB321 /* RemoveAllAdjacentDuplicatesInStringIITests.swift in Sources */,
435434852AF5550D002B996E /* RemoveCoveredIntervalsTests.swift in Sources */,
3D42A47727F6342C00687F75 /* ProductOfArrayExceptSelfTests.swift in Sources */,
43B737F02B8BEB5500C7F1FB /* MaximumCountOfPositiveIntegerAndNegativeIntegerTests.swift in Sources */,
3D5C92E727A7F76C0035C399 /* PowTests.swift in Sources */,
3D5C930427A7F76C0035C399 /* StringToIntegerTests.swift in Sources */,
430094D02B02842100650F1E /* BinarySearchTreeToGreaterSumTreeTests.swift in Sources */,
Expand Down
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
}
}
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)
}
}

0 comments on commit 2b4239e

Please sign in to comment.