Passed
Push — master ( c29376...731f78 )
by Nguyen
36s queued 11s
created

topics/stack/next-greater-element/index.js   A

Complexity

Total Complexity 5
Complexity/F 5

Size

Lines of Code 22
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 16
mnd 4
bc 4
fnc 1
dl 0
loc 22
bpm 4
cpm 5
noi 0
c 0
b 0
f 0
rs 10
1
const Stack = require('../index')
2
const NGE = (arr) => {
3
    let stack = new Stack(),
4
        result = {}
5
    for (let n of arr) {
6
        if (stack.isEmpty() || n < stack.top()) {
7
            stack.push(n)
8
            continue
9
        }
10
        while (!stack.isEmpty() && n >= stack.top()) {
11
            let p = stack.pop()
12
            result[p] = n
13
        }
14
        stack.push(n)
15
    }
16
17
    while (!stack.isEmpty()) {
18
        result[stack.pop()] = -1
19
    }
20
    return result
21
}
22
module.exports = NGE;