-
Notifications
You must be signed in to change notification settings - Fork 0
4월12일 #9
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
base: main
Are you sure you want to change the base?
The head ref may contain hidden characters: "\uD22C\uD3EC\uC778\uD130"
4월12일 #9
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| n = int(input()) | ||
| arr = list(map(int,input().split())) | ||
| arr.sort() | ||
|
|
||
| count = 0 | ||
| for i in range(n): | ||
| temp = arr.copy() | ||
| temp.pop(i) #자기자신 제거 | ||
| left, right = 0, len(temp)-1 | ||
|
|
||
| while left<right: | ||
| result = temp[left]+temp[right] | ||
| if result == arr[i]: | ||
| count += 1 | ||
| break | ||
| elif result > arr[i]: | ||
| right -= 1 | ||
| else: | ||
| left += 1 | ||
|
|
||
| print(count) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import sys | ||
| input = sys.stdin.readline | ||
|
|
||
| N = int(input()) | ||
|
|
||
| arr = list(map(int,input().split())) | ||
| arr.sort() | ||
|
|
||
| def sum_liquid(N, arr): | ||
|
|
||
| MIN = float("inf") | ||
| answer = [] | ||
|
|
||
| for i in range(N): | ||
| temp_arr = arr.copy() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기서도 앞서 말씀드렸다시피 copy() 사용은 지양해주세요! (아마 시간 초과가 나면 이 부분 때문일 수도 있을 것 같아요!) |
||
| temp_arr.pop(i) | ||
|
|
||
| left = 0 | ||
| right = N-2 | ||
|
|
||
| while left < right: | ||
| result = temp_arr[left] + temp_arr[right] + arr[i] | ||
| if result == 0: # 값이 0이라면 이보다 더 0에 가까워질 수는 없기 때문에 바로 리턴 | ||
| sort_answer = [temp_arr[left], temp_arr[right], arr[i]] | ||
| sort_answer.sort() | ||
| return sort_answer | ||
| break | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P3. return을 하면 해당 함수가 값을 반환하며 종료되기 때문에 이후에 break문을 사용하실 필요가 없습니다! |
||
|
|
||
| if (abs(result) < MIN):#0에 가까운 값을 갱신할 수 있는지 확인 | ||
| MIN = abs(result) | ||
| answer = [temp_arr[left], temp_arr[right], arr[i]] | ||
| if result > 0: #0보다 크다면 더 작은 값을 만들어야 함 -> right 이동 | ||
| right -= 1 | ||
| else: #0보다 작다면 더 큰 값을 만들어야 함 -> left 이동 | ||
| left += 1 | ||
|
|
||
| answer.sort() | ||
| return answer | ||
|
|
||
| for i in sum_liquid(N, arr): | ||
| print(i, end=' ') | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| from collections import deque | ||
| N, d, k, c = map(int,(input().split())) | ||
|
|
||
| arr = [] | ||
|
|
||
| for i in range(N): | ||
| arr.append(int(input())) | ||
|
|
||
| arr = arr+ arr[:k-1] | ||
|
|
||
| window = deque() | ||
| MAX = 1 | ||
|
|
||
| for i in range(k): | ||
| window.append(arr[i]) | ||
|
|
||
| set_window = set(window) | ||
| set_window.add(c) | ||
| MAX = max(MAX, len(set_window)) | ||
|
|
||
| for i in range(k,N+k-1): | ||
| window.popleft() #이번 윈도우에서 제외되는 값 | ||
| window.append(arr[i]) #이번 윈도우에서 추가되는 값 | ||
|
|
||
| set_window = set(window) | ||
| set_window.add(c) | ||
| MAX = max(MAX, len(set_window)) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 편리하게 이전에 배웠던 deque를 생각하신 것 같아요! deque를 사용하지 않고 투 포인터로 풀이할 수 있는 방법도 함께 고민해보셨으면 좋겠습니다~! 회전 때문에 사용하신 코드인지 |
||
|
|
||
| print(MAX) | ||
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.
자기 자신을 피하기 위해서 copy로 복사해서 자기자신이 없는 배열을 만들어 주셨는데, copy는 시간복잡도가 o(n)이라서 배열이 길거나, 이렇게 반복문 안에 넣어 사용하는 경우 시간 복잡도 측면에서 좋지 않아요 ㅠㅠ!! 다른 방법을 고민해보셨으면 좋겠습니다!