-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaximumpeople.java
76 lines (63 loc) · 1.71 KB
/
Maximumpeople.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package Stack;
public class Maximumpeople {
public static int leftindex(int[] heights, int idx,
int n)
{
int h = heights[idx];
for (int i = idx - 1; i >= 0; i--) {
// If height of person i is
// greater than or equal to
// current person of height h
// then the current person(idx)
// cannot see him
if (heights[i] >= h)
return i;
}
// If a person can see all other people
// who are standing on his left
return -1;
}
public static int rightindex(int[] heights, int idx,
int n)
{
int h = heights[idx];
for (int i = idx + 1; i < n; i++) {
// If height of person i is
// greater than or equal to
// current person of height h
// then the current person(idx)
// cannot see him
if (heights[i] >= h)
return i;
}
// If a person can see all other people
// who are standing on his right
return n;
}
public static int max_people(int[] heights, int n)
{
// Ans stores the maximum of people
// a person can see
int ans = 0;
for (int i = 0; i < n; i++) {
// Leftptr stores the leftmost index
// of the person which
// the current person cannot see
int leftptr = leftindex(heights, i, n);
// Rightptr stores the rightmost index
// of the person which
// the current person cannot see
int rightptr = rightindex(heights, i, n);
// Maximum number of people
// a person can see
ans = Math.max(ans, rightptr - leftptr - 1);
}
return ans;
}
public static void main(String[] args) {
int N = 7;
int[] heights = new int[] { 6, 2, 5, 4, 5, 1, 6 };
// Function call
System.out.println(max_people(heights, N));
}
}