Re: [閒聊] 每日leetcode
看板Marginalman作者DJYOMIYAHINA (通通打死)時間10月前 (2025/02/09 16:10)推噓0(0推 0噓 0→)留言0則, 0人參與討論串1331/1552 (看更多)
改算good_pair數目
再用減的
一二三四五
def countBadPairs(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
good_pair_cnt = 0
cnt = defaultdict(int)
cnt[0-nums[0]] = 1
for i in range(1,len(nums)):
if i-nums[i] in cnt:
good_pair_cnt += cnt[i-nums[i]]
cnt[i-nums[i]] += 1
return len(nums)*(len(nums)-1)/2-good_pair_cnt
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.229.37.69 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1739088608.A.A80.html
討論串 (同標題文章)
完整討論串 (本文為第 1331 之 1552 篇):