/Algorithm

Algorithm Study

Primary LanguageJava

๐Ÿ•น Algorithm

๐Ÿง Algorithm Study


๐Ÿ“Œ LinkedHashMap์˜ removeEldestEntry()


LinkedHash Map์€ HashMap์„ ์ƒ์†, ๊ธฐ๋ณธ ๊ธฐ๋Šฅ์€ ๊ฐ™์œผ๋‚˜ ์ˆœ์„œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

๐Ÿง removeEldestEntry()

  • put์„ ํ•  ๋•Œ ํ˜ธ์ถœ.
  • ๊ฐ€์žฅ ์ตœ๊ทผ์— ๋“ค์–ด์˜จ ๋ฐ์ดํ„ฐ๋“ค์„ ์ผ์ • ๊ฐœ์ˆ˜๋กœ ์œ ์ง€ํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉ ๊ฐ€๋Šฅ.
  • eldest๋กœ ๊ฐ€์žฅ ์˜ค๋ž˜๋œ ์—”ํŠธ๋ฆฌ๋ฅผ ์•Œ๊ณ ์žˆ๋‹ค.

- ๊ธฐ๋ณธ ์ •์˜

protected boolean removeEldestEntry(Map.Entry<K,V> eldest){
    return false;
}

####- ์žฌ์ •์˜

protected boolean removeEldestEntry(Map.Entry<K,V> eldest){
    return size() > 3;
}

์‚ฌ์ด์ฆˆ๊ฐ€ 3๋ณด๋‹ค ์ปค์ง€๋ฉด ๊ฐ€์žฅ ์˜ค๋ž˜๋œ ๊ฐ’์„ ์ง€์šฐ๊ณ , ๊ทธ ์ž๋ฆฌ๋ฅผ ๋ฐฉ๊ธˆ ๋“ค์–ด์˜จ ์—”ํŠธ๋ฆฌ๋กœ ๋Œ€์ฒดํ•œ๋‹ค.

๐Ÿ“Œ Matches, Pattern


String pattern = "^[A-Z]*$";
boolean regex = Pattern.matches(pattern, elm);
        if(regex){
            return true;
        }

        return false;
  • ๋‘๋ฒˆ์งธ ์ธ์ž์˜ ๋ฌธ์ž์—ด์ด ํŒจํ„ด๊ณผ ์ผ์น˜ํ•˜๋ฉด true ๋ฐ˜ํ™˜.

๐Ÿ“Œ Stream, reduce์™€ mapToInt


answer = scores.stream().mapToInt(score -> score).sum();

scores.stream().reduce(0,Integer::sum);
  • ๋‘ ๋ผ์ธ ๋ชจ๋‘ Stream ๋‚ด์˜ ์š”์†Œ๋“ค์„ int๊ฐ’์œผ๋กœ ๋”ํ•˜๊ธฐ ์œ„ํ•œ ์ฝ”๋“œ์ด๋‹ค.
  • ํ•˜์ง€๋งŒ ์†๋„๋ฅผ ์ธก์ •ํ•ด๋ณด๋ฉด mapToInt๊ฐ€ ๋” ์ข‹์€ ์„ฑ๋Šฅ์„ ๋ณด์—ฌ์ค€๋‹ค
  • ๐Ÿค” reduce์˜ ๊ฒฝ์šฐ์—๋Š” ๋ฐ•์‹ฑ, ์–ธ๋ฐ•์‹ฑ์˜ ๊ณผ์ •์„ ๊ฑฐ์น˜๊ธฐ ๋•Œ๋ฌธ์— ์†๋„๊ฐ€ ๋” ๋Š๋ฆฌ๋‹ค.

๐Ÿ“Œ Map Sort์™€ getOrDefault


๐Ÿง Map.Entry.comparingBy*

List<Map.Entry<Integer, Integer>> orderByValue = elmCount.entrySet().stream()
            .sorted(Map.Entry.comparingByValue())
            .collect(Collectors.toList());
  • comparingByValue | comparingByKey๋ฅผ ์‚ฌ์šฉํ•˜Map์„ ์ •๋ ฌํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๊ธฐ๋ณธ์ ์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ, ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ํ•  ์ˆ˜ ์žˆ๋‹ค.
.sorted(reverseOrder(Map.Entry.comparingByValue))

๐Ÿง getOrDefault

