Recent Posts
Recent Comments
Link
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

kohigowild

221018 :: [ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] 2016๋…„, ์œ„์žฅ, ๋„คํŠธ์›Œํฌ ๋ณธ๋ฌธ

Algorithm

221018 :: [ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] 2016๋…„, ์œ„์žฅ, ๋„คํŠธ์›Œํฌ

kohi โ˜• 2022. 10. 19. 00:58
2016๋…„
๐Ÿ‘พ ๋ฌธ์ œ ์„ค๋ช…
2016๋…„ 1์›” 1์ผ์€ ๊ธˆ์š”์ผ์ž…๋‹ˆ๋‹ค. 2016๋…„ a์›” b์ผ์€ ๋ฌด์Šจ ์š”์ผ์ผ๊นŒ์š”? ๋‘ ์ˆ˜ a ,b๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ 2016๋…„ a์›” b์ผ์ด ๋ฌด์Šจ ์š”์ผ์ธ์ง€ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•˜์„ธ์š”.

์š”์ผ์˜ ์ด๋ฆ„์€ ์ผ์š”์ผ๋ถ€ํ„ฐ ํ† ์š”์ผ๊นŒ์ง€ ๊ฐ๊ฐ SUN,MON,TUE,WED,THU,FRI,SAT์ž…๋‹ˆ๋‹ค.
์˜ˆ๋ฅผ ๋“ค์–ด a=5, b=24๋ผ๋ฉด 5์›” 24์ผ์€ ํ™”์š”์ผ์ด๋ฏ€๋กœ ๋ฌธ์ž์—ด "TUE"๋ฅผ ๋ฐ˜ํ™˜ํ•˜์„ธ์š”.

๐Ÿ‘พ ์ œํ•œ ์‚ฌํ•ญ
  • 2016๋…„์€ ์œค๋…„์ž…๋‹ˆ๋‹ค.
  • 2016๋…„ a์›” b์ผ์€ ์‹ค์ œ๋กœ ์žˆ๋Š” ๋‚ ์ž…๋‹ˆ๋‹ค. (13์›” 26์ผ์ด๋‚˜ 2์›” 45์ผ๊ฐ™์€ ๋‚ ์งœ๋Š” ์ฃผ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค)

 

๐Ÿ‘พ ๋‚˜์˜ ๋‹ต์•ˆ

function solution(a, b) {
    return new Date(2016, a - 1, b).toString().split(" ")[0].toUpperCase();
}

 

  • Date ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ’€์ดํ•˜์˜€๋‹ค. a ์ž๋ฆฌ์˜ ๊ฒฝ์šฐ monthIndex์˜ ์ž๋ฆฌ์ด๋ฏ€๋กœ (ex:: 0 - Jan, 1 - Feb, 2 - Mar...) a์— -1์„ ํ•ด ์ฃผ์–ด์•ผ ์›ํ•˜๋Š” ๊ฐ’์„ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.
  • new Date(2016, 3, 12).toString() // Output: Tue Apr 12 2016 00:00:00 GMT+0900 (๋Œ€ํ•œ๋ฏผ๊ตญ ํ‘œ์ค€์‹œ)
  • ์š”์ผ๋งŒ ๋Œ€๋ฌธ์ž ๋ฐ˜ํ™˜ํ•˜๋ฉด ๋˜๋ฏ€๋กœ ๋„์–ด์“ฐ๊ธฐ ๋‹จ์œ„๋กœ ์ชผ๊ฐœ์„œ ๋ฐฐ์—ด์„ ๋งŒ๋“  ๋’ค ์ธ๋ฑ์Šค 0 ๊ฐ’์„ ๋Œ€๋ฌธ์ž ๋ณ€ํ™˜ ํ›„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

๐Ÿ‘พ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด

