Skip to content

Commit 1930127

Browse files
committedSep 15, 2024
📹 [tiktok]
1 parent 9f2b97d commit 1930127

File tree

2 files changed

+518
-0
lines changed

2 files changed

+518
-0
lines changed
 
+291
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,291 @@
1+
<div id="main-splitpane-left" class="coding-question__left-pane"><section class="question-view__title-wrapper"><h1 class="question-view__title">4. Maximizing Task Priority</h1></section><section class="question-view__instruction"><div class="candidate-rich-text"><div id="2mbl80d86og-instruction"><style type="text/css">.ps-content-wrapper-v0 div { margin: 0 auto; overflow: auto; } .ps-content-wrapper-v0 div.preheader { display: none; } .ps-content-wrapper-v0 p { white-space: pre-wrap; padding-left: 4px; padding-right: 4px; padding-top: 0px; padding-bottom: 2px; } .ps-content-wrapper-v0 p.section-title { font-weight: bold; padding-bottom: 0px; } .ps-content-wrapper-v0 ol.plain-list, .ps-content-wrapper-v0 ul.plain-list { list-style-type: none; padding: 4px; } .ps-content-wrapper-v0 li { white-space: normal; margin-top: 4px; margin-bottom: 4px; } .ps-content-wrapper-v0 code { color: black; } .ps-content-wrapper-v0 pre { background-color: #f4faff; border: 0; border-radius: 2px; margin: 8px; padding: 10px; } .ps-content-wrapper-v0 pre.scrollable-full-json { overflow-x: scroll; white-space: pre; } .ps-content-wrapper-v0 pre.scrollable-json { height: 300px; overflow-y: scroll; display: inline-grid; white-space: pre-wrap; padding-left: 8px; padding-right: 8px; padding-top: 4px; padding-bottom: 4px; } .ps-content-wrapper-v0 div.equation-parent { width: 400px; text-align: center; border: 1px solid #000; padding: 8px; } .ps-content-wrapper-v0 div.equation-parent.equation { width: 100%; display: inline-block; } .ps-content-wrapper-v0 figure { background-color: transparent; display: table; margin-top: 8px; margin-bottom: 8px; text-align: center; margin-left: auto; margin-right: auto; } .ps-content-wrapper-v0 figcaption { text-align: center; display: table-caption; caption-side: bottom; margin-top: 4px; margin-bottom: 4px; } .ps-content-wrapper-v0 img { width: auto; max-width: 100%; height: auto; } .ps-content-wrapper-v0 details { background-color: transparent; padding-left: 4px; padding-right: 4px; padding-top: 0px; padding-bottom: 2px; } .ps-content-wrapper-v0 details details { padding-left: 8px; padding-right: 8px; } .ps-content-wrapper-v0 details summary { background-color: #39424e; color: white; font-weight: bold; margin-top: 4px; margin-bottom: 4px; padding: 8px; } .ps-content-wrapper-v0 details details summary code { color: black; font-weight: bold; padding-left: 2px; padding-right: 2px; padding-top: 4px; padding-right: 4px; margin-left: 4px; } .ps-content-wrapper-v0 details div.collapsable-details { margin: 0 auto; padding-left: 4px; padding-right: 4px; padding-top: 0px; padding-bottom: 2px; overflow: auto; } .ps-content-wrapper-v0 details div.collapsable-details pre { margin-left: 4px; margin-right: 4px; margin-top: 4px; margin-bottom: 4px; } .ps-content-wrapper-v0 table.normal { border: 1px solid black; border-collapse: collapse; border-color: darkgray; margin: 0 auto; margin-top: 8px; margin-bottom: 8px; padding: 8px; width: 96%; table-layout: fixed; } .ps-content-wrapper-v0 table.normal tbody { display: block; overflow-x: auto; overflow-y: hidden; } .ps-content-wrapper-v0 table.normal tbody tr:first-child th { font-weight: bold; white-space: normal; } .ps-content-wrapper-v0 table.normal tbody tr th, .ps-content-wrapper-v0 table.normal tbody tr td { font-weight: normal; white-space: nowrap; text-align: center; vertical-align: middle; border: 1px solid black; border-color: darkgray; padding: 8px; } .ps-content-wrapper-v0 table.database-table { border-collapse: collapse; border-color: darkgray; border: 1px solid black; width: auto; margin-left: 4px; margin-top: 8px; margin-bottom: 8px; padding: 8px; } .ps-content-wrapper-v0 table.database-table tbody { overflow-x: auto; overflow-y: hidden; border: none; } .ps-content-wrapper-v0 table.database-table tbody tr th, .ps-content-wrapper-v0 table.database-table tbody tr td { font-weight: normal; white-space: nowrap; text-align: center; vertical-align: middle; border: 1px solid black; border-color: darkgray; padding: 8px; } .ps-content-wrapper-v0 table.database-table tbody tr th { font-weight: bold; border: 1px solid black; } .ps-content-wrapper-v0 table.database-table tbody tr:nth-child(2) td { border: 1px solid black; } .ps-content-wrapper-v0 table.database-table tbody tr:nth-child(n+2) td:first-child { border-left-color: black; } .ps-content-wrapper-v0 table.database-table tbody tr:nth-child(n+2) td:last-child { border-right-color: black; } .ps-content-wrapper-v0 table.database-table tbody tr:last-child td { border-bottom-color: black; } .ps-content-wrapper-v0 table.database-table tbody tr td.description { text-align: left; white-space: pre-wrap; } .ps-content-wrapper-v0 table.normal tbody tr th.description { width: 60%; } .ps-content-wrapper-v0 table.function-params tbody tr:first-child td.headers { border-bottom-width: 2px; } .ps-content-wrapper-v0 table.function-params tbody tr:last-child td { border-top-width: 2px; border-top-color: darkgray; } .ps-content-wrapper-v0 table.function-params tbody tr td.headers { width: 25%; font-weight: bold; text-align: center; border: 1px solid black; border-right-width: 2px; border-color: darkgray; } .ps-content-wrapper-v0 table.function-params tbody tr td.params-table-cell { width: 100%; height: 100%; padding: 0px; } .ps-content-wrapper-v0 table.function-params tbody tr td.params-table-cell table.params-table { width: 100%; height: 100%; padding: 0px; margin: 0px; border: 0; } .ps-content-wrapper-v0 table.function-params tbody tr td.params-table-cell table.params-table tbody tr td.code { white-space: normal; } .ps-content-wrapper-v0 table.function-params tbody tr td.params-table-cell table.params-table tbody tr th { border-top: 0; } .ps-content-wrapper-v0 table.function-params tbody tr td.params-table-cell table.params-table tbody tr th:first-child { border-left: 0; } .ps-content-wrapper-v0 table.function-params tbody tr td.params-table-cell table.params-table tbody tr th:last-child { border-right: 0; } .ps-content-wrapper-v0 table.function-params tbody tr td.params-table-cell table.params-table tbody tr:last-child td { border-bottom: 0; border-top-width: 1px; } .ps-content-wrapper-v0 table.function-params tbody tr td.params-table-cell table.params-table tbody tr td:first-child { border-left: 0; } .ps-content-wrapper-v0 table.function-params tbody tr td.params-table-cell table.params-table tbody tr td:last-child { border-right: 0; } .ps-content-wrapper-v0 table.sudoku { border-collapse: collapse; border-color: darkgray; margin: 0 auto; margin-top: 8px; margin-bottom: 8px; padding: 8px; } .ps-content-wrapper-v0 table.sudoku colgroup, tbody { border: 3px solid black; } .ps-content-wrapper-v0 table.sudoku td { border: 1px solid black; height: 25px; width: 25px; text-align: center; padding: 0; } .ps-content-wrapper-v0 .left { text-align: left; } .ps-content-wrapper-v0 .right { text-align: right; } .ps-content-wrapper-v0 .code { font-family: monospace; white-space: nowrap; } .ps-content-wrapper-v0 .json-object-array ol, .ps-content-wrapper-v0 .json-object-array ol ul { margin-top: 0px; padding-left: 14px; } .json-object-array li { float: left; margin-right: 30px; margin-left: 10px; } .json-object-array pre { padding: 4px; margin-left: 0px; }
2+
</style>
3+
<div class="ps-content-wrapper-v0">
4+
<p>ByteDance is working on a new algorithm that operates on a special CPU that can handle <em>n</em> types of tasks, and the priority of the<em> i<sup>th</sup></em> task is represented by the array<em> priority[i]</em>. The CPU will run for <em>y</em> seconds and you can choose any task to perform which follows the given conditions:</p>
5+
6+
<ul>
7+
<li>In each second, you&nbsp;user can choose to perform any available task from the <em>n</em> types (if there are any tasks to perform).</li>
8+
<li>It is allowed to perform multiple tasks of the same type within the given time frame, but there is a time constraint that there must be at least <em>x</em> seconds between any two consecutive tasks of the same type.</li>
9+
<li>It is not allowed to perform more than one task at any second.</li>
10+
</ul>
11+
12+
<p>&nbsp;</p>
13+
14+
<p>Given <em>n</em> tasks, an array <em>priority, </em>and<em> </em>an integer<em> x</em> and <i>y</i>, find the maximum possible sum of the priority of tasks performed during the <em>y</em> seconds.</p>
15+
16+
<p>&nbsp;</p>
17+
18+
<p class="section-title">Example</p>
19+
20+
<p>Given, <em>n</em> = 3, <em>priority = </em>[3, 1, 2], <em>x =</em> 5, and <em>y = </em>7.</p>
21+
22+
<p>&nbsp;</p>
23+
24+
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;">
25+
<thead>
26+
<tr>
27+
<th scope="col">Seconds</th>
28+
<th scope="col">Task Performed</th>
29+
<th scope="col">Sum of priority</th>
30+
</tr>
31+
</thead>
32+
<tbody>
33+
<tr>
34+
<td>
35+
<strong>&nbsp; &nbsp; &nbsp; </strong>1<span style="font-size:12.5px;">.&nbsp;</span>
36+
</td>
37+
<td>Perform a task of the 1<sup>st</sup> type</td>
38+
<td>3</td>
39+
</tr>
40+
<tr>
41+
<td>&nbsp; &nbsp; &nbsp; 2.&nbsp;</td>
42+
<td>Perform a task of the 3<sup>rd</sup> type</td>
43+
<td>3 + 2 = 5</td>
44+
</tr>
45+
<tr>
46+
<td>&nbsp; &nbsp; &nbsp; 3.&nbsp;</td>
47+
<td>Perform a task of the 2<sup>nd</sup> type</td>
48+
<td>3 + 2 +1 = 6</td>
49+
</tr>
50+
<tr>
51+
<td>&nbsp; &nbsp; &nbsp; 4.&nbsp;</td>
52+
<td>No task perfomed</td>
53+
<td>3 + 2 + 1 = 6</td>
54+
</tr>
55+
<tr>
56+
<td>&nbsp; &nbsp; &nbsp; 5.&nbsp;</td>
57+
<td>No task perfomed</td>
58+
<td>3 + 2 + 1 = 6</td>
59+
</tr>
60+
<tr>
61+
<td>&nbsp; &nbsp; &nbsp; 6.&nbsp;</td>
62+
<td>Perform a task of the 1<sup>st</sup> type</td>
63+
<td>3 + 2 + 1 + 3 = 9</td>
64+
</tr>
65+
<tr>
66+
<td>&nbsp; &nbsp; &nbsp; 7.&nbsp;</td>
67+
<td>Perform a task of the 3<sup>rd</sup> type</td>
68+
<td>3 + 2 + 1 + 3 + 2 = 11</td>
69+
</tr>
70+
</tbody>
71+
</table>
72+
73+
<p>Hence, the maximum sum of priority of tasks we can get is equal to 11. We performed the task of the 1<sup>st</sup> type then till the second 5 we cannot perform the task of the 1<sup>st</sup> type again because of the time constraint x, from the 6th second we can perform the task of the 1<sup>st</sup> type. The same applies to the task of the 3<sup>rd</sup>&nbsp;type as we once performed it in the 2<sup>nd</sup> second, and then till the 6<sup>th</sup> second, we cannot perform the task of the 3<sup>rd</sup> type.</p>
74+
75+
<p>&nbsp;</p>
76+
77+
<p class="section-title">Function Description</p>
78+
79+
<p>Complete the function <i>maximumPrioritySum</i> in the editor below.</p>
80+
81+
<p><i>maximumPrioritySum</i> has the following parameters:</p>
82+
83+
<ul>
84+
<li>
85+
<em>int priority[n]:</em>&nbsp; an array of integers representing the priorities of each task.</li>
86+
<li>
87+
<em>int x</em>: an integer&nbsp;denoting the minimum time constraint between every <em>2</em>&nbsp;tasks of the same type to be performed.</li>
88+
<li>
89+
<em>int y</em>: an integer&nbsp;denoting the number of seconds the CPU will run.</li>
90+
</ul>
91+
92+
<p>&nbsp;</p>
93+
94+
<p><strong>Returns</strong></p>
95+
96+
<p><em>int:</em> the maximum possible sum of the priority of tasks performed during the <em>y</em> seconds.</p>
97+
98+
<p>&nbsp;</p>
99+
100+
<p class="section-title">Constraints</p>
101+
102+
<ul>
103+
<li>1&nbsp;≤<em> n&nbsp;</em>≤ 10<sup>5</sup>
104+
</li>
105+
<li>1&nbsp;≤<em> priority<span style="font-size:12px;">[i]&nbsp;</span></em>≤&nbsp;10<sup>5</sup>
106+
</li>
107+
<li>1&nbsp;≤<em> x&nbsp;</em>≤ 10<sup>9</sup>
108+
</li>
109+
<li>1&nbsp;≤<em> y&nbsp;</em>≤ 10<sup>9</sup>
110+
</li>
111+
</ul>
112+
113+
<p>&nbsp;</p>
114+
<!-- <StartOfInputFormat> DO NOT REMOVE THIS LINE-->
115+
116+
<details open=""><summary class="section-title">Input Format For Custom Testing</summary>
117+
118+
<div class="collapsable-details">
119+
<p>The first line contains an integer <em>n, </em>the number of types of tasks present.</p>
120+
121+
<p>Each of the next <em>n</em> lines contains an integer <em>priority[i]</em>.</p>
122+
123+
<p>The next line contains an integer <em>x, </em>the minimum time constraint between every <em>2</em>&nbsp;tasks of the same type to be performed.</p>
124+
125+
<p>The next line contains an integer <em>y, </em>the number of seconds the CPU will run.</p>
126+
</div>
127+
</details>
128+
<!-- </StartOfInputFormat> DO NOT REMOVE THIS LINE-->
129+
130+
<details open=""><summary class="section-title">Sample Case 0</summary>
131+
132+
<div class="collapsable-details">
133+
<p class="section-title">Sample Input For Custom Testing</p>
134+
135+
<pre>STDIN Function
136+
----- --------
137+
3 ⇒ priority[] size n = 3
138+
2 ⇒ priority = [2, 1, 3]
139+
1
140+
3
141+
2 ⇒ x = 2
142+
3 ⇒ y = 3</pre>
143+
144+
<p class="section-title">Sample Output</p>
145+
146+
<pre>8</pre>
147+
148+
<p class="section-title">Explanation</p>
149+
150+
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;">
151+
<thead>
152+
<tr>
153+
<th scope="col">Seconds</th>
154+
<th scope="col">Tasks Performed</th>
155+
<th scope="col">Sum of priority</th>
156+
</tr>
157+
</thead>
158+
<tbody>
159+
<tr>
160+
<td>
161+
<strong>&nbsp; &nbsp; &nbsp; &nbsp;</strong> 1<span style="font-size:12.5px;">.&nbsp;</span>
162+
</td>
163+
<td>Perform a task of the 3<sup>rd </sup>type</td>
164+
<td>3</td>
165+
</tr>
166+
<tr>
167+
<td>
168+
<strong>&nbsp; &nbsp; &nbsp; &nbsp;</strong> 2<span style="font-size:12.5px;">.&nbsp;</span>
169+
</td>
170+
<td>Perform a task of the 1<sup>st</sup> type</td>
171+
<td>3 + 2 =5</td>
172+
</tr>
173+
<tr>
174+
<td>
175+
<strong>&nbsp; </strong>&nbsp; &nbsp; &nbsp; 3<span style="font-size:12.5px;">.&nbsp;</span>
176+
</td>
177+
<td>Perform a task of the 3<sup>rd</sup> type</td>
178+
<td>3 + 2 + 3 =8</td>
179+
</tr>
180+
</tbody>
181+
</table>
182+
183+
<p>Hence, the maximum sum of priority we can get is equal to 8.</p>
184+
</div>
185+
</details>
186+
187+
<details><summary class="section-title">Sample Case 1</summary>
188+
189+
<div class="collapsable-details">
190+
<p class="section-title">Sample Input For Custom Testing</p>
191+
192+
<pre>STDIN &nbsp; &nbsp; &nbsp; &nbsp; Function
193+
----- &nbsp; &nbsp; &nbsp; &nbsp; --------
194+
1 &nbsp; ⇒ priority[] size n = 1
195+
1 ⇒ priority = [1]
196+
3 ⇒ x = 3
197+
10 ⇒ y = 10</pre>
198+
199+
<p class="section-title">Sample Output</p>
200+
201+
<pre>4</pre>
202+
203+
<p class="section-title">Explanation</p>
204+
205+
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;">
206+
<thead>
207+
<tr>
208+
<th scope="col">Seconds</th>
209+
<th scope="col">Tasks Performed</th>
210+
<th scope="col">Sum of priority</th>
211+
</tr>
212+
</thead>
213+
<tbody>
214+
<tr>
215+
<td>
216+
<strong>&nbsp; </strong>&nbsp; &nbsp; &nbsp; &nbsp; 1<span style="font-size:12.5px;">.&nbsp;</span>
217+
</td>
218+
<td>Perform a task of the 1<sup>st</sup> type</td>
219+
<td>1</td>
220+
</tr>
221+
<tr>
222+
<td>
223+
<strong>&nbsp; </strong>&nbsp; &nbsp; &nbsp; &nbsp; 2<span style="font-size:12.5px;">.&nbsp;</span>
224+
</td>
225+
<td>No task perfomed</td>
226+
<td>1+0= 1</td>
227+
</tr>
228+
<tr>
229+
<td>
230+
<strong>&nbsp; </strong>&nbsp; &nbsp; &nbsp; &nbsp; 3<span style="font-size:12.5px;">.&nbsp;</span>
231+
</td>
232+
<td>No task perfomed</td>
233+
<td>1+0+0= 1</td>
234+
</tr>
235+
<tr>
236+
<td>
237+
<strong>&nbsp; </strong>&nbsp; &nbsp; &nbsp; &nbsp; 4<span style="font-size:12.5px;">.&nbsp;</span>
238+
</td>
239+
<td>Perform a task of the 1<sup>st</sup> type</td>
240+
<td>1+0+0+1= 2</td>
241+
</tr>
242+
<tr>
243+
<td>
244+
<strong>&nbsp; </strong>&nbsp; &nbsp; &nbsp; &nbsp; 5<span style="font-size:12.5px;">.&nbsp;</span>
245+
</td>
246+
<td>No task perfomed</td>
247+
<td>1+0+0+1+0= 2</td>
248+
</tr>
249+
<tr>
250+
<td>
251+
<strong>&nbsp; </strong>&nbsp; &nbsp; &nbsp; &nbsp; 6<span style="font-size:12.5px;">.&nbsp;</span>
252+
</td>
253+
<td>No task perfomed</td>
254+
<td>1+0+0+1+0+0= 2</td>
255+
</tr>
256+
<tr>
257+
<td>
258+
<strong>&nbsp; </strong>&nbsp; &nbsp; &nbsp; &nbsp; 7<span style="font-size:12.5px;">.&nbsp;</span>
259+
</td>
260+
<td>Perform a task of the 1<sup>st</sup> type</td>
261+
<td>1+0+0+1+0+0+1= 3</td>
262+
</tr>
263+
<tr>
264+
<td>
265+
<strong>&nbsp; </strong>&nbsp; &nbsp; &nbsp; &nbsp; 8<span style="font-size:12.5px;">&nbsp;</span>
266+
</td>
267+
<td>No task perfomed</td>
268+
<td>1+0+0+1+0+0+1+0= 3</td>
269+
</tr>
270+
<tr>
271+
<td>
272+
<strong>&nbsp; </strong>&nbsp; &nbsp; &nbsp; &nbsp; 9<span style="font-size:12.5px;">.&nbsp;</span>
273+
</td>
274+
<td>No task perfomed</td>
275+
<td>1+0+0+1+0+0+1+0+0= 3</td>
276+
</tr>
277+
<tr>
278+
<td>
279+
<strong>&nbsp; </strong>&nbsp; &nbsp; &nbsp; 10<span style="font-size:12.5px;">.&nbsp;</span>
280+
</td>
281+
<td>Perform a task of the 1<sup>st</sup> type</td>
282+
<td>1+0+0+1+0+0+1+0+0+1= 4</td>
283+
</tr>
284+
</tbody>
285+
</table>
286+
287+
<p>Hence, the maximum possible sum of priority we can get is 4 by performing the task of 1<sup>st</sup>&nbsp;type in the interval of <em>x</em> seconds.</p>
288+
</div>
289+
</details>
290+
</div>
291+
</div></div></section></div>
+227
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
<div id="main-splitpane-left" class="coding-question__left-pane"><section class="question-view__title-wrapper"><h1 class="question-view__title">5. Compatible GPUs</h1></section><section class="question-view__instruction"><div class="candidate-rich-text"><div id="f8plgkrd26b-instruction"><p>ByteDance is planning to buy some&nbsp;GPUs for training their new computer vision models using them. They have 2 clusters on which all the models are saved.&nbsp;There are&nbsp;<em>n</em>&nbsp;GPUs available, where the cost of the&nbsp;<em>i<sup>th</sup></em>&nbsp;GPU is represented by array element&nbsp;<em>cost[i]</em>. Also, there are two&nbsp;arrays&nbsp;<em>compatible1&nbsp;</em>and&nbsp;<em>compatible2</em>&nbsp;each&nbsp;containing <em>n</em>&nbsp;integers, where each integer is either 0 or 1, representing the following:</p>
2+
3+
<ul>
4+
<li>If&nbsp;<em>compatible1[i]</em>&nbsp;= 1, then the&nbsp;<em>i<sup>th</sup>&nbsp;</em>GPU is compatible with cluster 1, else it is not compatible with cluster 1.</li>
5+
<li>If&nbsp;<em>compatible2[i]</em>&nbsp;= 1, then the&nbsp;<em>i<sup>th</sup></em>&nbsp;GPU is compatible with cluster 2, else it is not compatible with cluster 2.</li>
6+
</ul>
7+
8+
<p>&nbsp;</p>
9+
10+
<p>The company wants to buy the GPUs such that there are at least a&nbsp;<i>min_compatible&nbsp;</i>number of&nbsp;GPUs compatible with each of cluster 1 and cluster 2.</p>
11+
12+
<p>
13+
<style type="text/css">.ps-content-wrapper-v0 div { margin: 0 auto; overflow: auto; } .ps-content-wrapper-v0 div.preheader { display: none; } .ps-content-wrapper-v0 p { white-space: pre-wrap; padding-left: 4px; padding-right: 4px; padding-top: 0px; padding-bottom: 2px; } .ps-content-wrapper-v0 p.section-title { font-weight: bold; padding-bottom: 0px; } .ps-content-wrapper-v0 ol.plain-list, .ps-content-wrapper-v0 ul.plain-list { list-style-type: none; padding: 4px; } .ps-content-wrapper-v0 li { white-space: normal; margin-top: 4px; margin-bottom: 4px; } .ps-content-wrapper-v0 code { color: black; } .ps-content-wrapper-v0 pre { background-color: #f4faff; border: 0; border-radius: 2px; margin: 8px; padding: 10px; } .ps-content-wrapper-v0 pre.scrollable-full-json { overflow-x: scroll; white-space: pre; } .ps-content-wrapper-v0 pre.scrollable-json { height: 300px; overflow-y: scroll; display: inline-grid; white-space: pre-wrap; padding-left: 8px; padding-right: 8px; padding-top: 4px; padding-bottom: 4px; } .ps-content-wrapper-v0 div.equation-parent { width: 400px; text-align: center; border: 1px solid #000; padding: 8px; } .ps-content-wrapper-v0 div.equation-parent.equation { width: 100%; display: inline-block; } .ps-content-wrapper-v0 figure { background-color: transparent; display: table; margin-top: 8px; margin-bottom: 8px; text-align: center; margin-left: auto; margin-right: auto; } .ps-content-wrapper-v0 figcaption { text-align: center; display: table-caption; caption-side: bottom; margin-top: 4px; margin-bottom: 4px; } .ps-content-wrapper-v0 img { width: auto; max-width: 100%; height: auto; } .ps-content-wrapper-v0 details { background-color: transparent; padding-left: 4px; padding-right: 4px; padding-top: 0px; padding-bottom: 2px; } .ps-content-wrapper-v0 details details { padding-left: 8px; padding-right: 8px; } .ps-content-wrapper-v0 details summary { background-color: #39424e; color: white; font-weight: bold; margin-top: 4px; margin-bottom: 4px; padding: 8px; } .ps-content-wrapper-v0 details details summary code { color: black; font-weight: bold; padding-left: 2px; padding-right: 2px; padding-top: 4px; padding-right: 4px; margin-left: 4px; } .ps-content-wrapper-v0 details div.collapsable-details { margin: 0 auto; padding-left: 4px; padding-right: 4px; padding-top: 0px; padding-bottom: 2px; overflow: auto; } .ps-content-wrapper-v0 details div.collapsable-details pre { margin-left: 4px; margin-right: 4px; margin-top: 4px; margin-bottom: 4px; } .ps-content-wrapper-v0 table.normal { border: 1px solid black; border-collapse: collapse; border-color: darkgray; margin: 0 auto; margin-top: 8px; margin-bottom: 8px; padding: 8px; width: 96%; table-layout: fixed; } .ps-content-wrapper-v0 table.normal tbody { display: block; overflow-x: auto; overflow-y: hidden; } .ps-content-wrapper-v0 table.normal tbody tr:first-child th { font-weight: bold; white-space: normal; } .ps-content-wrapper-v0 table.normal tbody tr th, .ps-content-wrapper-v0 table.normal tbody tr td { font-weight: normal; white-space: nowrap; text-align: center; vertical-align: middle; border: 1px solid black; border-color: darkgray; padding: 8px; } .ps-content-wrapper-v0 table.database-table { border-collapse: collapse; border-color: darkgray; border: 1px solid black; width: auto; margin-left: 4px; margin-top: 8px; margin-bottom: 8px; padding: 8px; } .ps-content-wrapper-v0 table.database-table tbody { overflow-x: auto; overflow-y: hidden; border: none; } .ps-content-wrapper-v0 table.database-table tbody tr th, .ps-content-wrapper-v0 table.database-table tbody tr td { font-weight: normal; white-space: nowrap; text-align: center; vertical-align: middle; border: 1px solid black; border-color: darkgray; padding: 8px; } .ps-content-wrapper-v0 table.database-table tbody tr th { font-weight: bold; border: 1px solid black; } .ps-content-wrapper-v0 table.database-table tbody tr:nth-child(2) td { border: 1px solid black; } .ps-content-wrapper-v0 table.database-table tbody tr:nth-child(n+2) td:first-child { border-left-color: black; } .ps-content-wrapper-v0 table.database-table tbody tr:nth-child(n+2) td:last-child { border-right-color: black; } .ps-content-wrapper-v0 table.database-table tbody tr:last-child td { border-bottom-color: black; } .ps-content-wrapper-v0 table.database-table tbody tr td.description { text-align: left; white-space: pre-wrap; } .ps-content-wrapper-v0 table.normal tbody tr th.description { width: 60%; } .ps-content-wrapper-v0 table.function-params tbody tr:first-child td.headers { border-bottom-width: 2px; } .ps-content-wrapper-v0 table.function-params tbody tr:last-child td { border-top-width: 2px; border-top-color: darkgray; } .ps-content-wrapper-v0 table.function-params tbody tr td.headers { width: 25%; font-weight: bold; text-align: center; border: 1px solid black; border-right-width: 2px; border-color: darkgray; } .ps-content-wrapper-v0 table.function-params tbody tr td.params-table-cell { width: 100%; height: 100%; padding: 0px; } .ps-content-wrapper-v0 table.function-params tbody tr td.params-table-cell table.params-table { width: 100%; height: 100%; padding: 0px; margin: 0px; border: 0; } .ps-content-wrapper-v0 table.function-params tbody tr td.params-table-cell table.params-table tbody tr td.code { white-space: normal; } .ps-content-wrapper-v0 table.function-params tbody tr td.params-table-cell table.params-table tbody tr th { border-top: 0; } .ps-content-wrapper-v0 table.function-params tbody tr td.params-table-cell table.params-table tbody tr th:first-child { border-left: 0; } .ps-content-wrapper-v0 table.function-params tbody tr td.params-table-cell table.params-table tbody tr th:last-child { border-right: 0; } .ps-content-wrapper-v0 table.function-params tbody tr td.params-table-cell table.params-table tbody tr:last-child td { border-bottom: 0; border-top-width: 1px; } .ps-content-wrapper-v0 table.function-params tbody tr td.params-table-cell table.params-table tbody tr td:first-child { border-left: 0; } .ps-content-wrapper-v0 table.function-params tbody tr td.params-table-cell table.params-table tbody tr td:last-child { border-right: 0; } .ps-content-wrapper-v0 table.sudoku { border-collapse: collapse; border-color: darkgray; margin: 0 auto; margin-top: 8px; margin-bottom: 8px; padding: 8px; } .ps-content-wrapper-v0 table.sudoku colgroup, tbody { border: 3px solid black; } .ps-content-wrapper-v0 table.sudoku td { border: 1px solid black; height: 25px; width: 25px; text-align: center; padding: 0; } .ps-content-wrapper-v0 .left { text-align: left; } .ps-content-wrapper-v0 .right { text-align: right; } .ps-content-wrapper-v0 .code { font-family: monospace; white-space: nowrap; } .ps-content-wrapper-v0 .json-object-array ol, .ps-content-wrapper-v0 .json-object-array ol ul { margin-top: 0px; padding-left: 14px; } .json-object-array li { float: left; margin-right: 30px; margin-left: 10px; } .json-object-array pre { padding: 4px; margin-left: 0px; }
14+
</style>
15+
</p>
16+
17+
<p>Given&nbsp;<em>n</em>&nbsp;GPUs, an integer&nbsp;<i>min_compatible</i>, and three&nbsp;arrays&nbsp;<em>cost, compatible1</em>&nbsp;and&nbsp;<em>compatible2</em>, find the minimum possible cost of GPUs such that&nbsp;there are at least a&nbsp;<em>min_compatible</em> number of&nbsp;GPUs compatible with each of cluster 1 and cluster 2. Return -1 if it is not possible to buy the GPUs satisfying the above condition.</p>
18+
19+
<div class="ps-content-wrapper-v0">
20+
<p>&nbsp;</p>
21+
22+
<p><strong>Example</strong></p>
23+
24+
<p>Given, <em>cost = </em>[2, 4, 6, 5]<em>, compatible1 = </em>[1, 1, 1, 0]<em>, compatible2 = </em>[0, 0, 1, 1]<em>, </em>and<em> </em><em>min_compatible = </em>2<em>.</em></p>
25+
26+
<p>Some of the ways of buying the GPUs are explained below:</p>
27+
28+
<p>&nbsp;</p>
29+
30+
<table border="1" cellpadding="1" cellspacing="1" style="width:750px;">
31+
<thead>
32+
<tr>
33+
<th scope="col"><strong>GPUs Bought (Indices 0-based)</strong></th>
34+
<th scope="col"><strong>Cost</strong></th>
35+
<th scope="col"><strong>Is Valid?</strong></th>
36+
</tr>
37+
</thead>
38+
<tbody>
39+
<tr>
40+
<td>[0, 2]</td>
41+
<td>2 + 6 = 8</td>
42+
<td>
43+
<p>Indices GPUs Compatible with cluster 1 = [0, 2].</p>
44+
45+
<p>Indices GPUs Compatible with cluster 2 = [2].</p>
46+
47+
<p>Not valid.</p>
48+
</td>
49+
</tr>
50+
<tr>
51+
<td>[0, 2, 3]</td>
52+
<td>2 + 6 + 5 = 13</td>
53+
<td>
54+
<p>Indices GPUs Compatible with cluster 1 = [0, 2].</p>
55+
56+
<p>Indices GPUs Compatible with cluster 2 = [2, 3].</p>
57+
58+
<p>Valid.</p>
59+
</td>
60+
</tr>
61+
<tr>
62+
<td>[1, 2, 3]</td>
63+
<td>4 + 6 + 5 = 15</td>
64+
<td>
65+
<p>Indices GPUs Compatible with cluster 1 = [1, 2].</p>
66+
67+
<p>Indices GPUs Compatible with cluster 2 = [2, 3].</p>
68+
69+
<p>Valid.</p>
70+
</td>
71+
</tr>
72+
</tbody>
73+
</table>
74+
75+
<p>&nbsp;</p>
76+
77+
<p>Hence, the minimum possible cost is 13.</p>
78+
79+
<p>&nbsp;</p>
80+
81+
<p><strong>Function Description</strong></p>
82+
83+
<p>Complete the function <em>getMinCost</em> in the editor below.</p>
84+
85+
<p><em>getMinCost</em> has the following parameter(s):</p>
86+
87+
<p><em>&nbsp;&nbsp;&nbsp;&nbsp;int cost[n]:</em>&nbsp;the cost of the GPUs</p>
88+
89+
<p><em>&nbsp;&nbsp;&nbsp;&nbsp;int compatible1[n]:</em>&nbsp;the compatibility of GPUs with cluster 1</p>
90+
91+
<p>&nbsp;&nbsp;&nbsp;&nbsp;<em>int compatible2[n]</em><em>:</em>&nbsp;the compatibility of GPUs with cluster 2</p>
92+
93+
<p>&nbsp;&nbsp;&nbsp;&nbsp;<em>int min_compatible:</em> the minimum number of GPUs that need to be compatible with each computer</p>
94+
95+
<p>&nbsp;</p>
96+
97+
<p><strong>Returns</strong></p>
98+
99+
<p><em>&nbsp;&nbsp;&nbsp;&nbsp;long:</em> the minimum possible cost of GPUs such that&nbsp;there are at least a <em>min_compatible</em>&nbsp;number of&nbsp;GPUs compatible with each of cluster 1 and cluster 2</p>
100+
101+
<p>&nbsp;</p>
102+
103+
<p><strong>Constraints</strong></p>
104+
105+
<ul>
106+
<li>1&nbsp;<em>≤ n, min_compatible&nbsp;</em>≤ 2 *&nbsp;10<sup>5</sup>
107+
</li>
108+
<li>1&nbsp;<em>≤ cost[i]&nbsp;</em>≤ 10<sup>9</sup>
109+
</li>
110+
<li>0&nbsp;<em>≤ compatible1[i], compatible2[i] ≤&nbsp;</em>1</li>
111+
</ul>
112+
113+
<p>&nbsp;</p>
114+
<!-- <StartOfInputFormat> DO NOT REMOVE THIS LINE-->
115+
116+
<details><summary class="section-title">Input Format For Custom Testing</summary>
117+
118+
<div class="collapsable-details">
119+
<p>The first line contains an integer <em>n</em>, the number of GPUs.</p>
120+
121+
<p>Each of the next <em>n</em> lines contains an integer <em>cost[i]</em>.</p>
122+
123+
<p>The next line contains an integer <em>n</em>, the number of GPUs.</p>
124+
125+
<p>Each of the next <em>n</em> lines contains an integer <em>compatible1[i].</em></p>
126+
127+
<p>The next line contains an integer <em>n</em>, the number of GPUs.</p>
128+
129+
<p>Each of the next <em>n</em> lines contains an integer <em>compatible2[i].</em></p>
130+
131+
<p>The last line contains an integer <em>min_compatible.</em></p>
132+
</div>
133+
</details>
134+
<!-- </StartOfInputFormat> DO NOT REMOVE THIS LINE-->
135+
136+
<details open="open"><summary class="section-title">Sample Case 0</summary>
137+
138+
<div class="collapsable-details">
139+
<p class="section-title">Sample Input For Custom Testing</p>
140+
141+
<pre>STDIN&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FUNCTION
142+
-----&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--------
143+
6&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;→&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cost[] size n = 6
144+
4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;→&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cost = [4, 8, 7, 3, 2, 9]
145+
8
146+
7
147+
3
148+
2
149+
9
150+
6&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; →&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; compatible1[] size n = 6
151+
1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; →&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; compatible1 = [1, 1, 1, 0, 0, 1]
152+
1
153+
1
154+
0
155+
0
156+
1
157+
6&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; →&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; compatible2[] size n = 6
158+
1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; →&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; compatible2 = [1, 1, 0, 1, 0, 0]
159+
1
160+
0
161+
1
162+
0
163+
0
164+
2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; →&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; min_compatible = 2</pre>
165+
166+
<p class="section-title">Sample Output</p>
167+
168+
<pre>12&nbsp;</pre>
169+
170+
<p class="section-title">Explanation</p>
171+
172+
<p>One of the optimal ways is:</p>
173+
174+
<ul>
175+
<li>Buy the GPUs [0, 1] (indices 0-based)&nbsp;with a cost of (4 + 8) = 12.</li>
176+
</ul>
177+
</div>
178+
</details>
179+
180+
<details><summary class="section-title">Sample Case 1</summary>
181+
182+
<div class="collapsable-details">
183+
<p class="section-title">Sample Input For Custom Testing</p>
184+
185+
<pre>STDIN&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FUNCTION
186+
-----&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--------
187+
7&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; →&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cost[] size n = 7
188+
3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; →&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cost = [3, 4, 5, 6, 3, 2, 10]
189+
4
190+
5
191+
6
192+
3
193+
2
194+
10
195+
7&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; →&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;compatible1[] size n = 7
196+
1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; →&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;compatible1 = [1, 1, 0, 1, 1, 1, 0]
197+
1
198+
0
199+
1
200+
1
201+
1
202+
0
203+
7&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; →&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;compatible2[] size n = 7
204+
0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; →&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;compatible2 = [0, 0, 1, 0, 1, 1, 0]
205+
0
206+
1
207+
0
208+
1
209+
1
210+
0
211+
3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; →&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;min_compatible = 3</pre>
212+
213+
<p class="section-title">Sample Output</p>
214+
215+
<pre>13&nbsp;</pre>
216+
217+
<p class="section-title">Explanation</p>
218+
219+
<p>One of the optimal ways is:</p>
220+
221+
<ul>
222+
<li>Buy the GPUs [0, 2, 4, 5] (indices 0-based), with a cost of (3 + 5 + 3 + 2) = 13.</li>
223+
</ul>
224+
</div>
225+
</details>
226+
</div>
227+
</div></div></section></div>

0 commit comments

Comments
 (0)
Please sign in to comment.