You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You are given an array A representing heights of students. All the students are asked to stand in rows. The students arrive by one, sequentially (as their heights appear in A). For the i-th student, if there is a row in which all the students are taller than A[i], the student will stand in one of such rows. If there is no such row, the student will create a new row. Your task is to find the minimum number of rows created.
5
+
Write a function that, given a non-empty array A containing N integers, denoting the heights of the students, returns the minimum number of rows created.
6
+
7
+
For example, given A = [5, 4, 3, 6, 1], the function should return 2.
8
+
9
+
Students will arrive in sequential order from A[0] to A[N−1]. So, the first student will have height = 5, the second student will have height = 4, and so on.
10
+
For the first student, there is no row, so the student will create a new row.
11
+
Row1 = [5]
12
+
For the second student, all the students in Row1 have height greater than 4. So, the student will stand in Row1.
13
+
Row1 = [5, 4]
14
+
Similarly, for the third student, all the students in Row1 have height greater than 3. So, the student will stand in Row1.
15
+
Row1 = [5, 4, 3]
16
+
For the fourth student, there is no row in which all the students have height greater than 6. So, the student will create a new row.
17
+
Row1 = [5, 4, 3]
18
+
Row2 = [6]
19
+
For the fifth student, all the students in Row1 and Row2 have height greater than 1. So, the student can stand in either of the two rows.
20
+
Row1 = [5, 4, 3, 1]
21
+
Row2 = [6]
22
+
Since two rows are created, the function should return 2.
23
+
24
+
Assume that:
25
+
N is an integer within the range [1..1,000]
26
+
each element of array A is an integer within the range [1..10,000]
27
+
*/
28
+
classHeightRows {
29
+
30
+
data classHeight(varvalue:Int)
31
+
32
+
funcalc(students:Array<Int>): Int {
33
+
val rows:MutableList<Height> =mutableListOf()
34
+
35
+
students.forEach { studentHeight ->
36
+
val minHeightFromAllMatchingRows:Height?=
37
+
rows
38
+
.filter {
39
+
// student may join this row, everyone is high(er)
0 commit comments