https://school.programmers.co.kr/learn/courses/30/lessons/147354
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
주어진 조건에 맞게 구현을 하면 되는 문제였다
Arrays.sort(data, (o1, o2) -> { if (o1[col-1] == o2[col-1]) { return o2[0] - o1[0]; } return o1[col-1] - o2[col-1]; });
2번 조건을 만족하기 위해 오른차순으로 정렬, 키 값이 동일하면 내림차순으로 정렬
ArrayList<Integer> mods = new ArrayList<>(); for (int i = row_begin; i <= row_end; i++) { int mod = 0; for (int j = 0; j < data[0].length; j++) { mod += data[i-1][j] % i; } mods.add(mod); }
이후 각 컬럼의 값을 i로 나눈 나머지의 합을 구해주었다
answer = mods.get(0); for (int i = 1; i < mods.size(); i++) { answer ^= mods.get(i); }
그 다음 합들을 XOR계산을 하여 답을 구했다
import java.util.*; class Solution { public int solution(int[][] data, int col, int row_begin, int row_end) { int answer = 0; Arrays.sort(data, (o1, o2) -> { if (o1[col-1] == o2[col-1]) { return o2[0] - o1[0]; } return o1[col-1] - o2[col-1]; }); ArrayList<Integer> mods = new ArrayList<>(); for (int i = row_begin; i <= row_end; i++) { int mod = 0; for (int j = 0; j < data[0].length; j++) { mod += data[i-1][j] % i; } mods.add(mod); } answer = mods.get(0); for (int i = 1; i < mods.size(); i++) { answer ^= mods.get(i); } return answer; } }
'Programmers' 카테고리의 다른 글
[프로그래머스][JAVA]Lv. 2 - 이모티콘 할인행사 (0) | 2024.07.23 |
---|---|
[프로그래머스][JAVA]Lv. 2 - 마법의 엘리베이터 (0) | 2024.03.18 |
[프로그래머스][JAVA]Lv. 2 - 디펜스 게임 (1) | 2024.02.26 |
[프로그래머스][JAVA]Lv. 2 - 점 찍기 (0) | 2024.02.19 |
[프로그래머스][JAVA]Lv. 2 - 귤 고르기 (0) | 2024.02.19 |