-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Runs.kt
37 lines (31 loc) · 793 Bytes
/
Runs.kt
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
/*
* Any array may be viewed as a number of "runs" of equal numbers.
* For example, the following array has two runs:
* 1, 1, 1, 2, 2
* Three 1's in a row form the first run, and two 2's form the second.
* This array has two runs of length one:
* 3, 4
* And this one has five runs:
* 1, 0, 1, 1, 1, 2, 0, 0, 0, 0, 0, 0, 0
* Your task is to implement the runs() function so that it returns the number
* of runs in the given array.
*/
package runs
fun runs(a: IntArray): Int {
// Write your solution here
return 0
}
// Solution 1
fun runs(a: IntArray): Int {
var res = 0;
if (a.size == res)
return res
var base = a[res]
a.forEach({
if(it != base){
res ++
base = it
}
})
return (++res);
}