Skip to content

Commit 4f3b16a

Browse files
author
Dhananjay Nagargoje
committed
sliding window
1 parent 56d7796 commit 4f3b16a

File tree

3 files changed

+73
-3
lines changed

3 files changed

+73
-3
lines changed

Diff for: src/main/java/problems/patterns/ontwopointerstrategy/DeleteMiddleOfList.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package problems.ontwopointerstrategy;
1+
package problems.patterns.ontwopointerstrategy;
22

33
import datastructures.linkedlist.Node;
44

Diff for: src/main/java/problems/patterns/slidingwindows/LongestRepeatingCharacterReplacement.java

+36-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package problems.patterns.slidingwindows;
22

33
public class LongestRepeatingCharacterReplacement {
4-
public int characterReplacement(String s, int k) {
54
public int characterReplacement(String s, int k) {
65
int len = s.length();
76
int[] count = new int[26];
@@ -16,5 +15,40 @@ public int characterReplacement(String s, int k) {
1615
}
1716
return maxLength;
1817
}
19-
}
18+
19+
/**
20+
* Given a string s that consists of only uppercase English letters, you can perform at most k operations on that string.
21+
*
22+
* In one operation, you can choose any character of the string and change it to any other uppercase English character.
23+
*
24+
* Find the length of the longest sub-string containing all repeating letters you can get after performing the above operations.
25+
*
26+
* Note:
27+
* Both the string's length and k will not exceed 104.
28+
*
29+
* Example 1:
30+
*
31+
* Input:
32+
* s = "ABAB", k = 2
33+
*
34+
* Output:
35+
* 4
36+
*
37+
* Explanation:
38+
* Replace the two 'A's with two 'B's or vice versa.
39+
*
40+
*
41+
* Example 2:
42+
*
43+
* Input:
44+
* s = "AABABBA", k = 1
45+
*
46+
* Output:
47+
* 4
48+
*
49+
* Explanation:
50+
* Replace the one 'A' in the middle with 'B' and form "AABBBBA".
51+
* The substring "BBBB" has the longest repeating letters, which is 4.
52+
*
53+
*/
2054
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,40 @@
11
package problems.patterns.slidingwindows;
22

3+
import edu.princeton.cs.algs4.In;
4+
5+
import java.util.*;
6+
37
public class MaxInEachWIndow {
8+
// DO NOT MODIFY THE LIST. IT IS READ ONLY
9+
public static ArrayList<Integer> slidingMaximum(final List<Integer> A, int B) {
10+
Deque<Integer> maxInWindow = new LinkedList<>();
11+
ArrayList<Integer> result = new ArrayList<>();
12+
for (int i=0;i<A.size();i++) {
13+
if (maxInWindow.size() == 0) {
14+
maxInWindow.addLast(A.get(i));
15+
}
16+
else if (maxInWindow.size() >0 && maxInWindow.peekLast() < A.get(i)) {
17+
while (maxInWindow.size() >0 && maxInWindow.peekLast() < A.get(i)) {
18+
maxInWindow.removeLast();
19+
}
20+
maxInWindow.addLast(A.get(i));
21+
} else {
22+
maxInWindow.addLast(A.get(i));
23+
}
24+
if (i >= B-1) {
25+
result.add(maxInWindow.peekFirst());
26+
if (A.get(i+1-B).equals(maxInWindow.peekFirst())) {
27+
maxInWindow.removeFirst();
28+
}
29+
}
30+
}
31+
return result;
32+
33+
}
34+
35+
public static void main(String[] args) {
36+
List<Integer> sample = Arrays.asList(648, 614, 490, 138, 657, 544, 745, 582, 738, 229, 775, 665, 876, 448, 4, 81, 807, 578, 712, 951, 867, 328, 308, 440, 542, 178, 637, 446, 882, 760, 354, 523, 935, 277, 158, 698, 536, 165, 892, 327, 574, 516, 36, 705, 900, 482, 558, 937, 207, 368 );
37+
System.out.println(slidingMaximum(sample, 9));
38+
}
39+
440
}

0 commit comments

Comments
 (0)