πŸ“¦ Chango/🧩 Data Structure

[LRU μΊμ‹œκ΅μ²΄ μ•Œκ³ λ¦¬μ¦˜] ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€ 17680 : μΊμ‹œ

선달 2022. 8. 24. 01:57
λ°˜μ‘ν˜•

https://school.programmers.co.kr/learn/courses/30/lessons/17680?language=cpp# 

 

ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€

μ½”λ“œ μ€‘μ‹¬μ˜ 개발자 μ±„μš©. μŠ€νƒ 기반의 ν¬μ§€μ…˜ 맀칭. ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€μ˜ 개발자 λ§žμΆ€ν˜• ν”„λ‘œν•„μ„ λ“±λ‘ν•˜κ³ , λ‚˜μ™€ 기술 ꢁ합이 잘 λ§žλŠ” 기업듀을 맀칭 λ°›μœΌμ„Έμš”.

programmers.co.kr

 

μΊμ‹œ

μ§€λ„κ°œλ°œνŒ€μ—μ„œ κ·Όλ¬΄ν•˜λŠ” μ œμ΄μ§€λŠ” μ§€λ„μ—μ„œ λ„μ‹œ 이름을 κ²€μƒ‰ν•˜λ©΄ ν•΄λ‹Ή λ„μ‹œμ™€ κ΄€λ ¨λœ 맛집 κ²Œμ‹œλ¬Όλ“€μ„ λ°μ΄ν„°λ² μ΄μŠ€μ—μ„œ 읽어 λ³΄μ—¬μ£ΌλŠ” μ„œλΉ„μŠ€λ₯Ό κ°œλ°œν•˜κ³  μžˆλ‹€.
이 ν”„λ‘œκ·Έλž¨μ˜ ν…ŒμŠ€νŒ… 업무λ₯Ό λ‹΄λ‹Ήν•˜κ³  μžˆλŠ” μ–΄ν”ΌμΉ˜λŠ” μ„œλΉ„μŠ€λ₯Ό μ˜€ν”ˆν•˜κΈ° μ „ 각 λ‘œμ§μ— λŒ€ν•œ μ„±λŠ₯ 츑정을 μˆ˜ν–‰ν•˜μ˜€λŠ”λ°, μ œμ΄μ§€κ°€ μž‘μ„±ν•œ λΆ€λΆ„ 쀑 λ°μ΄ν„°λ² μ΄μŠ€μ—μ„œ κ²Œμ‹œλ¬Όμ„ κ°€μ Έμ˜€λŠ” λΆ€λΆ„μ˜ μ‹€ν–‰μ‹œκ°„μ΄ λ„ˆλ¬΄ 였래 κ±Έλ¦°λ‹€λŠ” 것을 μ•Œκ²Œ λ˜μ—ˆλ‹€.
μ–΄ν”ΌμΉ˜λŠ” μ œμ΄μ§€μ—κ²Œ ν•΄λ‹Ή λ‘œμ§μ„ κ°œμ„ ν•˜λΌκ³  λ‹¦λ‹¬ν•˜κΈ° μ‹œμž‘ν•˜μ˜€κ³ , μ œμ΄μ§€λŠ” DB μΊμ‹œλ₯Ό μ μš©ν•˜μ—¬ μ„±λŠ₯ κ°œμ„ μ„ μ‹œλ„ν•˜κ³  μžˆμ§€λ§Œ μΊμ‹œ 크기λ₯Ό μ–Όλ§ˆλ‘œ ν•΄μ•Ό νš¨μœ¨μ μΈμ§€ λͺ°λΌ λ‚œκ°ν•œ 상황이닀.

μ–΄ν”ΌμΉ˜μ—κ²Œ μ‹œλ‹¬λ¦¬λŠ” μ œμ΄μ§€λ₯Ό 도와, DB μΊμ‹œλ₯Ό μ μš©ν•  λ•Œ μΊμ‹œ 크기에 λ”°λ₯Έ μ‹€ν–‰μ‹œκ°„ μΈ‘μ • ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•˜μ‹œμ˜€.

μž…λ ₯ ν˜•μ‹

  • μΊμ‹œ 크기(cacheSize)와 λ„μ‹œμ΄λ¦„ λ°°μ—΄(cities)을 μž…λ ₯λ°›λŠ”λ‹€.
  • cacheSizeλŠ” μ •μˆ˜μ΄λ©°, λ²”μœ„λŠ” 0 ≦ cacheSize β‰¦ 30 이닀.
  • citiesλŠ” λ„μ‹œ μ΄λ¦„μœΌλ‘œ 이뀄진 λ¬Έμžμ—΄ λ°°μ—΄λ‘œ, μ΅œλŒ€ λ„μ‹œ μˆ˜λŠ” 100,000κ°œμ΄λ‹€.
  • 각 λ„μ‹œ 이름은 곡백, 숫자, 특수문자 등이 μ—†λŠ” 영문자둜 κ΅¬μ„±λ˜λ©°, λŒ€μ†Œλ¬Έμž ꡬ뢄을 ν•˜μ§€ μ•ŠλŠ”λ‹€. λ„μ‹œ 이름은 μ΅œλŒ€ 20자둜 이루어져 μžˆλ‹€.

좜λ ₯ ν˜•μ‹

  • μž…λ ₯된 λ„μ‹œμ΄λ¦„ 배열을 μˆœμ„œλŒ€λ‘œ μ²˜λ¦¬ν•  λ•Œ, "총 μ‹€ν–‰μ‹œκ°„"을 좜λ ₯ν•œλ‹€.

