๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Algorithm

[Algorithm/Java][ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ง์—…๊ตฐ ์ถ”์ฒœํ•˜๊ธฐ

๋ฐ˜์‘ํ˜•

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ง์—…๊ตฐ ์ถ”์ฒœํ•˜๊ธฐ

https://programmers.co.kr/learn/courses/30/lessons/84325

๋ฌธ์ œ ์„ค๋ช…

๊ฐœ๋ฐœ์ž๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ์–ธ์–ด์™€ ์–ธ์–ด ์„ ํ˜ธ๋„๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ๊ทธ์— ๋งž๋Š” ์ง์—…๊ตฐ์„ ์ถ”์ฒœํ•ด์ฃผ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ฐœ๋ฐœํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

์•„๋ž˜ ํ‘œ๋Š” 5๊ฐœ ์ง์—…๊ตฐ ๋ณ„๋กœ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” 5๊ฐœ ์–ธ์–ด์— ์ง์—…๊ตฐ ์–ธ์–ด ์ ์ˆ˜๋ฅผ ๋ถ€์—ฌํ•œ ํ‘œ์ž…๋‹ˆ๋‹ค.

์ ์ˆ˜ SI CONTENTS HARDWARE PORTAL GAME
5 JAVA JAVASCRIPT C JAVA C++
4 JAVASCRIPT JAVA C++ JAVASCRIPT C#
3 SQL PYTHON PYTHON PYTHON JAVASCRIPT
2 PYTHON SQL JAVA KOTLIN C
1 C# C++ JAVASCRIPT PHP JAVA
์˜ˆ๋ฅผ ๋“ค๋ฉด, SQL์˜ SI ์ง์—…๊ตฐ ์–ธ์–ด ์ ์ˆ˜๋Š” 3์ ์ด์ง€๋งŒ CONTENTS ์ง์—…๊ตฐ ์–ธ์–ด ์ ์ˆ˜๋Š” 2์ ์ž…๋‹ˆ๋‹ค. SQL์˜ HARDWARE, PORTAL, GAME ์ง์—…๊ตฐ ์–ธ์–ด ์ ์ˆ˜๋Š” 0์ ์ž…๋‹ˆ๋‹ค.

์ง์—…๊ตฐ ์–ธ์–ด ์ ์ˆ˜๋ฅผ ์ •๋ฆฌํ•œ ๋ฌธ์ž์—ด ๋ฐฐ์—ด table, ๊ฐœ๋ฐœ์ž๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ์–ธ์–ด๋ฅผ ๋‹ด์€ ๋ฌธ์ž์—ด ๋ฐฐ์—ด languages, ์–ธ์–ด ์„ ํ˜ธ๋„๋ฅผ ๋‹ด์€ ์ •์ˆ˜ ๋ฐฐ์—ด preference๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ๊ฐœ๋ฐœ์ž๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ์–ธ์–ด์˜ ์–ธ์–ด ์„ ํ˜ธ๋„ x ์ง์—…๊ตฐ ์–ธ์–ด ์ ์ˆ˜์˜ ์ดํ•ฉ์ด ๊ฐ€์žฅ ๋†’์€ ์ง์—…๊ตฐ์„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”. ์ดํ•ฉ์ด ๊ฐ™์€ ์ง์—…๊ตฐ์ด ์—ฌ๋Ÿฌ ๊ฐœ์ผ ๊ฒฝ์šฐ, ์ด๋ฆ„์ด ์‚ฌ์ „ ์ˆœ์œผ๋กœ ๊ฐ€์žฅ ๋น ๋ฅธ ์ง์—…๊ตฐ์„ return ํ•ด์ฃผ์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • table์˜ ๊ธธ์ด = 5
    table์˜ ์›์†Œ๋Š” "์ง์—…๊ตฐ 5์ ์–ธ์–ด 4์ ์–ธ์–ด 3์ ์–ธ์–ด 2์ ์–ธ์–ด 1์ ์–ธ์–ด"ํ˜•์‹์˜ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค. ์ง์—…๊ตฐ, 5์ ์–ธ์–ด, 4์–ธ์–ด, 3์ ์–ธ์–ด, 2์  ์–ธ์–ด, 1์ ์–ธ์–ด๋Š” ํ•˜๋‚˜์˜ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.
    table์€ ๋ชจ๋“  ํ…Œ์ŠคํŠธ์ผ€์ด์Šค์—์„œ ๋™์ผํ•ฉ๋‹ˆ๋‹ค.
  • 1 โ‰ค languages์˜ ๊ธธ์ด โ‰ค 9
    languages์˜ ์›์†Œ๋Š” "JAVA", "JAVASCRIPT", "C", "C++" ,"C#" , "SQL", "PYTHON", "KOTLIN", "PHP" ์ค‘ ํ•œ ๊ฐœ ์ด์ƒ์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
    languages์˜ ์›์†Œ๋Š” ์ค‘๋ณต๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • preference์˜ ๊ธธ์ด = languages์˜ ๊ธธ์ด
    1 โ‰ค preference์˜ ์›์†Œ โ‰ค 10
  • preference์˜ i๋ฒˆ์งธ ์›์†Œ๋Š” languages์˜ i๋ฒˆ์งธ ์›์†Œ์˜ ์–ธ์–ด ์„ ํ˜ธ๋„์ž…๋‹ˆ๋‹ค.
  • return ํ•  ๋ฌธ์ž์—ด์€ "SI", "CONTENTS", "HARDWARE", "PORTAL", "GAME" ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค.

Code

Java
class Solution {
    public String solution(String[] table, String[] languages, int[] preference) {
        String answer = null;
        int cur_score,max_score;
        max_score = 0;
        for(int i=0; i<table.length; i++){
            cur_score = 0;
            String[] temp = table[i].split(" ");
            for(int j = 1; j<temp.length; j++){
                for(int k = 0; k<languages.length; k++){
                    if(temp[j].equals(languages[k])){
                        cur_score += (6-j)*preference[k];
                    }
                }
            }
           if(max_score < cur_score){
               max_score = cur_score;
               answer = temp[0];
           }
            if(max_score == cur_score){
                if(answer.compareTo(temp[0])>0){
                    answer=temp[0];
                }
            }

        }
        return answer;
    }
}

Code ์„ค๋ช…

table์„ ๋ฐ˜๋ณต๋ฌธ์„ ํ†ตํ•ด์„œ ํ•œ์ค„์”ฉ ์ฒ˜๋ฆฌํ•œ๋‹ค.
๋จผ์ € split์„ ์ด์šฉํ•ด์„œ ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ์ž˜๋ผ์„œ String ๋ฐฐ์—ด "temp"์— ๋„ฃ์–ด์ค€๋‹ค.
temp์˜ ์ฒซ๋ฒˆ์งธ ์›์†Œ๋Š” ์ง์—…์— ๊ด€ํ•œ ๊ฒƒ์ด๋ฏ€๋กœ ๋‘๋ฒˆ์งธ๋ถ€ํ„ฐ ๋ฐ˜๋ณต๋ฌธ์„ ์ด์šฉํ•ด์„œ languages์— ์žˆ๋Š” ๊ฐ’๋“ค๊ณผ ๋น„๊ต๋ฅผ ํ•ด์ค€๋‹ค.
temp์˜ ๋‘๋ฒˆ์งธ ์›์†Œ๋ถ€ํ„ฐ 5์  ์ง์—…๊ตฐ์ด๋ฏ€๋กœ score๋Š” (6-j)๋ฅผ ํ•˜์—ฌ ๋งž์ถฐ์ค€๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์„ ํ˜ธ๋„์™€ ๊ณฑํ•ด์„œ cur_score์— ๋”ํ•ด์ค€๋‹ค. cur_score๋Š” ํ˜„์žฌ ์ง์—…์— ์„ ํ˜ธ๋„์™€ ์ ์ˆ˜๋ฅผ ๋ชจ๋‘ ๋”ํ•ด์ค€๊ฒƒ์ด๋‹ค.
๊ทธ๋ฆฌ๊ณ  ๋งˆ์ง€๋ง‰์— max_score์™€ ๋น„๊ตํ•ด์ฃผ์–ด ๋†’์€ ์ ์ˆ˜๋ฅผ ๊ฐ€์ง„ ์ง์—…๊ตฐ์„ answer์— ๋„ฃ์–ด์ค€๋‹ค. ์ด๋•Œ max_score์™€ cur_score๊ฐ€ ๊ฐ™๋‹ค๋ฉด ๋‘ ์ง์—…๊ตฐ์ค‘์—์„œ compareTo๋ฅผ ์ด์šฉํ•ด์„œ ์‚ฌ์ „์ˆœ์œผ๋กœ ๋น ๋ฅธ ์ง์—…๊ตฐ์„ anwer์— ๋„ฃ์–ด์ค€๋‹ค.

๋ฐฐ์šด์ 

  1. String[] arr ์ด ์žˆ์„๋•Œ String[] arr2 = arr.split("์›ํ•˜๋Š” ๋ฌธ์ž")๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด ์›ํ•˜๋Š” ๋ฌธ์ž๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์ž์—ด์„ ์ž˜๋ผ์„œ arr2๋ฐฐ์—ด์— ๋„ฃ์–ด์ค„ ์ˆ˜ ์žˆ๋‹ค.
  2. String str1,str2 ๊ฐ€ ์žˆ์„ ๋•Œ str1.compareTo(str2)๋ฅผ ํ•˜๊ฒŒ๋œ๋‹ค๋ฉด
    • str1๊ณผ str2๊ฐ€ ๊ฐ™๋‹ค๋ฉด 0
    • str1์ด str2 ๋ณด๋‹ค ์‚ฌ์ „์ˆœ์œผ๋กœ ๋น ๋ฅด๋‹ค๋ฉด ์Œ์ˆ˜
    • str1์ด str2๋ณด๋‹ค ์‚ฌ์ „์ˆœ์œผ๋กœ ๋Š๋ฆฌ๋‹ค๋ฉด ์–‘์ˆ˜
      ๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ค€๋‹ค.
๋ฐ˜์‘ํ˜•