Maximum Score After Binary Swaps

You are given an integer array nums of length n and a binary string s of the same length.

Initially, your score is 0. Each index i where s[i] = '1' contributes nums[i] to the score.

You may perform any number of operations, including zero. In one operation, you may choose an index i such that 0 <= i < n - 1, s[i] = '0', and s[i + 1] = '1', and swap these two characters.

Return an integer denoting the maximum possible score you can achieve.

Example 1
Inputnums = [2,1,5,2,3], s = "01010"
Output7
By swapping at indices 0 and 2, s becomes "10100", so positions 0 and 2 contribute 2 + 5 = 7, which is the maximum achievable score.
Example 2
Inputnums = [4,7,2,9], s = "0000"
Output0
There are no '1' characters in s, so no swaps can be performed and the score remains 0.

Constraints

  • n == nums.length == s.length
  • 1 <= n <= 10^5
  • 1 <= nums[i] <= 10^9
  • s[i] is either '0' or '1'

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate