Need help with this string problem

Правка en1, от Rushhh, 2021-04-11 18:06:53

given a binary string s having length N*N. N is even.number of 1s in the string is equal to number of 0s in the string.you have to rearrange the string in the minimum possible swaps to make the string satisfy the following conditions,

1.if (i+1)%N — i%N =1 then s[i]+s[i+1] =1

2.if i+N < N*N then s[i]+s[i+1]=1

(assume 0 based indexing).

i have tried doing it by creating a temporary string satisfying the conditions and greedily checked each element of s with the temporary string and the answer would be difference count/2. why is this wrong?

example if string s= 1100

temporary string t=1001

difference count=2

ans=2/2=1

Теги #string, #greedy

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en2 Английский Rushhh 2021-04-12 22:04:47 316
en1 Английский Rushhh 2021-04-11 18:06:53 695 Initial revision (published)