Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions bishoe01/BFS_DFS/Count Servers that Communicate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
class Solution {
func countServers(_ grid: [[Int]]) -> Int {
var visitedGraph = Array(repeating: Array(repeating: false, count: grid[0].count), count: grid.count)
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
}
Comment on lines +4 to +6
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์จ์ฃผ์‹  ์•ผ๋ฌด์ง„ ํ•จ์ˆ˜ ํ•œ ์ˆ˜ ๋ฐฐ์›Œ๊ฐ‘๋‹ˆ๋‹ค..!

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

var answer = 0
for i in 0 ..< grid.count {
for j in 0 ..< grid[0].count {
var queue = [(Int, Int)]()
var pointer = 0
var connectCnt = 0

if isRange(x: i, y: j) {
queue.append((i, j))
visitedGraph[i][j] = true // ์ถ”๊ฐ€
connectCnt += 1
}

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)
}
}
Comment on lines +20 to +36
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.

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

// ์—ด ๊ณ„์‹ผ
for col in 0 ..< grid[0].count {
Comment on lines +37 to +38
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.

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

// print("next:", nextX, nextY)
if isRange(x: currentX, y: col) {
// print("Hi")
queue.append((currentX, col))
visitedGraph[currentX][col] = true
connectCnt += 1
// print("COL", currentX, col)
}
}

pointer += 1
}
Comment on lines +40 to +50
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 : ์ €๋„ ์˜ˆ์‹œ์ผ€์ด์Šค๋ณด๊ณ  ์ž˜ ๋ชจ๋ฅด๊ฒ ๋‹ค์‹ถ์œผ๋ฉด ๋ฐ”๋กœ ๋ฒˆ์—ญ๊ธฐ๋กœ..

if connectCnt > 1 {
answer += connectCnt
connectCnt = 1
}
}
}

// print(answer)
return answer
}
}

//
// Solution().countServers([[1, 0], [1, 1]])
// Solution().countServers([[1, 1, 0, 0], [0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 0, 1]])
// Solution().countServers([[1, 0], [1, 1]])
Solution().countServers([[1, 0, 0, 1, 0], [0, 0, 0, 0, 0], [0, 0, 0, 1, 0]])
3 changes: 2 additions & 1 deletion bishoe01/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
| ์ฐจ์‹œ | ๋‚ ์งœ | ๋ฌธ์ œ์œ ํ˜• | ๋งํฌ | ํ’€์ด |
|:----:|:---------:|:----:|:-----:|:----:|
| 1์ฐจ์‹œ | 2025.03.27 | ๊ตฌํ˜„ | [[3์ฐจ] ํŒŒ์ผ๋ช… ์ •๋ ฌ](https://school.programmers.co.kr/learn/courses/30/lessons/17686)|https://github.com/AlgoLeadMe/AlgoLeadMe-14/pull/1|
| 1์ฐจ์‹œ | 2025.04.09 | DP | [1277. Count Square Submatrices with All Ones](https://leetcode.com/problems/count-square-submatrices-with-all-ones/description/?envType=problem-list-v2&envId=dynamic-programming)|https://github.com/AlgoLeadMe/AlgoLeadMe-14/pull/11|
| 2์ฐจ์‹œ | 2025.04.09 | DP | [1277. Count Square Submatrices with All Ones](https://leetcode.com/problems/count-square-submatrices-with-all-ones/description/?envType=problem-list-v2&envId=dynamic-programming)|https://github.com/AlgoLeadMe/AlgoLeadMe-14/pull/11|
| 3์ฐจ์‹œ | 2025.04.30 | DP | [1267. Count Servers that Communicate](https://leetcode.com/problems/count-servers-that-communicate/description/?envType=daily-question&envId=2025-04-29)|https://github.com/AlgoLeadMe/AlgoLeadMe-14/pull/16|
---