Skip to content

Commit 37880be

Browse files
Add files via upload
1 parent 16ecb43 commit 37880be

File tree

1 file changed

+176
-6
lines changed

1 file changed

+176
-6
lines changed

chpt11_greedy_problem.ipynb

+176-6
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,31 @@
3838
"print(result)"
3939
]
4040
},
41+
{
42+
"cell_type": "code",
43+
"execution_count": null,
44+
"id": "eb1f025b",
45+
"metadata": {},
46+
"outputs": [],
47+
"source": [
48+
"# Q1. answer \n",
49+
"# 현재 그룹에 포함된 모험가의 수가 현재의 공포도 이상이라면, 그룹 결성\n",
50+
"n = int(input())\n",
51+
"data = list(map(int, input().split()))\n",
52+
"data.sort()\n",
53+
"\n",
54+
"result = 0\n",
55+
"count = 0\n",
56+
"\n",
57+
"for i in data:\n",
58+
" count+=1\n",
59+
" if count >= i:\n",
60+
" result+=1\n",
61+
" count = 0\n",
62+
"\n",
63+
"print(result)"
64+
]
65+
},
4166
{
4267
"cell_type": "code",
4368
"execution_count": 6,
@@ -70,6 +95,29 @@
7095
"print(result)"
7196
]
7297
},
98+
{
99+
"cell_type": "code",
100+
"execution_count": null,
101+
"id": "f7c8b9fa",
102+
"metadata": {},
103+
"outputs": [],
104+
"source": [
105+
"# Q3. answer\n",
106+
"# 두 수에 대하여 연산을 수행할 때, 두 수 중에서 하나라도 1 이하인 경우에는 더하며, 두 수가 모두 2이상인 경우에는 곱하면 된다.\n",
107+
"\n",
108+
"data = input()\n",
109+
"result = int(data[0])\n",
110+
"\n",
111+
"for i in range(1, len(data)):\n",
112+
" num = int(data[i])\n",
113+
" if num <= 1 or result <= 1:\n",
114+
" result+=num\n",
115+
" else:\n",
116+
" result*=num\n",
117+
" \n",
118+
"print(result)"
119+
]
120+
},
73121
{
74122
"cell_type": "code",
75123
"execution_count": 16,
@@ -104,6 +152,34 @@
104152
"print(result)"
105153
]
106154
},
155+
{
156+
"cell_type": "code",
157+
"execution_count": null,
158+
"id": "678a3753",
159+
"metadata": {},
160+
"outputs": [],
161+
"source": [
162+
"# Q3. answer\n",
163+
"# 전부 0으로 바꾸는 경우와 전부 1로 바꾸는 경우 중에서 더 적은 횟수를 가지는 경우를 계산\n",
164+
"data = int(input())\n",
165+
"count0 = 0\n",
166+
"count1 = 0\n",
167+
"\n",
168+
"if data[0] == '1':\n",
169+
" count0+=1\n",
170+
"else:\n",
171+
" count1+=1\n",
172+
" \n",
173+
"for i in range(len(data)-1):\n",
174+
" if data[i] != data[i+1]:\n",
175+
" if data[i+1]=='1':\n",
176+
" count0+=1\n",
177+
" else:\n",
178+
" count1 += 1\n",
179+
" \n",
180+
"print(min(count0, count1))"
181+
]
182+
},
107183
{
108184
"cell_type": "code",
109185
"execution_count": 3,
@@ -150,17 +226,52 @@
150226
},
151227
{
152228
"cell_type": "code",
153-
"execution_count": 8,
229+
"execution_count": 1,
230+
"id": "6e72da85",
231+
"metadata": {},
232+
"outputs": [
233+
{
234+
"name": "stdout",
235+
"output_type": "stream",
236+
"text": [
237+
"5\n",
238+
"3 2 1 1 9\n",
239+
"8\n"
240+
]
241+
}
242+
],
243+
"source": [
244+
"# Q4. answer\n",
245+
"# 단순히 동전을 화폐 단위 기준으로 정렬한 뒤에, 화폐 단위가 작은 동전부터 하나씩 확인하면서\n",
246+
"# 타겟 변수를 업데이트하는 방법으로 최적의 해를 계산\n",
247+
"\n",
248+
"n = int(input())\n",
249+
"data = list(map(int, input().split()))\n",
250+
"data.sort()\n",
251+
"\n",
252+
"target = 1\n",
253+
"for x in data:\n",
254+
" if target < x:\n",
255+
" break\n",
256+
" target += x\n",
257+
"\n",
258+
"print(target)\n",
259+
"# must review"
260+
]
261+
},
262+
{
263+
"cell_type": "code",
264+
"execution_count": 3,
154265
"id": "5c03265c",
155266
"metadata": {},
156267
"outputs": [
157268
{
158269
"name": "stdout",
159270
"output_type": "stream",
160271
"text": [
161-
"8 5\n",
162-
"1 5 4 3 2 4 5 2\n",
163-
"25\n"
272+
"5 3\n",
273+
"1 3 2 3 2\n",
274+
"8\n"
164275
]
165276
}
166277
],
@@ -181,6 +292,33 @@
181292
"print(result)"
182293
]
183294
},
295+
{
296+
"cell_type": "code",
297+
"execution_count": null,
298+
"id": "8bfd2a3d",
299+
"metadata": {},
300+
"outputs": [],
301+
"source": [
302+
"# Q5. answer\n",
303+
"# A가 특정한 무게의 볼링 공을 선택했을 때, 이어서 B가 볼링공을 선택하는 경우를 차례대로 계산\n",
304+
"n, m = map(int, input().split())\n",
305+
"data = list(map(int, input().split()))\n",
306+
"\n",
307+
"array = [0]*11\n",
308+
"\n",
309+
"for x in data:\n",
310+
" array[x]+=1 # 각 무게에 해당하는 볼링공의 개수 카운트\n",
311+
" \n",
312+
"result = 0\n",
313+
"\n",
314+
"for i in range(1, m+1):\n",
315+
" n -= array[i] # 무게가 i인 볼링공의 개수(A가 선택할 수 있는 개수) 제외\n",
316+
" result += array[i]*n # B가 선택하는 경우의 수와 곱하기\n",
317+
"\n",
318+
"print(result)\n",
319+
"# must review!"
320+
]
321+
},
184322
{
185323
"cell_type": "code",
186324
"execution_count": 34,
@@ -258,10 +396,42 @@
258396
{
259397
"cell_type": "code",
260398
"execution_count": null,
261-
"id": "19ecf8ea",
399+
"id": "1929ddf6",
262400
"metadata": {},
263401
"outputs": [],
264-
"source": []
402+
"source": [
403+
"# Q6. answer\n",
404+
"\n",
405+
"import heapq\n",
406+
"\n",
407+
"def solution(food_times, k):\n",
408+
" # 전체 음식을 먹는 시간보다 k가 크거나 같다면 -1\n",
409+
" if sum(food_times) <= k:\n",
410+
" return -1\n",
411+
" \n",
412+
" # 시간이 적은 음식부터 뺴야 하므로 우선순위 큐 이용\n",
413+
" q = []\n",
414+
" for i in range(len(food_times)):\n",
415+
" # (음식 시간, 음식 번호)형태로 우선순위 큐에 삽입\n",
416+
" heapq.heappush((food_times[i], i+1))\n",
417+
" \n",
418+
" sum_val = 0 # 먹기 위해 사용한 시간\n",
419+
" pre = 0 # 직전에 다 먹은 음식 시간\n",
420+
" \n",
421+
" length = len(food_times)\n",
422+
" # sum_val + (현재의 음식 시간 - 이전 음식 시간) * 현재 음식 개수와 k 비교\n",
423+
" while sum_val + ((q[0][0] - pre)*length) <= k:\n",
424+
" now = heapq.heappop(q)[0]\n",
425+
" sum_val += (now-pre) * length\n",
426+
" length -= 1\n",
427+
" pre = now\n",
428+
" \n",
429+
" # 남은 음식 중에서 몇 번째 음식인지 확인하여 출력\n",
430+
" result = sorted(q, key = lambda x: x[1])\n",
431+
" return result[(k-sum_val)%length][1]\n",
432+
"\n",
433+
"# must review!"
434+
]
265435
}
266436
],
267437
"metadata": {

0 commit comments

Comments
 (0)