十年匠心定制 · 商业建站与技术教学双线并行 咨询热线:400-886-1026 service@lmnt.cn
ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

leetcode 1769. Minimum Number of Operations to Move All Balls to Each Box

leetcode 1769. Minimum Number of Operations to Move All Balls to Each Box Problem: 1769. 移动所有球到每个盒子所需的最小操作数得到初始1的位置数组对每个box求出差的绝对值的累加和Codeclass Solution { public: vectorint minOperations(string boxes) { int n boxes.size(); vectorint tr; for(int i 0; i n; i) { if(boxes[i] 1) tr.push_back(i); } vectorint ret(n, 0); for(int i 0; i n; i) { int sum 0; for(int j : tr) { sum abs(i - j); } ret[i] sum; } return ret; } };
返回列表