Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- AI역량평가
- 참새목
- 솔딱새과
- 참새과
- IBK기업은행 인턴
- structured_array
- 기러기목
- keras
- Python
- django
- 한국의새
- 흰날개해오라기
- 가마우지과
- 계수정렬
- Birthday paradox
- 딱다구리과
- 비둘기목
- 맑은소리 스피치학원
- 백로과
- 딥러닝 공부
- python3
- ADsP
- 오리과
- 딥러닝공부
- AI전략게임
- SimpleCraft
- 생일문제
- 비둘기과
- 직박구리과
- 한국의 새
Archives
- Today
- Total
진박사의 일상
[이코테] Greedy 문제 - 3. 문자열 뒤집기 본문
문제 : 0과 1로 된 문자열에서 연속된 숫자를 뒤집어 같은 숫자로 만들기
실패)
line = [int(i) for i in list(input())]
index_list = []
gini = 1.0 - line.count(0)/len(line) * line.count(1)/len(line)
result = 0
while gini != 1.0:
index = [0,0]
for i in range(len(line)-1):
if line[i] == line[i + 1]:
index[1] += 1
else:
index_list.append(index)
index = [i+1,i+1]
index_list.append(index)
print(index_list)
index = min(index_list, key=(lambda i: i[1]-i[0]))
line[index[0]:index[1]+1] = [1-a for a in line[index[0]:index[1]+1]]
gini = 1.0 - line.count(0)/len(line) * line.count(1)/len(line)
index_list = []
result += 1
print(result)
뻘짓했다(...)
쉬운 방법을 생각 못하고 쓸데없이 어렵게 생각하다가 완전 말아먹음.
정답)
line = [int(i) for i in list(input())]
result = 0
count0 = 0 #0으로 바뀌는 횟수
count1 = 0 #1로 바뀌는 횟수
if line[0]: #시작이 1이면
count0 += 1 #0으로 바뀜
else:
count1 += 1
for i in range(len(line)-1):
if line[i] != line[i+1]: #다르면
if line[i+1]: #0->1
count0 += 1
else: #1->0
count1 += 1
#print(count0, count1)
result = min(count1, count0)
print(result)
쉬운거였는데 엄청 해맸다. 끙...
'프로그래밍 > 코딩테스트 공부' 카테고리의 다른 글
[이코테] Greedy 문제 - 5. 볼링공 고르기 (0) | 2021.09.06 |
---|---|
[이코테] Greedy 문제 - 4. 만들 수 없는 금액 (0) | 2021.09.05 |
[이코테] Greedy 문제 - 2. 곱하기 혹은 더하기 (0) | 2021.09.05 |
[이코테] Greedy 문제 - 1. 모험가 길드 (0) | 2021.09.05 |
[TIL] 2021.07.16 - SQL 복습 (0) | 2021.07.16 |