非常感謝你閱讀本文~
歡迎【👍點贊】【?收藏】【📝評論】~
放棄不難,但堅持一定很酷~
希望我們大家都能每天進步一點點~
本文由 二當家的白帽子 https://le-yi.blog.csdn.net/ 博客原創~
文章目錄
- 1769. 移動所有球到每個盒子所需的最小運算元:
- 樣例 1
- 樣例 2
- 提示
- 分析
- 題解
- java
- c
- c++
- python
- go
- rust
- 原題傳送門:https://leetcode-cn.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box/
1769. 移動所有球到每個盒子所需的最小運算元:
有 n 個盒子,給你一個長度為 n 的二進制字串 boxes ,其中 boxes[i] 的值為 ‘0’ 表示第 i 個盒子是 空 的,而 boxes[i] 的值為 ‘1’ 表示盒子里有 一個 小球,
在一步操作中,你可以將 一個 小球從某個盒子移動到一個與之相鄰的盒子中,第 i 個盒子和第 j 個盒子相鄰需滿足 abs(i - j) == 1 ,注意,操作執行后,某些盒子中可能會存在不止一個小球,
回傳一個長度為 n 的陣列 answer ,其中 answer[i] 是將所有小球移動到第 i 個盒子所需的 最小 運算元,
每個 answer[i] 都需要根據盒子的 初始狀態 進行計算,
樣例 1
輸入:
boxes = "110"
輸出:
[1,1,3]
解釋:
每個盒子對應的最小運算元如下:
1) 第 1 個盒子:將一個小球從第 2 個盒子移動到第 1 個盒子,需要 1 步操作,
2) 第 2 個盒子:將一個小球從第 1 個盒子移動到第 2 個盒子,需要 1 步操作,
3) 第 3 個盒子:將一個小球從第 1 個盒子移動到第 3 個盒子,需要 2 步操作,將一個小球從第 2 個盒子移動到第 3 個盒子,需要 1 步操作,共計 3 步操作,
樣例 2
輸入:
boxes = "001011"
輸出:
[11,8,5,4,3,4]
提示
- n == boxes.length
- 1 <= n <= 2000
- boxes[i] 為 ‘0’ 或 ‘1’
分析
answer[i] 是將所有小球移動到第 i 個盒子所需的 最小 運算元,包含:
- 當前盒子的不用移動
- 左邊盒子的小球與當前盒子的距離之和
- 右邊盒子的小球與當前盒子的距離之和
乍看之下,每個位置都需要統計左邊和右邊的小球距離之和,O(n2)的節奏,但是如果我們能夠復用前一個位置的結果,那就可以降低時間復雜度,
題解
java
class Solution {
public int[] minOperations(String boxes) {
int[] ans = new int[boxes.length()];
//===從左向右移動===
// 左邊小球移動到當前位置的操作次數
int ops = 0;
// 左邊小球數量
int count = 0;
for (int i = 0; i < boxes.length(); ++i) {
// 每次移動,所有小球的操作次數都要加1,也就是操作次數需要增加小球的數量那么多次
ops += count;
ans[i] = ops;
if (boxes.charAt(i) == '1') {
count++;
}
}
//===從右左向移動===
// 右邊小球移動到當前位置的操作次數
ops = 0;
// 右邊小球數量
count = 0;
for (int i = boxes.length() - 1; i >= 0; --i) {
ops += count;
ans[i] += ops;
if (boxes.charAt(i) == '1') {
count++;
}
}
return ans;
}
}
c
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* minOperations(char * boxes, int* returnSize){
int len = strlen(boxes);
*returnSize = len;
int *ans = (int *) malloc(sizeof(int) * len);
//===從左向右移動===
// 左邊小球移動到當前位置的操作次數
int ops = 0;
// 左邊小球數量
int count = 0;
for (int i = 0; i < len; ++i) {
// 每次移動,所有小球的操作次數都要加1,也就是操作次數需要增加小球的數量那么多次
ops += count;
ans[i] = ops;
if (boxes[i] == '1') {
count++;
}
}
//===從右左向移動===
// 右邊小球移動到當前位置的操作次數
ops = 0;
// 右邊小球數量
count = 0;
for (int i = len - 1; i >= 0; --i) {
ops += count;
ans[i] += ops;
if (boxes[i] == '1') {
count++;
}
}
return ans;
}
c++
class Solution {
public:
vector<int> minOperations(string boxes) {
int n = boxes.length();
vector<int> ans;
//===從左向右移動===
// 左邊小球移動到當前位置的操作次數
int ops = 0;
// 左邊小球數量
int count = 0;
for (int i = 0; i < n; ++i) {
// 每次移動,所有小球的操作次數都要加1,也就是操作次數需要增加小球的數量那么多次
ops += count;
ans.push_back(ops);
if (boxes[i] == '1') {
count++;
}
}
//===從右左向移動===
// 右邊小球移動到當前位置的操作次數
ops = 0;
// 右邊小球數量
count = 0;
for (int i = n - 1; i >= 0; --i) {
ops += count;
ans[i] += ops;
if (boxes[i] == '1') {
count++;
}
}
return ans;
}
};
python
class Solution:
def minOperations(self, boxes: str) -> List[int]:
n = len(boxes)
ans = []
# === 從左向右移動 ===
# 左邊小球移動到當前位置的操作次數
ops = 0
# 左邊小球數量
count = 0
for i in range(n):
# 每次移動,所有小球的操作次數都要加1,也就是操作次數需要增加小球的數量那么多次
ops += count
ans.append(ops)
if boxes[i] == '1':
count += 1
# === 從右左向移動 ===
# 右邊小球移動到當前位置的操作次數
ops = 0
# 右邊小球數量
count = 0
for i in range(n - 1, -1, -1):
# 每次移動,所有小球的操作次數都要加1,也就是操作次數需要增加小球的數量那么多次
ops += count
ans[i] += ops
if boxes[i] == '1':
count += 1
return ans
go
func minOperations(boxes string) []int {
n := len(boxes)
ans := make([]int, n)
//===從左向右移動===
// 左邊小球移動到當前位置的操作次數
ops := 0
// 左邊小球數量
count := 0
for i, c := range boxes {
// 每次移動,所有小球的操作次數都要加1,也就是操作次數需要增加小球的數量那么多次
ops += count
ans[i] = ops
if c == '1' {
count++
}
}
//===從右左向移動===
// 右邊小球移動到當前位置的操作次數
ops = 0
// 右邊小球數量
count = 0
for i := n - 1; i >= 0; i-- {
// 每次移動,所有小球的操作次數都要加1,也就是操作次數需要增加小球的數量那么多次
ops += count
ans[i] += ops
if boxes[i] == '1' {
count++
}
}
return ans
}
rust
impl Solution {
pub fn min_operations(boxes: String) -> Vec<i32> {
let n = boxes.len();
let mut ans = vec![];
//===從左向右移動===
// 左邊小球移動到當前位置的操作次數
let mut ops = 0;
// 左邊小球數量
let mut count = 0;
boxes.chars().enumerate().for_each(|(i,c)|{
// 每次移動,所有小球的操作次數都要加1,也就是操作次數需要增加小球的數量那么多次
ops += count;
ans.push(ops);
if (c == '1') {
count += 1;
}
});
//===從右左向移動===
// 右邊小球移動到當前位置的操作次數
ops = 0;
// 右邊小球數量
count = 0;
boxes.chars().rev().enumerate().for_each(|(i,c)|{
// 每次移動,所有小球的操作次數都要加1,也就是操作次數需要增加小球的數量那么多次
ops += count;
ans[n - i - 1] += ops;
if (c == '1') {
count += 1;
}
});
ans
}
}

原題傳送門:https://leetcode-cn.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box/
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/325642.html
標籤:其他
上一篇:計算機原理以及進化
