Medium
You are given an integer array nums of length n and a binary string s of length n, where s[i] == '1' means index i initially contains a token and s[i] == '0' means it does not.
You may perform the following operation any number of times:
i, where i > 0, such that this token has not been moved before.i to index i - 1.An index is considered covered if it contains a token after all moves.
Return an integer denoting the maximum total value of nums at the covered indices after optimally performing the operations.
Example 1:
Input: nums = [9,2,6,1], s = “0101”
Output: 15
Explanation:
[0, 2], so the total value is nums[0] + nums[2] = 9 + 6 = 15.Example 2:
Input: nums = [5,1,4], s = “001”
Output: 4
Explanation:
[2], so the total value is nums[2] = 4.Example 3:
Input: nums = [9,3,5], s = “011”
Output: 14
Explanation:
[0, 2], so the total value is nums[0] + nums[2] = 9 + 5 = 14.Constraints:
1 <= n == nums.length == s.length <= 1051 <= nums[i] <= 105s[i] is either '0' or '1'public class Solution {
public long maxTotal(int[] nums, String s) {
int n = nums.length;
int mn = 100000;
long res = 0;
for (int i = n - 1; i >= 0; --i) {
res += nums[i];
mn = Math.min(mn, nums[i]);
if (s.charAt(i) == '0') {
res -= mn;
mn = 100000;
}
}
return res;
}
}