๊ธฐ๋ก๋ฐฉ

Lv.2 : ๋‰ด์Šค ํด๋Ÿฌ์Šคํ„ฐ๋ง ๋ณธ๋ฌธ

CodingTest/Java

Lv.2 : ๋‰ด์Šค ํด๋Ÿฌ์Šคํ„ฐ๋ง

Soom_1n 2023. 11. 7. 10:43

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr



๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • ์ž์นด๋“œ ์œ ์‚ฌ๋„๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ๊ต์ง‘ํ•ฉ๊ณผ ํ•ฉ์ง‘ํ•ฉ์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.
  • ์ฃผ์–ด์ง„ ๋‘ ๋ฌธ์ž์—ด์˜ ํŒŒ์‹ฑ์ด ํ•„์š”ํ•˜๋‹ค.

๐Ÿ”ธ ๋ฌธ์ œ ํ’€์ด ๐Ÿ”ธ

  • ๋‘ ๋ฌธ์ž์—ด์˜ ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ, ๋Œ€๋ฌธ์ž ํ˜น์€ ์†Œ๋ฌธ์ž๋กœ ์ผ์น˜์‹œํ‚จ๋‹ค.
  • 2๋ฌธ์ž์”ฉ ์ž˜๋ผ์„œ ์›์†Œ๋กœ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, ๋„์–ด์“ฐ๊ธฐ๋‚˜ ํŠน์ˆ˜๋ฌธ์ž ๋“ฑ ์•ŒํŒŒ๋ฒณ์ด ์•„๋‹Œ ๋ฌธ์ž๊ฐ€ ํฌํ•จ๋˜์–ด์žˆ์œผ๋ฉด ๋ฐฐ์ œํ•œ๋‹ค.
    • ์—ฌ๊ธฐ์„œ ์ฃผ์˜ ํ•  ์ ์€ ์•ŒํŒŒ๋ฒณ์ด ์•„๋‹Œ ๋ฌธ์ž๋“ค์„ ์ œ๊ฑฐํ•˜๊ณ  2๋ฌธ์ž์”ฉ ์ž๋ฅด๋Š”๊ฒŒ ์•„๋‹Œ, ๋จผ์ € ์ž๋ฅธ๋‹ค์Œ ํ•„ํ„ฐ๋ง ํ•˜๋Š” ์ˆœ์„œ์ด๋‹ค.
  • ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜๋Š” ๋‹ค์ค‘ ์ง‘ํ•ฉ์ด๊ธฐ ๋•Œ๋ฌธ์— Map์— ๊ฐœ์ˆ˜์™€ ํ•จ๊ป˜ ์ €์žฅํ•œ๋‹ค.
  • ๊ต์ง‘ํ•ฉ์€ ๋‘ ์ง‘ํ•ฉ์— ๊ณตํ†ต ์›์†Œ๋ฅผ ์ฐพ๊ณ , ๋‘ ๊ฐ’์ค‘ ์ตœ์†Œ๊ฐ’์„ ๊ต์ง‘ํ•ฉ ๊ฐœ์ˆ˜์— ๋”ํ•œ๋‹ค.
  • ํ•ฉ์ง‘ํ•ฉ์€ ๋‘ ์ง‘ํ•ฉ์— ๊ณตํ†ต ์›์†Œ์˜ ๋‘ ๊ฐ’์—์„œ๋Š” ์ตœ๋Œ€๊ฐ’์„ ๋”ํ•˜๊ณ , ๊ฒน์น˜์ง€ ์•Š๋Š” ์›์†Œ ๊ฐ’๋„ ๋”ํ•ด์ค€๋‹ค.
  • ๊ต์ง‘ํ•ฉ / ํ•ฉ์ง‘ํ•ฉ * 65536 ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

import java.util.Map;
import java.util.HashMap;

