Skip to content

Commit 3f64b51

Browse files
committed
Add solution #3385
1 parent 2903ccf commit 3f64b51

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2678,6 +2678,7 @@
26782678
3373|[Maximize the Number of Target Nodes After Connecting Trees II](./solutions/3373-maximize-the-number-of-target-nodes-after-connecting-trees-ii.js)|Hard|
26792679
3375|[Minimum Operations to Make Array Values Equal to K](./solutions/3375-minimum-operations-to-make-array-values-equal-to-k.js)|Easy|
26802680
3383|[Minimum Runes to Add to Cast Spell](./solutions/3383-minimum-runes-to-add-to-cast-spell.js)|Hard|
2681+
3385|[Minimum Time to Break Locks II](./solutions/3385-minimum-time-to-break-locks-ii.js)|Hard|
26812682
3392|[Count Subarrays of Length Three With a Condition](./solutions/3392-count-subarrays-of-length-three-with-a-condition.js)|Easy|
26822683
3394|[Check if Grid can be Cut into Sections](./solutions/3394-check-if-grid-can-be-cut-into-sections.js)|Medium|
26832684
3396|[Minimum Number of Operations to Make Elements in Array Distinct](./solutions/3396-minimum-number-of-operations-to-make-elements-in-array-distinct.js)|Easy|
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy