Skip to content

Conversation

@bishoe01
Copy link
Collaborator

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

1267. Count Servers that Communicate

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2025-04-30 แ„‹แ…ฉแ„Œแ…ฅแ†ซ 8 29 25

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

50๋ถ„

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

BFSํ’€์ด

  • ํ†ต์‹ ๋˜๋Š” ์ปดํ“จํ„ฐ๊ฐ€ ๋ช‡๊ฐœ์ธ์ง€ ์ฐพ์•„๋ผ! ๋ฌธ์ œ๋ผ์„œ BFS๋ฅผ ์ ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์ œ์—์„œ ํ‹ฐ๋ฅผ ๋งŽ์ด ๋‚ด๊ณ  ์žˆ๊ธฐ๋„ ํ–ˆ๊ตฌ์š”.
  • connectCnt ํ•˜๋‚˜๋งŒ ์žˆ๋Š” ์ปดํ“จํ„ฐ๋Š” ์ž‘์šฉ์„ ๋ชปํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ•˜๋‚˜๋Š” ์นด์šดํŒ…ํ•ด์ฃผ๊ธดํ•˜๋Š”๋ฐ BFS๋Œ๋ฉด์„œ ์ด๊ฒŒ 1๋ณด๋‹ค ์ปค์กŒ์„๋•Œ(์ฆ‰, ํ†ต์‹ ๋˜๋Š” ๋‹ค๋ฅธ ์ปดํ“จํ„ฐ ์นœ๊ตฌ๋ฅผ ๋งŒ๋‚ฌ์„๋•Œ ์ด์ œ ๊ทธ ๊ฐ’์ด ์‹ค์ œ ์ •๋‹ต์นด์šดํŠธ answer์— ์ถ”๊ฐ€๊ฐ€ ๋˜๋Š” ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค.
  • ์ฒ˜์Œ์—๋Š” ๊ทธ๋ƒฅ ๋ถ™์–ด์žˆ๋Š”๊ฑฐ๋งŒ ๋˜๋Š”์ง€์•Œ์•˜๋Š”๋ฐ, ๋‹ค ํ’€๊ณ ๋‚˜์„œ ๋ณด๋‹ˆ๊นŒ ๊ฐ™์€ ํ–‰/์—ด ์ด๋ฉด ์—ฐ๊ฒฐ์ด ๋˜๋Š”๊ฑฐ๋ผ๋”๊ตฌ์š”. ๊ทธ๋ž˜์„œ ๊ทธ๋ถ€๋ถ„์„ ๋“ค์–ด๋‚ด๊ณ  ํ–‰/์—ด ๊ฐ๊ฐ์— ๋Œ€ํ•œ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ ค์คฌ์Šต๋‹ˆ๋‹ค.

์ธ์ ‘ํ•œ ์ปดํ“จํ„ฐ๊ฐ€ ์•„๋‹ˆ๋‹ค ?!

๊ฐ™์€ ํ–‰๊ณผ ์—ด์„ ๊ตฌํ•˜๋ผ ํ•˜๋”๋ผ๊ตฌ์š”.. ๋ฌธ์ œ๋ฅผ ์ œ๋Œ€๋กœ ์ฝ์–ด์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค.

Two servers are said to communicate if they are on the same row or on the same column.

 while pointer < queue.count {
                    let target = queue[pointer]
                    let currentX = target.0, currentY = target.1
                    print(target.0, target.1)
                    for direction in 0 ..< 4 {
                        let nextX = currentX + dx[direction], nextY = currentY + dy[direction]
                        if isRange(x: nextX, y: nextY) {
                            queue.append((nextX, nextY))
                            visitedGraph[nextX][nextY] = true
                            connectCnt += 1
                        }
                    }

                    pointer += 1
                }

์ด๋ ‡๊ฒŒ ๊ทธ๋ƒฅ ์ผ๋ฐ˜์ ์ธ DFS ์ธ์ ‘ ์ฐพ๊ธฐ์—์„œ ,, ์•„๋ž˜์ฒ˜๋Ÿผ ๋ณ€๊ฒฝํ•ด์คฌ์Šต๋‹ˆ๋‹ค.

while pointer < queue.count {
                    let tmp = queue[pointer]
                    let currentX = tmp.0, currentY = tmp.1

//                     ๊ตด๋Ÿฌ๊ฐˆ ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธ   / 2์ฐจ ํ’€์ด : ์‚ฌ์‹ค์ƒ ์ด๋ถ€๋ถ„๋งŒ ๊ณ ์ณ์ฃผ๋ฉด ๋  ๊ฒƒ๊ฐ™์€๋ฐ, ํ–‰ - ์—ด์ด๋ผ

//                    ํ–‰ ๊ณ„์‚ฐ
                    for row in 0 ..< grid.count {
                        if isRange(x: row, y: currentY) {
                            queue.append((row, currentY))
                            visitedGraph[row][currentY] = true
                            connectCnt += 1
                        }
                    }
//                    ์—ด ๊ณ„์‚ฐ
                    for col in 0 ..< grid[0].count {
                        if isRange(x: currentX, y: col) {
                            queue.append((currentX, col))
                            visitedGraph[currentX][col] = true
                            connectCnt += 1
                        }
                    }

                    pointer += 1
                }

Index out of range

return x >= 0 && y >= 0 && x < grid.count && y < grid[0].count && grid[x][y] == 1 && visitedGraph[x][y] == false

๋ฅผ ๋น„๋กฏํ•ด์„œ
for row in 0 ..< grid[0].count , for col in 0 ..< grid.count ์˜€๋‹ค๊ฐ€ ๊ณ„์† ํ‹€๋ ค์„œ ๋ณด๋‹ˆ๊นŒ ๋ฐ˜๋Œ€์˜€๋„ค์š”..
for row in 0 ..< grid.count , for col in 0 ..< grid[0].count
๊ธ‰ํ•˜๊ฒŒ ํ’€์–ด์„œ ๋งจ๋‚  ํ‹€๋ฆฌ๊ณ  ๋ฐ”๊พธ๊ณ  ํ•˜๋‹ค๋ณด๋‹ˆ ์ € ๋‚˜๋ฆ„๋Œ€๋กœ์˜ ๊ธฐ์ค€์ด ๋œ์žกํ˜”๋‚˜๋ด…๋‹ˆ๋‹ค.. ์ดํ›„์—” ํ‹€๋ฆฌ๊ธฐ์ „์— ๋ฐ”๊พธ๋„๋ก ๋…ธ๋ ฅํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ท..

๊ฐœ์„ 

์•„๋งˆ ํ–‰ ๊ณผ ์—ด์— ์žˆ๋Š” ์ปดํ“จํ„ฐ ์ฐพ๋Š” ๋กœ์ง์ด ์ €๋ ‡๊ฒŒ ๋Œ€์ถฉ ์ฒ˜๋ฆฌํ•ด์ฃผ๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ ๋” ์ข‹์€ ๋ฐฉ์‹์ด ์žˆ์„ ๊ฒƒ ๊ฐ™์€๋ฐ, ํ•œ ๋ฒˆ ๋ฆฌ๋ทฐํ•ด์ฃผ์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ€ํƒ€์š”..! ์„ธ์…˜๊ฐ€๊ธฐ์ „์— ํ‘ผ๊ฑฐ๋ผ ์ €๋„ ์ฐพ์•„๋ณด๊ณ  ๊ณต์œ ๋“œ๋ฆฌ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค..!

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2025-04-30 แ„‹แ…ฉแ„Œแ…ฅแ†ซ 8 09 50

Copy link
Collaborator

@MuchanKim MuchanKim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ํ•€์ด ๋ฌธ์ œ๋ฅผ ์ œ๋Œ€๋กœ ์ฝ์œผ๋ผ๋Š” ํžŒํŠธ๋ฅผ ์คฌ๊ธฐ๋•Œ๋ฌธ์— ์–ด๋ ต์ง€์•Š๊ฒŒ ํ’€ ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.
(๋ฌธ์ œ ๊ทธ๋ฆผ๋งŒ ๋ณด๋ฉด ๋‹น์—ฐํžˆ ์ธ์ ‘ํ•œ ์ปดํ“จํ„ฐ๋งŒ ์„œ๋ฒ„ ์—ฐ๊ฒฐ ๋œ ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ž„;;)

์ž…๋ ฅ๋œ grid์˜ ํ–‰, ๋ ฌ์— ์œ„์น˜ํ•œ ์„œ๋ฒ„ ๊ฐœ์ˆ˜๋งŒ ์„ธ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
์™œ๋ƒํ•˜๋ฉด '์ธ์ ‘'ํ•œ ์„œ๋ฒ„๊ฐ€ ์•„๋‹ˆ๊ธฐ์— ๊ฐ™์€ ์œ„์น˜์— 2๊ฐœ ์ด์ƒ ์žˆ์œผ๋ฉด ๊ฑ”๋„ค๋Š” ์—ฐ๊ฒฐ๋œ๊ฑฐ๋‹ˆ๊นŒ์š”.

(ํ•„๊ธฐํ•˜๋ฉด์„œ ํ–ˆ๋Š”๋ฐ ๋„ˆ๋ฌด ์ง€์ €๋ถ„ํ•ด์„œ ์‹œ๊ฐ„ ๋“ค์—ฌ ์—ฌ๊ธฐ์— ์จ๋ด…๋‹ˆ๋‹ค.)

์ผ๋‹จ ์•„๋ž˜ grid๋ฅผ ์˜ˆ์‹œ๋กœ ๋ณผ๊ฒŒ์š”.

1 1 0 0
0 0 1 0
0 0 1 0
0 0 0 1

ํ–‰์„ ๊ธฐ์ค€์œผ๋กœ ๋ด๋ณผ๊ฒŒ์—ฌ

[1]--[1]  [ ]  [ ]   (ํ–‰์— ์„œ๋ฒ„ 2๊ฐœ: ํ†ต์‹  ๊ฐ€๋Šฅ)
[ ]  [ ]  [1]  [ ]   (ํ–‰์— ์„œ๋ฒ„ 1๊ฐœ: X)
[ ]  [ ]  [1]  [ ]   (ํ–‰์— ์„œ๋ฒ„ 1๊ฐœ: X)
[ ]  [ ]  [ ]  [1]   (ํ–‰์— ์„œ๋ฒ„ 1๊ฐœ: X)

๊ฐ ์„œ๋ฒ„๊ฐ€ ์œ„์น˜ํ•œ ํ–‰์— ๋‹ค๋ฅธ ์„œ๋ฒ„๊ฐ€ 1๊ฐœ๋งŒ ์žˆ์œผ๋ฉด ๊ทธ ์„œ๋ฒ„๋Š” ์—ฐ๊ฒฐ๋œ ์„œ๋ฒ„๊ฒ ์ฃ ??

์—ด์„ ๊ธฐ์ค€์œผ๋กœ๋„ ํ•œ๋ฒˆ ๋ด๋ณด์ž…์‹œ๋”.

[1]  [1]  [ ]  [ ]
[ ]  [ ]  [1]  [ ]
           |
[ ]  [ ]  [1]  [ ]
[ ]  [ ]  [ ]  [1]

3(์ปดํ„ฐ๊ธฐ์ค€2)์—ด๋งŒ ์„œ๋ฒ„ 2๊ฐœ๊ณ  ์–˜๋„ค๋งŒ ์—ฐ๊ฒฐ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

์ด๋กœ์จ ์•Œ ์ˆ˜ ์žˆ๋Š” ์‚ฌ์‹ค

๊ฐ ์„œ๋ฒ„๊ฐ€ ์œ„์น˜ํ•œ ํ–‰์— ๋‹ค๋ฅธ ์„œ๋ฒ„๊ฐ€ ์ž์‹ ํฌํ•จ 2๊ฐœ๋งŒ ์žˆ์œผ๋ฉด ๊ทธ ์„œ๋ฒ„๋Š” ์—ฐ๊ฒฐ๋œ ์„œ๋ฒ„๋‹ค.
๊ฐ ์„œ๋ฒ„๊ฐ€ ์œ„์น˜ํ•œ ์—ด์— ๋‹ค๋ฅธ ์„œ๋ฒ„๊ฐ€ ์ž์‹ ํฌํ•จ 2๊ฐœ๋งŒ ์žˆ์œผ๋ฉด ๊ทธ ์„œ๋ฒ„๋Š” ์—ฐ๊ฒฐ๋œ ์„œ๋ฒ„๋‹ค.
= ๊ฐ ์„œ๋ฒ„์— ํ–‰or์—ด์— ์„œ๋ฒ„๊ฐ€ ์ž์‹ ํฌํ•จ 2๊ฐœ ์ด์ƒ์ด๋ฉด ๊ทธ ์„œ๋ฒ„๋Š” ์—ฐ๊ฒฐ๋œ ์„œ๋ฒ„๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์šฐ๋ฆฐ ์—ฐ๊ฒฐ๋œ ์„œ๋ฒ„๋ฅผ ์„ธ์–ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

๋ฌด์˜์ฝ”๋“œ

์ „ ํ–‰ ์„œ๋ฒ„ ๊ฐœ์ˆ˜, ์—ด ์„œ๋ฒ„ ๊ฐœ์ˆ˜๋ฅผ ๋”ฐ๋กœ ์„ ์–ธํ•ด์„œ ํ–ˆ๋Š”๋ฐ ๋” ๋‚˜์€ ๋ฐฉ๋ฒ•์ด ์žˆ์„์ง€ ๊ถ๊ธˆํ•˜๋„ค์š”

class Solution {
    func countServers(_ grid: [[Int]]) -> Int {
        let rows = grid.count
        let cols = grid[0].count
        
        var rowServer = Array(repeating: 0, count: rows)
        var colServer = Array(repeating: 0, count: cols)
        

        // ๊ฐ ํ–‰, ์—ด ์„œ๋ฒ„ ์ˆ˜ ์ฑ„์›Œ๋„ฃ๊ธฐ
        for i in 0..<rows {
            for j in 0..<cols {
                if grid[i][j] == 1 {
                    rowServer[i] += 1
                    colServer[j] += 1
                }
            }
        }
        
        // ์ˆœํšŒํ•˜๋ฉด์„œ ์„œ๋ฒ„๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ, ํ–‰ or ์—ด์˜ ์นด์šดํŠธ๊ฐ€ 2 ์ด์ƒ์ธ์ง€ ์ฒดํฌํ•˜๊ณ  ๋งž์œผ๋ฉด +1
        var res = 0
        for i in 0..<rows {
            for j in 0..<cols {
                if grid[i][j] == 1 && (rowServer[i] > 1 || colServer[j] > 1) {
                    res += 1
                }
            }
        }
        return res
    }
}

@sep037 sep037 added the bishoe01 label May 4, 2025
@sep037
Copy link
Collaborator

sep037 commented May 7, 2025

์ €๋„ ๊ฐ™์€ ํ–‰๊ณผ ์—ด์— 1๋Œ€ ์ด์ƒ ์žˆ์–ด์•ผ ํ†ต์‹ ์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๊ฑธ ์ธ์ง€ํ–ˆ๋Š”๋ฐ
๋ชจ๋ฅด๊ฒ ์–ด์„œ ์ด๊ฑฐ ์ฐธ๊ณ ํ–ˆ์–ด์š”

์ €๊ธฐ ๋งํฌ ์•ˆ์—์„œ๋Š” C++ ์ฝ”๋“œ๋กœ ์ž‘์„ฑ๋˜์–ด ์žˆ๋Š”๋ฐ ์Šค์œ„ํ”„ํŠธ๋กœ ๋ฐ”๊ฟ”๋ณด์•˜์Šต๋‹ˆ๋‹ค
๊ทผ๋ฐ ๋ฌด๋ž‘ ์™„์ „ ๋˜‘๊ฐ™์•„์š”

class Solution {
    func countServers(_ grid: [[Int]]) -> Int {
    let n = grid.count
    let m = grid[0].count
    
    var answer = 0
    var rows = [Int](repeating: 0, count: n)
    var cols = [Int](repeating: 0, count: m)
    
    // ๊ฐ ํ–‰๊ณผ ์—ด์˜ ์„œ๋ฒ„ ๊ฐœ์ˆ˜๋ฅผ ์„ผ๋‹ค
    for i in 0..<n {
        for j in 0..<m {
            if grid[i][j] == 1 {
                rows[i] += 1
                cols[j] += 1
            }
        }
    }
    
    // ํ†ต์‹  ๊ฐ€๋Šฅํ•œ ์„œ๋ฒ„๋งŒ ์นด์šดํŠธํ•œ๋‹ค
    for i in 0..<n {
        for j in 0..<m {
            if grid[i][j] == 1 && (rows[i] > 1 || cols[j] > 1) {
                answer += 1
            }
        }
    }
    
    return answer
}
}

Copy link
Collaborator

@YooGyeongMo YooGyeongMo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์บฌ ~ finn ์Œค์˜ BFS ์ฝ”๋“œ๋ฅผ ๋ณด๊ณ  ํํ•œ๋ฒˆ ๊ตฌํ˜„ํ•ด๋ณด๊ณ ์‹ถ๋‹ค๋Š” ์š•๊ตฌ๊ฐ€ ๋ง‰ ์†Ÿ์•„๋‚˜๋„ค์š” !!! ๋ฌด๋ฆŽ์„ ํƒ ์น˜๊ณ ๊ฐ‘๋‹ˆ๋‹ค !

Comment on lines +37 to +38
// ์—ด ๊ณ„์‹ผ
for col in 0 ..< grid[0].count {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ใ…‹ใ…‹ ์—ด ๊ณ„์‹ผ ~ finn ์ฝ”๋“œ ์ปจํŠธ๋ฆฌ๋ทฐํ„ฐ ์ฃผ์‹œ๋‚˜์šฉ ? ใ…Žใ…Ž

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@YooGyeongMo zใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹์•„๋‹ˆ ๋‹ค๋ฅธ๋ถ„๋“ค ์ฝ”๋“œ๋ณด๋‹ค๊ฐ€ ํ˜„์›ƒํ„ฐ์กŒ๋„ค์š” ใ…‹ใ…‹ใ…‹์•„

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๊ต‰์žฅํžˆ ๋ถ€๋„๋Ÿฝ๋„ค์š”

Comment on lines +20 to +36
while pointer < queue.count {
let tmp = queue[pointer]
let currentX = tmp.0, currentY = tmp.1

// ๊ตด๋Ÿฌ๊ฐˆ ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธ / 2์ฐจ ํ’€์ด : ์‚ฌ์‹ค์ƒ ์ด๋ถ€๋ถ„๋งŒ ๊ณ ์ณ์ฃผ๋ฉด ๋  ๊ฒƒ๊ฐ™์€๋ฐ, ํ–‰ - ์—ด์ด๋ผ

// ํ–‰ ๊ณ„์‚ฐ
for row in 0 ..< grid.count {
// print("next:", nextX, nextY)
if isRange(x: row, y: currentY) {
// print("Hi")
queue.append((row, currentY))
visitedGraph[row][currentY] = true
connectCnt += 1
// print("ROW", row, currentY)
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋กœ๊ทธ ์ฐ์–ด๋ณด์‹œ๋Š”๊ฒŒ ์ •๋ง ๋Œ€๋‹จํ•œ๊ฑฐ๊ฐ™์Šต๋‹ˆ๋‹ค! finn !!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๊ฒŒ๋‹ค๊ฐ€ ์ง์ ‘ ํ๋ฅผ ๊ตฌํ˜„ ํ•˜์…จ๊ตฐ์š”. ์—„์ฒญ ํž˜๋“œ์…จ์„ํ…๋ฐ..tmp.0 tmp.1๋กœ ์ ‘๊ทผํ•˜์…จ๋„ค์š” !!!!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์˜ˆ์ „ BFS/DFSํ• ๋•Œ ํ‚จ๋”์„ ์ƒ๋‹˜์ด pointer๋กœ ํ๊ฐ€ ๋˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผํ•˜์‹œ๊ธธ๋ž˜ ๊ณ„์† ์ž˜ ์จ๋จน๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค ใ…Žใ…Ž..

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ํ—‰ ์™ ์ง€ ๋ญ”๊ฐ€ ์ต์ˆ™ํ•˜๋‹ค ์‹ถ์—ˆ๋Š”๋ฐ ๊ทธ๋•Œ ์˜๊ฐ์„ ๋ฐ›์•„์„œ ์ž˜์“ฐ๊ณ  ๊ณ„์‹œ๋Š”๊ตฐ์š”!

Comment on lines +40 to +50
if isRange(x: currentX, y: col) {
// print("Hi")
queue.append((currentX, col))
visitedGraph[currentX][col] = true
connectCnt += 1
// print("COL", currentX, col)
}
}

pointer += 1
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๊ฐ™์€ ํ•˜์ด๋กœ ๋กœ๊ทธ ์ฐ์œผ๋ฉด ํ—ท๊ฐˆ๋ฆฌ์‹œ์ง„ ์•Š์œผ์…จ๋‚˜์š”? ใ…‹ใ…‹

Copy link
Collaborator

@YooGyeongMo YooGyeongMo May 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ €์˜ ์ฝ”๋“œ๋Š” ๊ทธ๋ƒฅ ์ผ๋ฐ˜ ๋ฐฐ์—ด ์—ฌ๋Ÿฌ๊ฐœ ์ƒ์„ฑํ•˜๊ณ  ์นด์šดํŠธํ•˜๋Š”๊ฒƒ์ด์—ˆ์–ด์š” ์‚ฌ์‹ค ์ฒ˜์Œ์— ๋ณด์ž๋งˆ์ž BFS๋กœ ํ•ด์•ผ๊ฒ ๋‹ค๋ผ๊ณ  ๊ณ ๋ฏผ์—†์ด ๊ณ  ํ•˜๋ ค๋‹ค๊ฐ€

์ €๋Š” ์˜คํžˆ๋ ค ์˜์–ด ๋ฌธ์ œ๊ฐ€ ๋” ์กฐ๊ฑด์ด ํ™•์‹คํ•œ๊ฑฐ๊ฐ™์•„์„œ ์ข‹๋”๋ผ๊ตฌ์š” !!!

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2025-05-07 แ„‹แ…ฉแ„’แ…ฎ 9 50 09

image

์—ฌ๊ธฐ ๋ณด์‹œ๋Š”๊ฒƒ์ฒ˜๋Ÿผ ๋”ฑ or๋กœ ์ฃผ๋‹ˆ ์ €๋Š” ๊ทธ๋ƒฅ ๋ฐฐ์—ด์— ์นด์šดํŠธํ•ด๋„ ๊ดœ์ถ˜ํ•˜์ง€์•Š์„๊นŒ ? 250250์ด์–ด์„œ ์กฐ๊ฑด๋ถ€๋กœ ์นด์šดํŒ…ํ•˜๋ฉด ์™„ํƒ์ด ์•„๋‹ˆ๋ผ O(MN) ์œผ๋กœํ•  ์ˆ˜ ์žˆ์ง€์•Š์„๊นŒ์ƒ๊ฐํ–ˆ๋˜๊ฑฐ๊ฐ™์•„์š” !

class Solution {
    func countServers(_ grid: [[Int]]) -> Int {
        let m = grid.count
        let n = grid[0].count
        
        var rowCount = Array(repeating: 0, count: m)
        var colCount = Array(repeating: 0, count: n)
        
        for i in 0..<m {
            for j in 0..<n{
                if grid[i][j] == 1 {
                    rowCount[i] += 1
                    colCount[j] += 1
                }
            }
        }
        var result = 0
        
        for i in 0..<m {
            for j in 0..<n {
                if grid[i][j] == 1 && (rowCount[i] > 1 || colCount[j] > 1) {
                    result += 1
                }
            }
        }
        return result
    }
}

๋กœ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค !!!

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2025-05-07 แ„‹แ…ฉแ„’แ…ฎ 10 14 48
์„ฑ๋Šฅ๋„ ์‹ค์ œ๋กœ ๋” ์ข‹์•˜์Šต๋‹ˆ๋‹ท !!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์˜์–ด ๋ฌธ์ œ๊ฐ€ ๋” ์กฐ๊ฑด์ด ํ™•์‹คํ•˜๊ฒŒ ๋А๊ปด์ง€์‹œ๋Š”๊ตฐ์š”..

์ €๋Š” ๋ฐ”~๋กœ ํŽ˜์ด์ง€ ๋ฒˆ์—ญ๊ธฐ ๋Œ๋ ธ์๋‹ˆ๋‹ค.........
.....

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@giljihun ๊ธธใ…‹ใ…‹ ์‚ฌ์‹ค ์ €๋„ ์˜์–ด ๊ฒ๋‚˜ ๋ชปํ•ฉ๋‹ˆ๋‹คใ…‹ใ…‹ใ…‹ ๋ญ๋ž„๊นŒ or? and ? ๋…ผ๋ฆฌ์—ฐ์‚ฐ์ž๊ฐ€ ์ข€ ๋” ์ง๊ด€์ ์ธ๊ฑฐ๊ฐ™๋”๋ผ๊ตฌ์š” !!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ด์•ผ ๋ฐ๋ฏธ์•ˆ์Œค์€ ํ•ญ์ƒ runTime์ƒ์œ„์— ๋žญํฌ๋˜์‹œ๋„ค์šฉ.. ๋ฆฌ์ŠคํŽ™

๊ฐ™์€ ํ•˜์ด๋กœ ๋กœ๊ทธ ์ฐ์œผ๋ฉด ํ—ท๊ฐˆ๋ฆฌ์‹œ์ง„ ์•Š์œผ์…จ๋‚˜์š”? ใ…‹ใ…‹

A : ๋‹ค ์ฃผ์„๋˜์–ด์žˆ์–ด์„œ ๊ทธ๋ž˜๋ณด์ด์‹ค ์ˆ˜ ์žˆ์ง€๋งŒ,, ํ•ญ์ƒ ํ•œ์ชฝ์€ ์ฃผ์„์ผ๊ฑฐ๋ผ ๊ดœ์ฐฎ์Šต๋‹ˆ๋‹ค..ใ…‹ใ…‹ ์‚ฌ์‹ค ๋” ๋ช…ํ™•ํ•˜๊ฒŒ ๋กœ๊ทธ์ฐ์–ด์ฃผ๋Š”๊ฒŒ ๋” ์ด์ƒ์ ์ด๊ธดํ•ฉ๋‹ˆ๋‹ค.. ๋‹ค์Œ๋ฒˆ์—” ๊ทธ๋ ‡๊ฒŒ ํ•ด๋ณผ๊ฒŒ์šฉ.

์˜์–ด ๋ฌธ์ œ๊ฐ€ ๋” ์กฐ๊ฑด์ด ํ™•์‹คํ•˜๊ฒŒ ๋А๊ปด์ง€์‹œ๋Š”๊ตฐ์š”..

A : ์ €๋„ ์˜ˆ์‹œ์ผ€์ด์Šค๋ณด๊ณ  ์ž˜ ๋ชจ๋ฅด๊ฒ ๋‹ค์‹ถ์œผ๋ฉด ๋ฐ”๋กœ ๋ฒˆ์—ญ๊ธฐ๋กœ..

Copy link
Member

@giljihun giljihun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • ๋ฆฌํŠธ ์ฝ”๋“œ๋Š” ํ•ญ์ƒ ๋‘๋ ต๋„ค์š”.

Comment on lines +4 to +6
func isRange(x: Int, y: Int) -> Bool {
return x >= 0 && y >= 0 && x < grid.count && y < grid[0].count && grid[x][y] == 1 && visitedGraph[x][y] == false
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isRange()๋ฅผ ๋ณด๊ณ ์„œ ๊ฐ€๋…์„ฑ ์ธก๋ฉด์—์„œ ์กฐ๊ธˆ ๋ฆฌํŒฉํ† ๋งํ•˜๋ฉด ์ข‹์ง€์•Š์„๊นŒ? ์ƒ๊ฐ์ด ๋“ค์—ˆ์–ด์š”!

ํ˜„์žฌ isRange()๋Š” ๋ฒ”์œ„ ์ฒดํฌ, ์„œ๋ฒ„ ์œ ํšจ์„ฑ?( == 1 + ๋ฐฉ๋ฌธ ์—ฌ๋ถ€)๋ฅผ ํ•จ๊ป˜ ์ฒ˜๋ฆฌํ•˜๊ณ  ์žˆ๋Š”๊ฑธ๋กœ ๋ณด์—ฌ์š”.
์˜๋ฏธ๋Š” ๋ช…ํ™•ํ•œ๋ฐ, ์ด๊ฒŒ ๋ญ‰์ณ์žˆ์–ด์„œ ๊ฐ€๋…์„ฑ์ด ์กฐ๊ธˆ ๋–จ์–ด์งˆ ์ˆ˜ ์žˆ๋‹ค? ์ƒ๊ฐ์ด ๋“ค์–ด์š”!

func isValidServer(x: Int, y: Int) -> Bool {
    guard (0..<grid.count).contains(x),
          (0..<grid[0].count).contains(y) else { return false }
    return grid[x][y] == 1 && !visitedGraph[x][y]
}
  • guard๋ฅผ ํ™œ์šฉํ•ด๋ณด์ฃ !! ๋ฒ”์œ„ ์ฒดํฌ๋ฅผ ๋จผ์ €ํ•ด์„œ main ์กฐ๊ฑด์— ๋” ์ง‘์ค‘ํ•ด๋ณด๋Š” ๊ฒ๋‹ˆ๋‹ค.
  • ํ•จ์ˆ˜๋ช…๋„ ์กฐ๊ธˆ ๋” ๋ฉ”์ธ ๊ธฐ๋Šฅ์— ์ง‘์ค‘ํ•ด์„œ ๋ฐ”๊ฟ”๋ดค์Šต๋‹ˆ๋‹ค.

๊ทธ ๋ณธ ์ฝ”๋“œ์— ์ ์šฉํ•ด๋„ ์กฐ๊ธˆ๋งŒ ๋ฐ”๊พธ๋ฉด ๋ฌธ์ œ ์—†์„ ๊ฒƒ ๊ฐ™์•„์š” ์•„๋งˆ๋„..?!!?!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ฝ”๋“œ๋Š” ๋‹ค๋ฅธ ๋ถ„๋“ค๊ณผ ์œ ์‚ฌํ•˜์—ฌ ์ƒ๋žตํ•ฉ๋‹ˆ๋‹ค!

  • ์ถ”๊ฐ€์ ์œผ๋กœ ๋ฆฌํŠธ์ฝ”๋“œ๊ฐ€ ๋งˆ๋ƒฅ ๋ถˆ์นœ์ ˆํ•˜์ง„ ์•Š๋„ค์š”
    ๋ญ”๊ฐ€ ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ƒ์œ„ํ˜ธํ™˜ ๊ฐ™์•„์š” ใ…‹ใ…‹

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2025-05-07 22 36 46

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์šฐ์™€ ๊ธธ์„ ์ƒ๋‹˜ ๋ฆฌ๋ทฐ๋Š” ํ•ญ์ƒ ํ€„๋ฆฌํ‹ฐ๊ฐ€ ์ฐธ ์ข‹๋‹ค๊ณ  ๋А๋‚๋‹ˆ๋‹ค..
์˜ˆ์ „์— ์ฒ˜์Œ DFS/BFS๋ฐฐ์šธ๋•Œ๋Š” isRange๊ฐ€ ๋งŽ์€ ๊ฒƒ์„ ์ˆ˜ํ–‰ํ•˜์ง€์•Š์•˜์—ˆ๋Š”๋ฐ,
๋ฌธ์ œ๋ฅผ ํ’€์ˆ˜๋ก ๋” ๊น”๋”ํ•˜๊ฒŒ ์••์ถ•ํ•˜๊ณ  ์‹ถ์€ ์š•์‹ฌ์— ์–ด๋А์ƒŒ๊ฐ€ ์ด๋ฆ„๋งŒ isRange์ง€ ๋‹ค ํ•ด์ฃผ๊ณ  ์žˆ์—ˆ๋„ค์š”..
guard์จ์ฃผ์‹  ์•ผ๋ฌด์ง„ ํ•จ์ˆ˜ ํ•œ ์ˆ˜ ๋ฐฐ์›Œ๊ฐ‘๋‹ˆ๋‹ค..!

  • ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ƒ์œ„ํ˜ธํ™˜๋งž๋Š” ๊ฒƒ ๊ฐ™์•„์š”..! ์ผ๋‹จ ์ž…๋ ฅ๋ฐ›์„๋•Œ์˜ ๋ถˆํŽธํ•จ์ด ์—†์–ด์„œ ์ฐธ ์ข‹์Šต๋‹ˆ๋‹น ( ๊ฒฐ์ œํ•˜๋ฉด ๋””๋ฒ„๊น…์„ ๋น„๋กฏํ•œ ์ถ”๊ฐ€์ ์ธ ๊ธฐ๋Šฅ๋„ ์žˆ๋Š” ๋“ฏํ•ด์š”)

Copy link
Member

@alstjr7437 alstjr7437 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ด๋ฒˆ์ฃผ์ฐจ ๋ฐ”๋น ์„œ ๋ฌธ์ œ๋ฅผ ์•„์ง ๋ชปํ’€์–ด๋ดฃ๋„ค์š” ใ… ใ… 
๋ฆฌ๋ทฐ๋Š” ๋‹ค๋ฅธ ๋ถ„๋“ค์ด ๋„ˆ๋ฌด ์ž˜๋‹ฌ์•„์ฃผ์…”์„œ ๋ช‡๊ฐœ ์ฝ”๋ฉ˜ํŠธ ์ถ”๊ฐ€๋ฐ–์— ์—†์—ˆ์–ด์š” ใ… 
๋‹ค์Œ๋ถ€ํ„ฐ ์ž˜ํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!

Comment on lines +20 to +36
while pointer < queue.count {
let tmp = queue[pointer]
let currentX = tmp.0, currentY = tmp.1

// ๊ตด๋Ÿฌ๊ฐˆ ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธ / 2์ฐจ ํ’€์ด : ์‚ฌ์‹ค์ƒ ์ด๋ถ€๋ถ„๋งŒ ๊ณ ์ณ์ฃผ๋ฉด ๋  ๊ฒƒ๊ฐ™์€๋ฐ, ํ–‰ - ์—ด์ด๋ผ

// ํ–‰ ๊ณ„์‚ฐ
for row in 0 ..< grid.count {
// print("next:", nextX, nextY)
if isRange(x: row, y: currentY) {
// print("Hi")
queue.append((row, currentY))
visitedGraph[row][currentY] = true
connectCnt += 1
// print("ROW", row, currentY)
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ํ—‰ ์™ ์ง€ ๋ญ”๊ฐ€ ์ต์ˆ™ํ•˜๋‹ค ์‹ถ์—ˆ๋Š”๋ฐ ๊ทธ๋•Œ ์˜๊ฐ์„ ๋ฐ›์•„์„œ ์ž˜์“ฐ๊ณ  ๊ณ„์‹œ๋Š”๊ตฐ์š”!

@bishoe01 bishoe01 merged commit de77f9c into main May 9, 2025
6 checks passed
@alstjr7437 alstjr7437 deleted the 3-Bishoe01 branch May 23, 2025 06:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants