cs: add «1769. Minimum Number of Operations to Move All Balls to Each Box»

URL:	https://leetcode.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box/
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2025-01-06 09:28:50 +01:00
parent 7166a19f10
commit d2669c3687
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -0,0 +1,36 @@
public class Solution {
private struct Direction {
int Balls;
int Moves;
public Direction() {
Balls = 0;
Moves = 0;
}
public int Update(char state) {
var moves = Moves;
Balls += state - '0';
Moves += Balls;
return moves;
}
}
public int[] MinOperations(string boxes) {
var answer = new int[boxes.Length];
var (left, right) = (new Direction(), new Direction());
for (var i = 0; i < boxes.Length; ++i) {
// from left side
answer[i] += left.Update(boxes[i]);
// from right side
var j = boxes.Length - i - 1;
answer[j] += right.Update(boxes[j]);
}
return answer;
}
}