Skip to content

Commit 093ae64

Browse files
Dungeon Game
1 parent 372dcaa commit 093ae64

File tree

4 files changed

+170
-1
lines changed

4 files changed

+170
-1
lines changed

19.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
3+
Longest Duplicate Substring
4+
---------------------------
5+
6+
Given a string S, consider all duplicated substrings: (contiguous) substrings of S that occur 2 or more times. (The occurrences may overlap.)
7+
8+
Return any duplicated substring that has the longest possible length. (If S does not have a duplicated substring, the answer is "".)
9+
10+
Example 1:
11+
12+
Input: "banana"
13+
Output: "ana"
14+
Example 2:
15+
16+
Input: "abcd"
17+
Output: ""
18+
19+
20+
Note:
21+
22+
2 <= S.length <= 10^5
23+
S consists of lowercase English letters.
24+
25+
*/
26+
27+
class Solution {
28+
public:
29+
string longestDupSubstring(string S) {
30+
std::string_view longest;
31+
std::unordered_set<std::string_view> set;
32+
int start = 1;
33+
int end = S.size() - 1;
34+
while (start <= end)
35+
{
36+
int len = start + (end - start) / 2;
37+
bool found = false;
38+
for (int i = 0; i != S.size() - len + 1; ++i)
39+
{
40+
const auto [it, inserted] = set.emplace(S.data() + i, len);
41+
if (!inserted)
42+
{
43+
found = true;
44+
longest = *it;
45+
break;
46+
}
47+
}
48+
49+
if (found)
50+
start = len + 1;
51+
else
52+
end = len - 1;
53+
54+
set.clear();
55+
}
56+
57+
return {longest.begin(), longest.end()};
58+
}
59+
};

20.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
3+
Permutation Sequence
4+
--------------------
5+
6+
The set [1,2,3,...,n] contains a total of n! unique permutations.
7+
8+
By listing and labeling all of the permutations in order, we get the following sequence for n = 3:
9+
10+
"123"
11+
"132"
12+
"213"
13+
"231"
14+
"312"
15+
"321"
16+
Given n and k, return the kth permutation sequence.
17+
18+
Note:
19+
20+
Given n will be between 1 and 9 inclusive.
21+
Given k will be between 1 and n! inclusive.
22+
Example 1:
23+
24+
Input: n = 3, k = 3
25+
Output: "213"
26+
Example 2:
27+
28+
Input: n = 4, k = 9
29+
Output: "2314"
30+
31+
*/
32+
33+
class Solution {
34+
public:
35+
string getPermutation(int n, int k) {
36+
string ans = "";
37+
vector <char> candidates(n);
38+
for(long long i = 0; i < n; i++)
39+
candidates[i] = ((i + 1) + '0');
40+
vector <long long> fact(n + 1);
41+
fact[0] = 1;
42+
for(long long i = 1; i <= n; i++)
43+
fact[i] = fact[i - 1] * i;
44+
k--;
45+
for(long long i = n - 1; i >= 0; i--){
46+
long long idx = k / fact[i];
47+
ans += candidates[idx];
48+
for(long long j = idx; j + 1< candidates.size(); j++)
49+
candidates[j] = candidates[j + 1];
50+
k = k % fact[i];
51+
}
52+
return ans;
53+
}
54+
};

21.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
3+
Dungeon Game
4+
------------
5+
6+
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M x N rooms laid out in a 2D grid. Our valiant knight (K) was initially positioned in the top-left room and must fight his way through the dungeon to rescue the princess.
7+
8+
The knight has an initial health point represented by a positive integer. If at any point his health point drops to 0 or below, he dies immediately.
9+
10+
Some of the rooms are guarded by demons, so the knight loses health (negative integers) upon entering these rooms; other rooms are either empty (0's) or contain magic orbs that increase the knight's health (positive integers).
11+
12+
In order to reach the princess as quickly as possible, the knight decides to move only rightward or downward in each step.
13+
14+
15+
16+
Write a function to determine the knight's minimum initial health so that he is able to rescue the princess.
17+
18+
For example, given the dungeon below, the initial health of the knight must be at least 7 if he follows the optimal path RIGHT-> RIGHT -> DOWN -> DOWN.
19+
20+
-2 (K) -3 3
21+
-5 -10 1
22+
10 30 -5 (P)
23+
24+
25+
Note:
26+
27+
The knight's health has no upper bound.
28+
Any room can contain threats or power-ups, even the first room the knight enters and the bottom-right room where the princess is imprisoned.
29+
30+
*/
31+
32+
class Solution {
33+
public:
34+
int calculateMinimumHP(vector<vector<int>>& dungeon) {
35+
if(dungeon.size()==0) return 0;
36+
37+
int row=dungeon.size();
38+
int col=dungeon[0].size();
39+
40+
for(int i=row-1; i>=0; i--) {
41+
42+
for(int j=col-1; j>=0; j--) {
43+
44+
if(i==row-1 && j==col-1) dungeon[i][j]=max(1, 1-dungeon[i][j]);
45+
else if(i==row-1) dungeon[i][j]=max(1, dungeon[i][j+1]-dungeon[i][j]);
46+
else if(j==col-1) dungeon[i][j]=max(1, dungeon[i+1][j]-dungeon[i][j]);
47+
else dungeon[i][j]=max(1, min(dungeon[i+1][j], dungeon[i][j+1])-dungeon[i][j]);
48+
}
49+
}
50+
51+
return dungeon[0][0];
52+
}
53+
};

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@
1919
| 15 | Search in a Binary Search Tree | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/15.cpp) |
2020
| 16 | Validate IP Address | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/16.cpp) |
2121
| 17 | Surrounded Regions | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/17.cpp) |
22-
| 18 | H-Index II | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/18.cpp) |
22+
| 18 | H-Index II | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/18.cpp) |
23+
| 19 | Longest Duplicate Substring | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/19.cpp) |
24+
| 20 | Permutation Sequence | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/20.cpp) |
25+
| 21 | Dungeon Game | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/21.cpp) |

0 commit comments

Comments
 (0)