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
45 changes: 45 additions & 0 deletions bishoe01/DP/Count Square Submatrices with All Ones.swift
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
Comment on lines +5 to +6
Copy link
Collaborator

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)

Copy link
Collaborator Author

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์œผ๋กœํ•ด์„œ ์“ฐ๋Š” ๋ฒ„๋ฆ‡์ด์žˆ์Šต๋‹ˆ๋‹ค ..!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

๋ฌด ์ฝ”๋“œ ์„ธ์…˜๋•Œ๋„ ๋ดค์ง€๋งŒ ์ด์ œ DP๋งˆ์Šคํ„ฐ๊ฐ€ ๋˜์–ด๋ฒ„๋ฆฌ์‹  ๊ฒƒ ๊ฐ™์•„์š”
์ฐ ์ •๋‹ต์ฝ”๋“œ ์ž˜ ๋ดค์Šต๋‹ˆ๋‹ค ๐Ÿ‘


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) { // ๋ณ€ ๊ธธ์ด๋กœ ํ•ด์ฃผ๊ธฐ
Copy link
Member

Choose a reason for hiding this comment

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

length๋ฅผ ํฐ ๊ฐ’์—์„œ ๊ฐ์†Œ์‹œํ‚ค๋Š” ๋ฐฉ์‹ ์•„์ฃผ ์ข‹๋‹ค๊ณ  ์ƒ๊ฐ์ด ๋“ค์—ˆ์–ด์š”
์•ฝ๊ฐ„ ๊ฑฐ๋ฅด๋Š”(?) ๋ฐฉ์‹์œผ๋กœ ํƒ์ƒ‰ ์ตœ์ ํ™”๋ฅผ ํ•˜๋Š” ์ ‘๊ทผ ์ข‹๋„ค์š”!

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

๊ทธ๋Ÿฐ๋ฐ ๋ ˆ์ด๋ธ”์„ ์จ์„œ ์ข…๋ฃŒ์‹œํ‚ค๋Š”๊ฒŒ ์ผ๋ฐ˜์ ์ธ ํ•จ์ˆ˜ ํƒ€๊ณ ํƒ€๊ณ  ๊ฐ€๋Š” ๋กœ์ง์„ ๋ณด๋Š” ๊ฒƒ ๋ณด๋‹ค ๋ญ”๊ฐ€
์ฝ”๋“œ ์ฝ๊ธฐ๊ฐ€ ๋” ํž˜๋“ค๋‹ค๊ณ  ๋А๊ผˆ์Šต๋‹ˆ๋‹ค!!
(์ €๋งŒ ๊ทธ๋Ÿด ์ˆ˜๋„ ์žˆ์”๋‹ˆ๋‹ค.)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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์€์•ˆ๋˜๋‹ˆ๊นŒ )

// ์ •์‚ฌ๊ฐํ˜• ๋Œ๊ณ  ์ด๊ฑฐ ๋‹ค 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
Copy link
Member

Choose a reason for hiding this comment

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

์ด์ค‘ ๋ฐ˜๋ณต๋ฌธ ์ค‘์— break ํ•˜๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ์ผ ์ƒ๋‹จ์˜ for๋ฌธ์ด ์ข…๋ฃŒ๋˜๋Š”๋ฐ ํ•ด๋‹น ๋ถ€๋ถ„์œผ๋กœ ํ•˜๋ฉด ์ด์ค‘ ๋ฐ˜๋ณต๋ฌธ ์ „์ฒด๊ฐ€ ์ค‘๋‹จ ๋ ๊นŒ์š”!?

Copy link
Collaborator Author

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
๋ฃจํ”„ ๋ ~

if isSquare {
// print("point : \(point), length : \(length)")
answer += length
break Pass
}
Comment on lines +21 to +34
Copy link
Member

Choose a reason for hiding this comment

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

์ด ์ฝ”๋“œ๊ฐ€ length๋ฅผ ์ค„์—ฌ๋‚˜๊ฐ€๋Š” ๋ฐฉ์‹๋•๋ถ„์— ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ์•ˆ๋‚˜๋Š”๊ตฐ๋‡จ

Copy link
Collaborator Author

@bishoe01 bishoe01 Apr 15, 2025

Choose a reason for hiding this comment

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

