【解決方法】最小スワップの Java プログラムを作成する

プログラミングQA


Minimum swaps
You are given the following:
	An Integer N
	A binary string S of length N×N
N is always even and string S has an equal number of 0's and 1's. You are allowed to swap any two elements S[i] and S[j] (0≤i ≠j<N×N) of string S.
Task
Determine the minimum number of swaps required to be done on string S such that the following conditions hold true for the entire string.
	If (i+1)  mod N-(i)  mod N=1, then (S[i+1]+S[i])=1 for (0≤i<N×N-1).
	If i+N<N×N, then (S[i+N]+S[i])=1.
Note
	Assuming 0 based indexing.
Example
Assumptions
	N=2
	String S="1010"
Approach
Swapping indices 0 and 1, we get string S="0110",
String S="0110" satisfies all the conditions, Hence answer is 1.
Function description
Complete the solve function provided in the editor.
This function takes the following 2 parameters and returns the minimum number of swaps required.
	N: represents the integer N
	S: Represents the binary string S of length N×N
[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/FUOJc.png
Sample Input:1,2,0011
Sample output:1
Explanation 
The first represents the number of test cases T=1
For test case 1
Given
	N=2
	String S="0011"
Approach
Swapping indices 0 and 1 we get string S="1001",
String S="1001"  satisfies all the conditions, Hence the answer is 1.

私が試したこと:

質問を理解するのは非常に難しかった

解決策 1

引用:

質問を理解するのは非常に難しかった

課題は、クラスで学んだことをテストするように設定されています。 質問が理解できない場合は、先生と話し合う必要があります。

コメント

タイトルとURLをコピーしました