-
Notifications
You must be signed in to change notification settings - Fork 0
/
MessageForum.java
35 lines (30 loc) · 963 Bytes
/
MessageForum.java
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
import java.util.Scanner;
public class MessageForum {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
System.out.println(deleteMessage(n, scanner, new int[1001]));
}
private static int deleteMessage(int n, Scanner scanner, int[] map) {
if (n > 0) {
map[scanner.nextInt()] = scanner.nextInt();
return deleteMessage(n - 1, scanner, map);
}
return delete(1, getKey(scanner.nextInt(), map), map);
}
private static int delete(int n, int del, int[] map) {
if (del == -1) {
return n;
}
return delete(n + 1, getKey(del, map), map);
}
private static int getKey(int del, int[] map) {
for (int i = 1; i < map.length; i++) {
if (map[i] == del) {
map[i] = -1;
return i;
}
}
return -1;
}
}