if(elmCount.containsKey(elmValue)){
    elmCount.put(elmValue, elmCount.get(elmValue) +1);
}
else{
   elmCount.put(elmValue, 1);
}
 map.put(n, map.getOrDefault(n, 0) + 1);
  • ์œ„์˜ ์ฝ”๋“œ๋ฅผ getOrDefault๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์•„๋ž˜ ์ฝ”๋“œ๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค

๐Ÿ“Œ StringBuffer : ๋ฌธ์ž์—ด์˜ ํŠน์ •์œ„์น˜ ์น˜ํ™˜.


String road = "00010101101"
StringBuffer stringBuffer = new StringBuffer(road);
StringBuffer newRoad = stringBuffer.replace(startIdx, endIndex, "1");
  • startIdx๋ถ€ํ„ฐ endIdx -1 ๊นŒ์ง€์˜ ๋ฒ”์œ„๋ฅผ 3๋ฒˆ์งธ ์ธ์ž์˜ ๋ฌธ์ž์—ด๋กœ ์น˜ํ™˜ํ•œ๋‹ค.

๐Ÿ“Œ ์™„์ „ํƒ์ƒ‰: n๊ฐœ์—์„œ m๊ฐœ๋ฅผ ์„ ํƒ, ์žฌ๊ท€์‚ฌ์šฉ.


public int pick(String road,List<Integer> zeroIndex, int n){
        if(zeroIndex.size() == 0){
            return getRoadLength(road);
        }

        int maxLength = 0;
        StringBuffer stringBuffer;
        List<Integer> myZeroIndex = new ArrayList<>();

        // idex List ๋ณต์‚ฌ
        myZeroIndex.addAll(zeroIndex);

        for(int i=0; i<myZeroIndex.size(); i++){
            myZeroIndex.clear();
            myZeroIndex.addAll(zeroIndex);
            stringBuffer = new StringBuffer(road);

            int idx = myZeroIndex.remove(i);
            int length = 0;

            // ๋„๋กœ ํ•œ ๊ณณ ๋ณด์ˆ˜
            StringBuffer newRoad = stringBuffer.replace(idx, idx+1, "1");

            // ์žฌ๊ท€๋กœ ๋ณด์ˆ˜๋œ ๋„๋กœ ๋ฐ›์•„์˜ค๊ธฐ
            if(n > 1){
                length = pick(newRoad.toString(), myZeroIndex, n-1);
            }
            // ๋งˆ์ง€๋ง‰ ์žฌ๊ท€ํ˜ธ์ถœ
            else{
                length = getRoadLength(newRoad.toString());
            }
            if(length > maxLength){
                maxLength = length;
            }
        }
        return maxLength;
    }
  • n๊ฐœ์˜ ์„ ํƒ์ง€์—์„œ m๊ฐœ๋ฅผ ๊ณ ๋ฅผ ๋•Œ ๊ฐ€์žฅ ์ตœ์„ ์˜ ์„ ํƒ์ด ๋˜๋„๋ก ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜.
  • ๋ฉ”์„œ๋“œ ์—์„œ๋Š” n-1๊ฐœ์˜ ์„ ํƒ์ง€๋กœ ๋‹ค์‹œ ์žฌ๊ท€ํ˜ธ์ถœ.
  • ์žฌ๊ท€ํ˜ธ์ถœ์—์„œ๋Š” ์ž์‹ ์—๊ฒŒ ์ฃผ์–ด์ง„ ์„ ํƒ์ง€๋ฅผ ๋ชจ๋‘ ํƒ์ƒ‰. ์ตœ์ ์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜.
  • ์ฆ‰ ์ž์‹ ์ด 1๊ฐœ๋ฅผ ๊ณ ๋ฅด๊ณ , ๋‚˜๋จธ์ง€ ์„ ํƒ์ง€๋ฅผ ์žฌ๊ท€ํ˜ธ์ถœ๋กœ ๋„˜๊ฒจ์ค€๋‹ค. ๊ฐ ์žฌ๊ท€ํ˜ธ์ถœ ๋ฉ”์„œ๋“œ์—์„œ๋Š” ๋˜ ๊ทธ์ค‘ ํ•˜๋‚˜๋ฅผ ์„ ํƒํ•˜๊ณ  ์žฌ๊ท€ํ˜ธ์ถœ. m๊ฐœ๋ฅผ ์„ ํƒ ํ•œ ํ›„์— ๊ฐ’ ๊ณ„์‚ฐ.

๐Ÿ“Œ Iterator ์„ ์ด์šฉํ•œ ConcurrentModificationException ํ•ด๊ฒฐ


Collection์„ for๋ฌธ์œผ๋กœ ํƒ์ƒ‰ํ•˜๋‹ค๊ฐ€ ํ•ด๋‹น ์ธ๋ฑ์Šค , ๋˜๋Š” Object๋กœ ์š”์†Œ๋ฅผ add/remove ํ•˜๋ ค๊ณ  ํ•˜๋ฉด, ๋‹ค๋ฅธ ์š”์†Œ๋“ค์˜ ์ธ๋ฑ์Šค์— ๋ณ€ํ™”๊ฐ€ ์ƒ๊ธฐ๊ธฐ ๋•Œ๋ฌธ์— ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

  • remove
 for(Iterator<String> it = dir.iterator(); it.hasNext();){
        if(it.next().startsWith(split[1])){
        it.remove();
    }
}
  • Iterator๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , removeํ•  ๋•Œ์—๋Š” iterator ์ž์ฒด๋ฅผ removeํ•ด์„œ ์ œ๊ฑฐํ•ด์ค€๋‹ค.

  • add

    • ๋‹ค๋ฅธ ๋ฆฌ์ŠคํŠธ์— ๋„ฃ์–ด ๋‘์—ˆ๋‹ค๊ฐ€ ์ˆœํšŒ๊ฐ€ ๋๋‚œ ํ›„ addAll

๐Ÿ“Œ Arrays.sort


๋ฐฐ์—ด์˜ ์˜ค๋ฆ„์ฐจ์ˆœ, ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ.

  • ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
Arrays.sort(arr);
  • ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ
Arrays.sort(arr,Collections.reverseOrder())
  • ์ปค์Šคํ…€ ์ •๋ ฌ
Arrays.sort(arr, new Comparator<..>{...})

๐Ÿ– Comparator์„ ์‚ฌ์šฉํ•  ๋•Œ๋Š” IllegalArgumentException์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š๋„๋ก ํ•ญ์ƒ -1,0,1์„ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ตฌํ˜„ํ•œ๋‹ค

return o1 - o2 or ์กฐ๊ฑด๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ -1,0,1์„ ๋ชจ๋‘ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ตฌํ˜„.

๐Ÿค” sortํ•จ์ˆ˜์—์„œ๋Š” Comparator์˜ ๊ฒ€์ฆ์„ ์ ๊ทน์ ์œผ๋กœ ํ•˜์ง€๋Š” ์•Š์ง€๋งŒ ์ž…๋ ฅ ๋ฐ์ดํ„ฐ์— ๋”ฐ๋ผ ์‹คํ–‰ ๋„์ค‘ ์ž˜๋ชป๋˜์—ˆ๋‹ค๋Š” ์ฆ๊ฑฐ๋ฅผ ๋ฐœ๊ฒฌํ•˜๊ฒŒ ๋˜๋ฉด ๋Ÿฐํƒ€์ž„ ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค.

๐Ÿ“Œ ์šฐ์„ ์ˆœ์œ„ ํ(PriorityQueue)


 PriorityQueue<Integer> que = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer integer, Integer t1) {
                return 0;
            }
        });
  • Comparator์„ ์ƒ๋žตํ•˜๋ฉด ๊ธฐ๋ณธ์ ์ธ ์˜ค๋ฆ„์ฐจ์ˆœ.

  • pick() -> ๊ฐ€์žฅ ์•ž์˜ ์š”์†Œ๋ฅผ ํ™•์ธ. ์—†๋‹ค๋ฉด null

  • poll() -> ๊ฐ€์žฅ ์•ž์˜ ์š”์†Œ๋ฅผ ๊บผ๋‚ด์˜จ๋‹ค.(remove) ์—†๋‹ค๋ฉด null

  • remove() -> ๋งจ ์•ž์˜ ์š”์†Œ ์ œ๊ฑฐ. boolean ๋ฐ˜ํ™˜.

  • clear() -> ํ๋ฅผ ๋น„์šด๋‹ค.

  • ๋˜๋Š” ํด๋ž˜์Šค์— Comparable์˜ compareTo ๋งค์†Œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•ด๋„ ๋œ๋‹ค.

