Added 17 solutions

This commit is contained in:
rshaghoulian
2017-02-21 00:05:09 -06:00
parent d6dcb21ea4
commit ff6167d56c
25 changed files with 676 additions and 69 deletions
+14 -22
View File
@@ -11,33 +11,23 @@ import java.util.HashMap;
// O(n) space complexity
public class test {
public static void main(String[] args) {
/* Create our data structures */
Deque<Integer> deque = new ArrayDeque<>();
HashMap<Integer, Integer> map = new HashMap<>();
Deque<Integer> deque = new ArrayDeque<>();
/* Save first m entries */
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int m = scan.nextInt();
for (int i = 0; i < m; i++) {
int num = scan.nextInt();
deque.add(num);
if (map.containsKey(num)) {
map.put(num, map.get(num) + 1);
} else {
map.put(num, 1);
}
}
/* Loop through remaining n-m entries */
int m = scan.nextInt();
int max = map.size();
for (int i = m; i < n; i++) {
/* Remove old value */
int old = deque.remove();
if (map.get(old) == 1) {
map.remove(old);
} else {
map.put(old, map.get(old) - 1);
for (int i = 0; i < n; i++) {
/* Remove old value (if necessary) */
if (i >= m) {
int old = deque.remove();
if (map.get(old) == 1) {
map.remove(old);
} else {
map.put(old, map.get(old) - 1);
}
}
/* Add new value */
int num = scan.nextInt();
@@ -50,6 +40,8 @@ public class test {
max = Math.max(max, map.size());
}
scan.close();
System.out.println(max);
}
}