Skip to content
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

1-g0rnn #4

Merged
merged 1 commit into from
Oct 1, 2024
Merged

1-g0rnn #4

merged 1 commit into from
Oct 1, 2024

Conversation

g0rnn
Copy link
Collaborator

@g0rnn g0rnn commented Sep 28, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

ํƒ€๊นƒ ๋„˜๋ฒ„ : https://school.programmers.co.kr/learn/courses/30/lessons/43165
๊ฐ™์€ ์ˆซ์ž๋Š” ์‹ซ์–ด : https://school.programmers.co.kr/learn/courses/30/lessons/12906
๊ธฐ๋Šฅ ๊ฐœ๋ฐœ : https://school.programmers.co.kr/learn/courses/30/lessons/42586

ํƒ€๊นƒ ๋„˜๋ฒ„ ๋ฌธ์ œ ์„ค๋ช…

  • ๋ฐฐ์—ด์˜ ์ˆซ์ž๋ฅผ ์กฐํ•ฉํ•˜์—ฌ target์ด ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•œ๋‹ค

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

1h 30m

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

๋„ˆ๋ฌด ์˜ค๋žœ๋งŒ์— ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š”๊ฑฐ๋ผ ์‰ฌ์šด ๋ฌธ์ œ ์œ„์ฃผ๋กœ 3๊ฐœ๋ฅผ ๊ณจ๋ผ ๋ง›๋ณด๊ธฐ ํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค!
์Šคํƒ/ํ ๋ฌธ์ œ๋Š” ์‰ฝ๊ฒŒ ํ’€๋ฆด ๊ฒƒ ๊ฐ™์•„. ์ฝ”๋“œ ์ž์ฒด๋กœ๋Š” ์ดํ•ดํ•˜๊ธฐ ์–ด๋ ค์šด DFS(ํƒ€๊ฒŸ ๋„˜๋ฒ„)์— ๋Œ€ํ•ด ์ž‘์„ฑํ• ๊ฒŒ์š”.

์กฐ๊ฑด

  • numbers ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋Š” 2์ด์ƒ 20์ดํ•˜
  • ๊ฐ ์ˆซ์ž๋Š” 1์ด์ƒ 50 ์ดํ•˜ ์ž์—ฐ์ˆ˜
  • target์€ 1์ด์ƒ 1000 ์ดํ•˜ ์ž์—ฐ์ˆ˜

ํ’€์ด

์ด๋ ‡๊ฒŒ ๋ฐฐ์—ด์„ ์ˆœ์ฐจ์ ์œผ๋กœ ์ˆœํšŒํ•˜๋ฉด์„œ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋”ฐ์ ธ์•ผํ•˜๋Š” ๋ฌธ์ œ๋Š” shift์—ฐ์‚ฐ์„ ์‚ฌ์šฉํ•œ ๋ฐฉ๋ฒ•๊ณผ recursion์ด ๋– ์˜ค๋ฅด๋Š”๋ฐ shift์—ฐ์‚ฐ์œผ๋กœ ํ•˜๊ธฐ๋Š” ์ต์ˆ™์น˜๋„ ์•Š๊ณ  ์ฝ”๋“œ๊ฐ€ ๊น”๋”ํ•˜์ง€ ์•Š์„๊ฒƒ ๊ฐ™์•„ dfs๋ฅผ ํ™œ์šฉํ•œ recursion์„ ํƒํ•˜๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋„ 20์ดํ•˜์ด๊ณ  ์ตœ๋Œ€ ๋ฐ˜๋ณต ํšŸ์ˆ˜๊ฐ€ 2^20์ด๋ผ์„œ ์‹œ๊ฐ„๋‚ด์— ์ถฉ๋ถ„ํžˆ ํ’€ ์ˆ˜ ์žˆ์œผ๋ฆฌ๋ผ ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค.

int dfs(vector<int>& numbers, int target, int i, int value) {
    // ๋ชจ๋“  ์›์†Œ๋ฅผ ์ˆœํšŒํ•˜์—ฌ ์—ฐ์‚ฐ์„ ๋๋‚ด๋ฉด ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค
    if(numbers.size() <= i) {

        //์—ฐ์‚ฐ์„ ๋ชจ๋‘ ๋๋‚ธ ํ›„ ๊ฐ’์ด target๊ณผ ๊ฐ™๋‹ค๋ฉด 1 ์•„๋‹ˆ๋ฉด 0์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค
        return (target == value) ? 1 : 0;
    }
    
    // ๋‹ค์Œ ์›์†Œ๋ฅผ ์ˆœํšŒํ•˜๊ธฐ ์œ„ํ•ด i๋ฅผ ์ฆ๊ฐ€ํ•˜๊ณ  value์— ํ˜„์žฌ ์›์†Œ๋ฅผ ๋”ํ•˜๊ฑฐ๋‚˜ ๋บ€๋‹ค
    return dfs(numbers, target, i+1, value + numbers[i]) 
        + dfs(numbers, target, i+1, value - numbers[i]);
}

