본문 바로가기

Problem Solving/프로그래머스

(42)
[프로그래머스/파이썬] 2023 KAKAO BLIND RECRUITMENT 개인정보 수집 유효기간 문제 해석 문제 풀이 문자열 구현이 주된 문제였다. 생각보다 문자열 다루는게 익숙치 않았고 파이썬이 주 언어가 아니다보니 시간이 좀 걸렸던 것 같다. 문자열 다루기에는 파이썬이 참 편리하고 좋은 것 같다. 이 문제의 핵심은 결국 모든 날수를 더해서 오늘 날짜보다 더 작다면 유효기간이 지난 것으로 간주하고 배열에 넣는 것이다. 코 드 def to_days(date): year, month, day = map(int,date.split('.')) return (year - 1) * 12 * 28 + (month - 1) * 28 + day def solution(today, terms, privacies): answer = [] d_day = to_days(today) terms_map = {i.split..
[프로그래머스/파이썬] 2023 카카오 블라인드 리크루팅 표 병합 문제 해석 문제 풀이 merge와 unmerge 부분을 보고 바로 유니온 파인드(분리집합) 문제임을 알게되었다. 그런데 input을 tokenize할 필요가 있는 문자열 문제로 보여 C++이 아닌 파이썬으로 바꿔 풀게 되었다. (C++에 string split을 구현할 수 있지만 구현 방법이 빠르게 떠오르지 않았다.) 단지 이 문제에서는 단순 분리집합 문제와 다르게 고려해야할 사항이 많았는데 MERGE 를 할 경우 같은 셀이면 무시, 둘 중 한 셀만 있을 땐 그 값만 유지, 모두 값이 있을 땐 왼쪽 윗 셀을 따른다. UNMERGE 되면 지정한 위치가 그 값을 가지게 되고 다른 값들은 모두 초기화된다. Input이 (1, 1) 부터 시작된다. (0, 0)으로 생각하면 틀린다. MERGE 할 때 반대쪽 노드..
[프로그래머스/C++] 콜라츠 추측 #include #include using namespace std; int solution(int num) { int answer = 0; for(answer; answer < 500; ++answer) { if(num == 1) break; num % 2 == 1 ? num = num*3 +1 : num /= 2; } return answer == 500 ? answer = -1 : answer; } 3항 연산자를 이용해 해당 조건을 반복하도록하고 500번 이상 반복할 경우 -1을 리턴하도록 구현하였습니다. 문제: https://programmers.co.kr/learn/courses/30/lessons/12943 코딩테스트 연습 - 콜라츠 추측 1937년 Collatz란 사람에 의해 제기된 이 추측..
[프로그래머스/C++] 체육복 #include #include #include using namespace std; int solution(int n, vector lost, vector reserve) { int answer = 0; answer = n - lost.size(); int i = 0; int j = 0; sort(lost.begin(), lost.end()); sort(reserve.begin(), reserve.end()); for (int k = 0; k < reserve.size(); ++k) { if (find(lost.begin(), lost.end(), reserve[k]) != lost.end()) { lost.erase(find(lost.begin(), lost.end(), reserve[k])); re..
[프로그래머스/C++] H-Index #include #include #include using namespace std; int solution(vector citations) { sort(citations.begin(), citations.end(), greater()); int answer = 0; for(int i = 0; i < citations.size(); i++) { if(i+1
[프로그래머스/C++] 가장 큰 수 #include #include #include #include #include using namespace std; bool compare(const string &a, const string &b) { return a + b > b + a; } string solution(vector numbers) { vector arr; string answer; for(int it : numbers) arr.push_back(to_string(it)); sort(arr.begin(), arr.end(), compare); for(string sit : arr) answer += sit; return answer.substr(0,2) == "00" ? "0" : answer; } 주어진 숫자를 sorting해서 ..
[프로그래머스/C++] 큰 수 만들기 #include #include using namespace std; string solution(string number, int k) { string arr = ""; arr.push_back(number[0]); int idx = 0; for(int i = 1; i < number.length(); i++) { if(idx == k) { arr.push_back(number[i]); continue; } if(arr.back() < number[i]) { arr.pop_back(); if(!arr.empty() && arr.back() < number[i])i--; else arr.push_back(number[i]); idx++; } else { arr.push_back(number[i]); } ..
[프로그래머스/C++] JadenCase 문자열 만들기 #include #include using namespace std; string solution(string s) { if(!isdigit(s[0])) s[0] = toupper(s[0]); for(int i = 1; i < s.length(); i++) { if(isdigit(s[i])) continue; if(isupper(s[i])) s[i] = tolower(s[i]); if(isspace(s[i-1]) && isalpha(s[i])) s[i] = toupper(s[i]); } return s; } 이 문제는 단어의 첫글자가 알파벳이면 대문자로 만든 문자열을 출력하는 문제입니다. 간단하게 숫자라면 계속 넘어가고 대문자는 소문자로 바꿔주고 이전 값이 공백이고 현재 값이 문자라면 대문자로 변경하도록..