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

23-InSange #82

Merged
merged 4 commits into from
Aug 9, 2024
Merged

23-InSange #82

merged 4 commits into from
Aug 9, 2024

Conversation

InSange
Copy link
Collaborator

@InSange InSange commented Aug 3, 2024

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

Minimum Swaps to Group All 1's Together II

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

4์‹œ๊ฐ„

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

๋ฌธ์ œ ๊ฐœ์š”

0๊ณผ 1๋กœ ์ด๋ฃจ์–ด์ง„ ์ˆœํ™˜ ๋ฐฐ์—ด nums๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ 1๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ๊ทธ๋ฃน์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ ์ตœ์†Œํ•œ์˜ ์Šค์™‘์ด ๋ช‡ ๋ฒˆ ์ด๋ฃจ์–ด์ง€๋Š” ์ง€ ๊ตฌํ•˜๊ธฐ.
์ฆ‰ 10001100์ผ ๋•Œ -> 00011100์œผ๋กœ ๊ตฌ์„ฑํ•˜๊ธฐ ์œ„ํ•˜์—ฌ ์ตœ์†Œ ํ•œ ๋ฒˆ์˜ ์Šค์™‘์ด ์ด๋ฃจ์–ด์ง€๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Œ.

์ฒซ๋ฒˆ์งธ ์ ‘๊ทผ ๋ฐฉ์‹

์ฃผ์–ด์ง„ nums ๋ฐฐ์—ด์˜ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ์™€ ๋งˆ์ง€๋ง‰ ์š”์†Œ๊ฐ€ 1์ผ ๊ฒฝ์šฐ 1๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ๊ทธ๋ฃน๋“ค์˜ ๊ฐœ์ˆ˜์—์„œ ๋‘˜์„ ํ•ฉํ•˜์—ฌ ๊ฐ€์žฅ ๋งŽ์ด ๋ชจ์ธ ๊ทธ๋ฃน์— ํ•ฉ์น˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ƒ๊ฐํ•˜์˜€๋‹ค.
๋งŒ์•ฝ nums๊ฐ€ 1001110001๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค๋ฉด 1์˜ ๊ทธ๋ฃน์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๋‹ด์€ ๋ฐฐ์—ด oneArr์—๋Š” 1, 3, 1์—์„œ ์ˆœํ™˜ ๋ฐฐ์—ด์ด๊ธฐ์— ์ฒซ ๋ฒˆ์งธ ๊ทธ๋ฃน๊ณผ ๋งˆ์ง€๋ง‰ ๊ทธ๋ฃน์„ ํ•ฉํ•˜์—ฌ 2, 3์œผ๋กœ ์ˆ˜์ •ํ•ด์ค€๋‹ค.
๊ฐ€์žฅ ํฐ ๊ทธ๋ฃน์€ 1์ด 3๊ฐœ๋ชจ์ธ ๊ทธ๋ฃน์ด๋ฏ€๋กœ ์ „์ฒด 5๊ฐœ์˜ 1๋“ค ์ค‘์—์„œ 2๊ฐœ๋งŒ ์›€์ง์—ฌ 3์œผ๋กœ ๋ถ™๋Š” ์‹์œผ๋กœ ์ƒ๊ฐํ•˜์˜€๋‹ค.

์—ฌ๊ธฐ์„œ ๋ฐœ๊ฒฌํ•œ ๋ฌธ์ œ์ ์€ ๋งŒ์•ฝ 10111001์ผ ๊ฒฝ์šฐ 11111000์œผ๋กœ 1๊ฐœ๋งŒ ์›€์ง์—ฌ์„œ 1์˜ ๊ทธ๋ฃน๋งŒ ๋งŒ๋“ค์–ด์ค„ ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์— ๋ด‰์ฐฉํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค.

ํ’€์ด ๋ฐฉ์‹