ํ•ญ์ƒ recursion์„ ํ™œ์šฉํ•œ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•  ๋•Œ ์ฃผ์˜ํ•ด์•ผํ•  ์ ์€ ํ•จ์ˆ˜์˜ ์ข…๋ฃŒ์‹œ์ ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ recursion์ด ๋๋‚˜๋Š” ์‹œ์ ์€ ๋ชจ๋“  ์—ฐ์‚ฐ์ด ๋๋‚œ ํ›„ ์ž…๋‹ˆ๋‹ค. if ์กฐ๊ฑด๋ฌธ ๋‚ด๋ถ€์— numbers.size() - 1 == i ์ด ์•„๋‹ˆ๋ผ size() == i์ธ ์ ์— ์œ ์˜ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

  • [๊ฐ™์€ ์ˆซ์ž๋Š” ์‹ซ์–ด] ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ uniqueํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋ฉด ์‰ฝ๊ฒŒ ํ’€๋ฆฐ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ์•˜์Šต๋‹ˆ๋‹ค.
    ์‚ฌ์‹ค ์ด๋•Œ๊นŒ์ง€๋Š” ์ค‘๋ณต๋˜๋Š” ์š”์†Œ๋ฅผ ๋ฐฐ์—ด์˜ ๋งจ ๋’ค๋กœ ๋„˜๊ธฐ๋Š”์ค„ ์•Œ์•˜๋Š”๋ฐ __์—ฐ์†๋œ ์ค‘๋ณต๋˜๋Š” ์ˆซ์ž__๊ฐ€ ๋’ค๋กœ ๊ฐ„๋‹ค๋Š” ๊ฑฐ๋ฅผ ๋’ค๋Šฆ๊ฒŒ ์•Œ๊ฒŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค ๐Ÿ˜…
  • ๊ทธ๋Ÿผ ์ค‘๋ณต๋˜๋Š” ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•ด์•ผํ•  ๋•Œ๋Š” ์–ด๋–ค ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ด์•ผ ์‰ฝ๊ฒŒ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ์„๊นŒ์š”?
    -> set ์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹น ๐Ÿ›ฉ๏ธ

Copy link
Collaborator

@kokeunho kokeunho left a comment

Choose a reason for hiding this comment

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

๋•๋ถ„์— DFS ๋ฌธ์ œ ๋ง› ๋ณผ ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

ํŒŒ์ด์ฌ์œผ๋กœ ์งœ๋ดค๋Š”๋ฐ ์ฝ”๋“œ๋Š” ๊ฑฐ์˜ ๋น„์Šทํ•˜๋„ค์š”.

๋‹ค์Œ์—๋Š” BFS๋กœ ํ•ด๋‹น ๋ฌธ์ œ ๋‹ค์‹œ ํ•œ๋ฒˆ ํ’€์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
์ˆ˜๊ณ ํ•˜์…จ์Šด๋‹ค

Copy link
Collaborator

@kangrae-jo kangrae-jo left a comment

Choose a reason for hiding this comment

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

๋‚˜๋จธ์ง€ ๋‘๊ฐœ์˜ ์ฝ”๋“œ๋„ ์ž˜ ๋ดค์Šต๋‹ˆ๋‹ค.
unique๋Š” ์ž˜ ์‚ฌ์šฉํ•ด๋ณด์ง€ ์•Š์•˜๋Š”๋ฐ ์ด๋ฒˆ ๊ธฐํšŒ์— ์‚ฌ์šฉํ•ด๋ด์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค.

Copy link
Collaborator

Choose a reason for hiding this comment

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

์žฌ๊ท€๋ฅผ ์‚ฌ์šฉํ•œ dfs์ฝ”๋“œ ์ž˜ ๋ดค์Šต๋‹ˆ๋‹ค.
๋ง์”€ํ•˜์‹ ๋Œ€๋กœ ์žฌ๊ท€์—์„œ๋Š” basecase๋ฅผ ์ž˜ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

@wnsmir
Copy link
Collaborator

wnsmir commented Oct 1, 2024

๋ฌธ์ œํ’€์ด์—๋Š” ์ƒ๊ด€์ด ์—†์ง€๋งŒ days.reserve(progresses.size());๋ฅผ ์‚ฌ์šฉํ•ด days ๋ฒกํ„ฐ๋ฅผ ๋ฏธ๋ฆฌ ํฌ๊ธฐ๋ฅผ ํ• ๋‹นํ•˜๋ฉด ๋ฉ”๋ชจ๋ฆฌ ์žฌํ• ๋‹น์„ ์•ˆํ•ด๋„๋ ๊ฒƒ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ฝ”๋“œ๊ฐ€ ๊ฐ„๊ฒฐํ•˜๊ณ  ๊น”๋”ํ•ฉ๋‹ˆ๋‹ค !!

