[TIL_Carrotww] 82 - 22/12/28

rjgjflยท2022๋…„ 12์›” 28์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
97/119
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 ์บ์‹œ ๊ต์ฒด ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋“ค์–ด๋ดค๋‚˜ ์ •๋„์ธ ๊ฒƒ ๊ฐ™๋‹ค.

๐Ÿงฒ ๋Š๋‚€์ 

๐Ÿ” ์˜ค๋žœ๋งŒ์— ํ•ด์„œ ๋งŽ์ด ๋…น์Šฌ์—ˆ๋‹ค. ๋นก์„ธ๊ฒŒ ๋‹ค์‹œ ๋‹ฌ๋ฆฌ์ž

0๊ฐœ์˜ ๋Œ“๊ธ€