function solution(a, b) {
    const monthDay = [31,29,31,30,31,30,31,31,30,31,30,31]
    const weekDay = ["THU", "FRI", "SAT", "SUN", "MON", "TUE", "WED"]

    let days = b
    for(let i=0 ; i<a-1 ; i++)
        days += monthDay[i];

    return weekDay[days%7];
}
function solution(a, b) {
    var answer = '';
    let days= ['THU','FRI','SAT','SUN','MON','TUE','WED'];
    let months = {
        29:[2],
        30:[4,6,9,11],
        31:[1,3,5,7,8,10,12]
    }
    let sum = 0;
    for (let key in months){
      months[key].forEach(el => {
          if (el < a){
            sum = sum + Number(key);
          }
      })       
    }
      let checkedNum = (sum+b)%7;
        answer = days[checkedNum]   
    return answer;
}

 

  • Date ๊ฐ์ฒด๋ฅผ ์“ฐ์ง€ ์•Š์€ ํ’€์ด
  • ๋‘ ๋ฒˆ์งธ ๋‹ต์•ˆ์€ months ๊ฐ์ฒด๋ฅผ ๊น”๋”ํ•˜๊ฒŒ ์ž˜ ๊ตฌํ˜„ํ•œ ๊ฒƒ ๊ฐ™๋‹ค

 


์œ„์žฅ
๐Ÿ‘พ ๋ฌธ์ œ ์„ค๋ช…

์ŠคํŒŒ์ด๋“ค์€ ๋งค์ผ ๋‹ค๋ฅธ ์˜ท์„ ์กฐํ•ฉํ•˜์—ฌ ์ž…์–ด ์ž์‹ ์„ ์œ„์žฅํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด ์ŠคํŒŒ์ด๊ฐ€ ๊ฐ€์ง„ ์˜ท์ด ์•„๋ž˜์™€ ๊ฐ™๊ณ  ์˜ค๋Š˜ ์ŠคํŒŒ์ด๊ฐ€ ๋™๊ทธ๋ž€ ์•ˆ๊ฒฝ, ๊ธด ์ฝ”ํŠธ, ํŒŒ๋ž€์ƒ‰ ํ‹ฐ์…”์ธ ๋ฅผ ์ž…์—ˆ๋‹ค๋ฉด ๋‹ค์Œ๋‚ ์€ ์ฒญ๋ฐ”์ง€๋ฅผ ์ถ”๊ฐ€๋กœ ์ž…๊ฑฐ๋‚˜ ๋™๊ทธ๋ž€ ์•ˆ๊ฒฝ ๋Œ€์‹  ๊ฒ€์ • ์„ ๊ธ€๋ผ์Šค๋ฅผ ์ฐฉ์šฉํ•˜๊ฑฐ๋‚˜ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์–ผ๊ตด ๋™๊ทธ๋ž€ ์•ˆ๊ฒฝ, ๊ฒ€์ • ์„ ๊ธ€๋ผ์Šค
์ƒ์˜ ํŒŒ๋ž€์ƒ‰ ํ‹ฐ์…”์ธ 
ํ•˜์˜ ์ฒญ๋ฐ”์ง€
๊ฒ‰์˜ท ๊ธด ์ฝ”ํŠธ

์ŠคํŒŒ์ด๊ฐ€ ๊ฐ€์ง„ ์˜์ƒ๋“ค์ด ๋‹ด๊ธด 2์ฐจ์› ๋ฐฐ์—ด clothes๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ ์„œ๋กœ ๋‹ค๋ฅธ ์˜ท์˜ ์กฐํ•ฉ์˜ ์ˆ˜๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

๐Ÿ‘พ ์ œํ•œ ์‚ฌํ•ญ

  • clothes์˜ ๊ฐ ํ–‰์€ [์˜์ƒ์˜ ์ด๋ฆ„, ์˜์ƒ์˜ ์ข…๋ฅ˜]๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ŠคํŒŒ์ด๊ฐ€ ๊ฐ€์ง„ ์˜์ƒ์˜ ์ˆ˜๋Š” 1๊ฐœ ์ด์ƒ 30๊ฐœ ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • ๊ฐ™์€ ์ด๋ฆ„์„ ๊ฐ€์ง„ ์˜์ƒ์€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • clothes์˜ ๋ชจ๋“  ์›์†Œ๋Š” ๋ฌธ์ž์—ด๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋ชจ๋“  ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋Š” 1 ์ด์ƒ 20 ์ดํ•˜์ธ ์ž์—ฐ์ˆ˜์ด๊ณ  ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž ๋˜๋Š” '_' ๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ŠคํŒŒ์ด๋Š” ํ•˜๋ฃจ์— ์ตœ์†Œ ํ•œ ๊ฐœ์˜ ์˜์ƒ์€ ์ž…์Šต๋‹ˆ๋‹ค.

 

๐Ÿ‘พ ๋‚˜์˜ ๋‹ต์•ˆ

function solution(clothes) {
    let obj = {};
    let cnt = 1;

    for (let i = 0; i < clothes.length; i++) {
        if (obj[clothes[i][1]] >= 1) {
            obj[clothes[i][1]] += 1;
        } else {
            obj[clothes[i][1]] = 1;
        }
    }

    for (let i = 0; i < obj.length; i++) {
        cnt *= obj[key] + 1;
    }

    return cnt - 1;
}

 

  • ์ฒซ ๋‹ต์•ˆ, ๋ชจ๋“  ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์—์„œ 0์ด ๋‚˜์˜ค๋Š” ๊ฑธ ๋ณด๋‹ˆ๊นŒ for๋ฌธ์ด ์˜๋„๋Œ€๋กœ ์ž‘์„ฑ์ด ์•ˆ ๋œ ๊ฒƒ ๊ฐ™์Œ
  • ๋นˆ ๊ฐ์ฒด obj ์ƒ์„ฑ ํ›„ ์˜ท์˜ ์ข…๋ฅ˜(clothes[i][1])๋ฅผ key๋ผ๊ณ  ํ•˜๊ณ , ๊ฐ์ฒด์˜ ๋™์ผ key๊ฐ€ ํ•œ ๊ฐœ ์ด์ƒ์ด๋ฉด 1์„ ๋”ํ•˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด 1๋กœ ๋งŒ๋“ ๋‹ค.
  • ์˜ˆ๋ฅผ ๋“ค์–ด, ์–ผ๊ตด 3, ์ƒ์˜ 2, ํ•˜์˜ 2์ธ ๊ฒฝ์šฐ ์˜ท์„ ์ž…๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋Š” (3 + 1) * (2 + 1) * (2 + 1)์ด ๋œ๋‹ค. +1์€ ์•„๋ฌด๊ฒƒ๋„ ์ž…์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์ด๋‹ค. ๋”ฐ๋ผ์„œ key๊ฐ’์— 1์„ ๋”ํ•ด์„œ ๋ชจ๋‘ ๊ณฑํ•ด์ฃผ๋ฉด ๋จ.
  • ์ŠคํŒŒ์ด๋Š” ์ตœ์†Œ ํ•œ ๊ฐœ์˜ ์˜ท์€ ์ž…์œผ๋ฏ€๋กœ ๋ˆ„์ ๋œ cnt ๊ฐ’์—์„œ 1์„ (๋ชจ๋“  ์˜ท์„ ์ž…์ง€ ์•Š์€ ๊ฒฝ์šฐ) ๋นผ์„œ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

function solution(clothes) {
    let obj = {};
    let cnt = 1;

    for (let i = 0; i < clothes.length; i++) {
        if (obj[clothes[i][1]] >= 1) {
            obj[clothes[i][1]] += 1;
        } else {
            obj[clothes[i][1]] = 1;
        }
    }

    for (let key in obj) {
        cnt *= obj[key] + 1;
    }

    return cnt - 1;
}

 

  • ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ๋ชจ๋‘ ํ†ต๊ณผํ•œ ๋‹ต์•ˆ
  • ๊ฐ์ฒด ์ ‘๊ทผ ์‹œ์—๋Š” for in ๋ฌธ์„ ์จ์•ผ ํ•จ!!

 

๐Ÿ‘พ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด

function solution(clothes) {
    return Object.values(clothes.reduce((obj, t)=> {
        obj[t[1]] = obj[t[1]] ? obj[t[1]] + 1 : 1;
        return obj;
    } , {})).reduce((a,b)=> a*(b+1), 1)-1;    
}

 

  • ์˜ท์˜ ์ข…๋ฅ˜(obj[t[1]])๋กœ ๊ฐ’์„ ๋ˆ„์ ํ•˜๊ณ , ๋ˆ„์ ํ•œ ๊ฐ’ + 1์„ ๋ชจ๋‘ ๊ณฑํ•œ ๋’ค 1์„ ๋นผ์„œ ๋ฆฌํ„ด

 


