240126 광물 캐기

Jongleee·2024년 1월 26일
0

TIL

목록 보기
479/737
public int solution(int[] picks, String[] minerals) {
	int answer = 0;
	int num = picks[0] + picks[1] + picks[2];
	int[][] section = new int[minerals.length / 5 + 1][3];

	for (int i = 0; i < minerals.length && num > 0; i++) {
		int index = i / 5;
		updateSection(section[index], minerals[i]);
		if (i % 5 == 4) {
			num--;
		}
	}

	Arrays.sort(section, new Comparator<int[]>() {
		@Override
		public int compare(int[] o1, int[] o2) {
			return o2[2] - o1[2];
		}
	});

	int pick = 0;
	for (int[] arr : section) {
		while (pick < 3 && picks[pick] == 0) {
			pick++;
		}
		if (pick == 3) {
			break;
		}
		picks[pick]--;
		answer += arr[pick];
	}

	return answer;
}

private void updateSection(int[] section, String mineral) {
	switch (mineral.charAt(0)) {
		case 'd':
			section[0]++;
			section[1] += 5;
			section[2] += 25;
			break;
		case 'i':
			section[0]++;
			section[1]++;
			section[2] += 5;
			break;
		default:
			section[0]++;
			section[1]++;
			section[2]++;
	}
}

출처:https://school.programmers.co.kr/learn/courses/30/lessons/172927

0개의 댓글