Skip to content

Commit c2aa2ab

Browse files
committed
contest 350
1 parent 5b87b91 commit c2aa2ab

File tree

1 file changed

+370
-0
lines changed

1 file changed

+370
-0
lines changed

00. Contests/26. LeetCode W-350.ipynb

Lines changed: 370 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,370 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 2,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"from typing import List"
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": 7,
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"class Solution:\n",
19+
" def findValueOfPartition(self, nums: List[int]) -> int:\n",
20+
" #nums = set(nums)\n",
21+
" nums = sorted(nums)\n",
22+
" length = len(nums)\n",
23+
" min_ = float('inf')\n",
24+
" for i in range(length-1):\n",
25+
" min_ = min(min_, abs(nums[i] - nums[i+1]))\n",
26+
" return min_"
27+
]
28+
},
29+
{
30+
"cell_type": "code",
31+
"execution_count": 8,
32+
"metadata": {},
33+
"outputs": [
34+
{
35+
"data": {
36+
"text/plain": [
37+
"0"
38+
]
39+
},
40+
"execution_count": 8,
41+
"metadata": {},
42+
"output_type": "execute_result"
43+
}
44+
],
45+
"source": [
46+
"nums = [1,3,2,4]\n",
47+
"nums = [100,1,10]\n",
48+
"nums = [2,2,2,2,2,3,4,5,5,6,7,7,5,4,1212,1435,876543,65432,2345,76543]\n",
49+
"nums = [84,11,100,100,75]\n",
50+
"Solution().findValueOfPartition(nums)"
51+
]
52+
},
53+
{
54+
"cell_type": "code",
55+
"execution_count": null,
56+
"metadata": {},
57+
"outputs": [],
58+
"source": []
59+
},
60+
{
61+
"cell_type": "code",
62+
"execution_count": 67,
63+
"metadata": {},
64+
"outputs": [],
65+
"source": [
66+
"class Solution:\n",
67+
" def specialPerm(self, nums: List[int]) -> int:\n",
68+
" \n",
69+
" def backtrack(curr_num, used):\n",
70+
" if used == (1 << len(nums)) - 1: return 1\n",
71+
"\n",
72+
" if (curr_num, used) in memo: return memo[(curr_num, used)]\n",
73+
"\n",
74+
" count = 0\n",
75+
" for i in range(len(nums)):\n",
76+
" if (not used & (1 << i)) and (nums[i] % curr_num == 0 or curr_num % nums[i] == 0):\n",
77+
" count += backtrack(nums[i], used | (1 << i))\n",
78+
"\n",
79+
" memo[(curr_num, used)] = count\n",
80+
" return count\n",
81+
"\n",
82+
" MOD = int(1e9) + 7\n",
83+
" nums.sort()\n",
84+
" memo = {}\n",
85+
"\n",
86+
" total_permutations = 0\n",
87+
" for i in range(len(nums)):\n",
88+
" total_permutations += backtrack(nums[i], 1 << i)\n",
89+
" total_permutations %= MOD\n",
90+
"\n",
91+
" return total_permutations"
92+
]
93+
},
94+
{
95+
"cell_type": "code",
96+
"execution_count": 68,
97+
"metadata": {},
98+
"outputs": [
99+
{
100+
"data": {
101+
"text/plain": [
102+
"0"
103+
]
104+
},
105+
"execution_count": 68,
106+
"metadata": {},
107+
"output_type": "execute_result"
108+
}
109+
],
110+
"source": [
111+
"#nums = [1,4,3]\n",
112+
"#nums = [2,3,6,7,8,9]\n",
113+
"Solution().specialPerm(nums)"
114+
]
115+
},
116+
{
117+
"cell_type": "code",
118+
"execution_count": 88,
119+
"metadata": {},
120+
"outputs": [],
121+
"source": [
122+
"class Solution:\n",
123+
" def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:\n",
124+
" \n",
125+
" mileage = 10\n",
126+
" total_fuel = 0\n",
127+
" covered = 0\n",
128+
" while mainTank>=1:\n",
129+
" if mainTank >= 5:\n",
130+
" total_fuel +=5\n",
131+
" mainTank -=5\n",
132+
" covered = 5\n",
133+
" else:\n",
134+
" total_fuel += mainTank\n",
135+
" covered = mainTank\n",
136+
" mainTank = 0\n",
137+
" if covered == 5 and additionalTank>=1:\n",
138+
" additionalTank-=1\n",
139+
" covered = 0\n",
140+
" mainTank +=1\n",
141+
" \n",
142+
" return total_fuel*mileage"
143+
]
144+
},
145+
{
146+
"cell_type": "code",
147+
"execution_count": 89,
148+
"metadata": {},
149+
"outputs": [
150+
{
151+
"data": {
152+
"text/plain": [
153+
"60"
154+
]
155+
},
156+
"execution_count": 89,
157+
"metadata": {},
158+
"output_type": "execute_result"
159+
}
160+
],
161+
"source": [
162+
"mainTank = 5; additionalTank = 10\n",
163+
"#mainTank = 1; additionalTank = 2\n",
164+
"mainTank = 5; additionalTank = 1\n",
165+
"Solution().distanceTraveled(mainTank, additionalTank)"
166+
]
167+
},
168+
{
169+
"cell_type": "code",
170+
"execution_count": null,
171+
"metadata": {},
172+
"outputs": [],
173+
"source": []
174+
},
175+
{
176+
"cell_type": "code",
177+
"execution_count": 165,
178+
"metadata": {},
179+
"outputs": [],
180+
"source": [
181+
"from functools import cache\n",
182+
"\n",
183+
"class Solution:\n",
184+
" def paintWalls(self, cost: List[int], time: List[int]) -> int:\n",
185+
" \n",
186+
" n=len(cost)\n",
187+
" \n",
188+
" @cache\n",
189+
" def dp(idx,t):\n",
190+
" if idx==n: return 0 if t>=0 else sys.maxsize\n",
191+
" #this index painted by paid painter\n",
192+
" ans=cost[idx] + dp(idx+1, min(n+1, t+time[idx]))\n",
193+
" #free painter\n",
194+
" ans = min(ans, dp(idx+1, t-1))\n",
195+
" return ans\n",
196+
" return dp(0,0)"
197+
]
198+
},
199+
{
200+
"cell_type": "code",
201+
"execution_count": 166,
202+
"metadata": {},
203+
"outputs": [
204+
{
205+
"data": {
206+
"text/plain": [
207+
"55"
208+
]
209+
},
210+
"execution_count": 166,
211+
"metadata": {},
212+
"output_type": "execute_result"
213+
}
214+
],
215+
"source": [
216+
"cost = [1,2,3,2]; time = [1,2,3,2]\n",
217+
"cost = [2,3,4,2]; time = [1,1,1,1]\n",
218+
"cost = [26,53,10,24,25,20,63,51]; time = [1,1,1,1,2,2,2,1]\n",
219+
"Solution().paintWalls(cost, time)"
220+
]
221+
},
222+
{
223+
"cell_type": "code",
224+
"execution_count": 158,
225+
"metadata": {},
226+
"outputs": [],
227+
"source": [
228+
"from typing import List\n",
229+
"\n",
230+
"class Solution:\n",
231+
" def paintWalls(self, cost: List[int], time: List[int]) -> int:\n",
232+
" n = len(cost)\n",
233+
"\n",
234+
" # Sort the walls based on cost in ascending order\n",
235+
" sorted_pairs = sorted(zip(cost, time))\n",
236+
"\n",
237+
" dp = [float('inf')] * (n + 1)\n",
238+
" dp[0] = 0\n",
239+
"\n",
240+
" for i in range(1, n + 1):\n",
241+
" curr_cost, curr_time = sorted_pairs[i - 1]\n",
242+
" for j in range(i, 0, -1):\n",
243+
" if curr_cost >= dp[j - 1]:\n",
244+
" dp[j] = min(dp[j], dp[j - 1] + curr_time)\n",
245+
"\n",
246+
" return dp[n]\n"
247+
]
248+
},
249+
{
250+
"cell_type": "code",
251+
"execution_count": 113,
252+
"metadata": {},
253+
"outputs": [
254+
{
255+
"data": {
256+
"text/plain": [
257+
"[1, 2, 1, 2, 1, 1, 1, 2]"
258+
]
259+
},
260+
"execution_count": 113,
261+
"metadata": {},
262+
"output_type": "execute_result"
263+
}
264+
],
265+
"source": [
266+
"[j for i,j in sorted(zip(cost, time))]\n",
267+
"# cost = [10, 20, 24, 25, 26, 51, 53, 63], time = [1, 2, 1, 2, 1, 1, 1, 2]"
268+
]
269+
},
270+
{
271+
"cell_type": "code",
272+
"execution_count": 109,
273+
"metadata": {},
274+
"outputs": [
275+
{
276+
"name": "stdout",
277+
"output_type": "stream",
278+
"text": [
279+
"11\n"
280+
]
281+
}
282+
],
283+
"source": [
284+
"def minCost(cost, time):\n",
285+
" n = len(cost)\n",
286+
"\n",
287+
" # Initialize the dynamic programming arrays\n",
288+
" dp = [float('inf')] * (n + 1)\n",
289+
" dp_free = [float('inf')] * (n + 1)\n",
290+
" dp[0] = 0\n",
291+
" dp_free[0] = 0\n",
292+
"\n",
293+
" for i in range(1, n + 1):\n",
294+
" # Calculate the cost when the paid painter is used\n",
295+
" dp[i] = min(dp[i], dp[i - 1] + cost[i - 1])\n",
296+
"\n",
297+
" # Calculate the cost when the free painter is used\n",
298+
" dp_free[i] = min(dp_free[i], dp[i - 1], dp_free[i - 1]) + time[i - 1]\n",
299+
"\n",
300+
" return min(dp[n], dp_free[n])\n",
301+
"\n",
302+
"cost = [26, 53, 10, 24, 25, 20, 63, 51]\n",
303+
"time = [1, 1, 1, 1, 2, 2, 2, 1]\n",
304+
"result = minCost(cost, time)\n",
305+
"print(result)\n"
306+
]
307+
},
308+
{
309+
"cell_type": "code",
310+
"execution_count": 96,
311+
"metadata": {},
312+
"outputs": [
313+
{
314+
"name": "stdout",
315+
"output_type": "stream",
316+
"text": [
317+
"[83, 83, 97, 71, 71, 86, 92, 85, 8, 1, 52, 55, 11, 79, 56, 3, 13, 95, 76, 8, 56, 7, 94, 77, 41, 43, 83, 54, 93, 14, 57, 101, 2, 33, 85, 40, 21, 57, 30, 27, 48, 4, 78, 104, 57, 78, 100, 40, 9, 3]\n"
318+
]
319+
}
320+
],
321+
"source": [
322+
"# 1 <= cost.length <= 500\n",
323+
"# cost.length == time.length\n",
324+
"# 1 <= cost[i] <= 106\n",
325+
"# 1 <= time[i] <= 500\n",
326+
"\n",
327+
"print([random.randrange(1,106) for i in range(50)])"
328+
]
329+
},
330+
{
331+
"cell_type": "code",
332+
"execution_count": 97,
333+
"metadata": {},
334+
"outputs": [
335+
{
336+
"name": "stdout",
337+
"output_type": "stream",
338+
"text": [
339+
"[475, 229, 141, 89, 101, 226, 14, 220, 51, 394, 149, 395, 335, 37, 111, 177, 266, 246, 22, 422, 298, 151, 381, 399, 193, 304, 136, 296, 84, 175, 424, 47, 90, 235, 471, 428, 38, 137, 137, 240, 192, 424, 408, 303, 450, 407, 168, 419, 494, 45]\n"
340+
]
341+
}
342+
],
343+
"source": [
344+
"print([random.randrange(1,500) for i in range(50)])"
345+
]
346+
}
347+
],
348+
"metadata": {
349+
"kernelspec": {
350+
"display_name": "base",
351+
"language": "python",
352+
"name": "python3"
353+
},
354+
"language_info": {
355+
"codemirror_mode": {
356+
"name": "ipython",
357+
"version": 3
358+
},
359+
"file_extension": ".py",
360+
"mimetype": "text/x-python",
361+
"name": "python",
362+
"nbconvert_exporter": "python",
363+
"pygments_lexer": "ipython3",
364+
"version": "3.9.13"
365+
},
366+
"orig_nbformat": 4
367+
},
368+
"nbformat": 4,
369+
"nbformat_minor": 2
370+
}

0 commit comments

Comments
 (0)