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
- 딕셔너리
- dict
- 브루트포스
- 코딩테스트
- 수학
- MYSQL
- python
- DP
- level3
- level2
- 다이나믹 프로그래밍
- 그리디
- programmers
- BFS
- 가상메모리 관리
- 운영체제
- 파이썬
- N과M
- 가상메모리
- 다익스트라
- 스택
- BOJ
- 백준
- 힙
- level0
- 구현
- level1
- 재귀
- dfs
- 프로그래머스
Archives
- Today
- Total
동캄의 코딩도장
프로그래머스 level2 [문자열 압축] 파이썬 본문
https://programmers.co.kr/learn/courses/30/lessons/60057
#프로그래머스 문자열 압축
def solution(s):
answer = 0
for i in range(1,(len(s)//2)+1):
cnt=1
lst=[]
j=0
previous=''
while (j+i)<=len(s):
if s[j:j+i]==previous:
cnt+=1
else:
if cnt!=1:
lst.append(cnt-1)
cnt=1
previous=s[j:j+i]
j+=i
if cnt>1:
lst.append(cnt-1)
gain=0
for k in range(len(lst)):
gain+=lst[k]*i-len(str(lst[k]+1))
answer=max(answer,gain)
return len(s)-answer
s길이의 절반까지 반복문을 시행한다. (길이가 절반이상이면 압축 불가능)
1부터 s절반길이까지 반복문을 반복해 나가면서, 문자열 압축 개수를 카운트하여 lst에 담는다.
gain을 통해 압축했을 때 얻는 이득을 구하고, 최종 길이( len(s)-answer)을 리턴한다.
'코테 > 프로그래머스' 카테고리의 다른 글
프로그래머스 level2 [위장] 파이썬 (0) | 2021.12.10 |
---|---|
프로그래머스 level2 [카펫] 파이썬 (0) | 2021.12.10 |
프로그래머스 level2 [방문 길이] 파이썬 (0) | 2021.12.09 |
프로그래머스 level2 [ [3차] 파일명 정렬] 파이썬 (0) | 2021.12.09 |
프로그래머스 level2 [1차 캐시] 파이썬 (0) | 2021.12.08 |