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:
parent
7166a19f10
commit
d2669c3687
1 changed files with 36 additions and 0 deletions
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue