From 67bbcc50946d47fe72474dc995d4aa9438408a0e Mon Sep 17 00:00:00 2001 From: heoseungyeon Date: Mon, 12 Apr 2021 23:34:16 +0900 Subject: [PATCH 1/7] =?UTF-8?q?=EC=B1=85=20=EC=88=99=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...6\353\212\224 \352\270\210\354\225\241.py" | 15 ++++++++++ ...5 \352\263\240\353\245\264\352\270\260.py" | 28 +++++++++++++++++++ ...1 \353\235\274\354\235\264\353\270\214.py" | 12 ++++++++ 3 files changed, 55 insertions(+) create mode 100644 "syheo/codingTest1/11-4-\353\247\214\353\223\244 \354\210\230 \354\227\206\353\212\224 \352\270\210\354\225\241.py" create mode 100644 "syheo/codingTest1/11-5-\353\263\274\353\247\201\352\263\265 \352\263\240\353\245\264\352\270\260.py" create mode 100644 "syheo/codingTest1/11-6-\353\254\264\354\247\200\354\235\230 \353\250\271\353\260\251 \353\235\274\354\235\264\353\270\214.py" diff --git "a/syheo/codingTest1/11-4-\353\247\214\353\223\244 \354\210\230 \354\227\206\353\212\224 \352\270\210\354\225\241.py" "b/syheo/codingTest1/11-4-\353\247\214\353\223\244 \354\210\230 \354\227\206\353\212\224 \352\270\210\354\225\241.py" new file mode 100644 index 0000000..da0c59d --- /dev/null +++ "b/syheo/codingTest1/11-4-\353\247\214\353\223\244 \354\210\230 \354\227\206\353\212\224 \352\270\210\354\225\241.py" @@ -0,0 +1,15 @@ +#CH11 그리디 기출 +#예제 11-4 +#만들 수 없는 금액 + +N = int(input()) +coins = list(map(int,input().split())) +answer = 1 +coins.sort() +for i in range(N): + if coins[i]<=answer: + answer+=coins[i] + else: + print(answer) + + diff --git "a/syheo/codingTest1/11-5-\353\263\274\353\247\201\352\263\265 \352\263\240\353\245\264\352\270\260.py" "b/syheo/codingTest1/11-5-\353\263\274\353\247\201\352\263\265 \352\263\240\353\245\264\352\270\260.py" new file mode 100644 index 0000000..c8f9ece --- /dev/null +++ "b/syheo/codingTest1/11-5-\353\263\274\353\247\201\352\263\265 \352\263\240\353\245\264\352\270\260.py" @@ -0,0 +1,28 @@ +#CH11 그리디 기출 +#예제 11-5 +#볼링공 고르기 + +#N은 볼링공 갯수, M은 공의 최대 무게 +N , M = map(int,input().split()) + +balls = list(map(int,input().split())) +balls.sort() +sum = 0 +postSum = 0 +curSum = 0 +cur = 0 +#현재 공 무게와 다른 이전 공의 갯수 * 현재 공 무게의 갯수를 sum에 더해나감 +for ball in balls: + if ball!=cur: + cur = ball + sum+=curSum*postSum + postSum+=curSum + curSum=1 + else: + curSum+=1 +sum+=curSum*postSum +print(sum) + + + + diff --git "a/syheo/codingTest1/11-6-\353\254\264\354\247\200\354\235\230 \353\250\271\353\260\251 \353\235\274\354\235\264\353\270\214.py" "b/syheo/codingTest1/11-6-\353\254\264\354\247\200\354\235\230 \353\250\271\353\260\251 \353\235\274\354\235\264\353\270\214.py" new file mode 100644 index 0000000..528789f --- /dev/null +++ "b/syheo/codingTest1/11-6-\353\254\264\354\247\200\354\235\230 \353\250\271\353\260\251 \353\235\274\354\235\264\353\270\214.py" @@ -0,0 +1,12 @@ +#CH11 그리디 기출 +#예제 11-6 +#무지의 먹방 라이브 + + +arr = [1,2,3,4] + +for a in arr: + print(a) + if a==2: + + arr.pop(arr.index(a)) From b35d9e70334a7d79e9f66ec0cbed6113f6cc07a4 Mon Sep 17 00:00:00 2001 From: heoseungyeon Date: Tue, 13 Apr 2021 09:06:12 +0900 Subject: [PATCH 2/7] 11-6 fix --- ...1 \353\235\274\354\235\264\353\270\214.py" | 36 +++++++++++++++---- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git "a/syheo/codingTest1/11-6-\353\254\264\354\247\200\354\235\230 \353\250\271\353\260\251 \353\235\274\354\235\264\353\270\214.py" "b/syheo/codingTest1/11-6-\353\254\264\354\247\200\354\235\230 \353\250\271\353\260\251 \353\235\274\354\235\264\353\270\214.py" index 528789f..531d30b 100644 --- "a/syheo/codingTest1/11-6-\353\254\264\354\247\200\354\235\230 \353\250\271\353\260\251 \353\235\274\354\235\264\353\270\214.py" +++ "b/syheo/codingTest1/11-6-\353\254\264\354\247\200\354\235\230 \353\250\271\353\260\251 \353\235\274\354\235\264\353\270\214.py" @@ -2,11 +2,33 @@ #예제 11-6 #무지의 먹방 라이브 +def solution(food_times, k): + answer = 0 + rotation = 1 + curIdx= 0 + size = len(food_times) + #계수 정렬 + idxSorted = [0 for i in range(100000001)] + for i in range(size): + idxSorted[food_times[i]]+=1 + #퀵 정렬 + #food_times.sort() + while True: + if size<=0: + answer = -1 + break + elif k//size==0: + answer = k+1 + break + else: + k-=size + #퀵정렬시 + # while food_times[curIdx]==rotation: + # size-=1 + # curIdx+=1 + size-=idxSorted[rotation] + rotation+=1 + + -arr = [1,2,3,4] - -for a in arr: - print(a) - if a==2: - - arr.pop(arr.index(a)) + return answer \ No newline at end of file From 95564ee57d255f9ec5575987b1ab8afcf63787c8 Mon Sep 17 00:00:00 2001 From: heoseungyeon Date: Tue, 13 Apr 2021 16:49:57 +0900 Subject: [PATCH 3/7] 11-6 fix --- ...1 \353\235\274\354\235\264\353\270\214.py" | 53 +++++++++++-------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git "a/syheo/codingTest1/11-6-\353\254\264\354\247\200\354\235\230 \353\250\271\353\260\251 \353\235\274\354\235\264\353\270\214.py" "b/syheo/codingTest1/11-6-\353\254\264\354\247\200\354\235\230 \353\250\271\353\260\251 \353\235\274\354\235\264\353\270\214.py" index 531d30b..bda2e40 100644 --- "a/syheo/codingTest1/11-6-\353\254\264\354\247\200\354\235\230 \353\250\271\353\260\251 \353\235\274\354\235\264\353\270\214.py" +++ "b/syheo/codingTest1/11-6-\353\254\264\354\247\200\354\235\230 \353\250\271\353\260\251 \353\235\274\354\235\264\353\270\214.py" @@ -1,34 +1,41 @@ #CH11 그리디 기출 #예제 11-6 #무지의 먹방 라이브 +import heapq +# food_times = [3,1,2] +# k=5 +# food_times = [3, 2, 2, 1, 1, 2, 4, 5] +# k= 12 +food_times = [2, 2, 2, 1, 1, 2, 2, 2] +k= 14 def solution(food_times, k): answer = 0 + rest = k + length = len(food_times) rotation = 1 - curIdx= 0 - size = len(food_times) - #계수 정렬 - idxSorted = [0 for i in range(100000001)] - for i in range(size): - idxSorted[food_times[i]]+=1 - #퀵 정렬 - #food_times.sort() + q = [] + for i in range(length): + heapq.heappush(q,(food_times[i],i)) + while True: - if size<=0: - answer = -1 - break - elif k//size==0: - answer = k+1 - break - else: - k-=size - #퀵정렬시 - # while food_times[curIdx]==rotation: - # size-=1 - # curIdx+=1 - size-=idxSorted[rotation] + if q and rest//len(q): + rest-=1*len(q) + while q and q[0][0]==rotation: + heapq.heappop(q) rotation+=1 + else: + q.sort(key=lambda x : x[1]) - + if len(q)==0: + answer = -1 + elif rest>=len(q): + answer = q[rest-len(q)][1] + else: + answer = q[rest][1]+1 + break + + return answer + - return answer \ No newline at end of file +print(solution(food_times,k)) \ No newline at end of file From 46eddd71e4f1ba5b4b4e9019241ae6e2a19ea45a Mon Sep 17 00:00:00 2001 From: heoseungyeon Date: Tue, 13 Apr 2021 17:19:55 +0900 Subject: [PATCH 4/7] 11-6 fix --- ...1 \353\235\274\354\235\264\353\270\214.py" | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git "a/syheo/codingTest1/11-6-\353\254\264\354\247\200\354\235\230 \353\250\271\353\260\251 \353\235\274\354\235\264\353\270\214.py" "b/syheo/codingTest1/11-6-\353\254\264\354\247\200\354\235\230 \353\250\271\353\260\251 \353\235\274\354\235\264\353\270\214.py" index bda2e40..4509dea 100644 --- "a/syheo/codingTest1/11-6-\353\254\264\354\247\200\354\235\230 \353\250\271\353\260\251 \353\235\274\354\235\264\353\270\214.py" +++ "b/syheo/codingTest1/11-6-\353\254\264\354\247\200\354\235\230 \353\250\271\353\260\251 \353\235\274\354\235\264\353\270\214.py" @@ -1,22 +1,28 @@ #CH11 그리디 기출 #예제 11-6 #무지의 먹방 라이브 +#카카오 신입 공채 2019 + +#엄청 오래 걸림 +#효율성 있게 하기 어려웠음. import heapq # food_times = [3,1,2] # k=5 -# food_times = [3, 2, 2, 1, 1, 2, 4, 5] -# k= 12 -food_times = [2, 2, 2, 1, 1, 2, 2, 2] -k= 14 +food_times = [3, 2, 2, 1, 1, 2, 4, 5] +k= 12 +# food_times = [2, 2, 2, 1, 1, 2, 2, 2] +# k= 14 def solution(food_times, k): answer = 0 rest = k length = len(food_times) rotation = 1 - q = [] + q = [0 for _ in range(length)] for i in range(length): - heapq.heappush(q,(food_times[i],i)) + q[i]=(food_times[i],i) + + heapq.heapify(q) while True: if q and rest//len(q): @@ -26,11 +32,9 @@ def solution(food_times, k): rotation+=1 else: q.sort(key=lambda x : x[1]) - + #큐가 비어있을 떄 (더 이상 먹을 음식이 없음) if len(q)==0: answer = -1 - elif rest>=len(q): - answer = q[rest-len(q)][1] else: answer = q[rest][1]+1 break From fc8e3a54a91c60492226f8bdb4282fe5c13ec24d Mon Sep 17 00:00:00 2001 From: heoseungyeon Date: Mon, 19 Apr 2021 13:07:12 +0900 Subject: [PATCH 5/7] fix --- ...1 \353\235\274\354\235\264\353\270\214.py" | 58 +++++++++++-------- 1 file changed, 34 insertions(+), 24 deletions(-) diff --git "a/syheo/codingTest1/11-6-\353\254\264\354\247\200\354\235\230 \353\250\271\353\260\251 \353\235\274\354\235\264\353\270\214.py" "b/syheo/codingTest1/11-6-\353\254\264\354\247\200\354\235\230 \353\250\271\353\260\251 \353\235\274\354\235\264\353\270\214.py" index 4509dea..25617fa 100644 --- "a/syheo/codingTest1/11-6-\353\254\264\354\247\200\354\235\230 \353\250\271\353\260\251 \353\235\274\354\235\264\353\270\214.py" +++ "b/syheo/codingTest1/11-6-\353\254\264\354\247\200\354\235\230 \353\250\271\353\260\251 \353\235\274\354\235\264\353\270\214.py" @@ -7,37 +7,47 @@ #효율성 있게 하기 어려웠음. import heapq +def keys(a): + return a[1] + # food_times = [3,1,2] # k=5 -food_times = [3, 2, 2, 1, 1, 2, 4, 5] -k= 12 -# food_times = [2, 2, 2, 1, 1, 2, 2, 2] +# food_times = [3, 2, 2, 1, 1, 2, 4, 5] +# k= 12 +# food_times = [2, 2, 2, 1, 1, 2, 2, 2] # k= 14 +# food_times = [7, 8, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2] +# k = 35 +food_times = [1, 6, 9, 3, 2, 5, 3, 2, 4, 4, 4, 7] +k = 26 def solution(food_times, k): answer = 0 - rest = k - length = len(food_times) + fixLength = len(food_times) + length = fixLength rotation = 1 - q = [0 for _ in range(length)] - for i in range(length): - q[i]=(food_times[i],i) + curIdx = 0 + cnt = 0 + for i in range(fixLength): + food_times[i]=(food_times[i],i) + food_times.sort() + + while k//length: + k-=length + cnt+=curIdx + while food_times[curIdx]==rotation: + curIdx+=1 + length-=1 + if curIdx==len(food_times): + return -1 + rotation+=1 + print(curIdx) + + food_times.sort(key=keys) + print(food_times,k+cnt) + answer = food_times[(k+cnt+1)%fixLength][1]+1 - heapq.heapify(q) - - while True: - if q and rest//len(q): - rest-=1*len(q) - while q and q[0][0]==rotation: - heapq.heappop(q) - rotation+=1 - else: - q.sort(key=lambda x : x[1]) - #큐가 비어있을 떄 (더 이상 먹을 음식이 없음) - if len(q)==0: - answer = -1 - else: - answer = q[rest][1]+1 - break + answer += 1 + return answer From a3d65d42eaf2b3c26cb81c417a758a4e41bdfb3b Mon Sep 17 00:00:00 2001 From: heoseungyeon Date: Tue, 20 Apr 2021 09:27:55 +0900 Subject: [PATCH 6/7] fix --- ...1 \353\235\274\354\235\264\353\270\214.py" | 71 ++++++++----------- syheo/codingTest1/Baekjoon/2437.py | 18 +++++ 2 files changed, 46 insertions(+), 43 deletions(-) create mode 100644 syheo/codingTest1/Baekjoon/2437.py diff --git "a/syheo/codingTest1/11-6-\353\254\264\354\247\200\354\235\230 \353\250\271\353\260\251 \353\235\274\354\235\264\353\270\214.py" "b/syheo/codingTest1/11-6-\353\254\264\354\247\200\354\235\230 \353\250\271\353\260\251 \353\235\274\354\235\264\353\270\214.py" index 25617fa..8f43645 100644 --- "a/syheo/codingTest1/11-6-\353\254\264\354\247\200\354\235\230 \353\250\271\353\260\251 \353\235\274\354\235\264\353\270\214.py" +++ "b/syheo/codingTest1/11-6-\353\254\264\354\247\200\354\235\230 \353\250\271\353\260\251 \353\235\274\354\235\264\353\270\214.py" @@ -6,50 +6,35 @@ #엄청 오래 걸림 #효율성 있게 하기 어려웠음. import heapq +from collections import deque -def keys(a): - return a[1] - -# food_times = [3,1,2] -# k=5 -# food_times = [3, 2, 2, 1, 1, 2, 4, 5] -# k= 12 -# food_times = [2, 2, 2, 1, 1, 2, 2, 2] -# k= 14 -# food_times = [7, 8, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2] -# k = 35 -food_times = [1, 6, 9, 3, 2, 5, 3, 2, 4, 4, 4, 7] -k = 26 def solution(food_times, k): answer = 0 - fixLength = len(food_times) - length = fixLength + rest = k + length = len(food_times) rotation = 1 - curIdx = 0 - cnt = 0 - for i in range(fixLength): - food_times[i]=(food_times[i],i) - food_times.sort() - - while k//length: - k-=length - cnt+=curIdx - while food_times[curIdx]==rotation: - curIdx+=1 - length-=1 - if curIdx==len(food_times): - return -1 - rotation+=1 - print(curIdx) - - food_times.sort(key=keys) - print(food_times,k+cnt) - answer = food_times[(k+cnt+1)%fixLength][1]+1 - - answer += 1 - - - return answer - - -print(solution(food_times,k)) \ No newline at end of file + q = [] + #(식사시간, 음식 위치)를 우선순위 큐에 넣어줌 O(logN)xN + for i in range(length): + heapq.heappush(q,(food_times[i],i)) + while True: + #큐에 음식이 있고, 로테이션 1회 진행 가능시 + if q and rest//len(q): + #남은 횟수 + rest-=1*len(q) + #큐에 음식이 있고, rotation만큼 돌릴 수 있는 음식 제거 + while q and q[0][0]==rotation: + heapq.heappop(q) + #로테이션 1증가 + rotation+=1 + else: + #음식 번호로 정렬 O(NlogN) + q.sort(key=lambda x : x[1]) + #큐에 남은 음식이 없을 경우 + if len(q)==0: + answer = -1 + else: + answer = q[rest][1]+1 + break + + return answer \ No newline at end of file diff --git a/syheo/codingTest1/Baekjoon/2437.py b/syheo/codingTest1/Baekjoon/2437.py new file mode 100644 index 0000000..caba08a --- /dev/null +++ b/syheo/codingTest1/Baekjoon/2437.py @@ -0,0 +1,18 @@ +#solved.ac +#골드3 +#그리디 +#저울 +#2437 +import sys +input = sys.stdin.readline + +N = int(input()) +ans = 1 +chuList = list(map(int,input().split())) +chuList.sort() +for chu in chuList: + if ans Date: Tue, 20 Apr 2021 18:01:36 +0900 Subject: [PATCH 7/7] =?UTF-8?q?=EB=AC=B4=EC=A7=80=EC=9D=98=20=EB=A8=B9?= =?UTF-8?q?=EB=B0=A9=20=EB=9D=BC=EC=9D=B4=EB=B8=8C=20=EC=B5=9C=EC=A2=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...1 \353\235\274\354\235\264\353\270\214.py" | 45 ++++++++++--------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git "a/syheo/codingTest1/11-6-\353\254\264\354\247\200\354\235\230 \353\250\271\353\260\251 \353\235\274\354\235\264\353\270\214.py" "b/syheo/codingTest1/11-6-\353\254\264\354\247\200\354\235\230 \353\250\271\353\260\251 \353\235\274\354\235\264\353\270\214.py" index 8f43645..74ac7ca 100644 --- "a/syheo/codingTest1/11-6-\353\254\264\354\247\200\354\235\230 \353\250\271\353\260\251 \353\235\274\354\235\264\353\270\214.py" +++ "b/syheo/codingTest1/11-6-\353\254\264\354\247\200\354\235\230 \353\250\271\353\260\251 \353\235\274\354\235\264\353\270\214.py" @@ -10,31 +10,32 @@ def solution(food_times, k): answer = 0 - rest = k - length = len(food_times) - rotation = 1 + rest = k # 남은 시간 + length = len(food_times) + #k 시간 안에 현재 음식을 다먹을 수 있을 경우 + if sum(food_times)<=k: + return -1 + q = [] #(식사시간, 음식 위치)를 우선순위 큐에 넣어줌 O(logN)xN for i in range(length): heapq.heappush(q,(food_times[i],i)) - while True: - #큐에 음식이 있고, 로테이션 1회 진행 가능시 - if q and rest//len(q): - #남은 횟수 - rest-=1*len(q) - #큐에 음식이 있고, rotation만큼 돌릴 수 있는 음식 제거 - while q and q[0][0]==rotation: - heapq.heappop(q) - #로테이션 1증가 - rotation+=1 - else: - #음식 번호로 정렬 O(NlogN) - q.sort(key=lambda x : x[1]) - #큐에 남은 음식이 없을 경우 - if len(q)==0: - answer = -1 - else: - answer = q[rest][1]+1 - break + + rotation = q[0][0] # 진행할 로테이션 횟수 + prev_rot = 0 + #큐에 음식이 있고, 로테이션 진행 가능시 + while rest>=len(q)*(rotation-prev_rot): + #남은 횟수 + rest-=(rotation-prev_rot)*len(q) + #rotation만큼 돌릴 수 있는 음식 제거 + while q[0][0]==rotation: + heapq.heappop(q) + #로테이션 갱신 + prev_rot = rotation + rotation = q[0][0] + + #음식 번호로 정렬 O(NlogN) + q.sort(key=lambda x : x[1]) + answer = q[rest%len(q)][1]+1 return answer \ No newline at end of file