Skip to content

Commit 08173a9

Browse files
authored
Merge pull request #5753 from Tirth1410/CSES-Apartments
Updated Java Solution solutions/silver/cses-1084.mdx
2 parents c9e0860 + 986c7f5 commit 08173a9

File tree

1 file changed

+42
-38
lines changed

1 file changed

+42
-38
lines changed

solutions/silver/cses-1084.mdx

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ author: Nathan Gong, Danh Ta Chi Thanh
88
[Unofficial Editorial (C++)](https://codeforces.com/blog/entry/83295)
99

1010
We can use a greedy approach to solve this problem. First, sort the applicants
11-
and apartments. We can keep two pointers $i$ and $j$ (initialized to 0),
12-
which keep track of the current index of the applicant and apartment we are
13-
looking at respectively. Then, while there are more applicants and apartments to
14-
look through, we repeatedly run the following check:
11+
and apartments. We can keep two pointers $i$ and $j$ (initialized to 0), which
12+
keep track of the current index of the applicant and apartment we are looking at
13+
respectively. Then, while there are more applicants and apartments to look
14+
through, we repeatedly run the following check:
1515

1616
- If $|\texttt{applicants}[i] - \texttt{apartments}[j]| \leq k$, we have found a
1717
suitable apartment for the current applicant. Thus, we increment $i$, $j$, and
@@ -84,54 +84,58 @@ int main() {
8484

8585
<JavaSection>
8686

87-
<Warning>
88-
Java will TLE on test case #17.
89-
</Warning>
90-
9187
```java
9288
import java.io.*;
9389
import java.util.*;
9490

9591
public class Apartments {
96-
public static void main(String[] args) {
97-
Kattio io = new Kattio();
92+
public static void main(String[] args) throws java.lang.Exception {
93+
BufferedReader fr = new BufferedReader(new InputStreamReader(System.in));
94+
PrintWriter out = new PrintWriter(System.out);
95+
96+
String[] t = fr.readLine().split(" ");
97+
int n = Integer.parseInt(t[0]);
98+
int m = Integer.parseInt(t[1]);
99+
long k = Integer.parseInt(t[2]);
100+
101+
List<Integer> applicants = new ArrayList<>();
102+
List<Integer> apartments = new ArrayList<>();
103+
104+
for (String ele : fr.readLine().split(" ")) {
105+
applicants.add(Integer.parseInt(ele));
106+
}
98107

99-
int n = io.nextInt(); // number of applicants
100-
int m = io.nextInt(); // number of apartments
101-
int k = io.nextInt(); // max diff between desired and actual size
108+
for (String ele : fr.readLine().split(" ")) {
109+
apartments.add(Integer.parseInt(ele));
110+
}
102111

103-
int[] applicants = new int[n];
104-
int[] apartments = new int[m];
105-
for (int i = 0; i < n; i++) { applicants[i] = io.nextInt(); }
106-
for (int i = 0; i < m; i++) { apartments[i] = io.nextInt(); }
112+
Collections.sort(applicants);
113+
Collections.sort(apartments);
107114

108-
Arrays.sort(applicants);
109-
Arrays.sort(apartments);
115+
int p1 = 0;
116+
int p2 = 0;
110117

111-
int i = 0;
112-
int j = 0;
113118
int ans = 0;
114-
while (i < n && j < m) {
115-
// Found suitable apartment
116-
if (Math.abs(applicants[i] - apartments[j]) <= k) {
117-
i++;
118-
j++;
119+
while (p1 < n && p2 < m) {
120+
// Found a suitable apartment for the applicant
121+
if (Math.abs(applicants.get(p1) - apartments.get(p2)) <= k) {
122+
p1++;
123+
p2++;
119124
ans++;
125+
continue;
120126
}
121-
// Apartment is too small -> increment apartment pointer
122-
else if (applicants[i] > apartments[j]) {
123-
j++;
124-
}
125-
// Apartment is too big -> increment applicant pointer
126-
else {
127-
i++;
128-
}
127+
128+
// If the desired apartment size is too small,
129+
// skip that applicant and move to the next person
130+
if (applicants.get(p1) < apartments.get(p2)) p1++;
131+
132+
// If the desired apartment size of the applicant is too big,
133+
// move the apartment pointer to the right to find a bigger one
134+
else p2++;
129135
}
130-
io.println(ans);
131-
io.close();
136+
out.println(ans);
137+
out.close();
132138
}
133-
134-
// CodeSnip{Kattio}
135139
}
136140
```
137141

0 commit comments

Comments
 (0)