2025年8月11日 星期一

[481] Magical String

不要問我過的好不好~我的心事你應該知道~(老師請下音樂)
久違的寫題目, 我就不為難我自己了...(痛哭) 
又是漫長的復健之路啊~(遠目)


#define size 100000

int magicalString(int n) {
    int *magic = malloc (sizeof(int)* size);
    int left=2, right=3;
    bool one = false;  //one (true) two (false)
    magic[0]=1;
    magic[1]=2;
    magic[2]=2;
    int count=1;
    while (right < n)
    {
        one = !one;
        int repeat = magic[left];
        magic[right++] = one?(1):(2);
        count += (one)?(1):(0);
        if (right==n)
            break;
        if (repeat==2)
        {
            magic[right++] = (one==true)?(1):(2);
            count += (one)?(1):(0);
        }
        left++;
    }

    return count;
}

沒有留言:

張貼留言