|
| 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 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> </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> </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> </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> </strong>1<span style="font-size:12.5px;">. </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> 2. </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> 3. </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> 4. </td> |
| 52 | + <td>No task perfomed</td> |
| 53 | + <td>3 + 2 + 1 = 6</td> |
| 54 | + </tr> |
| 55 | + <tr> |
| 56 | + <td> 5. </td> |
| 57 | + <td>No task perfomed</td> |
| 58 | + <td>3 + 2 + 1 = 6</td> |
| 59 | + </tr> |
| 60 | + <tr> |
| 61 | + <td> 6. </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> 7. </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> 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> </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> an array of integers representing the priorities of each task.</li> |
| 86 | + <li> |
| 87 | +<em>int x</em>: an integer denoting the minimum time constraint between every <em>2</em> tasks of the same type to be performed.</li> |
| 88 | + <li> |
| 89 | +<em>int y</em>: an integer denoting the number of seconds the CPU will run.</li> |
| 90 | +</ul> |
| 91 | + |
| 92 | +<p> </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> </p> |
| 99 | + |
| 100 | +<p class="section-title">Constraints</p> |
| 101 | + |
| 102 | +<ul> |
| 103 | + <li>1 ≤<em> n </em>≤ 10<sup>5</sup> |
| 104 | +</li> |
| 105 | + <li>1 ≤<em> priority<span style="font-size:12px;">[i] </span></em>≤ 10<sup>5</sup> |
| 106 | +</li> |
| 107 | + <li>1 ≤<em> x </em>≤ 10<sup>9</sup> |
| 108 | +</li> |
| 109 | + <li>1 ≤<em> y </em>≤ 10<sup>9</sup> |
| 110 | +</li> |
| 111 | +</ul> |
| 112 | + |
| 113 | +<p> </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> 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> </strong> 1<span style="font-size:12.5px;">. </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> </strong> 2<span style="font-size:12.5px;">. </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> </strong> 3<span style="font-size:12.5px;">. </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 Function |
| 193 | +----- -------- |
| 194 | +1 ⇒ 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> </strong> 1<span style="font-size:12.5px;">. </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> </strong> 2<span style="font-size:12.5px;">. </span> |
| 224 | +</td> |
| 225 | + <td>No task perfomed</td> |
| 226 | + <td>1+0= 1</td> |
| 227 | + </tr> |
| 228 | + <tr> |
| 229 | + <td> |
| 230 | +<strong> </strong> 3<span style="font-size:12.5px;">. </span> |
| 231 | +</td> |
| 232 | + <td>No task perfomed</td> |
| 233 | + <td>1+0+0= 1</td> |
| 234 | + </tr> |
| 235 | + <tr> |
| 236 | + <td> |
| 237 | +<strong> </strong> 4<span style="font-size:12.5px;">. </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> </strong> 5<span style="font-size:12.5px;">. </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> </strong> 6<span style="font-size:12.5px;">. </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> </strong> 7<span style="font-size:12.5px;">. </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> </strong> 8<span style="font-size:12.5px;"> </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> </strong> 9<span style="font-size:12.5px;">. </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> </strong> 10<span style="font-size:12.5px;">. </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> type in the interval of <em>x</em> seconds.</p> |
| 288 | +</div> |
| 289 | +</details> |
| 290 | +</div> |
| 291 | +</div></div></section></div> |
0 commit comments