@Override
public int compareTo(Stage target) {
    // ์‹คํŒจ์œจ์ด ๊ฐ™๋‹ค๋ฉด ์Šคํ…Œ์ด์ง€๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ.
    if(this.failure == target.failure) return this.stage - target.stage;
    // ์‹คํŒจ์œจ ๊ธฐ์ค€ ๋‚ด๋ฆผ์ฐจ์ˆœ
    else{
        if(this.failure > target.failure) return -1;
        else return 1;
    }
}
  • this๊ฐ€ ์ด๋ฒˆ์— ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ๋˜๊ณ , ๋ฐ˜ํ™˜๊ฐ’์ด ์Œ์ˆ˜๋ฉด ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ ์•ž์—, ์–‘์ˆ˜์ด๊ฑฐ๋‚˜ 0์ด๋ฉด ๋’ค์— ๋‘๊ฒŒ ๋œ๋‹ค.

๐Ÿ– ์ฃผ์˜ํ•ด์•ผํ•  ์ .

  • ์šฐ์„ ์ˆœ์œ„ ํ๋Š” heap ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์ด์šฉํ•˜๊ณ , poll ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊บผ๋‚ผ ๋•Œ root ๋…ธ๋“œ์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ๊บผ๋‚ด์ง€๊ณ , ๋ฐ์ดํ„ฐ๋ฅผ ์žฌ์ •๋ ฌํ•˜๊ฒŒ ๋œ๋‹ค.
  • ๋•Œ๋ฌธ์— poll ๋ฉ”์†Œ๋“œ๊ฐ€ ์•„๋‹Œ Iterator๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆœํšŒํ•˜๊ฒŒ ๋˜๋ฉด ์˜ค๋ฆ„์ฐจ์ˆœ, ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋œ ๊ฐ’์„ ์–ป์Œ์„ ๋ณด์žฅํ•  ์ˆ˜ ์—†๋‹ค. max heap ๋˜๋Š” min heap์€ ๋ถ€๋ชจ ๋…ธ๋“œ๊ฐ€ ์ž์‹ ๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜, ์ž‘์€๊ฒƒ ๋งŒ์„ ๋ณด์žฅํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
  • ๋ฐ์ดํ„ฐ์˜ ์ผ๋ถ€๋งŒ์„ ๊ฐ€์ง€๊ณ  ์šฐ์„ ์ˆœ์œ„๋ฅผ ์ •ํ•˜๊ณ , ์ผ๋ถ€ ๋ฐ์ดํ„ฐ๋กœ ์šฐ์„ ์ˆœ์œ„๋ฅผ ์ •ํ•  ์ˆ˜ ์—†์„ ๊ฒฝ์šฐ ๋“ค์–ด์˜จ ์ˆœ์„œ๋Œ€๋กœ ๋‚˜๊ฐ์„ ๋ณด์žฅํ•˜์ง€ ๋ชปํ•œ๋‹ค.(๋งˆ์ฐฌ๊ฐ€์ง€๋กœ heap์ด๊ธฐ ๋•Œ๋ฌธ)
    • sequence ๋ณ€์ˆ˜๋ฅผ ํ•˜๋‚˜ ์„ ์–ธํ•ด๋‘๊ณ  ๋“ค์–ด์˜จ ์ˆœ์„œ๋ฅผ ๋น„๊ตํ•˜๋„๋ก ๊ตฌํ˜„ํ•ด์•ผํ•œ๋‹ค.

๐Ÿ“Œ String ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ


char[] chars = numString.toCharArray();
Arrays.sort(chars);

StringBuilder sb = new StringBuilder(new String(chars)).reverse();
System.out.println(sb.toString());
  • ๋จผ์ € String์„ charArray๋กœ ๋งŒ๋“ค์–ด ์ค€ ํ›„ Arrays.sort()๋ฅผ ์ด์šฉํ•˜์—ฌ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ.
  • charArray๋ฅผ ๋‹ค์‹œ new String(char[])๋กœ String์œผ๋กœ ๋งŒ๋“ค์–ด์ค€๋‹ค.
  • StringBuilder.reverse๋กœ ์—ญ์ˆœ์œผ๋กœ ์ •๋ ฌ.

Collections.reverseOrder()๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด Charactor ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•˜๊ณ , charAt์œผ๋กœ ๋ฐฐ์—ด์„ ์ฑ„์šฐ๋Š” ๋ฐฉ๋ฒ•๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.

๐Ÿ“Œ ์ด๋ถ„ ํƒ์ƒ‰(Binary Search)