class Solution {
    public int solution(String str1, String str2) {
        // Input
        str1 = str1.toUpperCase();
        str2 = str2.toUpperCase();

        Map<String, Integer> map1 = new HashMap<>();
        Map<String, Integer> map2 = new HashMap<>();

        for (int i = 0; i < str1.length() - 1; i++) {
            String temp = str1.substring(i, i + 2).replaceAll("[^a-zA-Z]", "");

            if (temp.length() != 2)
                continue;

            if (!map1.containsKey(temp))
                map1.put(temp, 1);
            else
                map1.put(temp, map1.get(temp) + 1);
        }

        for (int i = 0; i < str2.length() - 1; i++) {
            String temp = str2.substring(i, i + 2).replaceAll("[^a-zA-Z]", "");

            if (temp.length() != 2)
                continue;

            if (!map2.containsKey(temp))
                map2.put(temp, 1);
            else
                map2.put(temp, map2.get(temp) + 1);
        }

        if (map1.isEmpty() && map2.isEmpty())
            return 65536;

        // ๊ต์ง‘ํ•ฉ
        double cnt1 = 0;

        for (Map.Entry<String, Integer> entry : map1.entrySet()) {

            if (map2.containsKey(entry.getKey()) && map2.get(entry.getKey()) > 0) {
                cnt1 += Math.min(entry.getValue(), map2.get(entry.getKey()));
            }
        }

        // ํ•ฉ์ง‘ํ•ฉ
        double cnt2 = 0;

        for (Map.Entry<String, Integer> entry : map1.entrySet()) {

            if (map2.containsKey(entry.getKey())) {
                cnt2 += Math.max(entry.getValue(), map2.get(entry.getKey()));
                map2.remove(entry.getKey());
            } else {
                cnt2 += entry.getValue();
            }
        }

        for (Map.Entry<String, Integer> entry : map2.entrySet()) {
            cnt2 += entry.getValue();
        }

        return (int)(cnt1 / cnt2 * 65536);
    }
}

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„์ด ์—†์œผ๋ฏ€๋กœ, ์ฃผ์–ด์ง„ str1์™€ str2๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•ด์„œ ํ†ต์ผํ•œ๋‹ค.
  • ๋‘ ์ง‘ํ•ฉ์˜ ์›์†Œ๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด Map์„ 2๊ฐœ ๋งŒ๋“ ๋‹ค.
    • ๊ฐ๊ฐ 2๋ฌธ์ž์”ฉ ์ž๋ฅด๊ณ  ์•ŒํŒŒ๋ฒณ์„ ์ œ์™ธํ•œ ๋ฌธ์ž๋Š” ์ œ๊ฑฐํ•ด์„œ ๊ธธ์ด๊ฐ€ 2์ด๋ฉด ์ง‘ํ•ฉ์— ํฌํ•จํ•œ๋‹ค.
    • ์ง‘ํ•ฉ์— ์ฒ˜์Œ ๋„ฃ๋Š” ์›์†Œ๋ฉด ๊ฐ’์„ 1, ์ฒ˜์Œ์ด ์•„๋‹ˆ๋ฉด ๊ธฐ์กด๊ฐ’+1 ๋กœ ์ €์žฅํ•œ๋‹ค.
    • ๋งŒ์•ฝ ๋‘ ์ง‘ํ•ฉ์˜ ์›์†Œ ๊ฐœ์ˆ˜๊ฐ€ ๋ชจ๋‘ 0์ด๋ผ๋ฉด, ํ•ฉ์ง‘ํ•ฉ์˜ ๊ฐœ์ˆ˜๊ฐ€ 0์ด ๋‚˜์˜จ๋‹ค. ๋ฌธ์ œ์—์„œ 1๋กœ ๊ฐœ์‚ฐํ•˜๋ผ ํ–ˆ์œผ๋ฏ€๋กœ, 1 * 65536 ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • ๊ต์ง‘ํ•ฉ์€ ๊ฒน์น˜๋Š” ์›์†Œ๋ฅผ ์ฐพ๊ณ , ์ €์žฅ๋œ ๋‘ ๊ฐ’์ค‘ ์ตœ์†Œ๊ฐ’์„ ๊ต์ง‘ํ•ฉ ๊ฐœ์ˆ˜์— ๋”ํ•œ๋‹ค.
  • ํ•ฉ์ง‘ํ•ฉ์€ ๊ฒน์น˜๋Š” ์›์†Œ๋ฅผ ์ฐพ๊ณ , ์ €์žฅ๋œ ๋‘ ๊ฐ’์ค‘ ์ตœ๋Œ€๊ฐ’์„ ํ•ฉ์ง‘ํ•ฉ ๊ฐœ์ˆ˜์— ๋”ํ•œ๋‹ค.
    • ์ถ”๊ฐ€๋กœ ๊ฒน์น˜์ง€ ์•Š์€ ์›์†Œ์˜ ๊ฐ’๋„ ํ•ฉ์ง‘ํ•ฉ ๊ฐœ์ˆ˜์— ๋”ํ•œ๋‹ค.
  • ๊ต์ง‘ํ•ฉ / ํ•ฉ์ง‘ํ•ฉ * 65536์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ์นด์นด์˜ค ๋ฌธ์ œ๋ผ์„œ ๊ทธ๋Ÿฐ์ง€ 2๋ ˆ๋ฒจ ์น˜๊ณ ๋Š” ๊ฝค ๋ณต์žกํ–ˆ๋˜ ๊ฒƒ ๊ฐ™๋‹ค. ํ•˜์ง€๋งŒ ๋‹ค์‹œ๋ณด๋‹ˆ ๊ทธ๋ƒฅ ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ์™€ ๊ต์ง‘ํ•ฉ, ํ•ฉ์ง‘ํ•ฉ ๊ตฌํ•˜๊ธฐ์—ฌ์„œ ๊ทธ๋ ‡๊ฒŒ ์–ด๋ ต์ง€๋Š” ์•Š์€ ๊ฒƒ ๊ฐ™๋‹ค.
  • ์ฒ˜์Œ์—” ์•ŒํŒŒ๋ฒณ์ด ์•„๋‹Œ ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•œ ๋’ค 2๋ฌธ์ž์”ฉ ์ž๋ฅด๋Š” ๊ฑฐ๋กœ ์ดํ•ดํ–ˆ๋Š”๋ฐ, ์ž๋ฅธ ๋‹ค์Œ ์ œ๊ฑฐํ•˜๋Š” ๊ฒƒ์ด์—ˆ๋‹ค.
    • ์˜ˆ์ œ 3๋ฒˆ์—์„œ ๋””๋ฒ„๊น…ํ•ด์ค˜์„œ ์•Œ ์ˆ˜ ์žˆ์—ˆ๋‹ค.
    • ์˜ˆ์ œ 4๋ฒˆ์€ ๋‘ ์ง‘ํ•ฉ์ด ๋ชจ๋‘ 0์ธ ๊ฒฝ์šฐ๋ฅผ ๋””๋ฒ„๊น…ํ•ด์ค˜์„œ ๋˜ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.
    • ์˜ˆ์ œ๋งŒ ๋งž์ถ”๋ฉด ํ†ต๊ณผ๋˜๋„๋ก ํ•ด๋‘ฌ์„œ 2๋ ˆ๋ฒจ์ธ ๊ฒƒ ๊ฐ™๊ธฐ๋„ ํ•˜๋‹ค.
  • 2018 KAKAO BLIND RECRUITMENT์˜ 5๋ฒˆ ๋ฌธ์ œ์ธ๋ฐ, ์ค‘๊ธ‰ ๋‚œ์ด๋„์ด๊ณ  ์ •๋‹ต๋ฅ ์€ 41.84%์ด๋‹ค.
    • ๋‹ค์ค‘ ์ง‘ํ•ฉ์—์„œ ํ•ฉ์ง‘ํ•ฉ, ๊ต์ง‘ํ•ฉ์„ ๊ตฌํ˜„ํ•˜๋Š”๊ฒŒ ํ•ต์‹ฌ์ธ ๊ฒƒ ๊ฐ™์€๋ฐ, ๋‹ค์ค‘ ์ง‘ํ•ฉ์„ ๊ตฌํ•˜์ง€ ์•Š๋”๋ผ๋„, ๋ณ‘ํ•ฉ์ •๋ ฌ์„ ํ†ตํ•ด ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค.
  • ๋‹ค์Œ์€ ๋ฌธ์ œ ํ•ด์„ค์ด๋‹ค.