๋„คํŠธ์›Œํฌ
๐Ÿ‘พ ๋ฌธ์ œ ์„ค๋ช…
๋„คํŠธ์›Œํฌ๋ž€ ์ปดํ“จํ„ฐ ์ƒํ˜ธ ๊ฐ„์— ์ •๋ณด๋ฅผ ๊ตํ™˜ํ•  ์ˆ˜ ์žˆ๋„๋ก ์—ฐ๊ฒฐ๋œ ํ˜•ํƒœ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์ปดํ“จํ„ฐ A์™€ ์ปดํ“จํ„ฐ B๊ฐ€ ์ง์ ‘์ ์œผ๋กœ ์—ฐ๊ฒฐ๋˜์–ด์žˆ๊ณ , ์ปดํ“จํ„ฐ B์™€ ์ปดํ“จํ„ฐ C๊ฐ€ ์ง์ ‘์ ์œผ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์„ ๋•Œ ์ปดํ“จํ„ฐ A์™€ ์ปดํ“จํ„ฐ C๋„ ๊ฐ„์ ‘์ ์œผ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์ •๋ณด๋ฅผ ๊ตํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ปดํ“จํ„ฐ A, B, C๋Š” ๋ชจ๋‘ ๊ฐ™์€ ๋„คํŠธ์›Œํฌ ์ƒ์— ์žˆ๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
์ปดํ“จํ„ฐ์˜ ๊ฐœ์ˆ˜ n, ์—ฐ๊ฒฐ์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ๋‹ด๊ธด 2์ฐจ์› ๋ฐฐ์—ด computers๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ๋„คํŠธ์›Œํฌ์˜ ๊ฐœ์ˆ˜๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜์‹œ์˜ค.

๐Ÿ‘พ ์ œํ•œ ์‚ฌํ•ญ
  • ์ปดํ“จํ„ฐ์˜ ๊ฐœ์ˆ˜ n์€ 1 ์ด์ƒ 200 ์ดํ•˜์ธ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.
  • ๊ฐ ์ปดํ“จํ„ฐ๋Š” 0๋ถ€ํ„ฐ n-1์ธ ์ •์ˆ˜๋กœ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค.
  • i๋ฒˆ ์ปดํ“จํ„ฐ์™€ j๋ฒˆ ์ปดํ“จํ„ฐ๊ฐ€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์œผ๋ฉด computers[i][j]๋ฅผ 1๋กœ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค.
  • computer[i][i]๋Š” ํ•ญ์ƒ 1์ž…๋‹ˆ๋‹ค.

 

์ฑ… ์•ˆ ์ฝ์€ ๋†ˆ๋ณด๋‹ค ์ฑ… ํ•œ ๊ถŒ ์ฝ์€ ๋†ˆ์ด ๋ฌด์„ญ๋‹ค ํ•จ

์˜ˆ์ „์— ์ปดํ“จํ„ฐ์ผ๋ฐ˜ ๊ณผ๋ชฉ ๊ณต๋ถ€ํ•  ๋•Œ DFS BFS ํƒ์ƒ‰ ์ˆœ์„œ ๊ฐ™์€ ๊ฑฐ ์™ธ์šด ์ ์ด ์žˆ์–ด์„œ ๋Œ€์ถฉ ๊ทธ๊ฒŒ ๋ญ”์ง€๋Š” ์•Ž

๋‚ด๊ฐ€ ์ง€๊ธˆ ์ฑ… ํ•œ ๊ถŒ ์ฝ์€ ๋†ˆ์ธ ๊ฑฐ์ž„

์ด ๋ฌธ์ œ๋ฅผ ํ†ตํ•ด ์ฑ… ๋‘ ๊ถŒ ์ •๋„๋Š” ์ฝ์€ ๋†ˆ์ด ๋  ๊ฒƒ์ด๋‹ค

 

๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰

  • ํ•˜๋‚˜์˜ ์ •์ ์œผ๋กœ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์—ฌ ์ฐจ๋ก€๋Œ€๋กœ ๋ชจ๋“  ์ •์ ๋“ค์„ ํ•œ ๋ฒˆ์”ฉ ๋ฐฉ๋ฌธํ•˜๋Š” ๊ฒƒ

๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰ (DFS)

  • ๋ฃจํŠธ ๋…ธํŠธ(ํ˜น์€ ๋‹ค๋ฅธ ์ž„์˜์˜ ๋…ธ๋“œ)์—์„œ ์‹œ์ž‘ํ•ด์„œ ๋‹ค์Œ ๋ถ„๊ธฐ๋กœ ๋„˜์–ด๊ฐ€๊ธฐ ์ „์— ํ•ด๋‹น ๋ถ„๊ธฐ๋ฅผ ์™„๋ฒฝํ•˜๊ฒŒ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ•. ์ฆ‰, ๋„“๊ฒŒ ํƒ์ƒ‰ํ•˜๊ธฐ ์ „์— ๊นŠ๊ฒŒ ํƒ์ƒ‰ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.
  • ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฒฝ์šฐ ์ด ๋ฐฉ๋ฒ•์„ ์„ ํƒํ•œ๋‹ค.
  • ์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ตฌํ˜„ํ•  ๋•Œ ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰์˜ ๊ฒฝ์šฐ, ์–ด๋–ค ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ–ˆ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ๋ฐ˜๋“œ์‹œ ๊ฒ€์‚ฌํ•ด์•ผ ํ•œ๋‹ค. ์ด๋ฅผ ๊ฒ€์‚ฌํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ ๋ฌดํ•œ๋ฃจํ”„์— ๋น ์งˆ ์œ„ํ—˜์ด ์žˆ๋‹ค.

 

๊ตฌํ˜„ ์ฝ”๋“œ

const graph = {
  A: ["B", "C"],
  B: ["A", "D"],
  C: ["A", "G", "H", "I"],
  D: ["B", "E", "F"],
  E: ["D"],
  F: ["D"],
  G: ["C"],
  H: ["C"],
  I: ["C", "J"],
  J: ["I"]
};

const DFS = (graph, startNode) => {
  const visited = []; // ํƒ์ƒ‰์„ ๋งˆ์นœ ๋…ธ๋“œ๋“ค
  let needVisit = []; // ํƒ์ƒ‰ํ•ด์•ผํ•  ๋…ธ๋“œ๋“ค

  needVisit.push(startNode); // ๋…ธ๋“œ ํƒ์ƒ‰ ์‹œ์ž‘

  while (needVisit.length !== 0) { // ํƒ์ƒ‰ํ•ด์•ผํ•  ๋…ธ๋“œ๊ฐ€ ๋‚จ์•„์žˆ๋‹ค๋ฉด
    const node = needVisit.shift(); // queue์ด๊ธฐ ๋•Œ๋ฌธ์— ์„ ์ž…์„ ์ถœ, shift()๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
    if (!visited.includes(node)) { // ํ•ด๋‹น ๋…ธ๋“œ๊ฐ€ ํƒ์ƒ‰๋œ ์  ์—†๋‹ค๋ฉด
      visited.push(node); 
      needVisit = [...graph[node], ...needVisit];
    }
  }
  return visited;
};

console.log(DFS(graph, "A"));
// ["A", "B", "D", "E", "F", "C", "G", "H", "I", "J"]

 

๋ ˆํผ๋Ÿฐ์Šค

https://gmlwjd9405.github.io/2018/08/14/algorithm-dfs.html

 

[์•Œ๊ณ ๋ฆฌ์ฆ˜] ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰(DFS)์ด๋ž€ - Heee's Development Blog

Step by step goes a long way.

gmlwjd9405.github.io

https://velog.io/@sangbooom/JS-BFS-DFS

 

[JS] BFS, DFS

