作者DJYOSHITAKA (franchouchouISBEST)
標題Re: [閒聊] 每日leetcode
時間2025-06-25 22:44:20
唉
有想到binary search 但我還是偷看了一下答案
現在連寫binary search也要想半天
我又鬱鬱了
def kthSmallestProduct(self, nums1: List[int], nums2: List[int], k: int) ->
int:
def count(x):
# number of products which are less than or equal to x
total_cnt = 0
for num1 in nums1:
if num1>0:
total_cnt += bisect_right(nums2, x/num1)
elif num1==0:
total_cnt += len(nums2) if x>=0 else 0
else:
total_cnt += (len(nums2)-bisect_left(nums2, x/num1))
return total_cnt
l, r = -10**10, 10**10
while l<r:
mid = (l+r)//2
if count(mid)<k:
l = mid+1
else:
r = mid
return l
--
※ 發信站: 批踢踢實業坊(pttsite.org.tw), 來自: 220.132.58.28 (臺灣)
※ 文章網址: https://pttsite.org.tw/Marginalman/M.1750862662.A.E0A
推 sustainer123: 大師 06/25 22:51