Skip to content

[DP] jieun-0414#113

Open
lectura7942 wants to merge 1 commit intomainfrom
jieun-04-14
Open

[DP] jieun-0414#113
lectura7942 wants to merge 1 commit intomainfrom
jieun-04-14

Conversation

@lectura7942
Copy link
Collaborator

@lectura7942 lectura7942 commented Apr 13, 2023

๐Ÿ“Œ ํ‘ผ ๋ฌธ์ œ๋“ค

  • [๋ชป์ƒ๊ธด ์ˆ˜]
  • [ํŽธ์ง‘ ๊ฑฐ๋ฆฌ]

๐Ÿ“ ๊ฐ„๋‹จํ•œ ํ’€์ด ๊ณผ์ •

[๋ชป์ƒ๊ธด ์ˆ˜]

  • dp[i]: i ๋ฒˆ์งธ ๋ชป์ƒ๊ธด ์ˆ˜
    • j < i์ธ ๋ชจ๋“  j์— ๋Œ€ํ•ด dp[j]*2, dp[j]*3, dp[j]*5๋ฅผ ๋ณธ๋‹ค.
    • dp[i-1]๋ณด๋‹ค ํฐ ๊ฐ’ ์ค‘ ์ตœ์†Œ๊ฐ’์„ ๊ณจ๋ผ dp[i]์— ๋„ฃ๋Š”๋‹ค.

[ํŽธ์ง‘ ๊ฑฐ๋ฆฌ]

(์ถ”๊ฐ€)
leetcode๋กœ ๋Œ๋ ค๋ณด๋‹ˆ ํ‹€๋ฆฐ ํ’€์ด๋„ค์š”. ๐Ÿ˜ญ

  • A,B๋Š” ์ธ๋ฑ์Šค 0๋ถ€ํ„ฐ ์‹œ์ž‘.
  • dp[a][b] : A์˜ a๋ฒˆ์งธ ๋ฌธ์ž, B์˜ b๋ฒˆ์งธ ๋ฌธ์ž๊นŒ์ง€ ๋น„๊ตํ–ˆ์„ ๋•Œ ์ผ์น˜ํ•˜๋Š” ๊ฐ€์žฅ ๊ธด ๋ถ€๋ถ„๋ฌธ์ž์—ด ๊ธธ์ด
    • ํŒจ๋”ฉ์ด ์žˆ์–ด์„œ ์ธ๋ฑ์Šค 1 ๋ถ€ํ„ฐ ์‹œ์ž‘.
  • same[b] = a : B์˜ b๋ฒˆ์งธ ๋ฌธ์ž์™€ A์˜ a๋ฒˆ์งธ ๋ฌธ์ž๊ฐ€ ์ผ์น˜
    • -1 ์ด๋ฉด A ๋ฌธ์ž์™€ ์ผ์น˜ํ•˜์ง€ ์•Š์Œ
  • same์„ ํ™œ์šฉํ•ด์„œ ์„œ๋กœ ์ผ์น˜ํ•˜์ง€ ์•Š๋Š” ๋ฌธ์ž ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋ฉด ๋‹ต.

์˜ˆ์‹œ1

A_len = 5, B_len = 6
same = [-1, 2, -1, 3, -1, -1]
(1) A์™€ ์ผ์น˜ํ•˜์ง€ ์•Š์€ B๋ฌธ์ œ ๊ต์ฒด/์‚ญ์ œ/์‚ฝ์ž…: -1์˜ ๊ฐœ์ˆ˜
(2) B์™€ ์ผ์น˜ํ•˜์ง€ ์•Š์€ A๋ฌธ์ž ์ค‘ ๋’ท๋ถ€๋ถ„ ์‚ญ์ œ: A_len - 3 -1 = 1 ๊ฐœ
(3) B์™€ ์ผ์น˜ํ•˜์ง€ ์•Š์€ A๋ฌธ์ž ์ค‘ ์•ž๋ถ€๋ถ„ ์‚ญ์ œ: 2 ๊ฐœ

์˜ˆ์‹œ2

A = sunday
B = saturday
ans = 3
same = [0, -1, -1, 1, -1, 3, 4, 5]
dp   s  a  t  u  r  d  a  y
 s  [1, 1, 1, 1, 1, 1, 1, 1]
 u  [1, 1, 1, 2, 2, 2, 2, 2]
 n  [1, 1, 1, 2, 2, 2, 2, 2]
 d  [1, 1, 1, 2, 2, 3, 3, 3]
 a  [1, 2, 2, 2, 2, 3, 4, 4]
 y  [1, 2, 2, 2, 2, 3, 4, 5]

Copy link
Collaborator

@Da-Hye-JUNG Da-Hye-JUNG left a comment

