-
-
Notifications
You must be signed in to change notification settings - Fork 305
[radiantchoi] WEEK 09 Solutions #2258
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
Conversation
ys-han00
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이번 주 알고리즘 푸시느라 수고하셨습니다! 올려주신 코드 잘 보았습니다.
늦게 승인 해드려 죄송합니다 😢
| /* | ||
| 사이클 시작 지점을 찾으시오, 라는 문제일 경우? | ||
| entry라는 별도의 포인터를 하나 더 둔 다음 slow와 함께 한 칸씩 이동시킨다. | ||
| 두 포인터가 만나는 지점이 사이클 시작 지점! | ||
| 이것이 가능한 이유는, 아래의 이동 거리 공식 때문에 그렇다. | ||
|
|
||
| L: 사이클 입구까지의 길이 | ||
| C: 사이클 길이 | ||
| d: 사이클 내에서, 사이클 종료(==시작)까지 남은 거리 | ||
|
|
||
| fast의 이동 거리: L + n * C + d | ||
| slow의 이동 거리: L + d | ||
|
|
||
| fast의 이동 거리는 slow의 이동 거리의 2배이므로 | ||
| 2 * (L + d) = L + n * C + d | ||
|
|
||
| 따라서, L = n * C - d 이다. | ||
| 그리고 L은 entry 포인터가 이동해야 하는 거리이다. | ||
| n * C - d는 사이클 안에서는 C - d와 같다. fast 노드가 n번 돌았을 뿐, 사이클 안이었기 때문. | ||
| 따라서 entry 포인터와 slow 포인터를 다시 한 칸씩 이동시키다 보면, 반드시 만나게 된다. | ||
| 이 만나는 지점이 사이클 시작 지점. | ||
| */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
문제를 확장해서도 생각 하셨네요!
덕분에, 시작 지점 찾는 방법 배워가요 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
사실 이번에 Floyd's cycle detection algorithm이라는 걸 아예 처음 접하다 보니, 결국 만나는 지점을 찾아서 어디다 쓰지? 라는 의문이 들었습니다. 그리고 역시나, 그런 문제가 있긴 하더라구요. 도움이 되셨다니 기쁩니다.
답안 제출 문제
작성자 체크 리스트
In Review로 설정해주세요.검토자 체크 리스트
Important
본인 답안 제출 뿐만 아니라 다른 분 PR 하나 이상을 반드시 검토를 해주셔야 합니다!