|
| 1 | +package chapter05BitManipulation; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +/** |
| 7 | + * |
| 8 | + * Problem: Flip Bit to Win: You have an integer and you can flip exactly one |
| 9 | + * bit from a O to a 1. Write code to find the length of the longest sequence of |
| 10 | + * 1 s you could create. |
| 11 | + * |
| 12 | + * EXAMPLE Input: 1775 (or: 11011101111) Output: 8 |
| 13 | + * |
| 14 | + * |
| 15 | + */ |
| 16 | + |
| 17 | +public class FlipBitToWin { |
| 18 | + |
| 19 | + /** |
| 20 | + * Method 1: Brute Force |
| 21 | + * |
| 22 | + * Time Complexity: O(b), where b is the length of the sequence |
| 23 | + * |
| 24 | + * Space COmplexity: O(b) |
| 25 | + */ |
| 26 | + public int longestSequence1(int n) { |
| 27 | + if (n == -1) { |
| 28 | + return Integer.BYTES * 8; |
| 29 | + } |
| 30 | + List<Integer> list = getSequence(n); |
| 31 | + return findLongestSequence(list); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * return a list of the size of the sequence. The sequence starts off with |
| 36 | + * the number of 0s and then alternates with the counts of each value. |
| 37 | + */ |
| 38 | + private List<Integer> getSequence(int num) { |
| 39 | + List<Integer> list = new ArrayList<>(); |
| 40 | + int iter = 0; |
| 41 | + int counter = 0; |
| 42 | + for (int i = 0; i < Integer.BYTES * 8; i++) { |
| 43 | + if ((num & 1) != iter) { |
| 44 | + list.add(counter); |
| 45 | + iter = iter == 0 ? 1 : 0; // flip 1 to 0, or 0 to 1 |
| 46 | + counter = 0; |
| 47 | + } |
| 48 | + counter++; |
| 49 | + num >>>= 1; |
| 50 | + } |
| 51 | + list.add(counter); |
| 52 | + return list; |
| 53 | + } |
| 54 | + |
| 55 | + private int findLongestSequence(List<Integer> list) { |
| 56 | + int max = 1; |
| 57 | + for (int i = 0; i < list.size(); i += 2) { |
| 58 | + int zerosSeq = list.get(i); |
| 59 | + int onesSeqRight = i - 1 >= 0 ? list.get(i - 1) : 0; |
| 60 | + int onesSeqLeft = i + 1 < list.size() ? list.get(i + 1) : 0; |
| 61 | + int thisSeq = 0; |
| 62 | + if (zerosSeq == 1) { // can merge |
| 63 | + thisSeq = onesSeqLeft + 1 + onesSeqRight; |
| 64 | + } else if (zerosSeq > 1) { // just ad a zero to either side |
| 65 | + thisSeq = 1 + Math.max(onesSeqLeft, onesSeqRight); |
| 66 | + } else if (zerosSeq == 0) { // no zero, but take either side |
| 67 | + thisSeq = Math.max(onesSeqRight, onesSeqRight); |
| 68 | + } |
| 69 | + max = Math.max(max, thisSeq); |
| 70 | + } |
| 71 | + return max; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Method 2: |
| 76 | + * |
| 77 | + * Walk through the integer, tracking the current 1s sequence length and the |
| 78 | + * previous 1s sequence length. When we meet a 0, update previousLength: |
| 79 | + * |
| 80 | + * 1. If the next bit is a 1, priviousLength should be set to current |
| 81 | + * length; |
| 82 | + * |
| 83 | + * 2. If the next bit is a 0, we cannot merge these sequences together. So, |
| 84 | + * set previousLength to 0; |
| 85 | + * |
| 86 | + * Time Complexity: O(b) |
| 87 | + * |
| 88 | + * Space Complexity: O(1) |
| 89 | + * |
| 90 | + */ |
| 91 | + public int longestSequence2(int num) { |
| 92 | + if (num == -1) { |
| 93 | + return Integer.BYTES; |
| 94 | + } |
| 95 | + int curLength = 0; |
| 96 | + int preLength = 0; |
| 97 | + int max = 1; |
| 98 | + while (num != 0) { |
| 99 | + if ((num & 1) == 1) { // current bit is a 1 |
| 100 | + curLength++; |
| 101 | + } else if ((num & 1) == 0) { // current bit is a 0 |
| 102 | + preLength = (num & 2) == 0 ? 0 : curLength; |
| 103 | + curLength = 0; |
| 104 | + } |
| 105 | + max = Math.max(max, preLength + 1 + curLength); |
| 106 | + num >>>= 1; |
| 107 | + } |
| 108 | + return max; |
| 109 | + } |
| 110 | + |
| 111 | + public static void main(String[] args) { |
| 112 | + FlipBitToWin f = new FlipBitToWin(); |
| 113 | + System.out.println(f.longestSequence1(1775)); |
| 114 | + System.out.println(f.longestSequence2(1775)); |
| 115 | + } |
| 116 | +} |
0 commit comments