-
Notifications
You must be signed in to change notification settings - Fork 1
32-wnsmir #131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
wnsmir/ํด์/์์ ๊ฒ์.py
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
16๊ฐ ์กฐํฉ์ ๋ง๋๋ ๋ถ๋ถ์ด ์์ ์ด๋ค์.
์ ๋ 16๊ฐ ํ๋์ฝ๋ฉ ํ์ต๋๋ค.
๋ชธ์ด ์กฐ๊ธ ๊ณ ์ํ๋ค๋ ๋ง์ธ๋..;;
kangrae-jo
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ฒ์์๋ map์ map์ map์.... ๋ก ์ ์ฅํด์
์ธ๋ฑ์ค ์ ๊ทผ์ํด์ ๊ฐ ์ฟผ๋ฆฌ์ ํด๋นํ๋ ๋ต์ ๊ตฌํ์ต๋๋ค.
์ ๋ ํ 20๋ถ ๊ฑธ๋ ธ๋๋ฐ ์๊ฐ์ด๊ณผ๊ฐ ๋ฌ์ต๋๋ค.
๊ทธ๋์ map์ map์ map์... ๋ฅผ ์ฐ์ง ์๊ธฐ ์ํด์ string์ผ๋ก ํฉํด์ ๊ทธ๊ฑธ key๋ก ์ผ์ต๋๋ค.
๊ทธ๋๋ ์๊ฐ์ด๊ณผ๋๋ผ๊ตฌ์.
๊ทธ๋์ ์ ๋ gpt ์๊ฒ ๋ฌผ์ด๋ดค๋๋ฐ, query๋ฅผ ํด๊ฒฐํ ๋ ์ด๋ถํ์์ ์จ์ผํ๋ค๊ณ ํ๋๋ผ๊ตฌ์...
info ๊ฐ์๊ฐ 5๋ง๊ฐ ๋ฐ์ ๋์ง ์๋๋ฐ ํจ์จ์ฑ ํ
์คํธ์๋ ๊ทน์
์ ํ
์ผ๊ฐ ์๋๋ด์.
๊ทธ๋์ ์ด๋ถํ์์ผ๋ก ๋ฐ๊พธ์ด์คฌ๋๋ ์ ๋ต์ด์์ต๋๋ค.
์ค์ฉ๋ ๋ง์๋๋ก ์ด ๋ฌธ์ ํต์ฌ์
- ์ ์ ํ ์๋ฃ๊ตฌ์กฐ๋ฅผ ์ฌ์ฉํ ์ ์ฅ
- ์ด๋ถํ์์ ํตํด query ํ๊ธฐ
๊ฐ ๋๊ฒ ๋ค์.
์ข์ ๋ฌธ์ ๊ฐ์ฌํฉ๋๋ค.
#include <string>
#include <vector>
#include <unordered_map>
#include <sstream>
#include <algorithm>
using namespace std;
const string ANY = "-";
unordered_map<string, vector<int>> um;
string key(const string& a, const string& b, const string& c, const string& d) {
return a + b + c + d;
}
vector<int> solution(vector<string> info, vector<string> query) {
for (const string& str : info) {
string lang, pos, year, food;
int score;
stringstream ss(str);
ss >> lang >> pos >> year >> food >> score;
um[key(lang, pos, year, food)].push_back(score);
um[key(ANY, pos, year, food)].push_back(score);
um[key(lang, ANY, year, food)].push_back(score);
um[key(lang, pos, ANY, food)].push_back(score);
um[key(lang, pos, year, ANY )].push_back(score);
um[key(ANY, ANY, year, food)].push_back(score);
um[key(ANY, pos, ANY, food)].push_back(score);
um[key(ANY, pos, year, ANY )].push_back(score);
um[key(lang, ANY, ANY, food)].push_back(score);
um[key(lang, ANY, year, ANY )].push_back(score);
um[key(lang, pos, ANY, ANY )].push_back(score);
um[key(ANY, ANY, ANY, food)].push_back(score);
um[key(ANY, ANY, year, ANY )].push_back(score);
um[key(ANY, pos, ANY, ANY )].push_back(score);
um[key(lang, ANY, ANY, ANY )].push_back(score);
um[key(ANY, ANY, ANY, ANY )].push_back(score);
}
// socre ๊ธฐ์ค ์ ๋ ฌ
for (auto& entry : um) sort(entry.second.begin(), entry.second.end());
vector<int> answer(query.size(), 0);
for (int i = 0; i < query.size(); i++) {
string lang, pos, year, food, ands;
int score;
stringstream ss(query[i]);
ss >> lang >> ands >> pos >> ands >> year >> ands >> food >> score;
auto it = um.find(key(lang, pos, year, food));
if (it == um.end()) {
answer[i] = 0;
continue;
}
// v๋ ์ ๋ ฌํ์ผ๋ ์ด๋ถํ์
vector<int> v = it->second;
answer[i] = (v.end() - lower_bound(v.begin(), v.end(), score));
}
return answer;
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
C++์์๋ ๊ทธ๋ฅ ๋ฌด์ํ๊ฒ ๊ฐ ์นดํ ๊ณ ๋ฆฌ ๋ณ ์ ์ ๋ฆฌ์คํธ ํ ์ด๋ธ์ ๋งค ์ง์๋ง๋ค ์ํํด๋ ํต๊ณผํ๊ธธ๋ ํ์ด์ฌ๋ ๋๊ฐ์ด ๋๋ ์ค ์๊ณ ๋ธ๊น ํ๋๋ฐ... ์ด์ง ํ์์ ์จ์ผ ํจ์จ์ฑ์ ํต๊ณผํ๋๊ตฐ์...
#include <vector>
#include <string>
#include <unordered_map>
#include <sstream>
using namespace std;
vector<int> solution(vector<string> info, vector<string> query)
{
unordered_map<string, int> cat =
{
{"cpp", 0}, {"java", 1}, {"python", 2},
{"backend", 0}, {"frontend", 1},
{"junior", 0}, {"senior", 1},
{"chicken", 0}, {"pizza", 1},
};
vector<int> table[3][2][2][2];
for (const auto &str : info)
{
istringstream ss(str); string buffer;
vector<string> v;
while (getline(ss, buffer, ' '))
v.emplace_back(buffer);
table[cat[v[0]]][cat[v[1]]][cat[v[2]]][cat[v[3]]].emplace_back(stoi(v[4]));
}
vector<int> result;
for (const auto &q : query)
{
istringstream ss(q); string buffer;
vector<string> v;
while (getline(ss, buffer, ' '))
v.emplace_back(buffer);
int score = stoi(v[7]);
int cnt = 0;
vector<int> range[4];
if (v[0] == "-") range[0] = {0, 1, 2};
else range[0] = {cat[v[0]]};
if (v[2] == "-") range[1] = {0, 1};
else range[1] = {cat[v[2]]};
if (v[4] == "-") range[2] = {0, 1};
else range[2] = {cat[v[4]]};
if (v[6] == "-") range[3] = {0, 1};
else range[3] = {cat[v[6]]};
for (const auto &i : range[0])
for (const auto &j : range[1])
for (const auto &k : range[2])
for (const auto &l : range[3])
for (const auto &s : table[i][j][k][l])
if (s >= score) cnt++;
result.emplace_back(cnt);
}
return result;
}
kokeunho
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ ๋ ์ง์ ์์ด๋ ๊ฒฐ๊ตญ ๊ฐ์ด ํ์์ต๋๋ค.
์ด๋ฐ ํ์ด๋ ๋์ ํ ์ค์ค๋ก ์๊ฐํด๋ผ ์๊ฐ ์์๋ค์...
ํจ์จ์ฑ ๊ฒ์ฌ๋ฅผ ์ํด ์ด๋ถ ํ์๋ ์ฌ์ฉํด์ผํ๊ณ , ์ฌ๋ฌ๋ชจ๋ก ์ฌ๋ฐ๋ ๋ฌธ์ ์์ต๋๋ค.
๊ทผ๋ฐ ์ ์ฒ๋ฆฌ์ O(N log N)์ด ๋ ๋ค๊ณ ํ์
จ๋๋ฐ
info๋ง๋ค key๊ฐ ๋ง๋ค์ด์ list์ ์ ์ฅ ํ score ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํด์ O(N log N)์ธ๊ฑธ๊น์?
Details
import java.util.*;
class Solution {
public int[] solution(String[] info, String[] query) {
String[] languages = {"cpp", "java", "python", "-"};
String[] jobs = {"backend", "frontend", "-"};
String[] careers = {"junior", "senior", "-"};
String[] foods = {"chicken", "pizza", "-"};
Map<String, List<Integer>> map = new HashMap<>();
for (String l : languages) {
for (String j : jobs) {
for (String c : careers) {
for (String f : foods) {
String key = l + j + c + f;
map.put(key, new ArrayList<>());
}
}
}
}
for (String detail : info) {
String[] parts = detail.split(" ");
String l = parts[0], j = parts[1], career = parts[2], food = parts[3];
int score = Integer.parseInt(parts[4]);
String[] ls = {l, "-"};
String[] js = {j, "-"};
String[] cs = {career, "-"};
String[] fs = {food, "-"};
for (String a : ls) {
for (String b : js) {
for (String c : cs) {
for (String f : fs) {
String key = a + b + c + f;
map.get(key).add(score);
}
}
}
}
}
for (List<Integer> list : map.values()) {
Collections.sort(list);
}
int n = query.length;
int[] answer = new int[n];
for (int i = 0; i < n; i++) {
String q = query[i].replaceAll(" and ", " ");
String[] parts = q.split(" ");
String key = parts[0] + parts[1] + parts[2] + parts[3];
int score = Integer.parseInt(parts[4]);
List<Integer> list = map.get(key);
if (list == null || list.isEmpty()) {
answer[i] = 0;
continue;
}
int low = 0, high = list.size();
while (low < high) {
int mid = (low + high) / 2;
if (list.get(mid) < score) low = mid + 1;
else high = mid;
}
answer[i] = list.size() - low;
}
return answer;
}
}
g0rnn
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ด๋ ต๋ค์..key-value๋ก map์ ๊ตฌ์ํด๋ด๋ ๊ณผ์ ์ด ์ ์ด๋ ๊ฒ ์๊ฐ๋์ง ์๋์ง.. ์์ฌ์์ด ๋ง์ ๋ฌธ์ ์ ๋๋ค.
์ด๋ ค์์ ํ๋ค๊ฐ ๋ค๋ฅธ ๋ถ๋ค์ ์ฝ๋๋ฅผ ๋ดค๊ณ , ๊ทธ๋ฌ๋ค๋ณด๋ ์ฝ๋๊ฐ ๋ง์ด ๋น์ทํด์ก๋ค์..
๋ฏธ๋ฆฌ key-value๋ก ๊ตฌ์ฑ๋ map์ ์ ๋ถ ๋ง๋ค๊ณ , info๋ก๋ถํฐ ๊ฐ๋ฅํ ๋ชจ๋ ํค ์กฐํฉ์ ๊ฐ์ ์ฝ์ ํ์์ต๋๋ค.
์ฌ๋ฐ๋ ๋ฌธ์ ๊ฐ์ฌํฉ๋๋ค :)
import java.util.*;
class Solution {
public int[] solution(String[] info, String[] query) {
int[] answer = new int[query.length];
Map<String, List<Integer>> map = makeWithAllKey();
for (String data : info) {
StringTokenizer st = new StringTokenizer(data);
String lang = st.nextToken();
String job = st.nextToken();
String career = st.nextToken();
String food = st.nextToken();
Integer score = Integer.parseInt(st.nextToken());
for (String l : new String[]{lang, "-"}) {
for (String j : new String[]{job, "-"}) {
for (String c : new String[]{career, "-"}) {
for (String f : new String[]{food, "-"}) {
String key = l + j + c + f;
map.get(key).add(score);
}
}
}
}
}
for (List<Integer> score : map.values()) {
score.sort(Comparator.naturalOrder());
}
for (int i = 0; i < query.length; i++) {
String q = query[i].replaceAll(" and ", " ");
String[] qs = q.split(" ");
String key = qs[0] + qs[1] + qs[2] + qs[3];
Integer target = Integer.parseInt(qs[4]);
List<Integer> score = map.get(key);
int s = 0, e = score.size();
while (s < e) {
int mid = (s + e) / 2;
if (score.get(mid) < target) s = mid + 1;
else e = mid;
}
answer[i] = score.size() - s;
}
return answer;
}
private Map<String, List<Integer>> makeWithAllKey() {
String[] lang = {"cpp", "java", "python", "-"};
String[] jobs = {"backend", "frontend", "-"};
String[] career = {"junior", "senior", "-"};
String[] foods = {"chicken", "pizza", "-"};
Map<String, List<Integer>> m = new HashMap<>();
for (String l : lang) {
for (String j : jobs) {
for (String c : career) {
for (String f : foods) {
String key = l + j + c + f;
m.put(key, new ArrayList<>());
}
}
}
}
return m;
}
}
๐ ๋ฌธ์ ๋งํฌ
https://school.programmers.co.kr/learn/courses/30/lessons/72412
โ๏ธ ์์๋ ์๊ฐ
40min
โจ ์๋ ์ฝ๋
์ฟผ๋ฆฌ์ ํ์์ ๋ณด๋ฅผ ๋ฏธ๋ฆฌ ํ์ฑํด๋๊ณ ๊ฐ ์ฟผ๋ฆฌ๋ฅผ ํ์๋ฆฌ์คํธ์ ๋์กฐํด๋ณด๋ฉด์ ์ ๋ต๋ฆฌ์คํธ์ ์๋ฅผ ์ฑ์๋๊ฐ๋ ๊ทธ๋ฅ ๊ตฌํ์ผ๋ก 20๋ถ๋ง์ ํ์๋ค๊ฐ...
๋ฐ๋ก ์๊ฐ์ด๊ณผ๋์ GPT์ํจ๊ป ๋ฌธ์ ๋ฅผ ํ์ด๋ณด์์ต๋๋ค ใ ..
์ด๋ ๊ฒ ํ๊ฒ๋๋ฉด ์ผ์ข ์ ๋ธ๋ฃจํธํฌ์ค๋ฐฉ์์ผ๋ก ์๊ฐ๋ณต์ก๋๊ฐ N*Q๊ฐ ๋ฉ๋๋ค.
๋ฐ๋ผ์ ์งํผํฐ5๋ ์ ์ฒ๋ฆฌ + ์ด๋ถํ์์ ํ์ฉํ๋ผ๊ณ ๊ฐ๋ฅด์ณ์ฃผ์์ต๋๋ค..
โข 4๊ฐ ํ๋ ๊ฐ๊ฐ์ด **๊ฐ ๋๋ -**์ผ ๋ฟ โ ์ด 2โด=16 ๊ฐ์ง ํค ์กฐํฉ
โข ๊ฐ ์ง์์์ ๋ํด 16๊ฐ์ ๋ชจ๋ ํค๋ฅผ ๋ง๋ค์ด ํด๋น ํค์ ์ ์ ๋ฆฌ์คํธ์ ํธ์
โข ์ฟผ๋ฆฌ๋ 4ํ๋์ ์ ํํ ํค(์ -)๋ก ํด๋น ๋ฆฌ์คํธ๋ง ์กฐํ โ ์ด๋ถ ํ์์ผ๋ก โ์ต์ ์ ์ ์ด์โ์ ๊ฐ์๋ฅผ O(log L)์ ๊ณ์ฐ
16๊ฐ์ง์ ํค๋ ๊ฐ ์์ฑ์ ๊ฐ๋ค์ด ์๋๋ผ -์ธ์ง ๊ฐ์ธ์ง ์ ๊ฒฝ์ฐ์ ์์ ๋๋ค.
์ด๋ถ๋ถ์ ์ฒ์์ ํ๊ฐ๋ ธ๋๋ฐ ๊ฐ ์ฟผ๋ฆฌ๋ ์์ผ๋์นด๋ ์๋๋ฉด ์ด๋ค ๊ฐ์ด ์ฃผ์ด์ง๊ณ , ์์ผ๋์นด๋๋ผ๋ฉด ํด๋น์กฐ๊ฑด์ ๋ฌด์ํด๋ ๋ฉ๋๋ค.
์ฆ info: python backend junior pizza 150์์ ์๊ธฐ๋ ํค๋ค์
โข("python","backend","junior","pizza") โ ์ ํ ์ผ์น์ฉ
โข("python","backend","junior","-")
โข("python","-","junior","pizza")
โข("-","backend","junior","pizza")
โข("-","-","-","-")
โฆ (์ด 16๊ฐ) ๊ฐ ๋ฉ๋๋ค.
๋ฐ๋ผ์ ์ ์ฒ๋ฆฌ O(N log N) + ์ฟผ๋ฆฌ๋น O(log L) ๋ ํจ์จ์ฑ๊ฒ์ฌ๋ ํต๊ณผํฉ๋๋ค!