230713 금과 은 운반하기

Jongleee·2023년 7월 13일
0

TIL

목록 보기
310/737
public long solution(int a, int b, int[] g, int[] s, int[] w, int[] t) {
	long answer = 4 * (long) 10e13;

	int cityLength = g.length;
	long start = 0;
	long end = answer;

	while (start <= end) {
		long mid = (start + end) / 2;
		int gold = 0;
		int silver = 0;
		int sum = 0;

		for (int i = 0; i < cityLength; i++) {
			int nowGold = g[i];
			int nowSilver = s[i];
			int nowWeight = w[i];
			long nowTime = t[i];

			long moveCount = mid / (nowTime * 2);
			if (mid % (nowTime * 2) >= t[i]) {
				moveCount++;
			}

			gold += Math.min(nowGold, moveCount * nowWeight);
			silver += Math.min(nowSilver, moveCount * nowWeight);
			sum += Math.min(nowGold + nowSilver, moveCount * nowWeight);
		}

		if (a <= gold && b <= silver && a + b <= sum) {
			end = mid - 1;
			answer = Math.min(mid, answer);
			continue;
		}

		start = mid + 1;
	}

	return answer;
}

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

0개의 댓글