[프로그래머스] [1차] 캐시

박신희·2022년 4월 12일
0

[풀이] 프로그래머스

목록 보기
13/33
post-thumbnail

❗️ 풀이 과정

  • 제일 먼저 검사해야할 조건문 : 캐시에 있나?
    • 캐시에 있을 경우 : hit이기 때문에 +1, update 해줘야함
    • 캐시에 없을 경우 : miss, 캐시가 꽉 찼니?
      • 캐시가 남았을 경우 : 넣기만 하면 됨
      • 캐시가 꽉 찼을 경우 : 사용한지 제일 오래된 걸 빼고 새로운 걸 넣는다.

🤜 풀이 코드

def solution(cacheSize, cities):
    answer = 0
    cache=[]
    for c in cities:
        c=c.upper()
        if c in cache:
            # hit
            answer+=1
            # update the time
            del cache[cache.index(c)]
            cache.append(c)
        else:
            # miss
            answer+=5
            if len(cache)<cacheSize:
                cache.append(c)
            else:
                # 최소값을 빼고 집어넣기
                cache.append(c)
                cache.pop(0)
            
    return answer
profile
log my moments 'u')/

0개의 댓글