1. ๋ฌธ์ ์์ฝ
์ด์ ์ปฌ๋ผ, ํ์ ํํ
์ฒซ๋ฒ์งธ ์ปฌ๋ผ์ ๊ธฐ๋ณธํค๋ก์ ๋ชจ๋ ํํ์ ๋ํด ๊ทธ ๊ฐ์ด ์ค๋ณต๋์ง ์๋๋ก ๋ณด์ฅ
(์์ฝํ์๋ฉด id ๊ฐ์ ๊ณ ์ ๋ฒํธ๋ผ๋ ๊ฒ)
1. ํด์ ํจ์๋ col, row_begin, row_end ์ ์
๋ ฅ์ผ๋ก ๋ฐ์
2. ํ
์ด๋ธ์ ํํ(ํ)์ col๋ฒ์งธ ์ปฌ๋ผ์ ๊ฐ์ ๊ธฐ์ค์ผ๋ก ์ค๋ฆ์ฐจ์ ์ ๋ ฌ์ ํ๋,
๋ง์ฝ ๊ทธ ๊ฐ์ด ๋์ผํ๋ฉด ๊ธฐ๋ณธํค์ธ ์ฒซ ๋ฒ์งธ ์ปฌ๋ผ์ ๊ฐ์ ๊ธฐ์ค์ผ๋ก ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌํจ
์ ๋ ฌ๋ ๋ฐ์ดํฐ์์ S_i๋ฅผ i๋ฒ์งธ ํ์ ํํ์ ๋ํด ๊ฐ ์ปฌ๋ผ์ ๊ฐ์ i๋ก ๋๋ ๋๋จธ์ง๋ค์ ํฉ์ผ๋ก ์ ์
row_begin <= i <= row_end ์ธ ๋ชจ๋ S_i๋ฅผ ๋์ ํ์ฌ bitwise XOR ํ ๊ฐ์ ํด์ ๊ฐ์ผ๋ก์ ๋ฐํํจ
2. ์ฝ๋
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int sortIndex;
bool cmp(vector<int> lValue,vector<int> rValue){
if(lValue[sortIndex]==rValue[sortIndex]) return lValue[0]>rValue[0];
return lValue[sortIndex]<rValue[sortIndex];
}
int solution(vector<vector<int>> data, int col, int row_begin, int row_end) {
int answer = 0;
sortIndex=col-1;
/*for(int i=0;i<data.size();i++){
for(int j=0;j<data[i].size();j++){
printf("%d ",data[i][j]);
}
printf("\n");
}*/
sort(data.begin(),data.end(),cmp);
/*printf("\n\n");
for(int i=0;i<data.size();i++){
for(int j=0;j<data[i].size();j++){
printf("%d ",data[i][j]);
}
printf("\n");
}*/
int s2;
for(int i=row_begin-1;i<row_end;i++){
s2=0;
for(int j=0;j<data[i].size();j++){
s2+=data[i][j]%(i+1);
//printf("%d : %d %d ~~\n",(i+1),data[i][j],data[i][j]%(i+1));
}
//printf("%d !!\n",s2);
answer= answer^s2;
}
return answer;
}
3. ํด์ค
๋จผ์ , ํ ์ด๋ธ์ ํํ์ col ๋ฒ์งธ ์ปฌ๋ผ์ ๊ฐ์ ๊ธฐ์ค์ผ๋ก ์ค๋ฆ์ฐจ์ ์ ๋ ฌ์ ํฉ๋๋ค
์ฌ๊ธฐ์ ์ฃผ์ํ ์ ์ col ๋ฒ์ฌ ์ด๊ธฐ ๋๋ฌธ์ vector์ col-1์ ๊ฐ์ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํด์ผํ๋ค๋ ์ ์ ๋๋ค
int solution(vector<vector<int>> data, int col, int row_begin, int row_end) {
int answer = 0;
sortIndex=col-1;
...
}
bool cmp(vector<int> lValue,vector<int> rValue){
if(lValue[sortIndex]==rValue[sortIndex]) return lValue[0]>rValue[0];
return lValue[sortIndex]<rValue[sortIndex];
}
sortIndex๋ผ๋ ๋ณ์์ col-1์ ํ ๋นํด์ฃผ๊ณ , algorithm ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ sortํจ์์ ์ ๊ฐ ์ ์ํ๋ cmp ๋น๊ตํจ์๋ฅผ ์ด์ฉํ์ฌ ์ ๋ ฌ์ ์งํํ์ต๋๋ค
๋ง์ฝ ๋น๊ต ๋ฒกํฐ๋ค์ sortIndex ์์น์ ๊ฐ์ด ๊ฐ์ ๊ฒฝ์ฐ ๊ธฐ๋ณธํค๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ์ ์งํํ์์ต๋๋ค
๊ทธ ํ์๋ ^ ์ฐ์ฐ์๋ฅผ ์ด์ฉํ์ฌ XOR ๋นํธ ์ฐ์ฐ์ ์งํํ์์ต๋๋ค
๋นํธ์ฐ์ฐ XOR ์ด๋?
๋ฒ ํ์ OR ์ฐ์ฐ์ด๋ผ๊ณ ๋ ๋ถ๋ฅด๋๋ฐ
๋ ๋นํธ๊ฐ ์๋ก ๋ค๋ฅธ ๊ฒฝ์ฐ์๋ง 1์ด ๋๊ณ ๊ทธ๋ ์ง ์์ผ๋ฉด 0์ด ๋๋ ํน์ดํ ์ฐ์ฐ์
์ฐ์ฐ | ๊ฒฐ๊ณผ |
0 ^ 0 | 0 |
0 ^ 1 | 1 |
1 ^ 0 | 1 |
1 ^ 1 | 0 |
int s2;
for(int i=row_begin-1;i<row_end;i++){
s2=0;
for(int j=0;j<data[i].size();j++){
s2+=data[i][j]%(i+1);
}
answer= answer^s2;
}
row_begin-1 ์์ row_end๊น์ง ๋นํธ์ฐ์ฐ์ ํ ๊ฐ๋ค์ answer์ ์ ์ฅํ์์ต๋๋ค
data[i][j]%(i+1)
๋ ์ ๋ ฌ๋ ๋ฐ์ดํฐ์์ S_i๋ฅผ i๋ฒ์งธ ํ์ ํํ(ํ)์ ๋ํด ๊ฐ ์ปฌ๋ฝ์ ๊ฐ์ i๋ก ๋๋ ๋๋จธ์ง์ ๋๋ค
'์๊ณ ๋ฆฌ์ฆ > Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] ์ ์ฐ๊ธฐ (0) | 2025.01.02 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] ๊ทค ๊ณ ๋ฅด๊ธฐ (0) | 2025.01.01 |
[ํ๋ก๊ทธ๋๋จธ์ค] ๋ง๋ฒ์ ์๋ฆฌ๋ฒ ์ดํฐ (0) | 2024.12.30 |
[ํ๋ก๊ทธ๋๋จธ์ค] ๋ถ๋ ๊ฐ๊ธฐ (1) | 2024.01.16 |
[ํ๋ก๊ทธ๋๋จธ์ค] ์ฒด์ก๋ณต (1) | 2023.11.15 |