Created
November 10, 2023 08:53
-
-
Save PttCodingMan/af2dd4c15605a4bf4e7cd24ea55df324 to your computer and use it in GitHub Desktop.
反數列使用 binary search
This file contains hidden or 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
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