[TIL_Carrotww] 82 - 22/12/28

μœ ν˜•μ„Β·2022λ…„ 12μ›” 28일
0

TIL

λͺ©λ‘ 보기
97/138
post-thumbnail

πŸ“Carrotww의 μ½”λ”© 기둝μž₯

🧲 python algoritm

πŸ” Programmers ν…Œμ΄λΈ” ν•΄μ‹œ ν•¨μˆ˜ Level 2

def solution(data, col, row_begin, row_end):
    data = sorted(data, key=lambda x:(x[col-1], -x[0]))
    result = []
    for i in range(len(data)):
        data[i].append(i+1)
    temp_data = data[row_begin - 1:row_end]
    for i in temp_data:
        i_temp = 0
        for j in range(len(i) - 1):
            i_temp += (i[j] % i[-1])
        result.append(i_temp)

    temp = result[0]
    for i in result[1:]:
        temp = temp ^ i

    return temp

문제 자체λ₯Ό μ΄ν•΄ν•˜λŠ”λ° λ„ˆλ¬΄ μ˜€λž˜κ±Έλ Έλ‹€. ν‘ΈλŠ”λ°λŠ” μ–Όλ§ˆ μ•ˆκ±Έλ ΈλŠ”λ°...
μ˜€λžœλ§Œμ— ν’€μ–΄μ„œ κ·ΈλŸ°μ§€ μ΄ν•΄ν•˜κΈ° λ‚œν•΄ν–ˆλ‹€.

ν‘ΈλŠ” 방식은 κ°„λ‹¨ν•˜λ‹€
λ¬Έμ œμ—μ„œ μ›ν•˜λŠ” λ°©μ‹λŒ€λ‘œ 정렬을 ν•΄μ€€ ν›„
주어진 λ²”μœ„λŒ€λ‘œ xor ν•˜μ—¬ 값을 λ°˜ν™˜ν•˜λ©΄ 끝이닀.
javacript에 reduce λΌλŠ” ν•¨μˆ˜κ°€ μžˆλŠ”λ° python에도 import ν•˜μ—¬μ„œ μ‚¬μš© κ°€λŠ₯ν•˜λ‹€.
reduceλ₯Ό μ‚¬μš©ν•˜λ©΄ 이쀑for문을 μ‚¬μš©ν•˜μ§€ μ•Šκ³  μ½”λ“œλ₯Ό μ’€ 더 쀄일 수 μžˆλ‹€.
μΆ” 후에 λ‹€μ‹œ 풀어봐야겠닀.

πŸ” Programmers 카카였 μΊμ‹œ Level2

def solution(cacheSize, cities):
    cities = [x.lower() for x in cities]
    cache = set()
    result = 0
    time = 0

    for i in cities:
        time += 1
        temp_cache = [x[0] for x in cache]
        if i in temp_cache:
            for j in cache:
                if i == j[0]:
                    cache.remove(j)
                    cache.add((i, time))
                    continue
            result += 1
            continue

        result += 5
        if len(cache) < cacheSize:
            cache.add((i, time))
        else:
            if cacheSize < 2:
                continue
            Recent_val = min(cache, key=lambda x:x[1])
            cache.remove(Recent_val)
            cache.add((i, time))

    return result

이 λ¬Έμ œλŠ” 보자마자 μ΄ν•΄λŠ” λ°”λ‘œ λ˜μ—ˆμ§€λ§Œ μ΄μƒν•œ κ³³μ—μ„œ μ‹œκ°„μ΄ λŒλ Έλ‹€ γ… γ… 
λ¬Έμ œμ— λŒ€μ†Œλ¬Έμžλ₯Ό κ΅¬λΆ„ν•˜μ§€ μ•ŠλŠ”λ‹€κ³  λ‚˜μ™€μžˆμ–΄ λŒ€μ†Œλ¬Έμžλ₯Ό κ΅¬λΆ„ν•˜μ§€ μ•Šμ•„μ„œ 계속 μ‹€νŒ¨ν•΄ μ’€ λ§Žμ€ μ‹œκ°„μ„ 썻고

문제의 ν‚€ 포인트? λŠ” LRU μΊμ‹œ ꡐ체 μ•Œκ³ λ¦¬μ¦˜μ„ λ“€μ–΄λ΄€λ‚˜ 정도인 것 κ°™λ‹€.

🧲 λŠλ‚€μ 

πŸ” μ˜€λžœλ§Œμ— ν•΄μ„œ 많이 λ…ΉμŠ¬μ—ˆλ‹€. λΉ‘μ„Έκ²Œ λ‹€μ‹œ λ‹¬λ¦¬μž

profile
Carrot_hyeong

0개의 λŒ“κΈ€