5W.6D-summery

Dazz_heyDay ·2021년 8월 1일
1

Python) Algorithm_study

목록 보기
29/39

✏️[도토리 키재기]

참고블로그 : https://handhand.tistory.com/178

import sys

N, K, D = list(map(int, sys.stdin.readline().split()))
rules = [list(map(int, sys.stdin.readline().split())) for _ in range(K)]

def dotori(pivot):
    total = 0
    for start, end, step in rules:
        beta = min(end, pivot)
        if start <= beta:
            calc = (beta - start) // step + 1
            total += calc
    return total

def sol():
    lo, hi = 1, 1000000
    ans = 0
    while lo <= hi:
        mid = (lo + hi) // 2
        if dotori(mid) >= D:
            ans = mid
            hi = mid - 1
        else:
            lo = mid + 1
    return ans


print(sol())

✏️문제[가사 검색]

https://ariz1623.tistory.com/274

import bisect
import collections

def func(a,left,right):
    left_idx = bisect.bisect_left(a,left)
    right_idx = bisect.bisect_right(a,right)

    return right_idx - left_idx

def solution(words, queries):
    answer = []
    # 단어 길이 순 딕셔너리 생성
    dic = collections.defaultdict(list)
    dic_reverse = collections.defaultdict(list)
    for word in words:
        
        dic[len(word)].append(word)
        dic_reverse[len(word)].append(word[::-1])

    #정렬
    for key in dic.keys():
        dic[key].sort()
        dic_reverse[key].sort()

    for query in queries: 
        #접미사에 와일드 카드
        if query[0] != '?':
            answer.append(func(dic[len(query)],query.replace('?','a'),query.replace('?','z')))
        #접두사에 와일드 카드
        else :
            query = query[::-1]
            answer.append(func(dic_reverse[len(query)],query.replace('?','a'),query.replace('?','z')))
    return answer
profile
Why.Not.Now

2개의 댓글

comment-user-thumbnail
2021년 8월 1일

도토리 키재기 문제 저도 잘 이해가 안가요😂 이번주 정말 고생 많으셨습니다👍👍 다음주도 같이 열심히 해요!!

답글 달기
comment-user-thumbnail
2021년 8월 2일

안녕하세요 알고리줌입니다!
이진탐색주 복습 확인했습니다!
다음주도 화이팅해요!

답글 달기