while(start <= end){
    mid = (start + end) / 2;
    if(uniqueCoors.get(mid) == c){
        index = mid;
        break;
    }
    else if(uniqueCoors.get(mid) > c){
        end = mid -1;
    }
    else{
        start = mid +1;
    }
}
  • ๊ธฐ๋ณธ์ ์ธ ์ด๋ถ„ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜.

โ˜๏ธ LowerBound, UpperBound

private static int getLowerBound(List<Integer> arr, int target) {
    int left = 0;
    int right = arr.length -1;
    int mid = 0;

    while(left<=right){
        mid = (left + right) / 2;

        // target์ด ์•„๋‹๋•Œ๊นŒ์ง€ right์„ ์กฐ์ž„. -> target์ด ์•„๋‹Œ ๊ฐ€์žฅ ์ฒซ ์ธ๋ฑ์Šค.
        if(target > arr[mid]) left = mid +1;
        else right = mid -1;
    }
    return right;
}

private static int getUpperBound(List<Integer> arr, int target) {
    int left = 0;
    int right = arr.length -1;
    int mid = 0;

    while(left<=right){
        mid = (left + right) / 2;

        if(target >= arr[mid]) left = mid +1;
        else right = mid -1;
    }
    return left;
}
Arrays.binarySearch(arr, findValue);
  • ๊ฐ’์ด ์กด์žฌํ•˜๋ฉด index๋ฅผ ๋ฐ˜ํ™˜.
  • ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด ํ•ด๋‹น ๋ฐฐ์—ด์— ๋“ค์–ด๊ฐ„๋‹ค๋ฉด ๋“ค์–ด๊ฐ€๊ฒŒ ๋˜๋Š” index์˜ ์Œ์ˆ˜ -1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค

ex) {1,3,4,5} -> 2๋ฅผ ์ฐพ๋Š”๋‹ค๋ฉด -2 ๋ฐ˜ํ™˜.

๐Ÿ“Œ List.subList(start, end)


  • ๋ถ€๋ชจ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ฐธ์กฐํ•˜๋Š” subList๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
  • ์ฐธ์กฐ์ž„์— ์œ ์˜.
List<String> sub = new ArrayList<>(parent.subList(start, end));
  • ์œ„ ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•˜๋ฉด ์ฐธ์กฐ๊ฐ€ ์•„๋‹ˆ๊ณ  ๋ฆฌ์ŠคํŠธ์˜ ์ผ๋ถ€๋ฅผ ๋ณต์‚ฌํ•œ ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค.

๐Ÿ“Œ List <-> Array


List To Array

String[] answer = menuList.toArray(new String[menuList.size()]);
int[] intArr = IntegerList.stream().mapToInt(i->i).toArray();
  • Wrapper ํด๋ž˜์Šค์—์„œ ์›์‹œ๋ฐ์ดํ„ฐ ๋ฐฐ์—ด๋กœ์˜ ๋ณ€ํ™˜์€ mapToInt, mapToDouble ๋“ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋จผ์ € ์–ธ๋ฐ•์‹ฑ.

Array To List

List<T> list = Arrays.asList(arr);
  • ์ƒˆ๋กœ์šด List๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ. ํ•ด๋‹น ๋ฐฐ์—ด์— ๋Œ€ํ•œ List View๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • ๋ณ€ํ™˜๋œ list์— ๊ฐ’์„ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด ๋ถˆ๊ฐ€๋Šฅ(์˜ˆ์™ธ ๋ฐœ์ƒ). ์›๋ž˜์˜ ๋ฐฐ์—ด์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋ฉด ํ•จ๊ป˜ ๋ณ€๊ฒฝ๋œ๋‹ค.
List<T> list = new ArrayList<>(Arrays.asList(arr));
  • ์œ„์™€ ๋‹ฌ๋ฆฌ ์ƒˆ๋กœ์šด ArrayList ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
List<T> list = Stream.of(maxCount).collect(Collectors.toList());
  • Stream์„ ์ด์šฉํ•œ ๋ณ€ํ™˜.

๐Ÿ– ๊ทธ๋Ÿฌ๋‚˜ ์œ„์˜ ๋ฐฉ๋ฒ•๋“ค์€ ์›์‹œํƒ€์ž…์„ Wrapper ํด๋ž˜์Šค๋กœ ๋ณ€ํ™˜ํ•ด์ฃผ์ง€ ์•Š๋Š”๋‹ค

  • Arrays.asList(int[]) -> List<int[]>
