-
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
Conversation
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๋ฅผ ์ฌ๋ ค์ฃผ์ ์ ์ด์ฐธ์ ์ ๋๋ก ํ๋ณด์ํด์ ๋ ธ์ ์ ์ ๋ฆฌํ๋ฉด์ ํ์ด๋ดค์ต๋๋ค ใ
์์ฃผ ๋จ์ํ๊ฒ 2x2 matrix๋ถํฐ ์ฐพ์๋ดค์ด์.
[1, 1]
[1, 1]์ต๋ ํฌ๊ธฐ์ ์ ์ฌ๊ฐํ์ 2x2์ ๋๋ค. n*m์ด๋ผ ํ์ ๋, min(n,m)์ด ๋๊ฒ ๋ค์.
๊ทธ๋ ๋ด์ค.. dp ๋ฐฐ์ด์ ๋ง๋ค๊ณ ํ๋ ํ๋ ๋ง๋ค์ด๊ฐ๋ด ์๋ค. ์ ๋ ๊ธฐ์ค์ ์ค๋ฅธ์ชฝ ์๋๋ก ์ก์๋ณด๊ฒ ์ต๋๋ค.
ํ์ฌ ์์น๊ฐ 1์ด๋ฉด ๋น์ฐํ ์ ์ฌ๊ฐํ 1๊ฐ์ ๋๋ค. ๊ทธ๋ ๋ค๋ฉด 2x2์ธ ๊ฑด? [1, 1]๊ธฐ์ค์ผ๋ก [i-1, j-1], [i-1, j], [i, j-1]์ด 1์ผ ๊ฒฝ์ฐ์ ์ฑ๋ฆฝํฉ๋๋ค. ์ฌ๊ธฐ์ ํ๋์ ์์ ๋ฐ๊ฒฌํ์ต๋๋ค.
๋ง์ฝ ํ์ฌ ์์น์ ๋งคํธ๋ฆญ์ค [i][j]๊ฐ 1์ด๋ผ๋ฉด? โ dp[i][j] = min([i-1, j-1], [i-1, j], [i, j-1]) + 1 ์ด ์ฑ๋ฆฝํ๊ฒ ๋ค.
(์ข์๋จ, ์ข, ์๋จ์ ์ต์๊ฐ +1, ์ ๋ถ 1์ด๋ฉด [i][j]๊ฐ 2๊ฐ ๋๋ค. ๊ณ ๋ก, 2x2 ๊ฐ์๊น์ง ๊ณ์ฐ์ด ๊ฐ๋ฅํ๋ค.)
๊ทธ๋์ 3x3๋ ํด๋ณด๋ ๋๋๋ผ๊ณ ์.
๊ทธ๋์ ํ ์ผ๋ ์ ์ฉ๋๋ ์ถ์ด ์ ์ฉํด๋ณด์์ต๋๋ค. ๊ทผ๋ฐ ๋๋ค์?? ์ป์ด ๊ฑธ๋ฆฐ ๋๋์ด๋๊น..
ํ์ง๋ง ์ ํ์์ ์ฐพ์๋ค๊ณ ๋๋๊ฒ ์๋์ฃ . ์ผ์ด์ค๋ฅผ ์ฐพ์๋ดค์ต๋๋ค. (0,0) ๊ฐ์ ์ ๋ค์ 1๊ฐ ์ด์ ์ ์ฌ๊ฐํ์ ๊ฐ์ง ์ ์์ผ๋๊น์. ๊ทธ๋์ ๋ ๊ทธ๋ ธ์ต๋๋ค.

