-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimum_capacity.java
More file actions
46 lines (36 loc) · 895 Bytes
/
Copy pathminimum_capacity.java
File metadata and controls
46 lines (36 loc) · 895 Bytes
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
// Java implementation of the approach
import java.util.*;
class N
{
// Function to return the minimum capacity required
static int minCapacity(int enter[],
int exit[], int n)
{
// To store the minimum capacity
int minCap = 0;
// To store the current capacity
// of the train
int currCap = 0;
// For every station
for (int i = 0; i < n; i++)
{
// Add the number of people entering the
// train and subtract the number of people
// exiting the train to get the
// current capacity of the train
currCap = currCap + enter[i] - exit[i];
// Update the minimum capacity
minCap = Math.max(minCap, currCap);
}
return minCap;
}
// Driver code
public static void main(String[] args)
{
int enter[] = { 3, 5, 2, 0 };
int exit[] = { 0, 2, 4, 4 };
int n = enter.length;
System.out.println(minCapacity(enter, exit, n));
}
}
// This code is contributed by naman_d0shi