Problem · String

Maximize Consecutive Dance Moves 🩰

Learn this problem
MediumTiktokOA
See Tiktok hiring insights

Problem statement

A TikTok participant has a binary string s representing a sequence of dance moves. A 0 is a pause and a 1 is a dance move.

The participant must choose exactly k distinct positions and flip each selected character:

  • 0 becomes 1;
  • 1 becomes 0.

Return the maximum possible number of adjacent 11 pairs after the flips. In other words, count the indices i with 0 <= i < s.length - 1 such that both s[i] and s[i + 1] are 1 in the final string.

Function

maximizeConsecutiveDanceMoves(s: String, k: int) → int

Examples

Example 1

s = "01010"k = 2return = 3

Flip the first and third characters to obtain 11110. It contains three adjacent 11 pairs, which is maximum.

Constraints

  • 1 <= s.length <= 2000
  • s[i] is 0 or 1.
  • 0 <= k <= s.length
  • s.length * (k + 1) <= 2 * 10^6

More Tiktok problems

drafts saved locally
public int maximizeConsecutiveDanceMoves(String s, int k) {
  // write your code here
}
s"01010"
k2
expected3
checking account