๋งž์•„์š” ํฐ๋†ˆ ์ฐพ์œผ๋ฉด ๋ฐ”๋กœ ์•„์›ƒํ•ด์ฃผ๊ฒŒ๋” ํ•ด์„œ ํƒ์ƒ‰์‹œ๊ฐ„ ์ค„์—ฌ์ฃผ๋ ค๊ณ  ํ–ˆ์Šต๋‹ˆ๋‹ค !
์ €๋Š” ํ’€๋ฉด์„œ DP๋กœ ํ’€์–ด์•ผ์ง€..!ํ•˜๋‹ค๊ฐ€ ์‹œ๊ฐ„์„ ์ค„์—ฌ์ฃผ๋Š”๋ฐ์—๋งŒ ์ง‘์ค‘ํ•˜๋‹ค๋ณด๋‹ˆ ๊ทธ๋ƒฅ ํƒ์ƒ‰์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๋˜์–ด๋ฒ„๋ ธ๋„ค์š” ..! ํ†ต๊ณผํ•œ๊ฒŒ ์šด์ด์—ˆ์„์ง€๋„..
๋ฌด ์ฝ”๋“œ๊ฐ€ ์ฐ ์ •๋‹ต์ธ๊ฒƒ๊ฐ™์•„์š”

}
}
}

return answer
}
}
Comment on lines +8 to +41
Copy link
Collaborator

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๋กœ ์ตœ์ ํ™”๋ฅผ ํ–ˆ๋‹ค๋Š”๊ฒŒ ๋งž์„๊บผ๊ฐ™์•„์š” !

Copy link
Collaborator

Choose a reason for hiding this comment

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

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2025-04-21 แ„‹แ…ฉแ„Œแ…ฅแ†ซ 12 20 21 ๋ญ”๊ฐ€ ํ•€๋•๋•์— ๋ฆฌํŠธ์ฝ”๋“œ ํ’€์–ด๋ณธ๊ฑฐ๊ฐ™์•„์„œ ์ €๋„ ๋‚จ๊ฒจ๋ด…๋‹ˆ๋‹ค ใ…‹ใ…‹

Copy link
Collaborator Author

@bishoe01 bishoe01 Apr 21, 2025

Choose a reason for hiding this comment

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

์ € ๋“ฑ์ฐจ์ˆ˜์—ด ์“ธ์ค„ ์•Œ์•˜๋Š”๋ฐ ๊ฒฐ๊ตญ ์•ˆ์ผ์–ด์š” ใ…Žใ…‹ใ…‹.. ์•„์‰ฌ์šด๋Œ€๋กœ ๊ทธ๋ƒฅ ๊ณต์‹๋งŒ ๊ณต์œ ํ–ˆ์Šต๋‹ˆ๋‹ค..
๋ฌด์„ ์ƒ๋‹˜ DP๋ž‘ ๋น„์Šทํ•œ ๋ถ€๋ถ„์ด ์ข€ ์žˆ๋Š”๊ฑฐ๊ฐ™์€๋ฐ,
์šฐ์ธกํ•˜๋‹จ ๊ธฐ์ค€์œผ๋กœ ์ด์ œ ์™ผ์ชฝ์œ„์ชฝ์œผ๋กœ ์˜ฌ๋ผ๊ฐ€๋ฉด์„œ ์ฒ˜๋ฆฌํ•ด์ฃผ๋ฉด์„œ
temp๋กœ ์ขŒ์ƒ๋‹จ ๊ฐ’์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฑฐ๋ž‘, dp๊ฐ€ 1์ฐจ์›๋ฐฐ์—ด์ด๋„ค์—ฌ.. ๋Œ€๋ฐ•
๊ทธ๋ž˜์„œ ์ฝ”๋“œ๊ฐ€ ๊น”๋”ํ•˜๊ฒŒ ๋А๊ปด์กŒ๊ตฐ์š”..! ๋งŽ์ด ๋ฐฐ์›Œ๊ฐ‘๋‹ˆ๋‹ค.


func ๋“ฑ์ฐจ์ˆ˜์—ดํ•ฉ(_ n: Int) -> Int {
return (n * (2 + (n - 1))) / 2
}
1 change: 1 addition & 0 deletions bishoe01/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
| ์ฐจ์‹œ | ๋‚ ์งœ | ๋ฌธ์ œ์œ ํ˜• | ๋งํฌ | ํ’€์ด |
|:----:|:---------:|:----:|:-----:|:----:|
| 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|
---
Loading