์ด ๋ฌธ์ œ๋Š” ์ž์นด๋“œ ์œ ์‚ฌ๋„๋ฅผ ์„ค๋ช…ํ•ด์ฃผ๊ณ  ์ž์นด๋“œ ์œ ์‚ฌ๋„๋ฅผ ์ง์ ‘ ๊ณ„์‚ฐํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ์ž์นด๋“œ ์œ ์‚ฌ๋„๋Š” ์‹ค๋ฌด์—์„œ๋„ ์œ ์‚ฌํ•œ ๋ฌธ์„œ๋ฅผ ํŒ๋ณ„ํ•  ๋•Œ ์ฃผ๋กœ ์“ฐ์ด๋Š”๋ฐ์š”, ๋ชฐ๋ž๋”๋ผ๋„ ๋ฌธ์ œ์—์„œ ์ž์„ธํžˆ ์„ค๋ช…ํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ์ดํ•ดํ•˜๋Š”๋ฐ ์–ด๋ ค์›€์€ ์—†์—ˆ์„ ๊ฑฐ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ณต์‹์€ ๋งค์šฐ ๊ฐ„๋‹จํ•œ๋ฐ์š”, ๊ต์ง‘ํ•ฉ์„ ํ•ฉ์ง‘ํ•ฉ์œผ๋กœ ๋‚˜๋ˆˆ ์ˆ˜์ž…๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ์ด ๊ฐ’์€ 0์—์„œ 1 ์‚ฌ์ด์˜ ์‹ค์ˆ˜๊ฐ€ ๋˜๋Š”๋ฐ, ์—ฌ๊ธฐ์„œ๋Š” ์ด๋ฅผ ๋‹ค๋ฃจ๊ธฐ ์‰ฝ๋„๋ก 65536์„ ๊ณฑํ•œ ํ›„ ์†Œ์ˆ˜์  ์•„๋ž˜๋ฅผ ๋ฒ„๋ฆฌ๊ณ  ์ •์ˆ˜๋ถ€๋งŒ ์ทจํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

๋ฌธ์ œ ์„ค๋ช…์€ ์›์†Œ์˜ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜๋Š” ๋‹ค์ค‘์ง‘ํ•ฉmultiset์œผ๋กœ ๋˜์–ด ์žˆ๋Š”๋ฐ, ์ž์ฃผ ์ ‘ํ•˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ๊ฐ€ ์•„๋‹ˆ๊ณ , ์ผ๋ถ€ ์–ธ์–ด์—์„œ๋Š” ๊ธฐ๋ณธ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ๊ฐ€ ์•„๋‹ˆ๋ผ ์–ด๋ ค์›Œํ•˜๋Š” ๋ถ„๋“ค์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋‹ค์ค‘์ง‘ํ•ฉ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์“ฐ์ง€ ์•Š๋”๋ผ๋„, ๊ฐ ์›์†Œ๋ฅผ ์ •๋ ฌ๋œ ๋ฐฐ์—ด์— ๋„ฃ์€ ํ›„ ๋ณ‘ํ•ฉ ์ •๋ ฌMerge sort์—์„œ ๋ฐฐ์› ๋˜ ์ฝ”๋“œ๋ฅผ ์‘์šฉ, ์–ด๋ ต์ง€ ์•Š๊ฒŒ ํ•ฉ์ง‘ํ•ฉ๊ณผ ๊ต์ง‘ํ•ฉ ํ•จ์ˆ˜๋ฅผ ์ง์ ‘ ๊ตฌํ˜„ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

๋‹ค์ค‘์ง‘ํ•ฉ์˜ ๊ต์ง‘ํ•ฉ, ํ•ฉ์ง‘ํ•ฉ๋งŒ ์ž˜ ๊ตฌํ•ด๋‚ธ๋‹ค๋ฉด ์ด ๋ฌธ์ œ๋Š” ์–ด๋ ต์ง€ ์•Š๊ฒŒ ํ’€ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋‹ค๋งŒ ์ง‘ํ•ฉ A์™€ B๊ฐ€ ๋ชจ๋‘ ๊ณต์ง‘ํ•ฉ์ผ ๊ฒฝ์šฐ์—๋Š” ๋‚˜๋ˆ—์…ˆ์ด ์ •์˜๋˜์ง€ ์•Š์œผ๋ฏ€๋กœdivision by zero ๋”ฐ๋กœ J(A,B) = 1๋กœ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, 65536์„ ๊ณฑํ•˜๋ฉด ์ด ๊ฒฝ์šฐ 1 * 65536 = 65536์ด ์ •๋‹ต์ด ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ ์ž…์ถœ๋ ฅ์—๋„ ํ•ฉ์ง‘ํ•ฉ์ด ๊ณต์ง‘ํ•ฉ์ธ ๊ฒฝ์šฐ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ ์ด ๊ฒฝ์šฐ๋งŒ ์ฃผ์˜ํ•œ๋‹ค๋ฉด ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

์ด ๋ฌธ์ œ์˜ ์ •๋‹ต๋ฅ ์€ 41.84%์ž…๋‹ˆ๋‹ค.

728x90

'CodingTest > Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Lv.2 : ์••์ถ•  (0) 2023.11.09
Lv.2 : k์ง„์ˆ˜์—์„œ ์†Œ์ˆ˜ ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ  (0) 2023.11.08
Lv.2 : ํ”„๋กœ์„ธ์Šค  (0) 2023.11.03
BOJ_1235 : ํ•™์ƒ ๋ฒˆํ˜ธ  (0) 2023.11.02
Lv.2 : ๊ธฐ๋Šฅ๊ฐœ๋ฐœ  (0) 2023.11.02