본문 바로가기

프로그래머스40

[프로그래머스][C++] 덧칠하기 #include #include using namespace std; int solution(int n, int m, vector section) { int answer = 0; int index = 0; for(int i = section[index++]; i 리턴 if(index >= section.size()) return answer; } i = section[index]; // 현재 섹션을 반복자로 재정의 } return answer; } 1. answer과 index를 선언 2. 반복자 i가 섹션의 0번째 인덱스부터 시작해 n이 될 때까지 반복을 진행한다. 3. 매 반복은 페인트칠을 하는 횟수를 의미하므로 answer을 1씩 증가시킨다. 4. 이후 섹션의 다음 수가 현재 수에 롤러의 길이를 더.. 2023. 9. 13.
[프로그래머스][C++] 카드 뭉치 #include #include using namespace std; string solution(vector cards1, vector cards2, vector goal) { string answer = "Yes"; int x = 0; // card1에서 나와야하는 단어의 인덱스 번호 int y = 0; // card2에서 나와야하는 단어의 인덱스 번호 for(int i = 0 ; i < goal.size(); i++) { string curWord = goal[i]; if(curWord == cards1[x]) { // 현재 단어가 카드뭉치 1에 있는 경우 x++; } else if(curWord == cards2[y]) { // 현재 단어가 카드뭉치 2에 있는 경우 y++; } else { ans.. 2023. 9. 12.
[프로그래머스][C++] 추억 점수 #include #include #include using namespace std; vector solution(vector name, vector yearning, vector photo) { vector answer; map scores; for(int i = 0 ; i < name.size(); i++) { // 이름별 그리움 점수 저장 scores[name[i]] = yearning[i]; } for(int i = 0; i < photo.size(); i++) { answer.push_back({0}); for(int j = 0; j < photo[i].size(); j++) { string curName = photo[i][j]; answer[i] += scores[curName]; } } r.. 2023. 9. 12.
[프로그래머스][C++] 정수 삼각형 #include #include using namespace std; int solution(vector triangle) { int answer = 0; for (int i = triangle.size() - 1; i > 0; i--) { for (int j = 0; j < triangle[i].size() - 1; j++) { // 하위 두개의 원소 중 큰 원소를 윗단 원소에 더해줌 int left = triangle[i][j]; int right = triangle[i][j + 1]; triangle[i - 1][j] += max(left, right); } } answer = triangle[0][0]; // root 노드의 값이 최대값이 됨 return answer; } 1. 삼각형의 가장 밑의.. 2023. 8. 8.