3781. Maximum Score After Binary Swaps Medium

@problem@discussion
#Array#String#Greedy#Heap (Priority Queue)



1/**
2 * [3781] Maximum Score After Binary Swaps
3 *
4 * You are given an integer array nums of length n and a binary string s of the same length.
5 * Initially, your score is 0. Each index i where s[i] = '1' contributes nums[i] to the score.
6 * You may perform any number of operations (including zero). In one operation, you may choose an index i such that 0 <= i < n - 1, where s[i] = '0', and s[i + 1] = '1', and swap these two characters.
7 * Return an integer denoting the maximum possible score you can achieve.
8 *  
9 * <strong class="example">Example 1:
10 * <div class="example-block">
11 * Input: <span class="example-io">nums = [2,1,5,2,3], s = "01010"</span>
12 * Output: <span class="example-io">7</span>
13 * Explanation:
14 * We can perform the following swaps:
15 * 
16 * 	Swap at index i = 0: "01010" changes to "10010"
17 * 	Swap at index i = 2: "10010" changes to "10100"
18 * 
19 * Positions 0 and 2 contain '1', contributing nums[0] + nums[2] = 2 + 5 = 7. This is maximum score achievable.
20 * </div>
21 * <strong class="example">Example 2:
22 * <div class="example-block">
23 * Input: <span class="example-io">nums = [4,7,2,9], s = "0000"</span>
24 * Output: <span class="example-io">0</span>
25 * Explanation:
26 * There are no '1' characters in s, so no swaps can be performed. The score remains 0.
27 * </div>
28 *  
29 * Constraints:
30 * 
31 * 	n == nums.length == s.length
32 * 	1 <= n <= 10^5
33 * 	1 <= nums[i] <= 10^9
34 * 	s[i] is either '0' or '1'
35 * 
36 */
37pub struct Solution {}
38
39// problem: https://leetcode.com/problems/maximum-score-after-binary-swaps/
40// discuss: https://leetcode.com/problems/maximum-score-after-binary-swaps/discuss/?currentPage=1&orderBy=most_votes&query=
41
42// submission codes start here
43
44impl Solution {
45    pub fn maximum_score(nums: Vec<i32>, s: String) -> i64 {
46        
47    }
48}
49
50// submission codes end
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55
56    #[test]
57    fn test_3781() {
58    }
59}
60

Back
© 2026 bowen.ge All Rights Reserved.