๋ฐ๋ก ์์์ต๋๋ค. 0ํ or 0์ด์ ๋นผ์ค์ผ๊ฒ ๊ตฐ. ๊ทธ๋์ ์ฝ๋๋ฅผ ์์ฑํ๊ณ ๋ฐ๋ก submit ๋ด์ต๋๋ค.
๋ง๋ก๋ ์ด๋ ๊ฒ ํ์ง๋ง 1์๊ฐ์ ๊ฑธ๋ฆฐ ๊ฒ ๊ฐ๋ค์. DP ์ฌ๋ฐ๋๋ฐ์??
import Foundation
class Solution {
func countSquares(_ matrix: [[Int]]) -> Int {
let (row, col) = (matrix.count, matrix[0].count)
var dp = Array(repeating: Array(repeating: 0, count: col), count: row)
for i in 0..<row {
for j in 0..<col {
if matrix[i][j] == 0 {
dp[i][j] = 0
}
else if i == 0 || j == 0 {
dp[i][j] = 1
}
else {
dp[i][j] = min(dp[i-1][j-1], dp[i][j-1], dp[i-1][j]) + 1
}
}
}
var res = 0
for i in 0..<row {
for j in 0..<col {
res += dp[i][j]
}
}
return res
}
}| let WIDTH = matrix[0].count | ||
| let HEIGHT = matrix.count |
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.
๋ณ์๋ฅผ ๋๋ฌธ์๋ก ๋ ์ด์ ๋ ์์์ฌ์ ์ผ๊น์?! ์ ๋ ํ๋ ฌ์ผ๋ก ์๊ฐํ๊ณ ํ์๋ค์
let (row, col) = (matrix.count, matrix[0].count)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๋ง์คํฐ๊ฐ ๋์ด๋ฒ๋ฆฌ์ ๊ฒ ๊ฐ์์
์ฐ ์ ๋ต์ฝ๋ ์ ๋ดค์ต๋๋ค ๐
alstjr7437
left a comment
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์ธ์ค ๋ชจ๋ฅด๊ณ ํ๋ค๊ฐ ์๊ฐ์ ๋ง์ด ์ฐ๊ฒ ๋์ ๊ฒฐ๊ตญ์ ๊ทธ๋ฅ ํ์ ์ฝ๋๋ฅผ ์ฐธ๊ณ ํ๋ค์,,
๋ฐ๋ก Finn์ ์ฝ๋๋ฅผ ๋ณด๊ณ ๋ฐ๋ผ ์น ์ ๋๋ผ์ ์ ์ถํ์ง ์์๊ณ ๋ฌธ์ ๊ฐ ์ข์์ ๋์ค์ ๋ค์ ํ์ด๋ด์ผํ ๊ฒ ๊ฐ์์
| 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 | ||
| } | ||
| } | ||
| } |
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.
์ด์ค ๋ฐ๋ณต๋ฌธ ์ค์ break ํ๋ฉด ๊ธฐ๋ณธ์ ์ผ๋ก ์ ์ผ ์๋จ์ for๋ฌธ์ด ์ข ๋ฃ๋๋๋ฐ ํด๋น ๋ถ๋ถ์ผ๋ก ํ๋ฉด ์ด์ค ๋ฐ๋ณต๋ฌธ ์ ์ฒด๊ฐ ์ค๋จ ๋ ๊น์!?
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.
์ฌ๊ธฐ์ ๋ง์ํ์ ๊ฑด ์ ์ผ ์๋จ์ด๋ผ๋๊ฑด ์ ์ผ ๋ฐ๊นฅ๋ง์ํ์ ๊ฑธ๊น์? ์๋๋ฉด ๊ฐ์ฅ ๊ฐ๊น์ด ๋ฐ๋ณต๋ฌธ ๋ง์ํ์ ๊ฑธ๊น์ ?
์ผ๋จ ์ ๊ฐ ์ดํดํ ๋ฐ๋ break๋ ๋ณธ์ธ์ด ์ํ ๊ฐ์ฅ ๊ฐ๊น์ด ๋ฐ๋ณต๋ฌธ ์ค๋จํ๋๊ฑฐ๋ก ์์์, ์ด์ ๋ฐ๊นฅ๋ถ๋ถ์ ๋ผ๋ฒจ๋ก ์กฐ์ ํด์ฃผ๋๊ฑฐ๋ก ํ์ฉํ๊ณ ์์ต๋๋ค.
์์๋ก ์๋ ์ฝ๋ ์์ฑํด๋ดค๋๋ฐ, ์ถ๋ ฅ๊ฒฐ๊ณผ ์ฐจ์ด๋ฅผ ๋ณด์๋ฉด ์ข ์ฐจ์ด๊ฐ ๋๋ ทํ๊ฒ ๋ณด์ด์ค์ ์์ ๊ฒ ๊ฐ์์!!
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
๋ฃจํ ๋ ~
giljihun
left a comment
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.
์ด๋ฒ ๋ฌธ์ ๋ ๋ฌธ์ ๋ฅผ ๋ณด๊ณ
๊ธ๋ก๋์ #1 ๋ฌธ์ ๋ ๊ฐ๋?! ๋ผ๊ณ . ์๊ฐ์ด ๋ค์๋ค์.
๋ฆฌ๋๋ฏธ๋ฅผ ๋๋ฌด ์์จ์ฃผ์
์ ๋ฌธ์ ํ์ด๋ณด๋ค ์ฝ์ผ๋ฉด์ ๋ฐฐ์ฐ๋๊ฒ ํจ์ฌ ๋ง์์ต๋๋ค ใ
ใ
๋ฌด ์ฝ๋๋ ๋๋ฐ์ด๋ค์ ์์ ๊น๋ํ dp ํ์ด์ธ๋ฐ์?
์ถ๊ฐ์ ์ผ๋ก๋, ์ฝ๋์์ ์ฃผ์์ฒ๋ฆฌ๋ฅผ ํด๋น ๋ผ์ธ ์์ ํ์ค ๋๋ ์๊ณ , ์ฐ์ธก์ ํ์ค ๋๋ ์๋๋ผ๊ตฌ์ !
์ด ๋ถ๋ถ์ ํต์ผํ๋๊ฒ ๋ณด๊ธฐ ๋ ์ข์ง ์์๊น ์๊ฐ์ด ๋ค์์ด์~!! ์ด๋ฒ ์ฌ์ดํด๋ ๊ณ ์ํ์
จ์ต๋๋ค!
| // ์ฐ์ธก,&์๋๋ก๊ฐ์๋ก ๊ฐ๋ฅํ ๋ณ์ ๊ธธ์ด๊ฐ ์งง์์ง | ||
| for row in 0 ..< WIDTH { // ํ | ||
| point = (x: row, y: col) | ||
| Pass: for length in stride(from: maxLen, through: 0, by: -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.
length๋ฅผ ํฐ ๊ฐ์์ ๊ฐ์์ํค๋ ๋ฐฉ์ ์์ฃผ ์ข๋ค๊ณ ์๊ฐ์ด ๋ค์์ด์
์ฝ๊ฐ ๊ฑฐ๋ฅด๋(?) ๋ฐฉ์์ผ๋ก ํ์ ์ต์ ํ๋ฅผ ํ๋ ์ ๊ทผ ์ข๋ค์!
๊ทธ๋ฆฌ๊ณ , Pass ๋ ์ด๋ธ์ ์ฐ๋ ๋ฐฉ์์ด ์ ์ด๊ฑฐ ์ฌ์ฉํ๋๊ฑด outerLoop << ์ด๋ ๊ฒ ์ด ๊ฑฐ๋ฐ์ ๋ชป๋ดค๋๋ฐ
ํ์ ์ข
๋ฃ ์ ๋ต?์ผ๋ก ์ฐ์ ๊ฒ ๊ฐ์๋ฐ ์ข
๋ฃ ์กฐ๊ฑด ์ด๋ ๊ฒ ๊ฑฐ๋๊ฑด ๋๊ฒ ํจ์จ์ ์ธ ๊ฒ ๊ฐ์ต๋๋ค.
๊ทธ๋ฐ๋ฐ ๋ ์ด๋ธ์ ์จ์ ์ข
๋ฃ์ํค๋๊ฒ ์ผ๋ฐ์ ์ธ ํจ์ ํ๊ณ ํ๊ณ ๊ฐ๋ ๋ก์ง์ ๋ณด๋ ๊ฒ ๋ณด๋ค ๋ญ๊ฐ
์ฝ๋ ์ฝ๊ธฐ๊ฐ ๋ ํ๋ค๋ค๊ณ ๋๊ผ์ต๋๋ค!!
(์ ๋ง ๊ทธ๋ด ์๋ ์์๋๋ค.)
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.
+์์ ์ฝ๋ฉํธ ๋ฌ์์ฃผ์ ๊ฑฐ ๊ฐ์ฌํฉ๋๋ค. ํญ์ ๊ทธ๋ฅ ๋์ ๋ณด์ด๋๋๋ก ์ฃผ์์ ๋ฌ๋ค๋ณด๋๊น ์ผ๊ด์ฑ์ ๋ํด์๋ ์๊ฐ์ ์ํด๋ณธ ๊ฒ ๊ฐ๋ค์. ์ง์ผ๋ณด๋๋กํ๊ฒ ์ต๋๋ค ๐
์ ๋ ์ฐ๋ฉด์ ์ ๊ฐ ์ฝ๋์ ํ๋ฆ์ ํ์
ํ๊ณ ์๋ค๋ณด๋๊น ๊ตฌ๋ถํ๊ธฐ์๋ ์ฌ์์ ์ข์์ค ์์๋๋ฐ, ๋ง๋ฅ ๊ทธ๋ฐ๊ฒ์ ์๋๊ฒ๊ฐ๋ค์ ์ด๋ฒ์ ์ข ๋๊ผ์ต๋๋ค.
์ ๋ ์ฌ์ธ์ง๋ชฐ๋ผ๋ ์ฝ์ผ์๋ ๋ถ๋ค์๊ฒ๋ ํ์คํ ์ข ๋ฒ์กํ ์๋ ์๊ฒ ๋ค๊ณ ์๊ฐ์ ํ๊ฒ๋๋ค์ ..!
| 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 | ||
| } | ||
| } | ||
| } | ||
| if isSquare { | ||
| // print("point : \(point), length : \(length)") | ||
| answer += length | ||
| break Pass | ||
| } |
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.
์ด ์ฝ๋๊ฐ length๋ฅผ ์ค์ฌ๋๊ฐ๋ ๋ฐฉ์๋๋ถ์ ์๊ฐ์ด๊ณผ๊ฐ ์๋๋๊ตฐ๋จ
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๋ก ํ์ด์ผ์ง..!ํ๋ค๊ฐ ์๊ฐ์ ์ค์ฌ์ฃผ๋๋ฐ์๋ง ์ง์คํ๋ค๋ณด๋ ๊ทธ๋ฅ ํ์์๊ณ ๋ฆฌ์ฆ์ด ๋์ด๋ฒ๋ ธ๋ค์ ..! ํต๊ณผํ๊ฒ ์ด์ด์์์ง๋..
๋ฌด ์ฝ๋๊ฐ ์ฐ ์ ๋ต์ธ๊ฒ๊ฐ์์
YooGyeongMo
left a comment
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.
ํ! ๋ฑ์ฐจ์์ด๊ณผ ๋ธ๋ฃจํธํฌ์ค? ๋ก ํธ์ ๊ฑฐ์ฃ ! ๋๋จํด์ !
| 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) { // ๋ณ ๊ธธ์ด๋ก ํด์ฃผ๊ธฐ | ||
| 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์์๋๋๊น ) | ||
|
|
||
| // ์ ์ฌ๊ฐํ ๋๊ณ ์ด๊ฑฐ ๋ค 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 | ||
| } | ||
| } | ||
| } | ||
| if isSquare { | ||
| // print("point : \(point), length : \(length)") | ||
| answer += length | ||
| break Pass | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return answer | ||
| } | ||
| } |
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.
ํ์ ๋ธ๋ฃจํธํฌ์ค๋ฅผ ์ด์ฉํด์ ํธ์ จ๊ตฐ์ !! ๋ฑ์ฐจ์์ด๊น์ง ....์ข์ ์ ๊ทผ์ด๋ผ ์ ๋ง ๋ฐฐ์ฐ๊ณ ๊ฐ๋ค์..
์ ๋ถํฐ ๋๋ผ๋๊ฑฐ์ง๋ง ํ์ 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๋ก ์ฐ์ด๊ฒํ์ต๋๋ค.
ํ๋ฆ๋
(i-1,j-1) = (1,1) โ prev
(i-1,j) = (1,2) โ dp[j]
(i,j-1) = (2,1) โ dp[j-1]
(i,j) = (2,2) โ ํ์ฌ ๊ณ์ฐ ์ค
๊ทผ๋ฐ ์์ชฝ์ธ (1,2)์ ํด๋นํ๋ dp[j]๋ ์ด๋ฒ ๊ณ์ฐ์์ ๊ณง ๋ฎ์ด์์์ง ์์ ์ด๊ธฐ ๋๋ฌธ์ ๋ฎ์ด์ฐ๊ธฐ ์ ์ temp์ ๋ฏธ๋ฆฌ ์ ์ฅํด๋๊ณ
โ ๋ค์ ์นธ์ผ๋ก ๋์ด๊ฐ๋ฉด temp๋ฅผ prev๋ก ๋๊ฒจ์ ์ข์๋จ์ผ๋ก ํ์ฉํ๋ ๊ฒ์ด์ฃ . ๋ญ๊ฐ ์ฐ์ํธ์ถ๊ณผ ์ฌ๊ท๋ฅผ ์ค์ด๋ฉด์ dp๋ฅผ ์ฌ์ฉํ๋ค๊ธฐ๋ณด๋ค๋ ์บ์ฑ์ ํ์ฉํ dp๋ก ์ต์ ํ๋ฅผ ํ๋ค๋๊ฒ ๋ง์๊บผ๊ฐ์์ !
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.
์ ๋ฑ์ฐจ์์ด ์ธ์ค ์์๋๋ฐ ๊ฒฐ๊ตญ ์์ผ์ด์ ใ
ใ
ใ
.. ์์ฌ์ด๋๋ก ๊ทธ๋ฅ ๊ณต์๋ง ๊ณต์ ํ์ต๋๋ค..
๋ฌด์ ์๋ DP๋ ๋น์ทํ ๋ถ๋ถ์ด ์ข ์๋๊ฑฐ๊ฐ์๋ฐ,
์ฐ์ธกํ๋จ ๊ธฐ์ค์ผ๋ก ์ด์ ์ผ์ชฝ์์ชฝ์ผ๋ก ์ฌ๋ผ๊ฐ๋ฉด์ ์ฒ๋ฆฌํด์ฃผ๋ฉด์
temp๋ก ์ข์๋จ ๊ฐ์ ์ฒ๋ฆฌํ๋ ๊ฑฐ๋, dp๊ฐ 1์ฐจ์๋ฐฐ์ด์ด๋ค์ฌ.. ๋๋ฐ
๊ทธ๋์ ์ฝ๋๊ฐ ๊น๋ํ๊ฒ ๋๊ปด์ก๊ตฐ์..! ๋ง์ด ๋ฐฐ์๊ฐ๋๋ค.