Arrays.stream(arr).boxed().collect(Collectors.toList());
  • stream์„์ด์šฉํ•˜์—ฌ Wrapper ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ ํ›„ ๋ฆฌ์ŠคํŠธ๋กœ.

๐Ÿ“Œ BigInteger, BigDecimal


  • ๊ธฐ๋ณธํ˜• ์ด์ƒ์˜ ์ˆ˜๋ฅผ ๋‹ค๋ฃฐ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.
BigInteger number = new BigInteger(num);
number.add(BigInteger val);
number.subtract(BigInteger val);
number.multiply(BigInteger val);
number.divide(BigInteger val);
number.remainder(BigInteger val);

// ๊ธฐ๋ณธํ˜•์œผ๋กœ ๋ฐ˜ํ™˜
number.intValue()
...

// ๊ธฐ๋ณธํ˜• ๋ฐ˜ํ™˜, ํƒ€์ž…์˜ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๋ฉด ์˜ˆ์™ธ ๋ฐœ์ƒ.
number.intValueExact()
  • BigDecimal ๋˜ํ•œ ๋™์ผํ•˜๊ฒŒ ์‚ฌ์šฉํ•œ๋‹ค.

  • String์œผ๋กœ ๋ณ€ํ™˜ ํ›„ ์ˆซ์ž๋ฅผ ๋”ํ•ด ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ์†๋„๊ฐ€ ๋Š๋ฆฌ๋‹ค.

๐Ÿ“Œ BFS - queue


  1. ํ ์ƒ์„ฑ.
Queue<Integer> queue = new LinkedList<>();
  1. ์ฒซ ๋…ธ๋“œ ํƒ์ƒ‰.
queue.add(0);
visited[0] = true;
  1. ๋…ธ๋“œ ํƒ์ƒ‰.
while(!queue.isEmpty()){
    // queue์—์„œ ์ด๋ฒˆ ์ˆœ์„œ ๋…ธ๋“œ๋ฅผ ๊ฐ€์ ธ์˜ด.
    int node = queue.poll();
    
    // ํ•ด๋‹น ๋…ธ๋“œ๊ฐ€ ์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•˜๋Š”์ง€ ๊ฒ€์‚ฌ.
    if(์กฐ๊ฑด ๊ฒ€์‚ฌ){...}
    
    // ํ˜„์žฌ ๋…ธ๋“œ์˜ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ์ž์‹ ๋…ธ๋“œ๋“ค์„ queue์— ์ถ”๊ฐ€.
    for(์ž์‹ ๋…ธ๋“œ i){
        if(!visited[i]){
            queue.add(i);
            visited[i] = true;
        }
    }
}

๐Ÿ“Œ ๋ฐฐ์—ด์˜ ๊นŠ์€ ๋ณต์‚ฌ.


  • 1์ฐจ์› ๋ฐฐ์—ด์˜ ๊ฒฝ์šฐ
boolean copy = originArr.clone();

boolean copy = System.arraycopy(origin,0, copy, 0, origin.length);
  • 2์ฐจ์› ๋ฐฐ์—ด
  • ์œ„์ฒ˜๋Ÿผ ๋‹จ์ˆœํžˆ clone์„ ํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด origin[i][j]์—์„œ j๋ฅผ ๊ฐ€๋ฅดํ‚ค๋Š” origin[i] ๋ถ€๋ถ„๋งŒ ๊นŠ์€ ๋ณต์‚ฌ๊ฐ€ ๋˜๊ณ  ์‹ค์ œ ๊ฐ’์€ ๊นŠ์€ ๋ณต์‚ฌ๊ฐ€ ๋˜์ง€ ์•Š๋Š”๋‹ค.
boolean copy = new boolean[n][n];

// 1๋ฒˆ ๋ฐฉ๋ฒ•
for(int i=0; i<n; i++){
    copy[i] = originArr[i].clone();   
}
// 2๋ฒˆ ๋ฐฉ๋ฒ•
for(int i=0; i<n; i++){
    System.arraycopy(origin[i],0, copy[i], 0, origin[i].length);
}

๐Ÿ“Œ Comparator.comparingInt()


int[][] arr = new int[n][n];
Arrays.sort(line, Comparator.comparingInt(c -> c[0]));
  • arr[n][0] ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ.

๐Ÿ“Œ ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜(GCD)์™€ ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜ : ์œ ํด๋ฆฌ๋“œ ํ˜ธ์ œ๋ฒ•


