0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ABC124 C - Coloring Colorfully から学んだ

Last updated at Posted at 2021-09-18

abc124_1.png
abc124_2.png
abc124_3.png

シンプルに "0" から始めた場合、 "1" から始めた場合、
交互になってない箇所をカウントして比較する。最小を output すれば良いはずです。

ColoringColorfully.py
S = input()

cntst0 = 0 #"0"start の場合
for i in range(len(S)): # 計算量 O(N)
    if i%2 == 0 and S[i] != "0":
        cntst0 += 1
    elif i%2 != 0 and S[i] != "1":
        cntst0 += 1

cntst1 = 0 #"1"start の場合
for j in range(len(S)):# 計算量 O(N)
    if j%2 == 0 and S[j] != "1":
        cntst1 += 1
    elif j%2 != 0 and S[j] != "0":
        cntst1 += 1

print(min(cntst0,cntst1))#計算量 O(2)

#合計計算量 O(2N+2)

極解を見つけた。

abc124c.py
S = input()
cnt = S[0::2].count("1") + S[1::2].count("0")
print(min(cnt,len(S)-cnt))

#000 の場合
#cnt = 0 + 1 = 1
#len(S)-cnt = 2
#ans = min(1,2)

っと言う事らしい。んー、自分には少し早いようだ。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?