-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathts.ts
66 lines (57 loc) · 1.67 KB
/
ts.ts
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
// console.log = () => {}
class MyNode {
next?: MyNode
constructor(public value: number) {}
}
class DataStream {
sameAsValueCount: number
listLength: number
// @ts-ignore
firstNode: MyNode
// @ts-ignore
lastNode: MyNode
constructor(public value: number, public k: number) {
this.value = value
this.k = k
this.sameAsValueCount = 0
this.listLength = 0
console.log('NULL init')
}
consec(num: number): boolean {
const newNode = new MyNode(num)
if (!this.lastNode) {
// FIRST INIT
this.lastNode = new MyNode(num)
this.firstNode = this.lastNode
this.listLength = 1
} else {
this.lastNode.next = newNode
this.lastNode = newNode
this.listLength++
}
if (num === this.value) this.sameAsValueCount++
if (this.listLength < this.k) {
console.log('FALSE listLength < this.k', `this.listLength=${this.listLength}`, `this.k=${this.k}`)
return false
} else if (this.listLength > this.k) {
const oldFirstNode = this.firstNode
this.firstNode = oldFirstNode.next!
this.listLength--
if (oldFirstNode.value === this.value) this.sameAsValueCount--
}
// Check if conseq
console.log(
this.sameAsValueCount === this.listLength,
this.sameAsValueCount === this.k,
'this.sameAsValueCount === this.listLength',
`this.sameAsValueCount=${this.sameAsValueCount}`,
`this.listLength=${this.listLength}`
)
return this.sameAsValueCount === this.listLength && this.sameAsValueCount === this.k
}
}
/**
* Your DataStream object will be instantiated and called as such:
* var obj = new DataStream(value, k)
* var param_1 = obj.consec(num)
*/