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
15 changes: 15 additions & 0 deletions [W2]정렬_이진탐색/pine/[이진탐색]K번째수_1300_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'''
문제 풀이 1
1. 입력받은 N을 이용해서 배열 B를 제작한다.
1.1 1-N까지의 값을 반복문을 이용해 제작.
2. B를 오름차순 정렬한다.
3. B[k]를 구한다. -> 어차피 배열이니까 Index값으로 뽑으면 되는데 왜 이진 탐색인거지?

결과: 메모리초과
Copy link
Owner

Choose a reason for hiding this comment

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

ㅋㅋ

'''

n = int(input())
k = int(input())

b=sorted([i*j for i in range(1,n+1) for j in range(1,n+1)])
print(b[k])
30 changes: 30 additions & 0 deletions [W2]정렬_이진탐색/pine/[이진탐색]K번째수_1300_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'''
문제 풀이 2
1. 숫자의 개수 = N*N
2. (숫자의 개수)/2 와 K를 비교해 배열의 크기를 절반으로 축소
2.1 K가 클 경우 배열 = (1 ~ N) * ((N/2)+1 ~ N)
2.2 K가 작을 경우 배열 = (1 ~ N) * ((1~(N/2+나머지))
3. 남은 숫자의 개수 = ((N/2)+1) * N개
4. 반복하며 범위 축소
4.1 언제까지? 시작인덱스와 종료인덱스가 같을때까지
Comment on lines +4 to +9
Copy link
Owner

Choose a reason for hiding this comment

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

  1. 2차원 배열 내부에 값이 순서대로 정렬되어 있는 게 아니기 때문에 이 배열을 단순히 인덱스를 기준으로 자르는 건 의미가 없을 것 같습니다.
  2. 만약 정렬된 상태를 가정한다면 인덱스로도 접근할 수 있을 것 같습니다.
image


결과: 값이 정렬되지 않은 상태여서 정렬된 값을 구할 수 없음
'''
import math

n = int(input())
k = int(input())

startIndex = 1
endIndex = n
number= n*n # 숫자의 개수

while startIndex != endIndex:
isAboveMid = (number/2)<=k #16
if isAboveMid: startIndex += math.ceil((endIndex-startIndex)/2)
else: endIndex = sum(divmod(endIndex, 2)) # K가 같거나 작을 경우의 배열
Copy link
Owner

Choose a reason for hiding this comment

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

혹시 이 코드는 어떤 역할을 하는 지 알 수 있을까요?
divmod는 나눈 몫과 나머지를 반환해주는 함수로 알고 있는데, 이 두 값을 더한 값을 인덱스로 쓰는 이유가 있나요?

number = (endIndex-startIndex+1)*n

b = [i*j for i in range(1,n+1) for j in range(startIndex,endIndex+1)]
index = k-(n*n-n) # (endIndex-1) = 현재 선택된 행 이전의 행 -> *n을 하여 앞의 숫자 개수를 계산해서 k에서 빼준다.
print(b[index])
23 changes: 23 additions & 0 deletions [W2]정렬_이진탐색/pine/[정렬]회의실배정_1931.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'''
풀이 방식
1. 종료시간으로 정렬
1.1 종료시간으로만 정렬했다가 실패. -> 1순위는 종료시간, 2순위는 시작시간으로 정렬해줘야했음
2. 예약 배열에 예약된 시간을 넣어줌
3. 이때, 신청 시작시간이 최근 예약된 시간의 종료시간 이후일 경우 예약 확정
----------
예제 입력1(1.1에 해당하는 예시)
5
3 3
1 2
2 3
3 4
4 5
'''
n = int(input())
schedule = sorted([list(map(int, input().split())) for _ in range(n)],key=lambda time: (time[1],time[0]))
print(schedule)
Copy link
Owner

Choose a reason for hiding this comment

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

이건 디버깅 코드인가요? 아니면 아직 이 코드가 통과가 안 된 상태인가요?

reservation=[schedule[0]]
for time in schedule[1:]:
if time[0]>=reservation[-1][1]:reservation.append(time)
Comment on lines +19 to +21
Copy link
Owner

Choose a reason for hiding this comment

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

스터디 때 제 코드 설명드리면서 언급했던 아이디어인데, 가장 늦은 종료 시간을 변수에 담아 업데이트 하는 방식은 어떠신가요?
지금 코드에서 reservation 리스트에 time 값을 append 해주고 있지만 활용하는 값은 마지막에 추가된 종료시간(reservation[-1][1])만을 고려하고 있는 것으로 보여서요.

재사용되지 않는 데이터라 메모리 측면에서 비효율적인 것 같습니다. 한 번 고려해주시면 감사하겠습니다!


print(len(reservation))