-
Notifications
You must be signed in to change notification settings - Fork 0
3 - Bishoe01 #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
3 - Bishoe01 #16
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
| } | ||
| 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
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ๋ก๊ทธ ์ฐ์ด๋ณด์๋๊ฒ ์ ๋ง ๋๋จํ๊ฑฐ๊ฐ์ต๋๋ค! finn !!
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ๊ฒ๋ค๊ฐ ์ง์ ํ๋ฅผ ๊ตฌํ ํ์ จ๊ตฐ์. ์์ฒญ ํ๋์ จ์ํ ๋ฐ..tmp.0 tmp.1๋ก ์ ๊ทผํ์ จ๋ค์ !!!!
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ์์ BFS/DFSํ ๋ ํจ๋์ ์๋์ด pointer๋ก ํ๊ฐ ๋๋ ๊ฒ์ฒ๋ผํ์๊ธธ๋ ๊ณ์ ์ ์จ๋จน๊ณ ์์ต๋๋ค ใ ใ ..
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ใ ใ ์ด ๊ณ์ผ ~ finn ์ฝ๋ ์ปจํธ๋ฆฌ๋ทฐํฐ ์ฃผ์๋์ฉ ? ใ ใ
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @YooGyeongMo zใ ใ ใ ใ ใ ใ ์๋ ๋ค๋ฅธ๋ถ๋ค ์ฝ๋๋ณด๋ค๊ฐ ํ์ํฐ์ก๋ค์ ใ ใ ใ ์
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ๊ฐ์ ํ์ด๋ก ๋ก๊ทธ ์ฐ์ผ๋ฉด ํท๊ฐ๋ฆฌ์์ง ์์ผ์ จ๋์? ใ ใ
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ์ ์ ์ฝ๋๋ ๊ทธ๋ฅ ์ผ๋ฐ ๋ฐฐ์ด ์ฌ๋ฌ๊ฐ ์์ฑํ๊ณ ์นด์ดํธํ๋๊ฒ์ด์์ด์ ์ฌ์ค ์ฒ์์ ๋ณด์๋ง์ BFS๋ก ํด์ผ๊ฒ ๋ค๋ผ๊ณ ๊ณ ๋ฏผ์์ด ๊ณ ํ๋ ค๋ค๊ฐ ์ ๋ ์คํ๋ ค ์์ด ๋ฌธ์ ๊ฐ ๋ ์กฐ๊ฑด์ด ํ์คํ๊ฑฐ๊ฐ์์ ์ข๋๋ผ๊ตฌ์ !!! ์ฌ๊ธฐ ๋ณด์๋๊ฒ์ฒ๋ผ ๋ฑ 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
}
}๋ก ํ์์ต๋๋ค !!!
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ์์ด ๋ฌธ์ ๊ฐ ๋ ์กฐ๊ฑด์ด ํ์คํ๊ฒ ๋๊ปด์ง์๋๊ตฐ์.. ์ ๋ ๋ฐ~๋ก ํ์ด์ง ๋ฒ์ญ๊ธฐ ๋๋ ธ์๋๋ค.........
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @giljihun ๊ธธใ ใ ์ฌ์ค ์ ๋ ์์ด ๊ฒ๋ ๋ชปํฉ๋๋คใ ใ ใ ๋ญ๋๊น or? and ? ๋ ผ๋ฆฌ์ฐ์ฐ์๊ฐ ์ข ๋ ์ง๊ด์ ์ธ๊ฑฐ๊ฐ๋๋ผ๊ตฌ์ !!
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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]]) | ||



There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isRange()๋ฅผ ๋ณด๊ณ ์ ๊ฐ๋ ์ฑ ์ธก๋ฉด์์ ์กฐ๊ธ ๋ฆฌํฉํ ๋งํ๋ฉด ์ข์ง์์๊น? ์๊ฐ์ด ๋ค์์ด์!
ํ์ฌ isRange()๋ ๋ฒ์ ์ฒดํฌ, ์๋ฒ ์ ํจ์ฑ?( == 1 + ๋ฐฉ๋ฌธ ์ฌ๋ถ)๋ฅผ ํจ๊ป ์ฒ๋ฆฌํ๊ณ ์๋๊ฑธ๋ก ๋ณด์ฌ์.
์๋ฏธ๋ ๋ช ํํ๋ฐ, ์ด๊ฒ ๋ญ์ณ์์ด์ ๊ฐ๋ ์ฑ์ด ์กฐ๊ธ ๋จ์ด์ง ์ ์๋ค? ์๊ฐ์ด ๋ค์ด์!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ฝ๋๋ ๋ค๋ฅธ ๋ถ๋ค๊ณผ ์ ์ฌํ์ฌ ์๋ตํฉ๋๋ค!
๋ญ๊ฐ ํ๋ก๊ทธ๋๋จธ์ค ์์ํธํ ๊ฐ์์ ใ ใ
There was a problem hiding this comment.
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์จ์ฃผ์ ์ผ๋ฌด์ง ํจ์ ํ ์ ๋ฐฐ์๊ฐ๋๋ค..!