Choose a reason for hiding this comment

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

[๋ชป์ƒ๊ธด ์ˆ˜]
prim๊ฐ๊ฐ ๊ณฑํ•ด์ฃผ๋Š” for๋ฌธ์„ ์ œ์™ธํ•˜๊ณ  2์ค‘ for๋ฌธ์œผ๋กœ ๊ต์ฐจ๋กœ ๊ณฑํ•ด์ฃผ๋ฉด์„œ list์— ๋„ฃ์–ด์ฃผ์…จ๋„ค์š”. ์ž˜ ๋ณด์•˜์Šต๋‹ˆ๋‹ค.

[ํŽธ์ง‘ ๊ฑฐ๋ฆฌ]
A๋ฌธ์ž์™€ B๋ฌธ์ž์˜ ์ผ์น˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” same์œผ๋กœ ํ‘ธ์‹  ์•„์ด๋””์–ด ์ข‹์€ ๊ฒƒ ๊ฐ™์•„์š”!
์ œ ์ƒ๊ฐ์—๋Š” same[b] = a (B์˜ b๋ฒˆ์งธ ๋ฌธ์ž์™€ A์˜ a๋ฒˆ์งธ ๋ฌธ์ž๊ฐ€ ์ผ์น˜) ์—์„œ b๋ฒˆ์งธ ๋ฌธ์ž์™€ ์ผ์น˜ํ•˜๋Š” a๋ฒˆ์งธ ๋ฌธ์ž๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ ์ผ๋•Œ ์ผ€์ด์Šค๋ฅผ ๊ณ ๋ คํ•ด์•ผํ•  ๊ฒƒ ๊ฐ™์•„์š”! ๊ณ ์ƒ๋งŽ์œผ์…จ์Šต๋‹ˆ๋‹ค ใ…Žใ…Ž

Copy link
Member

@JLake310 JLake310 left a comment

Choose a reason for hiding this comment

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

[๋ชป ์ƒ๊ธด ์ˆ˜]
์–ด๋–ค ์ˆ˜๋ฅผ ๋ด์•ผํ•  ์ง€ ๋ฒ”์œ„ ๋น„๊ต๋ฅผ ํ†ตํ•ด ์ •ํ•œ ํ’€์ด ์ž˜ ๋ดค์Šต๋‹ˆ๋‹ค!

[ํŽธ์ง‘ ๊ฑฐ๋ฆฌ]
์ €์™€ ๋น„์Šทํ•˜๊ฒŒ lcs๋ฅผ ์–ด๋А ์ •๋„ ํ™œ์šฉํ•œ ํ’€์ด ๊ฐ™์Šต๋‹ˆ๋‹ค,,! ์ž˜ ๋ดค์Šต๋‹ˆ๋‹ค.

๊ณ ์ƒํ•˜์…จ์–ด์š” ๐Ÿ‘๐Ÿป

Copy link
Collaborator

@hoooolllly hoooolllly left a comment

Choose a reason for hiding this comment

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

[๋ชป์ƒ๊ธด ์ˆ˜]
์กฐ๊ฑด๋ฌธ์„ ๋„ฃ์–ด์ค˜์„œ ๋น ๋ฅด๊ฒŒ ๊ณ„์‚ฐ๋˜๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!

[ํŽธ์ง‘๊ฑฐ๋ฆฌ]
์ƒ๊ฐํ•˜์‹  ์•„์ด๋””์–ด๋ฅผ ๊ตฌํ˜„ํ•˜์‹  ๊ฒƒ ๋Œ€๋‹จํ•ฉ๋‹ˆ๋‹ค! ๊ณ ์ƒํ•˜์…จ์Šต๋‹ˆ๋‹ค!

Copy link
Member

@yunjinchoidev yunjinchoidev left a comment

Choose a reason for hiding this comment

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

[๋ชป์ƒ๊ธด ์ˆ˜]
์ด๋ ‡๊ฒŒ ๊ตฌํ•ด์•ผ dp๋กœ ์ˆœ์„œ๋ฅผ ๋ณด์žฅํ•  ์ˆ˜๊ฐ€ ์žˆ๊ตฐ์š”. ์ž˜ ๋ดค์Šต๋‹ˆ๋‹ค.

[ํŽธ์ง‘๊ฑฐ๋ฆฌ]
์ผ์น˜ํ•˜๋Š” ๊ฐ€์žฅ ๊ธด ๋ถ€๋ถ„ ๋ฌธ์ž์—ด ๊ตฌํ•˜๋Š” ๋ถ€๋ถ„ ์ž˜ ๋ดค์Šต๋‹ˆ๋‹ค! ๊ณ ์ƒ ๋งŽ์œผ์…จ์–ด์š”!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants