Notice
Recent Posts
Recent Comments
Today
Total
01-11 01:45
Link
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Partially Committed

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ „ํ™”๋ฒˆํ˜ธ ๋ชฉ๋ก ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm || ๋ฌธ์ œํ’€์ด/PS

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ „ํ™”๋ฒˆํ˜ธ ๋ชฉ๋ก

WonderJay 2022. 7. 8. 18:38
728x90
๋ฐ˜์‘ํ˜•
SMALL

https://school.programmers.co.kr/learn/courses/30/lessons/42577

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

  phone_book ์˜ ๊ธธ์ด๋Š” ์ตœ๋Œ€ 1,000,000 ์ด๊ณ  ๊ฐ๊ฐ์˜ ์ „ํ™”๋ฒˆํ˜ธ์˜ ๊ธธ์ด๋Š” ์ตœ๋Œ€ 20 ์ด๋‹ค. 

์ฆ‰, ์ตœ์•…์˜ ๊ฒฝ์šฐ์—๋Š” 20 ์ž๋ฆฟ์ˆ˜์˜ ์ „ํ™”๋ฒˆํ˜ธ 1,000,000 ๊ฐœ๊ฐ€ ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์˜จ๋‹ค๋Š” ๊ฒƒ์ธ๋ฐ ์ง๊ด€์ ์œผ๋กœ ๊ฐ€์žฅ ์‰ฝ๊ฒŒ ๋– ์˜ฌ๋ฆด ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์€ ์ •๋ ฌ์„ ์ˆ˜ํ–‰ํ•œ ๋’ค 2์ค‘ ๋ฃจํ”„๋ฅผ ์ด์šฉํ•ด์„œ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์กฐ์‚ฌํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋ฉด O(nlogn) + O(n^2) ์ด๊ณ  n = 1,000,000 ์œผ๋กœ TLE ์ฒ˜๋ฆฌ๋ฅผ ๋ฐ›๋Š”๋‹ค.

  ํ•ด์‰ฌ๋งต์„ ์ด์šฉํ•ด์„œ ํ’€์ดํ•˜๋ฉด ๋Œ€๋žต O(n*20) ~= O(n) ์— ์ˆ˜ํ–‰์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

 

[C++]

#include <bits/stdc++.h>
#define fastio cin.tie(0)->sync_with_stdio(0)
#define ll long long
using namespace std;


bool solution(vector<string> phone_book) {
    map<string, bool> h_map;
    bool answer = true;

    for (auto& ele : phone_book)
    {
        h_map[ele] = true;
    }

    for (int i = 0; i < phone_book.size(); i++)
    {
        string temp = "";
        temp = phone_book[i];
        h_map[temp] = false;
        for (int j = 1; j <= temp.size(); j++)
        {
            if (h_map[temp.substr(0, j)])
            {
                answer = false;
                break;
            }
        }
        h_map[temp] = true;
    }
    return answer;
}

728x90
๋ฐ˜์‘ํ˜•
LIST
Comments