Last active
December 27, 2022 10:27
-
-
Save JCSooHwanCho/61ae53c5d7c486c175d8aea4aecad35c to your computer and use it in GitHub Desktop.
비내림차순 배열에서의 upperBound&lowerBound 구현
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
extension Array where Element: Comparable { | |
func lowerBound(of element: Element) -> Int { | |
var left = startIndex | |
var right = count | |
while left < right { | |
let mid = (left+right)/2 | |
if self[mid] >= element { | |
right = mid | |
} else { | |
left = mid+1 | |
} | |
} | |
return right | |
} | |
func upperBound(of element: Element) -> Int { | |
var left = startIndex | |
var right = count | |
while left < right { | |
let mid = (left+right)/2 | |
if self[mid] <= element { | |
left = mid+1 | |
} else { right = mid } | |
} | |
return right | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment