Notice
Recent Posts
Recent Comments
Link
ยซ   2025/01   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Lennon FE

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์œ„ํด๋ฆฌ ์ฑŒ๋ฆฐ์ง€ 4์ฃผ ์ฐจ (javascript) ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์œ„ํด๋ฆฌ ์ฑŒ๋ฆฐ์ง€ 4์ฃผ ์ฐจ (javascript)

Lennon 2021. 8. 25. 19:13
728x90
๋ฐ˜์‘ํ˜•

์—ฌ๋Ÿฌ๋ถ„๋“ค์€ ์ด ๋ฌธ์ œ์— ์–ด๋–ป๊ฒŒ ์ ‘๊ทผํ•  ๊ฒƒ์ธ์ง€ ์ƒ๊ฐํ•ด๋ณด์„ธ์š”!

 

 

๋ฌธ์ œ ์„ค๋ช…

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

์•„๋ž˜ ํ‘œ๋Š” 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" ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

tablelanguagespreferenceresult

["SI JAVA JAVASCRIPT SQL PYTHON C#", "CONTENTS JAVASCRIPT JAVA PYTHON SQL C++", "HARDWARE C C++ PYTHON JAVA JAVASCRIPT", "PORTAL JAVA JAVASCRIPT PYTHON KOTLIN PHP", "GAME C++ C# JAVASCRIPT C JAVA"] ["PYTHON", "C++", "SQL"] [7, 5, 5] "HARDWARE"
["SI JAVA JAVASCRIPT SQL PYTHON C#", "CONTENTS JAVASCRIPT JAVA PYTHON SQL C++", "HARDWARE C C++ PYTHON JAVA JAVASCRIPT", "PORTAL JAVA JAVASCRIPT PYTHON KOTLIN PHP", "GAME C++ C# JAVASCRIPT C JAVA"] ["JAVA", "JAVASCRIPT"] [7, 5] "PORTAL"

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

์ž…์ถœ๋ ฅ ์˜ˆ #1

๊ฐ ์ง์—…๊ตฐ ๋ณ„๋กœ ์ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•ด๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์•„๋ž˜ ์‚ฌ์ง„์€ ๊ฐœ๋ฐœ์ž ์–ธ์–ด ์„ ํ˜ธ๋„ ๋‚˜ํƒ€๋‚ธ ํ‘œ์ž…๋‹ˆ๋‹ค.

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

๋”ฐ๋ผ์„œ ์ ์ˆ˜ ์ดํ•ฉ์ด 41๋กœ ๊ฐ€์žฅ ๋†’์€ "HARDWARE"๋ฅผ return ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #2

๊ฐ ์ง์—…๊ตฐ ๋ณ„๋กœ ์ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•ด๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์•„๋ž˜ ์‚ฌ์ง„์€ ๊ฐœ๋ฐœ์ž ์–ธ์–ด ์„ ํ˜ธ๋„ ๋‚˜ํƒ€๋‚ธ ํ‘œ์ž…๋‹ˆ๋‹ค.

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


์ ์ˆ˜ ์ดํ•ฉ์ด 55๋กœ ๊ฐ€์žฅ ๋†’์€ ์ง์—…๊ตฐ์€ "SI" ์™€ "PORTAL"์ž…๋‹ˆ๋‹ค.
๋”ฐ๋ผ์„œ ์‚ฌ์ „ ์ˆœ์œผ๋กœ ๋จผ์ € ์˜ค๋Š” "PORTAL"์„ return ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

 

 

 

 

์ด๋ฒˆ ์œ„ํด๋ฆฌ ๋ฌธ์ œ๋ฅผ ๋ณด๊ณ  ํ’€์ด๊ณผ์ •์„ ์ƒ๊ฐํ•ด๋ดค๋‹ค.

 

1. ์ œ๊ณต๋œ table์„ 2์ฐจ์› ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ 

2. ๋ณ€ํ™˜ ํ›„ ์ œ๊ณต๋œ ์–ธ์–ด์™€ ์ ์ˆ˜๋ฅผ ๊ฐ ํฐ ๋ฐฐ์—ด๋ณ„๋กœ ๊ณ„์‚ฐ ๋ฐ ๋ฐ˜ํ™˜ํ•˜์—ฌ ๋ฐฐ์—ด์— push

3. ๋ฐฐ์—ด์— ์ €์žฅํ•œ ํ›„ ๊ฐ’์— ๋งž๋Š” table์˜ ์ง์—…๊ตฐ์„ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์— push

4. ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ์‚ฌ์ „๋ณ„๋กœ ์ •๋ ฌ ํ›„ ๋งจ ์ฒซ ๊ฐ’ return

 

function solution(table, languages, preference) {
    let arr = [];
    for(let i = 0; i < table.length; i++) arr.push(table[i].split(" "));

    let result = [];
    let count = 0;
    for(let i = 0; i < arr.length; i++){ 
        for(let j = 0; j < languages.length; j++){
            if(arr[i].includes(languages[j])){
                count += (arr[i].length - arr[i].indexOf(languages[j])) * preference[j];
            }
        }
        result.push(count);
        count = 0;
    }

    let answer = [];
    for(let i = 0; i < result.length; i++){
        if(Math.max(...result) === result[i]){
            answer.push(arr[i][0]);
        }
    }
    answer.sort();
    return answer[0];
}

 

๊ฐ„๋‹จํ•œ ๋ฌธ์ œ๋ผ ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค๋„ ์‰ฝ๊ฒŒ ํ’€์—ˆ๊ฒ ๋‹ค ์ƒ๊ฐ ํ–ˆ์ง€๋งŒ

๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์˜ ํ’€์ด๋Š” ์ •๋ง ๊ฐ๋ช… ๊นŠ์–ด ๋ช‡ ๋ฒˆ์ด๊ณ  ๋น„๊ตํ–ˆ๋‹ค ใ…  ๋” ์—ด์‹ฌํžˆ ํ•ด์•ผ๊ฒ ๋‹ค!

 

728x90
๋ฐ˜์‘ํ˜•
Comments