โ˜๏ธ ์ตœ๋Œ€ ๊ณต์•ฝ์ˆ˜

  • a >=b ์ด๊ณ , r = a % b ์ผ๋•Œ GCD(a,b) == GCD(b,r)
    • ex) GCD(259,63) == GCD(63,7) == GCD(7, 0) -> ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜๋Š” 7์ด๋‹ค.
  • ๐Ÿ– ์‹ค์ œ๋กœ ๊ตฌํ˜„ํ•  ๋•Œ๋Š” a ์™€ b์ค‘ ์–ด๋Š๊ฒƒ์ด ํฐ์ง€ ์‹ ๊ฒฝ์“ฐ์ง€ ์•Š์•„๋„ ๋œ๋‹ค, a๊ฐ€ b๋ณด๋‹ค ์ž‘๋”๋ผ๋„ ๋‹ค์Œ ๋ฐ˜๋ณต์— b,a๋กœ ๋’ค์ง‘์–ด์ ธ ๋“ค์–ด๊ฐ€๊ธฐ ๋•Œ๋ฌธ. (GCD(16, 24) -> r= 16, b = 24๊ฐ€ ๋จ, ๋‹ค์Œ GCD(24,16)

โ˜๏ธ ์ตœ์†Œ ๊ณต๋ฐฐ์ˆ˜

  • a,b ๊ทธ๋ฆฌ๊ณ  a์™€ b์˜ ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜๋ฅผ l ์ด๋ผ๊ณ  ํ•  ๋•Œ, a=Al, b=Bl ์ด๋‹ค.
  • ์ตœ์†Œ ๊ณต๋ฐฐ์ˆ˜๋Š” A * B * l ์ด๋ฏ€๋กœ a * b / l ์ด๋‹ค.
// ๋ฐ˜๋ณต๋ฌธ
int LCM = a * b;

int r = a % b;
while(r > 0){
    a = b;
    b = r;
    r = a % b;
}
int GCD = b;
LCM /= GCD;

// ์žฌ๊ท€
private static int gcd(int a, int b) {
    if(b <= 0) return a;
    return gcd(b, a % b);
}

๐Ÿ“Œ ์ดํ•ญ๊ณ„์ˆ˜


  • nCk = n! / k!(n-k)!
  • nCk = n-1Ck-1 + n-1Ck
  • ํŒฉํ† ๋ฆฌ์–ผ์€ 12๋ฅผ ๋„˜์–ด๊ฐ€๋ฉด int๋ฅผ ๋ฒ—์–ด๋‚˜๊ณ  20์„ ๋„˜์–ด๊ฐ€๋ฉด long์„ ๋ฒ—์–ด๋‚œ๋‹ค.

๐Ÿ“Œ Array To String


int[] arr = {1,2,3};
String s = Arrays.toString(arr);
// [1,2,3]

String[] arr2 = {"a","b","c"};
String s2 = String.join("", arr);
// abc
        
Arrays.stream(arr2).collect(Collectors.joining());
// abc

String.valueOf(charArr);
// abc

๐Ÿ“Œ Deque

์•žใ… ๋’ค๋กœ ์š”์†Œ๋ฅผ ์ž… ์ถœ๋ ฅ ๊ฐ€๋Šฅ, ์Šคํƒ, ํ๋กœ ๋ชจ๋‘ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

Deque<T> deque = new ArrayDeque, LinkedList ..

โœ๏ธ ์š”์†Œ ์‚ฝ์ž….

  • addFirst, OfferFirst, push : ๋ฑ์˜ ์•ž์— ์š”์†Œ ์‚ฝ์ž…, add๋Š” ์˜ˆ์™ธ๋ฅผ, offer๋Š” boolean๊ฐ’ ๋ฐ˜ํ™˜.
  • addList, OfferLast, add : ๋ฑ์˜ ๋’ค์— ์š”์†Œ ์‚ฝ์ž….
  • addAll(Collection ) : collection์˜ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ๋ฑ์˜ ๋’ค์ชฝ์— ์‚ฝ์ž….

โœ๏ธ ์š”์†Œ ์ œ๊ฑฐ.

  • removeFirst, pollFirst, remove, poll, pop : ์•ž์ชฝ์—์„œ ์ œ๊ฑฐ ํ›„ ๋ฐ˜ํ™˜, remove๋Š” ๋ฑ์ด ๋น„์–ด์žˆ์œผ๋ฉด ์˜ˆ์™ธ๋ฅผ, poll์€ null์„ ๋ฆฌํ„ด.
  • removeLast, pollLast : ๋’ค์—์„œ ์ œ๊ฑฐํ•˜๊ณ  ๋ฐ˜ํ™˜.
  • removeFirstOccurrence(Obj o), remove(Obj o) : ์•ž์ชฝ์—์„œ ๋ถ€ํ„ฐ ํƒ์ƒ‰ํ•˜๋ฉฐ o๋ฅผ ์ฐพ์•„ ์ œ๊ฑฐ.
  • removeLastOccurrence(Obj o) : ๋’ค์ชฝ์—์„œ ๋ถ€ํ„ฐ ํƒ์ƒ‰ํ•˜๋ฉฐ o๋ฅผ ์ฐพ์•„ ์ œ๊ฑฐ.

โœ๏ธ ์กฐํšŒ

  • getFirst, PeekFirst, peek : ๋ฑ์˜ ๊ฐ€์žฅ ์•ž ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜, ๋ฑ์ด ๋น„์–ด์žˆ์œผ๋ฉด get์€ ์˜ˆ์™ธ๋ฅผ, peek๋Š” null์„ ๋ฐ˜ํ™˜.
  • getLast, peekLast : ๋ฑ์˜ ๊ฐ€์žฅ ๋’ท ์š”์†Œ ๋ฐ˜ํ™˜.
  • contain(Obj o) : o๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธ.
  • size
  • iterator : ๋ฑ์˜ ์•ž์ชฝ๋ถ€ํ„ฐ ์ˆœ์ฐจ์ ์œผ๋กœ ์‹คํ–‰๋˜๋Š” iterator
  • descendingIterator : ๋’ค์ชฝ๋ถ€ํ„ฐ ์ˆœ์ฐจ์ ์œผ๋กœ ์‹คํ–‰.

๐Ÿ“Œ ํŽ˜๋ฅด๋งˆ์˜ ์†Œ์ •๋ฆฌ

a๋Š” ์ •์ˆ˜, p๋Š” ์†Œ์ˆ˜์ด๊ณ , a๊ฐ€ p๋กœ ๋‚˜๋ˆ„์–ด์ง€์ง€ ์•Š์„ ๋•Œ.
a^p == a (mod p)
-> a^p mod p == a mod p
-> a^(p-1) == mod p 
-> a * a^(p-2) == mod p

์ฆ‰ a mod p ์˜ ์—ญ์›์€ a^(p-2) mod p
  • ์ดํ•ญ ๊ณ„์ˆ˜์—์„œ์˜ ํŽ˜๋ฅด๋งˆ ์†Œ์ •๋ฆฌ ์ด์šฉ.
  • (N!/ K!(N-K)!)) mod p == (N! * (K!(N-K)!)^-1) mod p
  • ํŽ˜๋ฅด๋งˆ์˜ ์†Œ์ •๋ฆฌ๋ฅผ ์ ์šฉํ•˜๋ฉด => (N! * (K!(N-K)!))^(p-2)) mod p
  • ๊ณฑ์˜ ํ˜•ํƒœ๋กœ ๋‚˜ํƒ€๋‚˜๋ฏ€๋กœ ๋ชจ๋“ˆ๋Ÿฌ์˜ ๋ถ„๋ฐฐ๋ฒ•์น™ ๊ฐ€๋Šฅ.
  • ์ตœ์ข… ์ ์œผ๋กœ ๊ตฌํ•ด์•ผ ํ•˜๋Š” ๊ฐ’์€ => ((N! mod p) * (K!(N-K)!)^(p-2) mod p) mod p

๐Ÿ“Œ N์ง„์ˆ˜ ๋ณ€ํ™˜ํ•˜๊ธฐ


1. N์œผ๋กœ ๋‚˜๋ˆˆ๋’ค ๋‚˜๋จธ์ง€๋ฅผ ๋’ค์ง‘์–ด ์ฝ๊ธฐ.

final static String[] convert  = new String[]{
            "0","1","2","3","4","5","6","7","8","9",
            "A","B","C","D","E","F"
        };

StringBuilder convNum = new StringBuilder();
while(num > 0){
    convNum.append(convert[num % n]);
    num /= n;
}

convNum.reverse();

2. Integer.toString(int n, int radix)

String result = Integer.toStirng(num, N);
  • Long.toString ๋“ฑ ๊ฐ€๋Šฅ.