본문 바로가기

IT/알고리즘

[코딜리티] - Lesson 11 ChocolatesByNumbers

SMALL

안녕하세요 남갯입니다.




boolean[] isEmpty = new boolean[N];
int index = 0;
int count = 0;

while (!isEmpty[index]) {
isEmpty[index] = true;
count++;

if (index + M > N - 1) {
index = (index + M) % N;
}else{
index += M;
}
}
return count;

위에는 

OOM



public int solution(int N, int M) {
// write your code in Java SE 8

BitSet bitSet = new BitSet();
int index = 0;
int count = 0;

while (!bitSet.get(index)) {
bitSet.set(index);
count++;

if (index + M > N - 1) {
index = (index + M) % N;
} else {
index += M;
}
}
return count;


https://app.codility.com/demo/results/trainingX2JTBZ-8SA/




//유클리드 호제법을 통해 최대 공약수를 구함.
int temp = N;
int R = N / M;
if(M * R == N){
return R;
}
while (N % M != 0) {
R = N % M;
N = M;
M = R;
}
return temp / R;





https://app.codility.com/demo/results/training5V8ANY-A8H/

LIST