|
| 1 | +/** |
| 2 | + * 3385. Minimum Time to Break Locks II |
| 3 | + * https://leetcode.com/problems/minimum-time-to-break-locks-ii/ |
| 4 | + * Difficulty: Hard |
| 5 | + * |
| 6 | + * Bob is stuck in a dungeon and must break n locks, each requiring some amount of energy |
| 7 | + * to break. The required energy for each lock is stored in an array called strength where |
| 8 | + * strength[i] indicates the energy needed to break the ith lock. |
| 9 | + * |
| 10 | + * To break a lock, Bob uses a sword with the following characteristics: |
| 11 | + * - The initial energy of the sword is 0. |
| 12 | + * - The initial factor X by which the energy of the sword increases is 1. |
| 13 | + * - Every minute, the energy of the sword increases by the current factor X. |
| 14 | + * - To break the ith lock, the energy of the sword must reach at least strength[i]. |
| 15 | + * - After breaking a lock, the energy of the sword resets to 0, and the factor X increases by 1. |
| 16 | + * |
| 17 | + * Your task is to determine the minimum time in minutes required for Bob to break all n locks |
| 18 | + * and escape the dungeon. |
| 19 | + * |
| 20 | + * Return the minimum time required for Bob to break all n locks. |
| 21 | + */ |
| 22 | + |
| 23 | +/** |
| 24 | + * @param {number[]} strength |
| 25 | + * @return {number} |
| 26 | + */ |
| 27 | +var findMinimumTime = function(strength) { |
| 28 | + const n = strength.length; |
| 29 | + const cost = Array.from({ length: n + 1 }, () => new Array(n + 1).fill(0)); |
| 30 | + |
| 31 | + for (let i = 0; i < n; i++) { |
| 32 | + for (let j = 0; j < n; j++) { |
| 33 | + cost[i + 1][j + 1] = Math.floor((strength[i] + j) / (j + 1)); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + const rowPotential = new Array(n + 1).fill(0); |
| 38 | + const colPotential = new Array(n + 1).fill(0); |
| 39 | + const assignment = new Array(n + 1).fill(0); |
| 40 | + const parent = new Array(n + 1).fill(0); |
| 41 | + |
| 42 | + for (let row = 1; row <= n; row++) { |
| 43 | + assignment[0] = row; |
| 44 | + let col = 0; |
| 45 | + const minCol = new Array(n + 1).fill(Number.MAX_SAFE_INTEGER); |
| 46 | + const visited = new Array(n + 1).fill(false); |
| 47 | + |
| 48 | + do { |
| 49 | + visited[col] = true; |
| 50 | + const currentRow = assignment[col]; |
| 51 | + let delta = Number.MAX_SAFE_INTEGER; |
| 52 | + let nextCol; |
| 53 | + |
| 54 | + for (let j = 1; j <= n; j++) { |
| 55 | + if (!visited[j]) { |
| 56 | + const reducedCost = cost[currentRow][j] - rowPotential[currentRow] - colPotential[j]; |
| 57 | + if (reducedCost < minCol[j]) { |
| 58 | + minCol[j] = reducedCost; |
| 59 | + parent[j] = col; |
| 60 | + } |
| 61 | + if (minCol[j] < delta) { |
| 62 | + delta = minCol[j]; |
| 63 | + nextCol = j; |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + for (let j = 0; j <= n; j++) { |
| 69 | + if (visited[j]) { |
| 70 | + rowPotential[assignment[j]] += delta; |
| 71 | + colPotential[j] -= delta; |
| 72 | + } else { |
| 73 | + minCol[j] -= delta; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + col = nextCol; |
| 78 | + } while (assignment[col] !== 0); |
| 79 | + |
| 80 | + do { |
| 81 | + const prevCol = parent[col]; |
| 82 | + assignment[col] = assignment[prevCol]; |
| 83 | + col = prevCol; |
| 84 | + } while (col); |
| 85 | + } |
| 86 | + |
| 87 | + return -colPotential[0]; |
| 88 | +}; |
0 commit comments