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

14-kangrae-jo #48

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

14-kangrae-jo #48

wants to merge 1 commit into from

Conversation

kangrae-jo
Copy link
Collaborator

@kangrae-jo kangrae-jo commented Dec 31, 2024

πŸ”— 문제 링크

[두 μš©μ•‘]

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

47m

✨ μˆ˜λ„ μ½”λ“œ

μƒˆλ‘œμš΄ μœ ν˜•μ˜ λ¬Έμ œκ°€ ν•„μš”ν•  것 κ°™μ•„μ„œ κ°€μ Έμ™€λ΄€μŠ΅λ‹ˆλ‹€~
투 포인터 λ¬Έμ œμΈλ°μš”, μ •λ ¬κ³Ό 이뢄 탐색 λ“±μ—μ„œ μ‚¬μš©ν•˜λŠ” 방식이라고 ν•©λ‹ˆλ‹€.

문제λ₯Ό 읽고 μš”μ•½ν•˜λ©΄ λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

μ„œλ‘œ ν•©μ³μ„œ κ°€μž₯ 0에 κ°€κΉŒμš΄ 값이 λ˜λŠ” 두 μš©μ•‘μ„ 좜λ ₯ν•˜λΌ.

λμž…λ‹ˆλ‹€!

투 포인터 문제인걸 μ•Œκ³  λ“€μ–΄μ™€μ„œ μ•„μ΄λ””μ–΄λŠ” κ½€ 금방 생각났고 λΉ λ₯΄κ²Œ κ΅¬ν˜„μ„ ν•΄λ‚˜κ°”μŠ΅λ‹ˆλ‹€.

1. μž…λ ₯받은 μš©μ•‘ μ •λ ¬
2. μ™Όμͺ½, 였λ₯Έμͺ½ λμ—μ„œ ν•œ μΉΈμ”© 쀄여가며 쑰건에 λ§žλŠ” 인덱슀찾기

1λ²ˆμ€ 쉽고 2λ²ˆμ€ λ‹€μŒκ³Ό 같은 ν•΅μ‹¬λ‘œμ§μ„ 톡해 κ΅¬ν˜„ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

if (mix > 0) right--;
else left++;

μ™œλƒν•˜λ©΄ 정렬이 λ˜μ–΄μžˆκΈ° λ•Œλ¬Έμ΄μ£΅

그런데 1번, 2번 κ³Όμ • μ€‘μ—μ„œ ν•΄μ•Όν•˜λŠ” 행동이 μžˆμŠ΅λ‹ˆλ‹€.
λ§Œμ•½ ν•©μ³μ„œ 0μ΄λ˜λŠ” 두 μš©μ•‘μ΄ μ‘΄μž¬ν•˜μ§€ μ•Šμ„ κ²½μš°μ—λŠ” κ°€μž₯ 0에 κ°€κΉŒμš΄ 녀석을 λ§Œλ“œλŠ” 두 값을 좜λ ₯ν•΄μ•Όν•©λ‹ˆλ‹€.

λ”°λΌμ„œ...
과거와 ν˜„μž¬μ˜ abs(mix)값을 λΉ„κ΅ν•˜κ³  더 μž‘μ€ 값을 μƒμ„±ν•˜λŠ” 두 값을 κ°±μ‹ ν•˜λ©° κΈ°λ‘ν•΄λ‘λŠ” 둜직이 ν•„μš”ν•©λ‹ˆλ‹€!
κ·Έλ ‡κ²Œ κ΅¬ν˜„ν•˜λ©΄ 이 λ¬Έμ œλŠ” μ •λ‹΅μž…λ‹ˆλ‹€~

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

ν˜Ήμ‹œ μ½”λ“œ λ³΄μ‹œκ³  μ΄μƒν•œ 점 λˆˆμΉ˜μ±„μ…¨λ‚˜μš”?

μ™œ μ΄λ ‡κ²Œ κ΅¬ν˜„ν–ˆμ„κΉŒμš”?

if (mix == 0) {
    cout << have[left] << " " << have[right];
    return 0;
}

μ €λŠ” λ‚΄λΆ€μ—μ„œ return ν•˜λŠ” μ„±ν–₯이 μ•„λ‹ˆλΌ breakν•΄μ„œ μ™ΈλΆ€μ—μ„œ 주둜 좜λ ₯ν•˜λŠ”λ°μš” μ™œ μ΄λž¬μ„κΉŒμš”?

그건 μ œκ°€ μ‹€μˆ˜λ‘œ while λ‚΄λΆ€μ—μ„œ long int mix = have[left] + have[right]; μ΄λ ‡κ²Œ... 해버렸기 λ•Œλ¬Έμž…λ‹ˆλ‹€.
(λ‚΄λΆ€ λ³€μˆ˜λ‘œ μ„ μ–Έλ˜μ–΄ λ°–μ—μ„œ μ‚¬μš©ν•˜λŠ” mix와 λ‚΄λΆ€ mixλŠ” μ„œλ‘œ λ‹€λ₯Έ λ³€μˆ˜κ°€ λ˜μ–΄λ²„λ Έλ˜ 것..)

μ§€κΈˆ λ³΄λ‹ˆκΉŒ λ³΄μ΄λŠ”λ° 문제 풀이 λ‹Ήμ‹œμ—λŠ” 이게 μ•ˆλ³΄μ—¬μ„œ 10λΆ„ 정도 κ³ λ―Όν–ˆλ„€μš”...

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.

1 participant