-
Notifications
You must be signed in to change notification settings - Fork 0
2-bishoe01 #11
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
2-bishoe01 #11
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,45 @@ | ||
| class Solution { | ||
| func countSquares(_ matrix: [[Int]]) -> Int { | ||
| var answer = 0 | ||
| let maxLen = min(matrix.count, matrix[0].count) // ์ ์ฌ๊ฐํ ํ๋ณ ์ต๋๊ธธ์ด | ||
| let WIDTH = matrix[0].count | ||
| let HEIGHT = matrix.count | ||
|
|
||
| for col in 0 ..< HEIGHT { // ์ด | ||
| var point: (x: Int, y: Int) | ||
| // ํฐ ๋ฐ์ค๊ฐ ์์ ๋ฐ์ค๋ฅผ ํฌํจํ๋ ๊ด๊ณ | ||
| // ์ฐ์ธก,&์๋๋ก๊ฐ์๋ก ๊ฐ๋ฅํ ๋ณ์ ๊ธธ์ด๊ฐ ์งง์์ง | ||
| for row in 0 ..< WIDTH { // ํ | ||
| point = (x: row, y: col) | ||
| Pass: for length in stride(from: maxLen, through: 0, by: -1) { // ๋ณ ๊ธธ์ด๋ก ํด์ฃผ๊ธฐ | ||
|
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. length๋ฅผ ํฐ ๊ฐ์์ ๊ฐ์์ํค๋ ๋ฐฉ์ ์์ฃผ ์ข๋ค๊ณ ์๊ฐ์ด ๋ค์์ด์ ๊ทธ๋ฆฌ๊ณ , Pass ๋ ์ด๋ธ์ ์ฐ๋ ๋ฐฉ์์ด ์ ์ด๊ฑฐ ์ฌ์ฉํ๋๊ฑด outerLoop << ์ด๋ ๊ฒ ์ด ๊ฑฐ๋ฐ์ ๋ชป๋ดค๋๋ฐ ๊ทธ๋ฐ๋ฐ ๋ ์ด๋ธ์ ์จ์ ์ข
๋ฃ์ํค๋๊ฒ ์ผ๋ฐ์ ์ธ ํจ์ ํ๊ณ ํ๊ณ ๊ฐ๋ ๋ก์ง์ ๋ณด๋ ๊ฒ ๋ณด๋ค ๋ญ๊ฐ
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. +์์ ์ฝ๋ฉํธ ๋ฌ์์ฃผ์ ๊ฑฐ ๊ฐ์ฌํฉ๋๋ค. ํญ์ ๊ทธ๋ฅ ๋์ ๋ณด์ด๋๋๋ก ์ฃผ์์ ๋ฌ๋ค๋ณด๋๊น ์ผ๊ด์ฑ์ ๋ํด์๋ ์๊ฐ์ ์ํด๋ณธ ๊ฒ ๊ฐ๋ค์. ์ง์ผ๋ณด๋๋กํ๊ฒ ์ต๋๋ค ๐ ์ ๋ ์ฐ๋ฉด์ ์ ๊ฐ ์ฝ๋์ ํ๋ฆ์ ํ์
ํ๊ณ ์๋ค๋ณด๋๊น ๊ตฌ๋ถํ๊ธฐ์๋ ์ฌ์์ ์ข์์ค ์์๋๋ฐ, ๋ง๋ฅ ๊ทธ๋ฐ๊ฒ์ ์๋๊ฒ๊ฐ๋ค์ ์ด๋ฒ์ ์ข ๋๊ผ์ต๋๋ค. |
||
| if point.y + length > HEIGHT || point.x + length > WIDTH { | ||
| // print("point : \(point), length : \(length) , Width : \(WIDTH), Height : \(HEIGHT)") | ||
| continue | ||
| } // ์ด๊ฒ ํ์์ด ์ด๊ณผํด๋ฒ๋ฆฌ๋ฉด ์๋๋๊น (ex ๊ฐ๋ก๊ธธ์ด๊ฐ 4์ธ๋ฐ, 0~3๋ ๋์ง๋ง 3~6์์๋๋๊น ) | ||
MuchanKim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // ์ ์ฌ๊ฐํ ๋๊ณ ์ด๊ฑฐ ๋ค 1์ธ์ง ํ๋จํ๋ ํจ์ | ||
| var isSquare = true | ||
| SquareFunc: for i in point.y ..< point.y + length { | ||
| for j in point.x ..< point.x + length { | ||
| if matrix[i][j] == 0 { | ||
| isSquare = false | ||
| break SquareFunc | ||
| } | ||
| } | ||
| } | ||
|
Comment on lines
+22
to
+29
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. ์ด์ค ๋ฐ๋ณต๋ฌธ ์ค์ break ํ๋ฉด ๊ธฐ๋ณธ์ ์ผ๋ก ์ ์ผ ์๋จ์ for๋ฌธ์ด ์ข ๋ฃ๋๋๋ฐ ํด๋น ๋ถ๋ถ์ผ๋ก ํ๋ฉด ์ด์ค ๋ฐ๋ณต๋ฌธ ์ ์ฒด๊ฐ ์ค๋จ ๋ ๊น์!?
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. ์ฌ๊ธฐ์ ๋ง์ํ์ ๊ฑด ์ ์ผ ์๋จ์ด๋ผ๋๊ฑด ์ ์ผ ๋ฐ๊นฅ๋ง์ํ์ ๊ฑธ๊น์? ์๋๋ฉด ๊ฐ์ฅ ๊ฐ๊น์ด ๋ฐ๋ณต๋ฌธ ๋ง์ํ์ ๊ฑธ๊น์ ? ์์๋ก ์๋ ์ฝ๋ ์์ฑํด๋ดค๋๋ฐ, ์ถ๋ ฅ๊ฒฐ๊ณผ ์ฐจ์ด๋ฅผ ๋ณด์๋ฉด ์ข ์ฐจ์ด๊ฐ ๋๋ ทํ๊ฒ ๋ณด์ด์ค์ ์์ ๊ฒ ๊ฐ์์!! outer: for i in 0..<5 {
print("๋ฃจํ ์์!!")
for j in 0..<5 {
print("j = \(j), i + j = \(i + j)")
if i + j == 3 {
print("break outer")
break outer
}
}
print("๋ฃจํ ๋ \(i)")
}
print("๋ฃจํ ๋ ~")์ถ๋ ฅ๋ฃจํ ์์!!
j = 0, i + j = 0
j = 1, i + j = 1
j = 2, i + j = 2
j = 3, i + j = 3
break outer
๋ฃจํ ๋ ~๋ฐ๋ณต๋ฌธ ๋ผ๋ฒจ์ด ์๋ค๋ฉด??outer: for i in 0..<5 {
print("๋ฃจํ ์์!!")
for j in 0..<5 {
print("j = \(j), i + j = \(i + j)")
if i + j == 3 {
print("break outer")
break
}
}
print("๋ฃจํ ๋ \(i)")
}
print("๋ฃจํ ๋ ~")์ถ๋ ฅ๊ฐ๋ฃจํ ์์!!
j = 0, i + j = 0
j = 1, i + j = 1
j = 2, i + j = 2
j = 3, i + j = 3
break outer
๋ฃจํ ๋ 0
๋ฃจํ ์์!!
j = 0, i + j = 1
j = 1, i + j = 2
j = 2, i + j = 3
break outer
๋ฃจํ ๋ 1
๋ฃจํ ์์!!
j = 0, i + j = 2
j = 1, i + j = 3
break outer
๋ฃจํ ๋ 2
๋ฃจํ ์์!!
j = 0, i + j = 3
break outer
๋ฃจํ ๋ 3
๋ฃจํ ์์!!
j = 0, i + j = 4
j = 1, i + j = 5
j = 2, i + j = 6
j = 3, i + j = 7
j = 4, i + j = 8
๋ฃจํ ๋ 4
๋ฃจํ ๋ ~ |
||
| if isSquare { | ||
| // print("point : \(point), length : \(length)") | ||
| answer += length | ||
| break Pass | ||
| } | ||
|
Comment on lines
+21
to
+34
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. ์ด ์ฝ๋๊ฐ length๋ฅผ ์ค์ฌ๋๊ฐ๋ ๋ฐฉ์๋๋ถ์ ์๊ฐ์ด๊ณผ๊ฐ ์๋๋๊ตฐ๋จ
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. ๋ง์์ ํฐ๋ ์ฐพ์ผ๋ฉด ๋ฐ๋ก ์์ํด์ฃผ๊ฒ๋ ํด์ ํ์์๊ฐ ์ค์ฌ์ฃผ๋ ค๊ณ ํ์ต๋๋ค ! |
||
| } | ||
| } | ||
| } | ||
|
|
||
| return answer | ||
| } | ||
| } | ||
|
Comment on lines
+8
to
+41
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. ํ์ ๋ธ๋ฃจํธํฌ์ค๋ฅผ ์ด์ฉํด์ ํธ์ จ๊ตฐ์ !! ๋ฑ์ฐจ์์ด๊น์ง ....์ข์ ์ ๊ทผ์ด๋ผ ์ ๋ง ๋ฐฐ์ฐ๊ณ ๊ฐ๋ค์.. ์ ๋ถํฐ ๋๋ผ๋๊ฑฐ์ง๋ง ํ์ break Pass๋ฅผ ์์ฐ์๊ณ ์ค์ฒฉ for๋ฌธ์์ ์์ฐํฐ๋ ์ด๋๋ฅผ ๊ตฌ๋ถ์ ํ์คํ ์ํ์๋ ๋๋์ด ์์ผ์ ๊ฑฐ๊ฐ์์. ์ ๋ ์ ๊ฐ ํผ ๋ฌธ์ ์ ๊ทธ๋ฆฌ๊ณ ์ ๋ฒ์ฃผ ์ค๋นํ๋ฉด์ ํ์๋ dfs bfs ๋ฐฑํธ๋ํน ๊ตฌํdp ๋ฌธ์ ๋ฅผ ๋ง์ด ๋ง๋ฌ๋๋ฐ์. @sep037 ๊ธ๋ก๋์ ์ ๋ฒ์ฃผ ๋ฌธ์ ์ ์๊ฒ ์ข ๊น๋ค๋ก์ ๋ ๊ทธ ๋ฌธ์ ์๋๋ฐ ๊ทธ ๋ฌธ์ ์ ์ ๊ฐ ์ด๋ฒ์ฃผ์ ํผ๋ฌธ์ ๋ฅผ ์ ๋ชฉ์์ผ์ ๊ณ ๋ฏผ์ ๋ง์ดํด๋ณด์์ด์. ๊ทธ๋์ ์ฌ์ค ๋ฌธ์ ๋ฅผ ๋ง๋์๋ง์ ์ด ! ์ด๊ฑด DP๋ค ๋ผ๊ณ ์๊ฐ์ด ๋ค์๊ณ , ์ ๋ ์ฒ์์ BFS๋ก ํ์๋ ์ ๊ทผํ์๋ ์๊ฐ์ด๋๋ค์. ์ขํ๊ฐ i.j์ 0์ด ๋ค์ด๊ฐ๊ณ ์ฐ์ธกํ๋จ ๊ธฐ์ค์ผ๋ก ์ข์๋จ, ์ผ,์๋ง ์ฒดํฌํ๋ฉด ์ ์ฌ๊ฐํ์์ ์์์๋ค๊ณ ์๊ฐ์ ํ๊ณ , ์์์ ๋งํ๋ ์ขํ ์ข์๋จ, ์, ์ผ, ์ฐ์ธกํ๋จ ๋๋ ๊ฐ๋ค์ด ์ขํ์์๋ถํฐ ํ๋ณ์ ๊ธธ์ด๋ง ๋ฝ์ผ๋ฉด ๋๊ฒ ๋ค ! ๋ผ๋ ์๊ฐ์ด ๋ค์์ด์. ๊ทธ๋์ ์ ๋ ์ฐ์ธกํ๋จ์ ๊ธฐ์ค์ผ๋ก ์ก์์ด์. ๊ทธ ๋ค์์ DP๋ฅผ ํ์ฉํ ๋ ๋ ์ ๋ง ์ข์๊ฒ, ๋ฉ๋ชจ์ด์ ์ด์ ์ผ๋ก ์๊ฐ,๊ณต๊ฐ๋ณต์ก๋๋ฅผ ๋ค ์ก๋๋ผ๊ตฌ์. ์ฌ์ค ์ ํ์์ ์ธ์ฐ๋ ๋ฐฉ์์ ์์๋๋ผ๋ฉด ์๊ฐ๋ณต์ก๋๋ฅผ ๋ ์ก์ ์ ์๋ค๊ณ ์๊ฐํ์ง๋ง. ์ด์ฐจ์๋ฐฐ์ด๋ณด๋จ ์ผ์ฐจ์์ผ๋ก ๊ณ์ ๊ฐ์ ๋ณ๊ฒฝํด๊ฐ๋ฉฐ ๊ณต๊ฐ๋ณต์ก๋๋ฅผ ๋ฎ์ถฐ๋ณด์๋ก ์๊ฐํ์ต๋๋ค. -> ์ฌ์ค ์ด ์๊ฐ๋ ์กฐ๊ธ ๋นจ๋ฆฌ ๋์ฌ ์ ์์๋ ์ด์ ๋ ์ ๊ฐ ํ์๋ ๋ฌธ์ ๊ฐ ํ์ด ํ์๋ ๋ฌธ์ ์ ์ ๋ง ๋น์ทํด์ ๊ฐ๋ฅํ์๋ ์ผ์ธ๊ฑฐ๊ฐ์ต๋๋ค. ๊ทธ๋์ ์ ๋ ์๋ ๋ฌด์ ๋ฉ์ธ ์์ด๋์ด๋ ๊ฐ์ผ๋ ๊ตฌ์กฐ๋ ์กฐ๊ธ ๋ค๋ฅธ๊ฑฐ ๊ฐ์ ์ ์ฒด ์ฝ๋๋ฅผ ๋จ๊ฒจ๋ด ๋๋ค. class Solution {
func countSquares(_ matrix: [[Int]]) -> Int {
let rows = matrix.count
let cols = matrix[0].count
var dp = [Int](repeating: 0, count: cols)
var result = 0
var prev = 0 // ์ข์๋จ ๋๊ฐ์ ๊ฐ
for i in 0..<rows {
prev = 0
for j in 0..<cols {
let temp = dp[j] // ํ์ฌ dp[j]๋ ์ด์ ํ์ ๊ฐ
if matrix[i][j] == 1 {
if i == 0 || j == 0 {
dp[j] = 1
} else {
dp[j] = min(dp[j], dp[j - 1], prev) + 1
}
} else {
dp[j] = 0
}
result += dp[j]
prev = temp
}
}
return result
}
}์ ์ฝ๋๋ฅผ ์์ฝํ์๋ฉด, ํ์ฌ i,j ์์ ๋ง๋ค ์ ์๋ ์ ์ฌ๊ฐํ์ ์ต๋ ํ ๋ณ์ ๊ธธ์ด๋ฅผ ๊ตฌํ๋ คํ์ต๋๋ค. ๊ทธ๋์ ํ์ํ ๊ฐ์, ์ผ์ชฝ dp[j-1], ์์ชฝ dp[j], ์ข์๋จ๋๊ฐ์ [prev]์ ๋๋ค. ํ์ง๋ง ์ฌ๊ธฐ์ ๋ฌธ์ ๊ฐ 2์ฐจ์ ๋ฐฐ์ด์ ์ฌ์ฉํ๋ค๋ฉด ์ข์๋จ ๋๊ฐ์ ์ ๊ธฐ์ตํ๋๋ฐ ์์ด์ ๋ ์ข์๊ฒ ์ง๋ง,1์ฐจ์ ๋ฐฐ์ด์์๋ ๋ง๋ ํ์ ์ฅํ ๋ฐฉ์์ด ์์ต๋๋ค. ๊ทธ๋์ ์ ๋ prev์ ์ข์๋จ ๋๊ฐ์ ๊ฐ์ temp์์ ๋ฐ์์ค๊ณ temp์ ํ์ฌ dp[j]์ ์ด์ ๊ฐ์ ์ ์ฅํ์ฌ ๋ค์ ์นธ์์ prev๋ก ์ฐ์ด๊ฒํ์ต๋๋ค. ํ๋ฆ๋ ๊ทผ๋ฐ ์์ชฝ์ธ (1,2)์ ํด๋นํ๋ dp[j]๋ ์ด๋ฒ ๊ณ์ฐ์์ ๊ณง ๋ฎ์ด์์์ง ์์ ์ด๊ธฐ ๋๋ฌธ์ ๋ฎ์ด์ฐ๊ธฐ ์ ์ temp์ ๋ฏธ๋ฆฌ ์ ์ฅํด๋๊ณ
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
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. ์ ๋ฑ์ฐจ์์ด ์ธ์ค ์์๋๋ฐ ๊ฒฐ๊ตญ ์์ผ์ด์ ใ
ใ
ใ
.. ์์ฌ์ด๋๋ก ๊ทธ๋ฅ ๊ณต์๋ง ๊ณต์ ํ์ต๋๋ค.. |
||
|
|
||
| func ๋ฑ์ฐจ์์ดํฉ(_ n: Int) -> Int { | ||
| return (n * (2 + (n - 1))) / 2 | ||
| } | ||

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.
๋ง์์ ์ ๋ ์ด๋ฐ๊ฑฐ
.countํญ์ ๊ฐ์ ธ์ค๋๊ฑฐ ๊ฐ๋ ์ฑ ๋จ์ด์ง๋๊ฑฐ๊ฐ์์ 2์ฐจ์๋ฐฐ์ด์ ์ด๋ ๊ฒ ์ฐ๊ณ 1์ฐจ์๋ฐฐ์ด์ด๋ฉดlet LEN์ผ๋กํด์ ์ฐ๋ ๋ฒ๋ฆ์ด์์ต๋๋ค ..!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.
๋ฌด ์ฝ๋ ์ธ์ ๋๋ ๋ดค์ง๋ง ์ด์ DP๋ง์คํฐ๊ฐ ๋์ด๋ฒ๋ฆฌ์ ๊ฒ ๊ฐ์์
์ฐ ์ ๋ต์ฝ๋ ์ ๋ดค์ต๋๋ค ๐