CodeWars 스물 네 번째 문제

Updated:

Double Cola

public static String WhoIsNext(String[] names, int n) {
    int beforeCircleFirstNamesOrder = 1;
    int afterCircleFirstNamesOrder = 1;
    int beforecircleEachNameLength = 1;
    int aftercircleEachNameLength = 1;

    for(int circleTime = 1; n >= afterCircleFirstNamesOrder; circleTime++) {
        beforeCircleFirstNamesOrder = afterCircleFirstNamesOrder;
        beforecircleEachNameLength = aftercircleEachNameLength;

        aftercircleEachNameLength = findCircleEachNameLength(circleTime);
        afterCircleFirstNamesOrder = findCircleFirstNamesOrder(aftercircleEachNameLength, names.length);
    }

    return names[(n - beforeCircleFirstNamesOrder) / beforecircleEachNameLength] ;
}

public static int findCircleFirstNamesOrder(int circleEachNameLength, int firstNameLength) {
    return (circleEachNameLength* firstNameLength) - (firstNameLength-1);
}

public static int findCircleEachNameLength(int circleTime) {
    return (int)Math.pow(2, circleTime);
}

*하… 나름 좋게 풀었다 생각했는데 best 코드는 정말 간단히(?) 풀었다. 문제는 봐도 이런 수식이 왜 나오는지 모르겠다는 거다.

*처음 문제 풀때 문제의 의도를 이해 못했다.

*문제의 의도는 다음과 같다.

S L P R H
SS LL PP RR HH
(SSS LLL PPP RRR HHH 이거 아니다 밑에 바로 이름당 4개로 늘어난다)
SSSS L(L)LL PPPP RRRE HHHH

*Best 코드는 결국 이해 못했다. 포기..ㅠ

*이래서 공대생은 기본적으로 수학을 가까이 하는게 맞는 것 같다. 아무리 개발자라도 해도.. 결국 알고리즘은 수학인걸

// 이게 베스트 코드인데.. 할말이 없다. 하 이건 죽었다 깨어나도 생각 못할 것 같다.
public class Line {
  public static String WhoIsNext(String[] names, int n){
    while ( n > names.length){
      n = (n - (names.length - 1)) / 2;
    }
    return names[n-1];
  }
}