class Solution {
int[] maxRyanShoot;
int maxScoreDiff;
public int[] solution(int n, int[] info) {
maxRyanShoot = new int[11];
maxScoreDiff = 0;
for(int bitmask = 0; bitmask < (1<<11); bitmask++) {
if(checkBitmaskValidity(bitmask, info) == false) continue;
int[] tempRyanShoot = getRyanShoot(bitmask);
int scoreDiff = getScoreDiff(bitmask);
if(scoreDiff < maxScoreDiff) {
continue;
}
else if(scoreDiff == maxScoreDiff) {
if(checkMoreLowerScore(tempRyanShoot)) {
maxRyanShoot = tempRyanShoot;
}
}
else {
maxRyanShoot = tempRyanShoot;
}
}
return maxRyanShoot;
}
public boolean checkBitmaskValidity(int bitmask, int[] info) {
}
public int[] getRyanShoot(int bitmask) {
}
public int getScoreDiff(int bitmask) {
}
public boolean checkMoreLowerScore(int[] tempRyanShoot) {
}
}
class Solution {
int[] maxRyanShoot;
int maxScoreDiff;
int N;
public int[] solution(int n, int[] info) {
maxRyanShoot = new int[11];
maxScoreDiff = 0;
N = n;
for(int bitmask = 0; bitmask < (1<<11); bitmask++) {
int cnt = checkBitmaskValidity(bitmask, info);
if(cnt > N) continue;
int[] tempRyanShoot = getRyanShoot(bitmask, info, cnt);
int tempScoreDiff = getScoreDiff(bitmask, info);
if(tempScoreDiff < maxScoreDiff) {
continue;
}
else if(tempScoreDiff == maxScoreDiff) {
if(checkMoreLowerScore(tempRyanShoot)) {
maxRyanShoot = tempRyanShoot;
}
}
else {
maxScoreDiff = tempScoreDiff;
maxRyanShoot = tempRyanShoot;
}
}
return maxScoreDiff == 0 ? new int[]{-1} : maxRyanShoot;
}
public int checkBitmaskValidity(int bitmask, int[] info) {
int cnt = 0;
for(int i = 0; i <= 10; i++) {
if((bitmask & (1<<i)) > 0) {
int toWinApeach = info[10-i] + 1;
cnt += toWinApeach;
}
}
return cnt;
}
public int[] getRyanShoot(int bitmask, int[] info, int cnt) {
int[] tempRyanShoot = new int[11];
for(int i = 0; i <= 10; i++) {
if((bitmask & 1<<i) > 0) {
int toWinApeach = info[10-i] + 1;
tempRyanShoot[10-i] = toWinApeach;
}
}
tempRyanShoot[10] += N-cnt;
return tempRyanShoot;
}
public int getScoreDiff(int bitmask, int[] info) {
int ryanScore = 0;
int apeachScore = 0;
for(int i = 0; i <= 10; i++) {
if ((bitmask & 1 << i) > 0) {
ryanScore += i;
} else {
if(info[10-i] > 0) {
apeachScore += i;
}
}
}
return ryanScore - apeachScore;
}
public boolean checkMoreLowerScore(int[] tempRyanShoot) {
for(int i = 0; i <= 10; i++) {
if(tempRyanShoot[10-i] > maxRyanShoot[10-i]) {
return true;
} else if(tempRyanShoot[10-i] == maxRyanShoot[10-i]) {
continue;
}
else {
return false;
}
}
return true;
}
}
- 남은 화살을 0점에 다 넣어주는 로직:
- 비트마스크에서 모든 케이스를 처리하기 때문에 원칙적으로는 비트마스크에서 1로 켜진 점수 중에서 가장 낮은 점수에 남은 화살을 모두 넣어주는 것이 맞습니다
- 하지만 이 경우 코드가 조금 길어집니다
- 같은 점수차이라면, 무조건 0점에 화살이 더 많은 케이스가 정답이 되기 때문에 0점에 남은 화살을 넣어주는 코드로도 정답을 도출할 수 있습니다.