All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0401_0500.s0481_magical_string.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.30
Show newest version
package g0401_0500.s0481_magical_string

// #Medium #String #Two_Pointers #2023_01_02_Time_124_ms_(100.00%)_Space_34.3_MB_(100.00%)

class Solution {
    fun magicalString(n: Int): Int {
        val a = IntArray(n + 2)
        var fast = 1
        var slow = 1
        var num = 1
        while (fast <= n) {
            a[fast++] = num
            if (a[slow++] == 2) {
                a[fast++] = num
            }
            num = 3 - num
        }
        var count = 0
        for (j in 1..n) {
            if (a[j] == 1) {
                count++
            }
        }
        return count
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy