-
Notifications
You must be signed in to change notification settings - Fork 0
/
Books.java
60 lines (54 loc) · 1.35 KB
/
Books.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/* https://codeforces.com/contest/279/problem/B
tag: #binary-search #implementation #two-pointer
max (get number of books can be read that counted from i-th element)
*/
/* Example:
10 15
10 9 1 1 5 10 5 3 7 2
f |
l |
*/
import java.util.Scanner;
public class Books {
static int calMaxNumBooks() {
Scanner scanner = new Scanner(System.in);
int numberOfBooks = scanner.nextInt();
int freeMinutes = scanner.nextInt();
int[] bookMinuteArr = new int[numberOfBooks];
for (int i = 0; i < numberOfBooks; i++) {
int minute = scanner.nextInt();
bookMinuteArr[i] = minute;
}
int maxBooksBeRead = 0;
int count = 0;
int firstIdx = 0;
int lastIdx = 0;
while (lastIdx < numberOfBooks) {
if (bookMinuteArr[lastIdx] <= freeMinutes) {
freeMinutes -= bookMinuteArr[lastIdx];
count++;
lastIdx++;
if (count > maxBooksBeRead) {
maxBooksBeRead = count;
}
} else {
freeMinutes += bookMinuteArr[firstIdx];
count--;
firstIdx++;
/*
if (count > 0) {
freeMinutes += bookMinuteArr[firstIdx];
firstIdx++;
count--;
} else {
lastIdx++;
}
*/
}
}
return maxBooksBeRead;
}
public static void main(String[] args) {
System.out.println(calMaxNumBooks());
}
}