尧图网站设计 尧图网站设计YAOTU DESIGN
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; } };
返回列表