Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions 4월12일 투포인터/1253.py
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) #자기자신 제거
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

자기 자신을 피하기 위해서 copy로 복사해서 자기자신이 없는 배열을 만들어 주셨는데, copy는 시간복잡도가 o(n)이라서 배열이 길거나, 이렇게 반복문 안에 넣어 사용하는 경우 시간 복잡도 측면에서 좋지 않아요 ㅠㅠ!! 다른 방법을 고민해보셨으면 좋겠습니다!

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)
41 changes: 41 additions & 0 deletions 4월12일 투포인터/2473.py
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()
Copy link
Copy Markdown

@nanometre380 nanometre380 May 2, 2022

Choose a reason for hiding this comment

The 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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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=' ')
29 changes: 29 additions & 0 deletions 4월12일 투포인터/2531.py
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))
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

편리하게 이전에 배웠던 deque를 생각하신 것 같아요! deque를 사용하지 않고 투 포인터로 풀이할 수 있는 방법도 함께 고민해보셨으면 좋겠습니다~! 회전 때문에 사용하신 코드인지 arr = arr + arr[:k-1]를 사용하셨는데 시간복잡도 측면에서 좋지 못하니 여기도 다른 방법을 생각해보시면 더 좋을 것 같습니다. 원 형태에서는 모듈러 연산도 사용할 수 있을 것 같아요~


print(MAX)