Skip to content
This repository was archived by the owner on Mar 18, 2024. It is now read-only.

[2023-07-09] jisu #81 #83

Merged
merged 1 commit into from
Aug 3, 2023
Merged

[2023-07-09] jisu #81 #83

merged 1 commit into from
Aug 3, 2023

Conversation

zsmalla
Copy link
Contributor

@zsmalla zsmalla commented Jul 9, 2023

PR Summary

1시간 정도 소요
[제한조건]
5 <= len(sequence) <= 1,000,000
1 <= sequence <= 1,000
5 <= k <= 1,000,000,000

[idea]
0, 1, 2, ... i 번째 원소부터 누적합을 구한 후, 누적합이 k인 경우 찾기
이 때 기본 오름차순으로 정렬되어 있으므로 누적합은 항상 증가하는 방향이다.
또한 오름차순인 것에 힌트, 누적합이 k인 경우를 찾을 때 이진 탐색을 활용할 수 있다.

[누적합 활용]
sequence의 각 원소를 해당 원소까지의 누적 합으로 업데이트 한 후, i번째 원소 전까지의 누적합을 빼어
i번째부터의 누적합을 활용할 수 있다.
image

[이진 탐색]
i번째 인덱스 부터의 인덱스를 활용할 수 있다면, 오름차순으로 정렬되어있음을 활용해 k를 탐색하는 과정에서 이진 탐색(파라메트릭 서치)을 적용할 수 있다.

ISSUE NUMBER

@zsmalla zsmalla requested a review from Woo-Yeol as a code owner July 9, 2023 14:32
@zsmalla zsmalla self-assigned this Jul 9, 2023
@zsmalla zsmalla linked an issue Jul 10, 2023 that may be closed by this pull request
Copy link
Contributor

@limstonestone limstonestone left a comment

Choose a reason for hiding this comment

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

누적합을 미리 구해놓고 이진 탐색을 하는 방법도 있군요! 뭔가 이게 더 robust 하고 정석(?)적으로 풀어낸 느낌이 드네요?! 풀이 감사합니당~!

sub = 0 if i == -1 else sequence[i] # sub : i번째 이전까지의 누적합
start, end = 0, len(sequence)-1 # -1 : 0번째 이전에는 누적합이 없는 것에 대한 예외

while start < end: # 이진탐색으로 k 값을 가지는 인덱스 찾기
Copy link
Member

Choose a reason for hiding this comment

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

누적합에 대한 접근이 너무 인상 깊어서 좋았습니다. 저도 비슷한 접근으로 접근하였으나 앞과 뒤를 나타내는 부분에서 투 포인터로 접근을 진행하였었습니다. 하지만 다음의 방식이 더 접근 방식이 한 눈에 다가와서 좋았던 것 같습니다. 고생하였습니다!

@ksumini
Copy link
Contributor

ksumini commented Jul 12, 2023

누적합과 이진탐색으로 풀이하시고, 그림도 그려주셔서 이해하기 쉬웠어요!
저도 처음에 input값만 보고 이진탐색으로 풀까 생각했었는데 문제를 보다보니 투포인터 기본 유형문제여서 투포인터로 풀이했습니다! 대부분 이진탐색으로 푸는 문제가 투포인터로도 풀리고 그 반대의 경우도 가능한데 지수님이 이진탐색으로 풀어주셔서 저도 주말에 다시 이진탐색으로 도전해보겠습니다~!! 좋은 풀이와 주석 감사합니다 👍👍👍

Copy link
Contributor

@y2r1m y2r1m left a comment

Choose a reason for hiding this comment

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

그림으로 그려주셔서 한결 이해가 쉬웠습니다!! 이진 탐색으로 이렇게 구현할 수 있군요! 좋은 풀이 감사합니다~!

@zsmalla zsmalla merged commit 0e7d33a into RecoRecoNi:main Aug 3, 2023
@zsmalla zsmalla deleted the jisu branch August 3, 2023 05:59
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Programmers] 연속된 부분 수열의 합
5 participants