์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ๋ฐฉ์‹์œผ๋กœ ํ•ด๋‹น ๋ฐฐ์—ด์— ์กด์žฌํ•˜๋Š” 1๊ณผ 0์— ๋Œ€ํ•ด์„œ ๊ฐ๊ฐ ์ „์ฒด ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•œ ๋’ค. ์ „์ฒด ๊ฐœ์ˆ˜์—์„œ ๊ฐ€์žฅ ๋งŽ์€ ๊ทธ๋ฃน์„ ๋นผ์คŒ์œผ๋กœ์จ ์ฑ„์›Œ๋„ฃ์–ด์•ผํ•˜๋Š” ํ•„์š”ํ•œ ๊ฐœ์ˆ˜๋“ค์„ ํŒŒ์•…ํ•˜๋Š” ์‹์œผ๋กœ ์ง„ํ–‰.

  1. ์ˆœํ™˜ ๋ฐฐ์—ด์— ์กด์žฌํ•˜๋Š” 1์˜ ๊ฐœ์ˆ˜๋“ค์„ ๋จผ์ € ๊ตฌํ•ด totalCnt ๋ณ€์ˆ˜์—๋‹ค๊ฐ€ ๊ตฌํ•ด์ค€๋‹ค.
  2. ๋งŒ์•ฝ totalCnt๊ฐ€ 0์ด๊ฑฐ๋‚˜ ๋ฐฐ์—ด ๊ธธ์ด์™€ ๊ฐ™๋‹ค๋ฉด ์ด๋Š” ๋ชจ๋“  ๋ฐฐ์—ด์ด 1๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๊ฑฐ๋‚˜ ์•„์˜ˆ ์—†๋Š” ๊ฒฝ์šฐ์ด๋ฏ€๋กœ ๊ฒฝ์šฐ์˜ ์ˆ˜๋Š” ์—†๋‹ค.
  3. ๋ฐฐ์—ด์„ 0๋ฒˆ์งธ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ totalCnt๊ฐœ์ˆ˜ ๋งŒํผ ์ด๋™์‹œ์ผœ์„œ 1์ด ๋‚˜์˜จ๋งŒํผ curValCnt์— ์ถ”๊ฐ€ํ•ด์ค€๋‹ค.
  4. 0๋ฒˆ์งธ ์ธ๋ฑ์Šค์—์„œ totalCnt์ธ๋ฑ์Šค ๋งŒํผ์—๋Š” 1์ด curValCnt๋งŒํผ ์กด์žฌํ•˜๊ธฐ์— ์šฐ๋ฆฌ๋Š” ๊ทธ๋ฃน ์ตœ๋Œ€ ๊ฐœ์ˆ˜ maxValCnt๊ฐ’์„ ์—…๋ฐ์ดํŠธํ•ด์ค€๋‹ค.
  5. totalCnt๋งŒํผ ์ด๋™ํ•œ end๋ณ€์ˆ˜๋ถ€ํ„ฐ 0๋ฒˆ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ ๋ฐฉ๋ฌธํ•˜๋Š” start๋ณ€์ˆ˜๊นŒ์ง€ ๋น„๊ตํ•ด์ฃผ๋Š”๋ฐ end๋ณ€์ˆ˜๊ฐ€ ๋ฐฐ์—ด์˜ ๊ธธ์ด์™€ ๊ฐ™์•„์งˆ ๋•Œ ๊นŒ์ง€ ์ง„ํ–‰ํ•ด์ค€๋‹ค.
  6. ๋งŒ์•ฝ end์˜ ๊ฐ’์ด ์ ์  ์ฆ๊ฐ€ํ•จ์— ๋”ฐ๋ผ 1๋ฒˆ์ด ๋‚˜์˜ฌ ๊ฒฝ์šฐ curValCnt์— ๊ฐ’์„ ์ฆ๊ฐ€์‹œ์ผœ์ค€๋‹ค.
  7. ์šฐ๋ฆฌ๋Š” start์—์„œ end๊นŒ์ง€ totalCnt ํฌ๊ธฐ๋งŒํผ ๋น„๊ตํ•ด์ฃผ๊ธฐ์— start๋„ ๊ฐ’์„ ์ฆ๊ฐ€์‹œ์ผœ์ฃผ๋ฉด์„œ start๋ณ€์ˆ˜์˜ ๊ฐ’์— 1์ด ๋‚˜์˜ค๊ฒŒ ๋  ๊ฒฝ์šฐ ๋น ์ ธ๋‚˜๊ฐ€๊ฒŒ ๋˜๋ฏ€๋กœ curValCnt๊ฐ’์„ ๊ฐ์†Œ์‹œ์ผœ์ค€๋‹ค.
  8. ๋งค๋ฒˆ ์ด๋™ํ•  ๋•Œ ๋งˆ๋‹ค maxValCnt๋ณ€์ˆ˜์— ์ตœ๋Œ€ ๊ทธ๋ฃน ๊ฐœ์ˆ˜๋ฅผ ์—…๋ฐ์ดํŠธ ์‹œ์ผœ์ฃผ๊ณ 
  9. ์ „์ฒด 1์˜ ๊ฐœ์ˆ˜ totalCnt์—์„œ ๊ทธ๋ฃน์˜ ์ตœ๋Œ€ ๊ฐœ์ˆ˜ maxValCnt๊ฐ’ ๋งŒํผ ๋นผ์คŒ์œผ๋กœ์จ ์ฑ„์›Œ ๋„ฃ์–ด์•ผํ•  1์˜ ๊ฐœ์ˆ˜๋“ค์„ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ๋‹ค.

์—ฌ๊ธฐ์„œ ํ•˜๋‚˜ ์งš๊ณ  ๊ฐ€์•ผํ•  ์ ์€ ์ˆœํ™˜ํ•œ๋‹ค๋Š” ์ ์ด๋‹ค. 11001๊ณผ ๊ฐ™์ด ์ฃผ์–ด์กŒ์„ ๋•Œ 1๋กœ๋งŒ ํŒŒ์•…์„ ํ•˜๊ฒŒ๋˜๋ฉด 1๋ฒˆ๋งŒ ์Šค์™‘์„ ํ•˜๊ฒŒ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์ง€๋งŒ 0์„ ๊ธฐ์ค€์œผ๋กœ ์ง„ํ–‰ํ•˜์˜€์„ ๋•Œ ์Šค์™‘ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค๋Š” ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.

image
011100110์ด ์ฃผ์–ด์กŒ์„ ๋•Œ 1์˜ ๊ฐœ์ˆ˜๋Š” 5๊ฐœ ์ด๋ฏ€๋กœ totalCnt๊ฐ€ 5๋ผ๋Š” ๊ฒƒ์„ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ๋‹ค. ์šฐ๋ฆฌ๋Š” end๊ฐ€ 5์—์„œ ์‹œ์ž‘ํ•˜๊ณ  start๋ณ€์ˆ˜๊ฐ€ 0์—์„œ ์‹œ์ž‘ํ•˜๋ฉด์„œ end๊ฐ€ 1์„ ๋งˆ์ฃผํ–ˆ์„ ๋•Œ curValCnt๋ฅผ ์ฆ๊ฐ€. start๋ฅผ ๋งˆ์ฃผํ–ˆ์„ ๋•Œ ๊ฐ์†Œํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.
image
image
์ตœ๋Œ€ ๊ทธ๋ฃน์ด 3์œผ๋กœ ์ „์ฒด ๊ฐœ์ˆ˜ 5๊ฐœ์—์„œ 3๊ฐœ๋ฅผ ๋นผ์คŒ์œผ๋กœ์จ ์šฐ๋ฆฌ๋Š” 2๋ฒˆ์˜ ์Šค์™‘์ด ํ•„์š”ํ•˜๋‹ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

image

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

Copy link
Collaborator

@yuyu0830 yuyu0830 left a comment

Choose a reason for hiding this comment

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

์ดํ•ด๊ฐ€ ์–ด๋ ค์› ๋Š”๋ฐ ์„ค๋ช…์„ ์ฐฌ์ฐฌํžˆ ๊ณ„์† ์ฝ๋‹ค ๋ณด๋‹ˆ ์ดํ•ด๊ฐ€ ๊ฐ€๋„ค์š”.. ์ ‘๊ทผ ์ž์ฒด๊ฐ€ ๋˜๊ฒŒ ์‹ ๊ธฐํ•˜๋„ค์š”

10111001 ์„ ์˜ˆ์‹œ๋กœ ๋“  ๋ถ€๋ถ„์ด ์ดํ•ดํ•˜๋Š”๋ฐ ๋งŽ์€ ๋„์›€์ด ๋๋„ค์š” ใ…Žใ…Ž 10111 ์„ ์Šฌ๋ผ์ด๋“œ ์œˆ๋„์šฐ์— ๋„ฃ์œผ๋ฉด 1 ๊ฐœ๋งŒ ์˜ฎ๊ธฐ๋ฉด ๋œ๋‹ค๋Š” ๊ฒฐ๋ก ์ด ๋‚˜์˜ค๋Š”๊ตฐ์š”!

Copy link
Collaborator

@seongwon030 seongwon030 left a comment

Choose a reason for hiding this comment

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

์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ๋กœ ์ตœ๋Œ€๊ฐ’ ๊ทธ๋ฃน์„ ์ฐพ์•„๋‚ด๋Š” ๊ฒŒ ์‹ ๊ธฐํ–ˆ์–ด์š”. start์™€ end๋ฅผ ์ด์šฉํ•ด์„œ 1์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ณ„์† ๊ฐฑ์‹ ํ•˜๊ณ , ์ตœ๋Œ“๊ฐ’์„ ๊ฐฑ์‹ ํ•˜๋ฉด์„œ ๋งˆ์ง€๋ง‰์—” ๋ฐฐ์—ด์˜ 1์˜ ๊ฐœ์ˆ˜์˜ ํ•ฉ์—์„œ ์ตœ๋Œ€๊ฐ’ ๊ทธ๋ฃน์„ ๋นผ์„œ ๊ฒฐ๊ณผ๋ฅผ ๋„์ถœํ•  ์ˆ˜ ์žˆ๊ตฐ์š”. ์ƒˆ๋กœ์šด ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๋˜ ํ•˜๋‚˜ ๋ฐฐ์›Œ ๊ฐ‘๋‹ˆ๋‹ค.

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.

3 participants