Work dump
This commit is contained in:
parent
6e357de888
commit
5d8a5c9dc9
2 changed files with 93 additions and 0 deletions
|
@ -0,0 +1,65 @@
|
||||||
|
package eu.ztsh.training.hackerrank.datastructures.dequeue;
|
||||||
|
|
||||||
|
import java.util.ArrayDeque;
|
||||||
|
import java.util.Deque;
|
||||||
|
import java.util.Scanner;
|
||||||
|
import java.util.SortedMap;
|
||||||
|
import java.util.TreeMap;
|
||||||
|
import java.util.TreeSet;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Java Dequeue task.
|
||||||
|
* source: https://www.hackerrank.com/challenges/java-dequeue/problem
|
||||||
|
*/
|
||||||
|
public class Solution {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
var in = new Scanner(System.in);
|
||||||
|
int n = in.nextInt();
|
||||||
|
int m = in.nextInt();
|
||||||
|
final Deque<Integer> deque = new ArrayDeque<>(m);
|
||||||
|
var counter = new Counter();
|
||||||
|
for (int i = 0; i < m; i++) {
|
||||||
|
var content = in.nextInt();
|
||||||
|
deque.addLast(content);
|
||||||
|
counter.add(content);
|
||||||
|
}
|
||||||
|
int max = counter.size();
|
||||||
|
for (int i = m; i < n; i++) {
|
||||||
|
var content = in.nextInt();
|
||||||
|
counter.pop(deque.removeFirst());
|
||||||
|
deque.addLast(content);
|
||||||
|
counter.add(content);
|
||||||
|
var newMax = counter.size();
|
||||||
|
if (newMax > max) {
|
||||||
|
max = newMax;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println(max);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class Counter {
|
||||||
|
|
||||||
|
public void add(int input) {
|
||||||
|
var content = map.getOrDefault(input, 0);
|
||||||
|
map.put(input, content + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void pop(int input) {
|
||||||
|
var content = map.get(input);
|
||||||
|
if (content == 1) {
|
||||||
|
map.remove(input);
|
||||||
|
} else {
|
||||||
|
map.put(input, content - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int size() {
|
||||||
|
return map.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
private final SortedMap<Integer, Integer> map = new TreeMap<>();
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,28 @@
|
||||||
|
package eu.ztsh.training.hackerrank.datastructures.dequeue;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import eu.ztsh.training.hackerrank.HackerRankTest;
|
||||||
|
import eu.ztsh.training.hackerrank.SolutionClassDescription;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
@DisplayName("HackerRank challenges: Java Subarray")
|
||||||
|
class SolutionTest extends HackerRankTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCase0() {
|
||||||
|
simpleAssert(
|
||||||
|
List.of(
|
||||||
|
"6 3",
|
||||||
|
"5 3 5 2 3 2"
|
||||||
|
),
|
||||||
|
List.of("3")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected SolutionClassDescription getSolutionClassDescription() {
|
||||||
|
return new SolutionClassDescription(Solution.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue