알고리즘/구현

프로그래머스 - 순위 검색

2023. 10. 19. 19:01
목차
  1. 문제
  2. 문제 풀이

문제

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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

조건에 해당하는 사람의 수를 구하는 문제였다.

 

문제 풀이

완전탐색을 하게 될 경우 5000 * 100000번의 연산을 해야 해서 절대 시간 안에 못 풀 것 같았다. 점수순으로 정렬 후 조건으로 찾아볼까도 했는데 많은 시간이 줄어들 것 같진 않았다. 그래서 5차원 벡터를 MAP 처럼 만들어 사용했다.

 

먼저 info의 정보를 분해해서 벡터에 저장하고, query 또한 같은 방법으로 분해해서 어떤 list를 더해야 할지 정했다.

 

다 풀고 나서 생각하니... 굳이 " "로 단어를 자르지 않고 통째로 해시맵에 넣어주고.. 조건이 -일 경우에는 string을 더해줘서 키값을 만들어서 풀면 훨씬 간편하게 풀 수 있을 것 같다. 왜 다 나눠서 저장해야 한다고 생각했을까..?

 

#include <string>
#include <vector>
#include <map>
#include <iostream>
using namespace std;
vector<vector<vector<vector<vector<int>>>>> infoMap(3,vector<vector<vector<vector<int>>>>(2, vector<vector<vector<int>>>(2,vector<vector<int>>(2,vector<int>()))));
//개발언어(cpp, java, python), 직군(backend, frontend), 경력(junior, senior)
//소울푸드(chicken, pizza), 점수(자연수)
map<string, int> lanMap, jobMap, careerMap, foodMap;
void printInfoMap(){
for(int a=0;a<3;a++){
for(int b=0;b<2;b++){
for(int c=0;c<2;c++){
for(int d=0;d<2;d++){
for(int e=0;e<infoMap[a][b][c][d].size();e++){
printf("a: %d b: %d c: %d d: %d score: %d\n", a,b,c,d, infoMap[a][b][c][d][e]);
}
}
}
}
}
printf("\n");
}
void initMap(){
lanMap.insert({"cpp", 0});
lanMap.insert({"java", 1});
lanMap.insert({"python", 2});
lanMap.insert({"-", -1});
jobMap.insert({"backend", 0});
jobMap.insert({"frontend", 1});
jobMap.insert({"-", -1});
careerMap.insert({"junior", 0});
careerMap.insert({"senior", 1});
careerMap.insert({"-", -1});
foodMap.insert({"chicken", 0});
foodMap.insert({"pizza", 1});
foodMap.insert({"-", -1});
}
void initInfoMap(vector<string> info){ //info 내용을 infoMap에 저장
string word;
int cnt;
int a,b,c,d,e; //index
for(string s:info){
word="";
cnt =a=b=c=d=e= 0;
for(int i=0;i<s.length();i++){
if(s[i] == ' '|| i == s.length()-1){
//cout<<"cnt: "<<cnt<<" word: "<<word<<endl;
if(cnt == 0){ //lan
a = lanMap[word];
}
else if(cnt == 1){ //job
b = jobMap[word];
}
else if(cnt == 2){ //career
c = careerMap[word];
}
else if(cnt == 3){ //food
d = foodMap[word];
}
else{ //score
//printf("%d\n",stoi(word));
word+=s[i];
infoMap[a][b][c][d].push_back(stoi(word));
}
word = "";
cnt++;
}
else{
word+=s[i];
}
}
}
}
int countQueryInfo(string s){
int a,b,c,d;
int score;
string word="";
int cnt=0;
for(int i=0;i<s.length();i++){ //query 분석
if(s[i] == ' '|| i == s.length()-1){
if(word.compare("and") == 0){
word="";
continue;
}
if(cnt == 0){ //lan
a = lanMap[word];
}
else if(cnt == 1){ //job
b = jobMap[word];
}
else if(cnt == 2){ //career
c = careerMap[word];
}
else if(cnt == 3){ //food
d = foodMap[word];
}
else{ //score
//printf("%d\n",stoi(word));
word+=s[i];
score = stoi(word);
}
word="";
cnt++;
}
else{
word+=s[i];
}
}
//printf("query score: %d\n",score);
vector<int> alist,blist,clist,dlist;
if(a == -1){
for(int i=0;i<3;i++){
alist.push_back(i);
}
}
else alist.push_back(a);
if(b == -1){
for(int i=0;i<2;i++){
blist.push_back(i);
}
}
else blist.push_back(b);
if(c == -1){
for(int i=0;i<2;i++){
clist.push_back(i);
}
}
else clist.push_back(c);
if(d == -1){
for(int i=0;i<2;i++){
dlist.push_back(i);
}
}
else dlist.push_back(d);
int sum=0;
int ai,bi,ci,di;;
for(int i=0;i<alist.size();i++){
ai=alist[i];
for(int j=0;j<blist.size();j++){
bi=blist[j];
for(int k=0;k<clist.size();k++){
ci=clist[k];
for(int l=0;l<dlist.size();l++){
di=dlist[l];
for(int o=0;o<infoMap[ai][bi][ci][di].size();o++){
//printf("score: %d 비교: %d", score, infoMap[ai][bi][ci][di][o]);
if(infoMap[ai][bi][ci][di][o] >= score) {
sum++;
}
}
}
}
}
}
//printf("sum: %d\n",sum);
return sum;
}
vector<int> solution(vector<string> info, vector<string> query) {
vector<int> answer;
initMap();
initInfoMap(info);
//printInfoMap();
//printf("1번: %d\n", infoMap[1][0][0][1].size());
for(string q:query){
answer.push_back(countQueryInfo(q));
}
return answer;
}
  1. 문제
  2. 문제 풀이
'알고리즘/구현' 카테고리의 다른 글
  • 백준 - 14499
  • 백준 - 13458
  • 프로그래머스 - 주차 요금 계산
  • 프로그래머스 - 숫자 문자열과 영단어
hahihi
hahihi
hahihi
히호 노트
hahihi
전체
오늘
어제
  • 분류 전체보기 (224)
    • 알고리즘 (114)
      • 정렬 (3)
      • 그리디 (9)
      • 구현 (35)
      • 이분 탐색 (4)
      • 탐색 (2)
      • 동적 계획법 (DP) (11)
      • DFS BSF (29)
      • 최단 경로 (5)
      • 그래프 (4)
      • 주의할 점 (4)
      • 트리 (3)
    • spring (34)
      • JPA (12)
    • DevOps (10)
      • Docker (3)
    • java (15)
      • 이펙티브자바 (4)
      • Clean Code (4)
    • git (9)
    • DB (3)
    • 앱개발 (1)
    • 유닉스 (26)
    • 네트워크 (3)
      • IT 엔지니어를 위한 네트워크 입문 (3)
    • 유니티 (1)
    • 후기 (4)
    • 누누코코 (0)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • JWT
  • 그리디 알고리즘
  • 입출력
  • 이코테
  • 백준
  • Docker
  • 7465
  • 숫자 조각
  • 1868
  • BaseEntity
  • 프로그래머스
  • 13265
  • spring
  • SWEA
  • 팀 결성
  • 공통 response
  • 4193
  • allowPublicKeyRetrieval
  • 도넛과 막대 그래프
  • dp

최근 댓글

최근 글

hELLO · Designed By 정상우.
hahihi
프로그래머스 - 순위 검색
상단으로

티스토리툴바

개인정보

  • 티스토리 홈
  • 포럼
  • 로그인

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.