쑰건

  • μΊμ‹œ ꡐ체 μ•Œκ³ λ¦¬μ¦˜μ€ LRU(Least Recently Used)λ₯Ό μ‚¬μš©ν•œλ‹€.
  • cache hit일 경우 μ‹€ν–‰μ‹œκ°„μ€ 1이닀.
  • cache miss일 경우 μ‹€ν–‰μ‹œκ°„μ€ 5이닀.

 

풀이

 

쑰건에 주어진 μΊμ‹œκ΅μ²΄ μ•Œκ³ λ¦¬μ¦˜ LRU 에 λŒ€ν•œ 기본적인 지식이 μžˆμ–΄μ•Όν•˜λŠ” λ¬Έμ œμ˜€λ‹€. λ„ˆλ¬΄ν•΄ ;(

 

https://github.com/seondal/Daily_CS/issues/9

 

μΊμ‹œ ꡐ체 μ•Œκ³ λ¦¬μ¦˜μ— λŒ€ν•΄ μ„€λͺ…ν•΄μ£Όμ„Έμš” · Issue #9 · seondal/Daily_CS

μΊμ‹œ(νŽ˜μ΄μ§€) ꡐ체 μ•Œκ³ λ¦¬μ¦˜μ΄λž€ LRU (Last Recently Used) μ‚¬μš©μžμ—κ²Œ λΉ λ₯΄κ²Œ 정보λ₯Ό μ œκ³΅ν•˜κΈ° μœ„ν•΄ μ‚¬μš©ν•˜λŠ” μΊμ‹œμ—μ„œ μƒˆλ‘œμš΄ 데이터가 λ°œμƒν–ˆμ„λ•Œ, κ°€μž₯ μ˜€λž˜μ „μ— μ‚¬μš©λœ 데이터λ₯Ό μ œκ±°ν•˜κ³  μƒˆλ‘œμš΄

github.com

 

#include <string>
#include <vector>
#include <deque>
#include <algorithm>

using namespace std;

deque<string> cache;

int solution(int cacheSize, vector<string> cities) {
    int answer = 0;

    if(cacheSize == 0) return answer = 5*cities.size();
    
    for(string input : cities) {
        transform(input.begin(), input.end(), input.begin(), ::tolower);
        
        auto number = find(cache.begin(), cache.end(), input);
        
        if(number != cache.end()) {
            // Hit
            cache.erase(number);
            answer++;
        } else {
            // Miss
            if(cache.size() == cacheSize)
                cache.pop_front();
            answer += 5;
        }
        
        cache.push_back(input);
    }
    
    return answer;
}

https://school.programmers.co.kr/learn/courses/30/lessons/17680/solution_groups?language=cpp&type=my

 

μ‹œν–‰μ°©μ˜€

μ²˜μŒμ—λŠ” μΊμ‹œν¬κΈ° 0인경우λ₯Ό λ”°λ‘œ μ²˜λ¦¬ν•˜μ§€ μ•Šμ•˜λŠ”λ°,

κ·Έλž¬λ”λ‹ˆ ν…ŒμŠ€νŠΈ7κ³Ό ν…ŒμŠ€νŠΈ17μ—μ„œ μ‹€νŒ¨(signal: segmentation fault (core dumped)) κ°€ λ–΄λ‹€

 ν…ŒμŠ€νŠΈ 6μ—μ„œ μΊμ‹œν¬κΈ°κ°€ 0μž„μ—λ„ 닡은 잘 λ‚˜μ™”λŠ”λ°.. μ΄μœ λŠ” 잘 λͺ¨λ₯΄κ² λ‹€.. γ… 

 

https://school.programmers.co.kr/questions/19566

 

ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€

μ½”λ“œ μ€‘μ‹¬μ˜ 개발자 μ±„μš©. μŠ€νƒ 기반의 ν¬μ§€μ…˜ 맀칭. ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€μ˜ 개발자 λ§žμΆ€ν˜• ν”„λ‘œν•„μ„ λ“±λ‘ν•˜κ³ , λ‚˜μ™€ 기술 ꢁ합이 잘 λ§žλŠ” 기업듀을 맀칭 λ°›μœΌμ„Έμš”.

programmers.co.kr

λ‚˜μ²˜λŸΌ 7번 17번만 ν‹€λ¦¬λŠ” 경우의 해결책이 μžˆλŠ”λ°..

λ³Έ 글속 λ°˜λ‘€ ν…ŒμΌ€λ„ 잘 ν†΅κ³Όν•˜κ³  μ œμΆœλ§Œν•˜λ©΄ ν‹€λ¦¬λŠ”..상황..γ… γ… 

 

#include <string>
#include <vector>
#include <deque>
#include <algorithm>

using namespace std;

deque<string> cache;

int solution(int cacheSize, vector<string> cities) {
    int answer = 0;

    for(string input : cities) {
        transform(input.begin(), input.end(), input.begin(), ::tolower);
        
        auto number = find(cache.begin(), cache.end(), input);
        
        if(number != cache.end()) {
            // Hit
            cache.erase(number);
            answer++;
        } else {
            // Miss
            if(cache.size() == cacheSize)
                cache.pop_front();
            answer += 5;
        }
        
        cache.push_back(input);
    }
    
    return answer;
}
λ°˜μ‘ν˜•

'πŸ“¦ Chango > 🧩 Data Structure' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

λ°°μ—΄κ³Ό 리슀트둜 큐 κ΅¬ν˜„ν•˜κΈ°  (0) 2022.09.04