@g0rnn
Copy link
Collaborator Author

g0rnn commented Oct 1, 2024

๋ฌธ์ œํ’€์ด์—๋Š” ์ƒ๊ด€์ด ์—†์ง€๋งŒ days.reserve(progresses.size());๋ฅผ ์‚ฌ์šฉํ•ด days ๋ฒกํ„ฐ๋ฅผ ๋ฏธ๋ฆฌ ํฌ๊ธฐ๋ฅผ ํ• ๋‹นํ•˜๋ฉด ๋ฉ”๋ชจ๋ฆฌ ์žฌํ• ๋‹น์„ ์•ˆํ•ด๋„๋ ๊ฒƒ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ฝ”๋“œ๊ฐ€ ๊ฐ„๊ฒฐํ•˜๊ณ  ๊น”๋”ํ•ฉ๋‹ˆ๋‹ค !!

reserve๋ผ๋Š” ๋ฉ”์„œ๋“œ๋Š” ์ฒ˜์Œ ๋“ค์–ด๋ณด๋Š”๋ฐ ์ฐพ์•„๋ณด๋‹ˆ ๋ฉ”๋ชจ๋ฆฌ๋งŒ์„ ๋ฏธ๋ฆฌ ํ• ๋‹นํ•˜๋Š” ํ•จ์ˆ˜๊ตฐ์š”

๊ทผ๋ฐ ๊ทธ๋Ÿฌ๋ฉด vector๋ฅผ ์ƒ์„ฑํ• ๋•Œ ์‚ฌ์ด์ฆˆ๋ฅผ ์ •ํ•˜๋Š” ๊ฒƒ๊ณผ๋Š” ์–ด๋–ค ์ฐจ์ด๊ฐ€ ์žˆ์„์ง€ ์•Œ์•„๋ณด๋‹ˆ

  • vector ์ƒ์„ฑ์ž : ๋ฏธ๋ฆฌ ๊ฐ’์„ ์ฑ„์šฐ๊ณ  capacity๋ฟ๋งŒ ์•„๋‹ˆ๋ผ size๊นŒ์ง€ ํ• ๋‹น๋จ
  • reserve : ๊ฐ’์„ ๋ฏธ๋ฆฌ ์ฑ„์šฐ์ง€ ์•Š๊ณ  size๋„ 0์ด๋ฉฐ, capacity๋งŒ ๋Š˜์–ด๋‚จ

์œ„์™€ ๊ฐ™์€ ์ฐจ์ด๊ฐ€ ์žˆ๋„ค์š”. ๊ทธ๋ฆฌ๊ณ  reserve๋กœ ๋ฏธ๋ฆฌ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹นํ•˜๋ฉด push_back()ํ•˜์—ฌ ์›์†Œ๋ฅผ ์ถ”๊ฐ€ํ•  ๋•Œ์—๋„ ์†๋„๊ฐ€ ๋” ๋นจ๋ผ์ง€๋„ค์š”!! (์ด๊ฑด ์•„๋งˆ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋ฏธ๋ฆฌ ํ• ๋‹นํ•ด์„œ ๊ทธ๋ ‡๊ฒ ์ฃ .?)

์ค€์šฉ๋‹˜ ๋•๋ถ„์— ๋” ํšจ์œจ์ ์€ ์ฝ”๋“œ ์ž‘์„ฑ๋ฒ•์„ ์•Œ๊ฒŒ๋˜์—ˆ์–ด์š” ๊ฐ์‚ผ๋‹น ๐Ÿ˜ธ

์ œ๊ฐ€ ์ฐธ๊ณ ํ•œ ๋ธ”๋กœ๊ทธ์ž…๋‹ˆ๋‹ค : https://www.cv-learn.com/20201217-std-vector-reserve/

@g0rnn g0rnn closed this Oct 1, 2024
@g0rnn g0rnn deleted the 4-g0rnn branch October 1, 2024 12:59
@g0rnn g0rnn restored the 4-g0rnn branch October 1, 2024 13:54
@g0rnn g0rnn reopened this Oct 1, 2024
@g0rnn g0rnn merged commit 71384af into main Oct 1, 2024
2 checks passed
@g0rnn g0rnn deleted the 4-g0rnn branch October 1, 2024 13:55
@g0rnn g0rnn changed the title 4 - g0rnn 1 - g0rnn Oct 2, 2024
@g0rnn g0rnn changed the title 1 - g0rnn 1-g0rnn Oct 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants