https://school.programmers.co.kr/learn/courses/30/lessons/147354
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
풀이
커스텀 정렬 함수만 작성할 수 있다면 쉽게 해결할 수 있는 문제입니다.
주어진 col에 따라 정렬하기 위해 전역변수 g_col을 정의하여 cmp 함수를 작성합니다.
이 후, S_i를 계산하고 ^연산자를 사용해 bitwise XOR 값을 리턴해줍니다.
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
int g_col = 0;
bool cmp(vector<int> a, vector<int> b)
{
if (a[g_col-1] == b[g_col-1])
return a[0] > b[0];
return a[g_col-1] < b[g_col-1];
}
int solution(vector<vector<int>> data, int col, int row_begin, int row_end) {
int answer = 0;
g_col = col;
// 1. 정렬
sort(data.begin(), data.end(), cmp);
// 2. S_i
vector<int> s_i;
for (int i = row_begin; i <= row_end; i++) {
int sum = 0;
for (int j = 0; j < data[i-1].size(); j++) {
sum += data[i-1][j] % i;
}
s_i.push_back(sum);
}
if (s_i.size() == 1)
return s_i[0];
answer = s_i[0];
for (int i = 1; i < s_i.size(); i++) {
answer = answer^s_i[i];
}
return answer;
}
'프로그래머스 > Lv.2' 카테고리의 다른 글
[Python] 프로그래머스 전화번호 목록 (0) | 2024.07.21 |
---|---|
[Python] 프로그래머스 아날로그 시계 (0) | 2024.06.23 |
[C++] 프로그래머스 혼자 놀기의 달인 (0) | 2024.05.07 |
[C++] 프로그래머스 쿼드압축 후 개수 세기 (0) | 2024.01.18 |
[C++] 프로그래머스 줄 서는 방법 (0) | 2024.01.16 |