-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problem: No.3376 (#3855)
No.3376.Minimum Time to Break Locks I
- Loading branch information
Showing
7 changed files
with
395 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
solution/3300-3399/3376.Minimum Time to Break Locks I/Solution.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
class Solution { | ||
public: | ||
int findMinimumTime(vector<int>& strength, int K) { | ||
int n = strength.size(); | ||
int f[1 << n]; | ||
memset(f, -1, sizeof(f)); | ||
int k = K; | ||
auto dfs = [&](auto&& dfs, int i) -> int { | ||
if (i == (1 << n) - 1) { | ||
return 0; | ||
} | ||
if (f[i] != -1) { | ||
return f[i]; | ||
} | ||
int cnt = __builtin_popcount(i); | ||
int x = 1 + k * cnt; | ||
f[i] = INT_MAX; | ||
for (int j = 0; j < n; ++j) { | ||
if (i >> j & 1 ^ 1) { | ||
f[i] = min(f[i], dfs(dfs, i | 1 << j) + (strength[j] + x - 1) / x); | ||
} | ||
} | ||
return f[i]; | ||
}; | ||
return dfs(dfs, 0); | ||
} | ||
}; |
25 changes: 25 additions & 0 deletions
25
solution/3300-3399/3376.Minimum Time to Break Locks I/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
func findMinimumTime(strength []int, K int) int { | ||
n := len(strength) | ||
f := make([]int, 1<<n) | ||
for i := range f { | ||
f[i] = -1 | ||
} | ||
var dfs func(int) int | ||
dfs = func(i int) int { | ||
if i == 1<<n-1 { | ||
return 0 | ||
} | ||
if f[i] != -1 { | ||
return f[i] | ||
} | ||
x := 1 + K*bits.OnesCount(uint(i)) | ||
f[i] = 1 << 30 | ||
for j, s := range strength { | ||
if i>>j&1 == 0 { | ||
f[i] = min(f[i], dfs(i|1<<j)+(s+x-1)/x) | ||
} | ||
} | ||
return f[i] | ||
} | ||
return dfs(0) | ||
} |
Oops, something went wrong.