Re: [閒聊] 每日leetcode

看板Marginalman作者 (dont)時間11月前 (2025/01/06 21:02), 編輯推噓2(200)
留言2則, 2人參與, 11月前最新討論串1248/1554 (看更多)
1769. Minimum Number of Operations to Move All Balls to Each Box ## 思路 分左右兩次計算 每次移動1個index的移動次數 會是原本次數加上球的個數 ## Code ```cpp class Solution { public: vector<int> minOperations(string boxes) { int n = boxes.size(); vector<int> res(n, 0); int curr_sum=0, curr_ball=0; for (int i=0; i<n; ++i) { res[i] = curr_sum; curr_ball += boxes[i] == '1'; curr_sum += curr_ball; } curr_sum=0, curr_ball=0; for (int i=n-1; i>=0; --i) { res[i] += curr_sum; curr_ball += boxes[i] == '1'; curr_sum += curr_ball; } return res; } }; ``` -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 203.10.99.172 (日本) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1736168563.A.4B1.html

01/06 21:03, 11月前 , 1F
大師 我寫超醜
01/06 21:03, 1F

01/06 21:04, 11月前 , 2F
大師 肥肥O(n)想超久才寫出來
01/06 21:04, 2F
文章代碼(AID): #1dUzHpIn (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1dUzHpIn (Marginalman)