-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReto #10.js
More file actions
56 lines (48 loc) · 1.77 KB
/
Reto #10.js
File metadata and controls
56 lines (48 loc) · 1.77 KB
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
/**
* Reto #10: 📨 Profundidad de la magia navideña
*
* En el Polo Norte, Santa Claus está revisando las cartas mágicas 📩✨ que recibe de los niños de todo el mundo. Estas cartas usan un antiguo
* lenguaje navideño en el que los corchetes [ y ] representan la intensidad del deseo.
*
* Cuanto más profunda sea la anidación de los corchetes, más fuerte es el deseo. Tu misión es averiguar la máxima profundidad en
* la que se anidan los [].
*
* Pero ¡cuidado! Algunas cartas pueden estar mal escritas. Si los corchetes no están correctamente balanceados (si se cierra antes de abrir,
* sobran cierres o faltan cierres), la carta es inválida y debes devolver -1.
*
* Ejemplos
*/
console.log(maxDepth('[[][[[]]][[]]]')) // -> 4
console.log(maxDepth('[[][[[]]]][[]]]')) // -> -1
console.log(maxDepth('[]')) // -> 1
console.log(maxDepth('[[]]')) // -> 2
console.log(maxDepth('[][]')) // -> 1
console.log(maxDepth('[[][]]')) // -> 2
console.log(maxDepth('[[[]]]')) // -> 3
console.log(maxDepth('[][[]][]')) // -> 2
console.log(maxDepth('][')) // -> -1 (cierra antes de abrir)
console.log(maxDepth('[[[')) // -> -1 (faltan cierres)
console.log(maxDepth('[]]]')) // -> -1 (sobran cierres)
console.log(maxDepth('[][][')) // -> -1 (queda uno sin cerrar)
/**
* @param {string} s - The string to check
* @returns {number} The maximum depth of the magic
*/
function maxDepth(s) {
let longDepth = 0;
let currDepth = 0;
for(const bracket of s){
if(bracket === ']')
{
if(currDepth === 0)
return -1;
if(currDepth > longDepth)
longDepth = currDepth;
currDepth--;
}
else {
currDepth ++;
}
}
return currDepth === 0 ? longDepth : -1;
}