-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindZerosToBeFlipped_Sliding_Window.java
More file actions
119 lines (104 loc) · 4.19 KB
/
Copy pathFindZerosToBeFlipped_Sliding_Window.java
File metadata and controls
119 lines (104 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package com.leetcode.year_2020.sliding_window;
import java.util.HashSet;
import java.util.Set;
/**
* https://leetcode.com/problems/max-consecutive-ones-iii/
* You are given with an array of 1s and 0s. And you are given with an integer m, which signifies number of flips allowed.
* <p>
* find the position of zeros which when flipped will produce maximum continuous series of 1s.
* <p>
* e.g.
* input:
* arr={1 1 0 1 1 0 0 1 1 1 } m=1
* output={1 1 1 1 1 0 0 1 1 1} position=2
* <p>
* arr={1 1 0 1 1 0 0 1 1 1 } m=2
* output={1 1 0 1 1 1 1 1 1 1} position=5,6
*/
public class FindZerosToBeFlipped_Sliding_Window {
public static void main(String[] args) {
findZerosToBeFlippedToGetMaximumConsecutiveOnes(new int[]{1, 0, 0, 1, 1, 0, 1, 0, 1, 1}, 2);
findZerosToBeFlippedToGetMaximumConsecutiveOnesNew(new int[]{1, 0, 0, 1, 1, 0, 1, 0, 1, 1}, 2);
System.out.println(longestOnes(new int[]{1, 0, 0, 1, 1, 0, 1, 0, 1, 1}, 2));
findZerosToBeFlippedToGetMaximumConsecutiveOnes(new int[]{0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1}, 3);
findZerosToBeFlippedToGetMaximumConsecutiveOnesNew(new int[]{0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1}, 3);
System.out.println(longestOnes(new int[]{0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1}, 3));
}
public static int longestOnes(int[] arr, int k) {
int beg = 0, end = 0, noOfZeros = 0;
int answer = Integer.MIN_VALUE;
for (end = 0; end < arr.length; end++) {
if (arr[end] == 0) noOfZeros++;
while (noOfZeros > k) {
if (arr[beg] == 0) noOfZeros--;
beg++;
}
answer = Math.max(answer, end - beg);
}
return answer;
}
public static void findZerosToBeFlippedToGetMaximumConsecutiveOnesNew(int[] arr, int maxZerosToBeFlipped) {
// {1, 0, 0, 1, 1, 0, 1, 0, 1, 1}
// 2
int begin = 0, end = 0;
Set<Integer> currentFlipIndex = new HashSet<>();
Set<Integer> zeroIndexForMaxConsecutiveOnes = new HashSet<>();
int MAX_LENGTH = Integer.MIN_VALUE;
while (end < arr.length) {
if (arr[end] == 0) {
maxZerosToBeFlipped--;
currentFlipIndex.add(end);
}
end++;
while (maxZerosToBeFlipped < 0) {
if (arr[begin] == 0) {
maxZerosToBeFlipped++;
currentFlipIndex.remove(begin);
}
begin++;
}
if (MAX_LENGTH < end - begin) {
MAX_LENGTH = end - begin;
zeroIndexForMaxConsecutiveOnes = new HashSet<>(currentFlipIndex);
}
}
System.out.println(zeroIndexForMaxConsecutiveOnes);
}
public static void findZerosToBeFlippedToGetMaximumConsecutiveOnes(int[] arr, int maxZerosToBeFlipped) {
int wL = 0; // Left Side of Window
int wR = 0; // Right Side of Window
int nZero = 0; // Current Zeros under window, this should always be less than maxZerosToBeFlipped
int BEST_WINDOW_WIDTH = -1; // Best window at any point of time
int best_wL = -1;
int best_wR = -1;
while (wR < arr.length) {
// Expand the Widow to the right, and update the '0' Count
if (nZero <= maxZerosToBeFlipped) {
if (arr[wR] == 0) {
nZero += 1;
}
wR++;
}
// Shrink the window from the left and update the '0' Count
if (nZero > maxZerosToBeFlipped) {
if (arr[wL] == 0) {
nZero -= 1;
}
wL++;
}
// Let's check out bestWindowWidth
if (wR - wL > BEST_WINDOW_WIDTH) {
BEST_WINDOW_WIDTH = wR - wL;
best_wL = wL;
best_wR = wR;
}
}
for (int i = best_wL; i < best_wR; i++) {
if (arr[i] == 0) {
System.out.print(i + ",");
}
}
System.out.println();
System.out.println("And the max consecutive ones after flipping is " + BEST_WINDOW_WIDTH);
}
}