<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實(shí)戰(zhàn)465:最優(yōu)賬單平衡

          共 3031字,需瀏覽 7分鐘

           ·

          2021-12-14 01:10

          算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎(chǔ)知識(shí)和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個(gè)公眾號(hào)后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

          今天和大家聊的問題叫做?最優(yōu)賬單平衡,我們先來看題面:
          https://leetcode-cn.com/problems/optimal-account-balancing/


          A group of friends went on holiday and sometimes lent each other money. For example, Alice paid for Bill’s lunch for $10. Then later Chris gave Alice $5 for a taxi ride. We can model each transaction as a tuple (x, y, z) which means person x gave person y $z. Assuming Alice, Bill, and Chris are person 0, 1, and 2 respectively (0, 1, 2 are the person’s ID), the transactions can be represented as [[0, 1, 10], [2, 0, 5]].
          Given a list of transactions between a group of people, return the minimum number of transactions required to settle the debt.
          Note:
          A transaction will be given as a tuple (x, y, z). Note that x ≠ y and z > 0.
          Person’s IDs may not be linear, e.g. we could have the persons 0, 1, 2 or we could also have the persons 0, 2, 6.


          一群朋友在度假期間會(huì)相互借錢。比如說,小愛同學(xué)支付了小新同學(xué)的午餐共計(jì) 10 美元。如果小明同學(xué)支付了小愛同學(xué)的出租車錢共計(jì) 5 美元。我們可以用一個(gè)三元組 (x, y, z) 表示一次交易,表示 x 借給 y 共計(jì) z 美元。用 0, 1, 2 表示小愛同學(xué)、小新同學(xué)和小明同學(xué)(0, 1, 2 為人的標(biāo)號(hào)),上述交易可以表示為 [[0, 1, 10], [2, 0, 5]]。
          給定一群人之間的交易信息列表,計(jì)算能夠還清所有債務(wù)的最小次數(shù)。

          注意:
          一次交易會(huì)以三元組 (x, y, z) 表示,并有 x ≠ y 且 z > 0。
          人的標(biāo)號(hào)可能不是按順序的,例如標(biāo)號(hào)可能為 0, 1, 2 也可能為 0, 2, 6。

          示例? ? ? ? ? ? ??

          示例 1

          輸入:
          [[0,1,10], [2,0,5]]
          輸出:
          2

          解釋:
          #0 給人 #1 共計(jì) 10 美元。
          #2 給人 #0 共計(jì) 5 美元。

          需要兩次交易。一種方式是人 #1 分別給人 #0 和人 #2 各 5 美元。

          示例 2

          輸入:
          [[0,1,10], [1,0,1], [1,2,5], [2,0,5]]
          輸出:
          1

          解釋:
          #0 給人 #1 共計(jì) 10 美元。Person #0 gave person #1 $10.
          #1 給人 #0 共計(jì) 1 美元。Person #1 gave person #0 $1.
          #1 給人 #2 共計(jì) 5 美元。Person #1 gave person #2 $5.
          #2 給人 #0 共計(jì) 5 美元。Person #2 gave person #0 $5.

          因此,人 #1 需要給人 #0 共計(jì) 4 美元,所有的債務(wù)即可還清。


          解題

          https://zhuanlan.zhihu.com/p/127316386

          解題思路:
          把整個(gè)借還錢過程,看成一個(gè)系統(tǒng),你從上帝視角看這些過程。
          一個(gè)人如果借出去和還出去錢相等,說明可以退出這個(gè)系統(tǒng),比如你借小明2元,小紅欠你2元,雖然是兩個(gè)過程,但是你在這個(gè)系統(tǒng),沒有導(dǎo)致自己的收入變多或者變少,上帝會(huì)幫你平衡這一切,你的退出不好影響系統(tǒng)。所以,我們可以計(jì)算出每個(gè)賬號(hào)上有多少錢,正負(fù)表示自己擁有的財(cái)產(chǎn)。
          本身就是NP難問題,暴力回溯解決問題
          直接看代碼,很容易理解的!

          class?Solution:
          ????def?minTransfers(self, transactions: List[List[int]])?-> int:
          ????????from?collections import?defaultdict
          ????????person = defaultdict(int)
          ????????for?x, y, z in?transactions:
          ????????????person[x] -= z
          ????????????person[y] += z
          ????????# 賬號(hào)
          ????????accounts = list(person.values())
          ???????
          ????????res = float("inf")

          ????????def?dfs(i, cnt):
          ????????????nonlocal?res
          ????????????# 全局變量退出遞歸
          ????????????if?cnt >= res: return?
          ????????????# 賬號(hào)為0不考慮
          ????????????while?i < len(accounts) and?accounts[i] == 0: i += 1
          ????????????# 遍歷完
          ????????????if?i == len(accounts):
          ????????????????res = min(res, cnt)
          ????????????????return
          ????????????for?j in?range(i + 1, len(accounts)):
          ????????????????if?accounts[i] * accounts[j] < 0:
          ????????????????????accounts[j] += accounts[i]
          ????????????????????dfs(i + 1, cnt + 1)
          ????????????????????accounts[j] -= accounts[i]

          ????????dfs(0, 0)
          ????????return?res


          好了,今天的文章就到這里,如果覺得有所收獲,請(qǐng)順手點(diǎn)個(gè)在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動(dòng)力 。

          上期推文:

          LeetCode1-460題匯總,希望對(duì)你有點(diǎn)幫助!

          LeetCode刷題實(shí)戰(zhàn)461:漢明距離

          LeetCode刷題實(shí)戰(zhàn)462:最少移動(dòng)次數(shù)使數(shù)組元素相等 II

          LeetCode刷題實(shí)戰(zhàn)463:島嶼的周長



          瀏覽 77
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  www.AV视频在线观看 | 亚洲AV成人精品日韩一区麻豆 | 久草com | 黄色日本网站 | 国内自拍网 |