Skip to content

Instantly share code, notes, and snippets.

@PttCodingMan
Created November 10, 2023 08:53
Show Gist options
  • Save PttCodingMan/af2dd4c15605a4bf4e7cd24ea55df324 to your computer and use it in GitHub Desktop.
Save PttCodingMan/af2dd4c15605a4bf4e7cd24ea55df324 to your computer and use it in GitHub Desktop.
反數列使用 binary search
import bisect
from typing import List
def reversePairs(nums: List[int]) -> int:
q = []
res = 0
for v in nums:
i = bisect.bisect_left(q, -v)
res += i
q[i:i] = [-v]
return res
if __name__ == '__main__':
data = [1, 2, 3, 5, 4]
print(reversePairs(data))
data = [1, 5, 3, 2, 4]
print(reversePairs(data))
data = [1, 4, 3, 2, 5]
print(reversePairs(data))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment