This repository was archived by the owner on Mar 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
[2023-07-09] jisu #81 #83
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
""" | ||
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번째부터의 누적합을 활용할 수 있다. | ||
|
||
[이진 탐색] | ||
i번째 인덱스 부터의 인덱스를 활용할 수 있다면, 오름차순으로 정렬되어있음을 활용해 이진 탐색을 적용할 수 있다. | ||
""" | ||
|
||
def solution1(sequence : list, k : int) -> list: # 시간 초과 케이스 O(N * N/2) | ||
nums : list = [0 for _ in range(len(sequence))] | ||
interval = 999999 | ||
answer = None | ||
|
||
for i in range(len(sequence)): | ||
for j in range(i, len(sequence)): | ||
nums[i] += sequence[j] | ||
if nums[i] == k: | ||
if j-i < interval: | ||
answer = [i, j] | ||
interval = j-i | ||
if nums[i] > k: | ||
break | ||
|
||
return answer | ||
|
||
|
||
def solution(sequence : list, k : int) -> list: # 이진 탐색을 적용, O(NlogN) | ||
|
||
interval = 1000000 # 최대 interval은 1000000 | ||
answer = None | ||
|
||
for i in range(1, len(sequence)): | ||
sequence[i] += sequence[i-1] | ||
|
||
for i in range(-1, len(sequence)): # i번쨰 부터의 누적합 활용 | ||
|
||
sub = 0 if i == -1 else sequence[i] # sub : i번째 이전까지의 누적합 | ||
start, end = 0, len(sequence)-1 # -1 : 0번째 이전에는 누적합이 없는 것에 대한 예외 | ||
|
||
while start < end: # 이진탐색으로 k 값을 가지는 인덱스 찾기 | ||
mid = (start + end) // 2 # 정확히는 처음으로 k 이상인 값을 가진 인덱스 찾기 (keyword : 파라메트릭 서치) | ||
if sequence[mid] - sub >= k: | ||
end = mid | ||
else: | ||
start = mid + 1 | ||
|
||
if sequence[start] - sub == k and (start - (i+1)) < interval: # 정답은 수열이 가장 짧아야하고, 짧은 것 중에는 먼저 나온 것임 | ||
answer = [i+1, start] | ||
interval = start - (i+1) | ||
|
||
return answer | ||
|
||
|
||
|
||
case1 = [[1, 2, 3, 4, 5], 7] | ||
case2 = [[1,1,1,2,3,4,5], 5] | ||
case3 = [[2,2,2,2,2], 6] | ||
|
||
print(solution(*case1)) | ||
print(solution(*case2)) | ||
print(solution(*case3)) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
누적합에 대한 접근이 너무 인상 깊어서 좋았습니다. 저도 비슷한 접근으로 접근하였으나 앞과 뒤를 나타내는 부분에서 투 포인터로 접근을 진행하였었습니다. 하지만 다음의 방식이 더 접근 방식이 한 눈에 다가와서 좋았던 것 같습니다. 고생하였습니다!