์›๋ž˜ c++๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ณต๋ถ€ํ•˜๋‹ค๊ฐ€ ์›น ํ”„๋ก ํŠธ์—”๋“œ๋งŒ ์ฃผ๊ตฌ์žฅ์ฐฝ ํ•˜๋‹ค๋ณด๋‹ˆ Javascript๊ฐ€ ์ต์ˆ™ํ•ด์„œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์–ธ์–ด๋ฅผ ๋ฐ”๊ฟจ๋‹ค..c++๋กœ BFS, DFS ์ฒ˜์Œ ์ดํ•ด ํ•  ๋•Œ ๋ณต์žกํ–ˆ์—ˆ๋Š”๋ฐ Javascript๋กœ BFS, DFS๋ฅผ ๊ตฌํ˜„ํ•ด๋ณด๋‹ˆ

velog.io

 

๐Ÿ‘พ ๋‚˜์˜ ๋‹ต์•ˆ

function solution(n, computers) {
    let network = 0;
    let visited = Array.from({ length: n }, () => false);

    function dfs(idx) {
        visited[idx] = true;

        for (let i = 0; i < computers[idx].length; i++) {
            if (computers[idx][i] == 1 && !visited[i]) {
                dfs(i);
            }
        }
    }

    for (let i = 0; i < computers.length; i++) {
        if (!visited[i]) {
            dfs(i);
            network++;
        }
    }
    return network;
}

 

  • ๋‚ด ๋จธ๋ฆฌ์—์„œ ๋‚˜์˜จ ๊ฑด ์•„๋‹ˆ๊ณ ... ๊ตฌ๊ธ€๋ง์˜ ๋„์›€์„ ๋งŽ์ด ๋นŒ๋ฆผ ์†”์งํžˆ ๋‚˜์˜ ๋‹ต์•ˆ์ด๋ผ๊ณ  ํ•˜๊ธฐ๋„ ๋ฏผ๋ง์‹œ๋Ÿฌ์›€
  • ๋ฐฉ๋ฌธ์„ ๋งˆ์นœ ์ปดํ“จํ„ฐ๋ฅผ ์ฒดํฌํ•˜๊ธฐ ์œ„ํ•ด ์ปดํ“จํ„ฐ ๊ฐœ์ˆ˜๋งŒํผ์˜ ๊ธธ์ด์˜ ๋ฐฐ์—ด์„ ์„ ์–ธ ํ›„ ๋ฐฐ์—ด์˜ ๊ฐ’๋“ค์„ ๋ชจ๋‘ false๋กœ ๋‘”๋‹ค.
  • dfs ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์‹œ์ž‘ ์ปดํ“จํ„ฐ๋กœ๋ถ€ํ„ฐ ๋ฐฉ๋ฌธ ๊ฐ€๋Šฅํ•œ ์ปดํ“จํ„ฐ๋ฅผ ๋ชจ๋‘ ๋ฐฉ๋ฌธํ•˜๋ฉฐ ํ•ด๋‹น ์ปดํ“จํ„ฐ์˜ visited๋ฅผ true๋กœ ๋ฐ”๊พผ๋‹ค.
  • ์ƒˆ๋กœ์šด ๋„คํŠธ์›Œํฌ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ๋งˆ๋‹ค ์ •๋‹ต์„ 1 ์ฆ๊ฐ€์‹œํ‚จ๋‹ค.

 

์˜คํ˜ธ๋ผ ๊ทธ๋ ‡๋‹จ ๋ง์ด์ง€

๋ธ”๋กœ๊ทธ ๋ช‡ ๊ฐœ ์ฐพ์•„๋ณด๋‹ˆ๊นŒ ์ด ๋ฌธ์ œ๊ฐ€ DFS ์ •์„์ด๋ผ๊ณ  ํ•˜๋˜๋ฐ

์†”์งํžˆ ์ด๊ฑด ๋„ˆ๋ฌด ์–ด๋ ค์› ๊ณ  ๊ทธ๋ž˜๋„ ๋น„๊ต์  ๋งŒ๋งŒํ•ด ๋ณด์ด๋Š” ๋ฌธ์ œ๋ถ€ํ„ฐ ์ฐจ๊ทผ์ฐจ๊ทผ ํ’€์–ด๋ด์•ผ๊ฒ ๋‹ค