Skip to content

Instantly share code, notes, and snippets.

@JCSooHwanCho
Last active December 27, 2022 10:27
Show Gist options
  • Save JCSooHwanCho/61ae53c5d7c486c175d8aea4aecad35c to your computer and use it in GitHub Desktop.
Save JCSooHwanCho/61ae53c5d7c486c175d8aea4aecad35c to your computer and use it in GitHub Desktop.
비내림차순 배열에서의 upperBound&lowerBound 구현
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