-
Notifications
You must be signed in to change notification settings - Fork 0
/
Question11.scala
66 lines (54 loc) · 1.71 KB
/
Question11.scala
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
package InterviewQuestions
/**
* Created by Ken.J.Zheng on 8/8/2018.
*/
/*
Given an array of integers and a number k, where 1 <= k <= length of the array, compute the maximum values of each subarray of length k.
For example, given array = [10, 5, 2, 7, 8, 7] and k = 3, we should get: [10, 7, 8, 8], since:
10 = max(10, 5, 2)
7 = max(5, 2, 7)
8 = max(2, 7, 8)
8 = max(7, 8, 7)
Do this in O(n) time and O(k) space. You can modify the input array in-place and you do not need to store the results. You can simply print them out as you compute them.
*/
case class Node(nodeValue:Int, nodeIndex:Int, var child:Node)
class NodeLink{
var rootNode : Node = null
//when
def insert(nodeValue:Int, nodeIndex:Int, node: Node, windowSize:Int): Node ={
if(node == null)
return Node(nodeValue, nodeIndex,null)
if(nodeValue >= node.nodeValue)
return Node(nodeValue, nodeIndex,null)
if(nodeValue < node.nodeValue){
node.child = insert(nodeValue, nodeIndex, node.child, windowSize)
}
if(nodeIndex - node.nodeIndex >= windowSize)
return node.child
node
}
}
object Question11 extends App {
def maxInWindow(arr: Array[Int], k: Int) = {
val bt = new NodeLink()
var count = 0
for(i <- 0 to arr.length-1){
count += 1
bt.rootNode = bt.insert(arr(i),i,bt.rootNode,k)
if(count>=k){
println(bt.rootNode.nodeValue)
}
val test = 0
}
}
val test1 = Array(10, 5, 2, 7, 8, 7)
println("maxInWindow(test1,3)")
maxInWindow(test1,3)
println("maxInWindow(test1,1)")
maxInWindow(test1,1)
println("maxInWindow(test1,5)")
maxInWindow(test1,5)
val test2 = Array(10,9,8,7,6,5,4,3,2,1)
println("maxInWindow(test2,3)")
maxInWindow(test2,3)
}