๐ ๋ฌธ์ ๋งํฌ
1277. Count Square Submatrices with All Ones
โ๏ธ ์์๋ ์๊ฐ
1์๊ฐ
โจ ์๋ ์ฝ๋
๋ฉ์ธ ์์ด๋์ด -> ํฐ๋ฐ์ค๋ ์์๋ฐ์ค๋ฅผ ํฌํจํ๊ณ ์๋๊ตฌ์กฐ๋๊น ํฐ ๋ฐ์คํ๋ ์กํ๋ฉด ์๋๋ฐ์ค๋ ํ๋จ์ํ ๊ฒ !
(์๋ฅผ๋ค์ด ๊ธธ์ด9์ง๋ฆฌ๋ฐ์ค๊ฐ ์๋ค๋๊ฒํ๋จ๋๋ฉด ๊ธธ์ด 9~1๊น์ง ๋ฐ์ค๊ฐ ์์ ์ ์์ผ๋ +9ํ๋ค์์ ๋ฐ๋ณต๋ฌธ ์๋๋ฆฌ๊ณ ๊ทธ๋ฅ ๋ค์ ์ขํ๋ก ์ด๋)
( x์ขํ + ํ์ฌ ์ถ์ ์ค์ธ ๋ณ์ ๊ธธ์ด) > ํ์ ๊ธธ์ด ์ต๋๊ฐ , (y์ขํ + ํ์ฌ ์ถ์ ์ค์ธ ๋ณ์ ๊ธธ์ด) > ์ด์ ๊ธธ์ด ์ต๋๊ฐ์ผ๋๋ ๊ทธ๋ฅ ๋ฐ๋ก ํจ์คํ๊ณ ๊ธธ์ด ์ค์ฌ์ฃผ๋๊ฒ๋๋ค !์ํ์ฐฉ์ค
๋ฑ์ฐจ์์ด์ ํฉ์ ์์๋ ?
์์ ์ค๋ช ๋๋ก
๊ธธ์ด๊ฐ 9๋ฉด์ 9~1์ด๋ผ ๊ทธ๋ฅ 9์ถ๊ฐํ๋ฉด ๋๊ฒ ๋ค๊ณ ์ ์๊ฐํ๋๋ฐ ๋ถ๋ช ํ ..๊ทผ๋ฐ ํธ๋ ๊ณผ์ ์์ ์ ์ ํ๋ ค์ "๊ฐ์๊ธฐ 9๋ถํฐ 1๊น์ง๋ฅผ ๋ํด์ค์ผํ๋ค"๊ณ ์๊ฐํด์ ํจ์๋ง๋ค์ด์คฌ๋๋ฐ,
์ ๊ฑธ๋ก answer๊ณ์ฐํด์ฃผ๋๊น, ๊ณ์ ๊ฐ์ด ์ด์ง ๋ ๋ง์ด ๋์์ ๊ดํ ๋ฒ์ ๊ฑด๋๋ฆฌ๊ณ ์กฐ์ ํ๋๋ผ ์๊ฐ์ด ๋ง์ด ์๋ชจ๋์ต๋๋ค, ๐คฏ
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ
์๋กญ๊ฒ ์๊ฒ๋๊ฑด์๋๊ณ , ๋ฑ์ฐจ์์ด์ ํฉ ์์ผ์ ๋ถ์ด๋ผ๋ฉด ํน์๋ผ๋ ๋์ค์ ์ธ์ผ์ด ์์ ์ ์์ผ๋ ์ธ์ ๊ฐ ๋์์ด ๋๊ธธ..

๋ฑ์ฐจ์์ด์ ํฉ๊ณต์
ํ๊ธฐ