TheRealSamuel/LeetCodeProblem
0564
1{2 "id": 3044,3 "name": "minimum_operations_to_collect_elements",4 "difficulty": "Easy",5 "link": "https://leetcode.com/problems/minimum-operations-to-collect-elements/",6 "date": "2023-09-16 00:00:00",7 "task_description": "You are given an array `nums` of positive integers and an integer `k`. In one operation, you can remove the last element of the array and add it to your collection. Return _the **minimum number of operations** needed to collect elements_ `1, 2, ..., k`. **Example 1:** ``` **Input:** nums = [3,1,5,4,2], k = 2 **Output:** 4 **Explanation:** After 4 operations, we collect elements 2, 4, 5, and 1, in this order. Our collection contains elements 1 and 2. Hence, the answer is 4. ``` **Example 2:** ``` **Input:** nums = [3,1,5,4,2], k = 5 **Output:** 5 **Explanation:** After 5 operations, we collect elements 2, 4, 5, 1, and 3, in this order. Our collection contains elements 1 through 5. Hence, the answer is 5. ``` **Example 3:** ``` **Input:** nums = [3,2,5,3,1], k = 3 **Output:** 4 **Explanation:** After 4 operations, we collect elements 1, 3, 5, and 2, in this order. Our collection contains elements 1 through 3. Hence, the answer is 4. ``` **Constraints:** `1 <= nums.length <= 50` `1 <= nums[i] <= nums.length` `1 <= k <= nums.length` The input is generated such that you can collect elements `1, 2, ..., k`.",8 "public_test_cases": [9 {10 "label": "Example 1",11 "input": "nums = [3,1,5,4,2], k = 2",12 "output": "4 "13 },14 {15 "label": "Example 2",16 "input": "nums = [3,1,5,4,2], k = 5",17 "output": "5 "18 },19 {20 "label": "Example 3",21 "input": "nums = [3,2,5,3,1], k = 3",22 "output": "4 "23 }24 ],25 "private_test_cases": [26 {27 "input": [28 [29 30,30 46,31 42,32 50,33 21,34 14,35 37,36 43,37 11,38 5,39 13,40 10,41 33,42 6,43 8,44 44,45 23,46 47,47 48,48 45,49 17,50 27,51 12,52 34,53 38,54 28,55 49,56 9,57 25,58 19,59 7,60 1,61 22,62 15,63 4,64 41,65 40,66 35,67 36,68 31,69 20,70 16,71 18,72 3,73 32,74 24,75 29,76 39,77 2,78 2679 ],80 1181 ],82 "output": 4283 },84 {85 "input": [86 [87 10,88 14,89 17,90 23,91 36,92 7,93 11,94 13,95 6,96 15,97 26,98 20,99 4,100 16,101 31,102 30,103 12,104 21,105 5,106 34,107 19,108 2,109 18,110 9,111 29,112 37,113 1,114 8,115 38,116 25,117 32,118 22,119 33,120 3,121 28,122 24,123 35,124 27125 ],126 13127 ],128 "output": 38129 },130 {131 "input": [132 [133 1,134 7,135 6,136 3,137 5,138 2,139 4140 ],141 4142 ],143 "output": 7144 },145 {146 "input": [147 [148 41,149 30,150 32,151 25,152 7,153 15,154 14,155 17,156 39,157 24,158 23,159 12,160 6,161 29,162 8,163 40,164 36,165 35,166 34,167 11,168 21,169 26,170 33,171 1,172 5,173 20,174 9,175 4,176 27,177 13,178 10,179 31,180 22,181 37,182 3,183 28,184 19,185 16,186 2,187 18,188 38189 ],190 20191 ],192 "output": 37193 },194 {195 "input": [196 [197 31,198 22,199 29,200 17,201 34,202 32,203 1,204 10,205 8,206 35,207 18,208 6,209 20,210 23,211 27,212 19,213 30,214 13,215 28,216 5,217 9,218 12,219 39,220 21,221 11,222 14,223 16,224 40,225 38,226 33,227 15,228 25,229 7,230 36,231 2,232 24,233 37,234 4,235 3,236 26237 ],238 25239 ],240 "output": 39241 },242 {243 "input": [244 [245 27,246 24,247 33,248 15,249 3,250 1,251 37,252 13,253 28,254 8,255 4,256 34,257 41,258 20,259 18,260 6,261 40,262 9,263 30,264 7,265 25,266 12,267 11,268 32,269 17,270 35,271 29,272 21,273 19,274 10,275 31,276 16,277 22,278 2,279 23,280 26,281 36,282 5,283 39,284 14,285 38286 ],287 13288 ],289 "output": 37290 },291 {292 "input": [293 [294 9,295 3,296 16,297 11,298 6,299 17,300 12,301 7,302 2,303 15,304 10,305 14,306 18,307 4,308 13,309 1,310 5,311 8312 ],313 10314 ],315 "output": 18316 },317 {318 "input": [319 [320 17,321 33,322 25,323 5,324 3,325 1,326 2,327 30,328 12,329 26,330 34,331 24,332 7,333 23,334 28,335 22,336 29,337 14,338 10,339 4,340 9,341 32,342 8,343 31,344 13,345 6,346 27,347 19,348 16,349 11,350 18,351 20,352 21,353 15354 ],355 15356 ],357 "output": 31358 },359 {360 "input": [361 [362 17,363 19,364 26,365 24,366 22,367 5,368 20,369 8,370 9,371 7,372 15,373 11,374 14,375 10,376 25,377 18,378 23,379 12,380 21,381 3,382 2,383 1,384 4,385 13,386 6,387 16388 ],389 7390 ],391 "output": 21392 },393 {394 "input": [395 [396 23,397 18,398 25,399 4,400 6,401 12,402 17,403 30,404 21,405 35,406 22,407 3,408 1,409 32,410 19,411 33,412 29,413 2,414 13,415 8,416 34,417 9,418 24,419 5,420 15,421 10,422 28,423 11,424 7,425 27,426 16,427 31,428 20,429 26,430 14431 ],432 32433 ],434 "output": 35435 }436 ],437 "haskell_template": "minOperations :: [Int] -> Int -> Int\nminOperations nums k ",438 "ocaml_template": "let minOperations (nums: int list) (k: int) : int = ",439 "scala_template": "def minOperations(nums: List[Int],k: Int): Int = { \n \n}",440 "java_template": "class Solution {\n public int minOperations(List<Integer> nums, int k) {\n \n }\n}",441 "python_template": "class Solution(object):\n def minOperations(self, nums, k):\n \"\"\"\n :type nums: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n "442}