995. Minimum Number of K Consecutive Bit Flips
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array. If it is not possible, return -1.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MinimumNumberOfKConsecutiveBitFlips_995 { | |
private final static Logger logger = LoggerFactory.getLogger(MinimumNumberOfKConsecutiveBitFlips_995.class); | |
public static void main(String[] args) { | |
MinimumNumberOfKConsecutiveBitFlips_995 thisClass = new MinimumNumberOfKConsecutiveBitFlips_995(); | |
thisClass.testMinimumNumberOfKConsecutiveBitFlips_995(); | |
} | |
private void testMinimumNumberOfKConsecutiveBitFlips_995() { | |
logger.info("result {} v.s. {}", "2", minKBitFlips(new int[]{0, 1, 0}, 1)); | |
logger.info("result {} v.s. {}", "-1", minKBitFlips(new int[]{1, 1, 0}, 2)); | |
logger.info("result {} v.s. {}", "3", minKBitFlips(new int[]{0, 0, 0, 1, 0, 1, 1, 0}, 3)); | |
logger.info("result {} v.s. {}", "-1", minKBitFlips(new int[]{1, 0, 1}, 3)); | |
} | |
public int minKBitFlips(int[] A, int K) { | |
// filter abnormal cases | |
if (A == null || A.length == 0) { | |
return 0; | |
} | |
// dp logic | |
if (K == 1) { | |
int count = 0; | |
for (int a : A) { | |
if (a == 0) { | |
count++; | |
} | |
} | |
return count; | |
} | |
int i = 0; | |
int n = A.length; | |
int steps = 0; | |
while (i < n) { | |
if (A[i] == 1) { | |
i++; | |
continue; | |
} | |
if (i > n - K) { | |
return -1; | |
} | |
for (int t = 0; t < K; t++) { | |
A[i + t] = A[i + t] == 0 ? 1 : 0; | |
} | |
i++; | |
steps++; | |
} | |
// return the final result | |
return steps; | |
} | |
private static class MyLogger { | |
private static final boolean isDebugging = false; | |
private static final boolean isInfoing = true; | |
private static final String DEBUG = "[DEBUG]"; | |
private static final String INFO = "[INFO]"; | |
static void debug(Object message) { | |
if (isDebugging) { | |
System.out.println(DEBUG + " = " + message); | |
} | |
} | |
static void info(Object message) { | |
if (isInfoing) { | |
System.out.println(INFO + " = " + message); | |
} | |
} | |
static void debug() { | |
if (isDebugging) { | |
System.out.println(DEBUG + " = "); | |
} | |
} | |
static void info() { | |
if (isInfoing) { | |
System.out.println(INFO + " = "); | |
} | |
} | |
} | |
} |
No comments:
Post a Comment