Skip to content

Commit 4addea6

Browse files
authored
Create 481-magical-string.js
1 parent ba31cc8 commit 4addea6

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

481-magical-string.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {number} n
3+
* @return {number}
4+
*/
5+
const magicalString = function(n) {
6+
const queue = []
7+
let one = true
8+
let count1 = 0
9+
while (n-- > 0) {
10+
queue.shift()
11+
let c = one ? 1 : 2
12+
one = !one
13+
queue.push(c)
14+
count1 += 2 - c
15+
if (queue[0] === 2 && n-- > 0) {
16+
queue.push(c)
17+
count1 += 2 - c
18+
}
19+
}
20+
return count1
21+
}

0 commit comments

Comments
 (0)