id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
2,573 | <p>You are given the <code>head</code> of a linked list.</p>
<p>Remove every node which has a node with a greater value anywhere to the right side of it.</p>
<p>Return <em>the </em><code>head</code><em> of the modified linked list.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/10/02/drawio.png" style="width: 631px; height: 51px;" />
<pre>
<strong>Input:</strong> head = [5,2,13,3,8]
<strong>Output:</strong> [13,8]
<strong>Explanation:</strong> The nodes that should be removed are 5, 2 and 3.
- Node 13 is to the right of node 5.
- Node 13 is to the right of node 2.
- Node 8 is to the right of node 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> head = [1,1,1,1]
<strong>Output:</strong> [1,1,1,1]
<strong>Explanation:</strong> Every node has value 1, so no nodes are removed.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the given list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* removeNodes(ListNode* head) {\n if(head==NULL)return NULL;\n vector<int>ans;\n ListNode* temp=head;\n while(temp!=NULL){\n ans.push_back(temp->val);\n temp=temp->next;\n }\n int n=ans.size();\n stack<int>s;\n for(int i=n-1;i>=0;i--){\n if(s.empty()){\n s.push(ans[i]);\n }\n else{\n if(ans[i]>=s.top()){\n s.push(ans[i]);\n }\n }\n }\n vector<int>ans1;\n head=new ListNode(-1);\n temp=head;\n while(!s.empty()){\n ListNode* node=new ListNode(s.top());\n s.pop();\n head->next=node;\n head=head->next;\n }\n \n \n \n \n return temp->next;\n\n }\n};",
"memory": "185393"
} |
2,360 | <p>The <strong>variance</strong> of a string is defined as the largest difference between the number of occurrences of <strong>any</strong> <code>2</code> characters present in the string. Note the two characters may or may not be the same.</p>
<p>Given a string <code>s</code> consisting of lowercase English letters only, return <em>the <strong>largest variance</strong> possible among all <strong>substrings</strong> of</em> <code>s</code>.</p>
<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbb"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
All possible variances along with their respective substrings are listed below:
- Variance 0 for substrings "a", "aa", "ab", "abab", "aababb", "ba", "b", "bb", and "bbb".
- Variance 1 for substrings "aab", "aba", "abb", "aabab", "ababb", "aababbb", and "bab".
- Variance 2 for substrings "aaba", "ababbb", "abbb", and "babb".
- Variance 3 for substring "babbb".
Since the largest possible variance is 3, we return it.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcde"
<strong>Output:</strong> 0
<strong>Explanation:</strong>
No letter occurs more than once in s, so the variance of every substring is 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int largestVariance(string s) {\n //26 * 26\n int largeVar = 0;\n\n for (char c1 = 'a'; c1 <= 'z'; c1++) {\n for (char c2 = 'a'; c2 <= 'z'; c2++) {\n if (c1 == c2)\n continue;\n\n int num_c1 = 0;\n int num_c2 = 0;\n\n for (int i = 0; i < s.length(); i++) {\n if (s[i] == c1) {\n num_c1++;\n } else if (s[i] == c2) {\n num_c2++;\n }\n \n if (num_c1 < num_c2) {\n num_c1 = 0;\n num_c2 = 0;\n }\n \n if (num_c1 > 0 && num_c2 > 0)\n largeVar = max(largeVar, num_c1 - num_c2);\n }\n \n num_c1 = 0;\n num_c2 = 0;\n for (int i = s.length() - 1; i >= 0; i--) {\n if (s[i] == c1) {\n num_c1++;\n } else if (s[i] == c2) {\n num_c2++;\n }\n \n if (num_c1 < num_c2) {\n num_c1 = 0;\n num_c2 = 0;\n }\n \n if (num_c1 > 0 && num_c2 > 0)\n largeVar = max(largeVar, num_c1 - num_c2);\n }\n }\n }\n\n return largeVar;\n }\n};",
"memory": "8100"
} |
2,360 | <p>The <strong>variance</strong> of a string is defined as the largest difference between the number of occurrences of <strong>any</strong> <code>2</code> characters present in the string. Note the two characters may or may not be the same.</p>
<p>Given a string <code>s</code> consisting of lowercase English letters only, return <em>the <strong>largest variance</strong> possible among all <strong>substrings</strong> of</em> <code>s</code>.</p>
<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbb"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
All possible variances along with their respective substrings are listed below:
- Variance 0 for substrings "a", "aa", "ab", "abab", "aababb", "ba", "b", "bb", and "bbb".
- Variance 1 for substrings "aab", "aba", "abb", "aabab", "ababb", "aababbb", and "bab".
- Variance 2 for substrings "aaba", "ababbb", "abbb", and "babb".
- Variance 3 for substring "babbb".
Since the largest possible variance is 3, we return it.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcde"
<strong>Output:</strong> 0
<strong>Explanation:</strong>
No letter occurs more than once in s, so the variance of every substring is 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int largestVariance(string s) {\n //26 * 26\n int largeVar = 0;\n\n for (char c1 = 'a'; c1 <= 'z'; c1++) {\n for (char c2 = 'a'; c2 <= 'z'; c2++) {\n if (c1 == c2)\n continue;\n\n int c1_inds = 0;\n int c2_inds = 0;\n bool first = false;\n\n for (int i = 0; i < s.length(); i++) {\n if (s[i] == c1) {\n c1_inds ++;\n } else if (s[i] == c2) {\n c2_inds ++;\n\n if (c2_inds == 2 && first) {\n c2_inds--;\n first = false;\n }\n\n }\n\n if (c1_inds < c2_inds) {\n c1_inds = 0;\n c2_inds = 1;\n first = true;\n }\n\n if (c1_inds > 0 && c2_inds > 0) {\n largeVar = max(largeVar, abs(c1_inds - c2_inds));\n }\n }\n }\n }\n return largeVar;\n }\n};",
"memory": "8100"
} |
2,360 | <p>The <strong>variance</strong> of a string is defined as the largest difference between the number of occurrences of <strong>any</strong> <code>2</code> characters present in the string. Note the two characters may or may not be the same.</p>
<p>Given a string <code>s</code> consisting of lowercase English letters only, return <em>the <strong>largest variance</strong> possible among all <strong>substrings</strong> of</em> <code>s</code>.</p>
<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbb"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
All possible variances along with their respective substrings are listed below:
- Variance 0 for substrings "a", "aa", "ab", "abab", "aababb", "ba", "b", "bb", and "bbb".
- Variance 1 for substrings "aab", "aba", "abb", "aabab", "ababb", "aababbb", and "bab".
- Variance 2 for substrings "aaba", "ababbb", "abbb", and "babb".
- Variance 3 for substring "babbb".
Since the largest possible variance is 3, we return it.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcde"
<strong>Output:</strong> 0
<strong>Explanation:</strong>
No letter occurs more than once in s, so the variance of every substring is 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int largestVariance(string s) {\n vector<int> counter(26, 0);\n for(auto &ch: s) {\n counter[ch - 'a']++;\n }\n\n int globalMax = 0;\n\n for(int i = 0; i < 26; i++) {\n for(int j = 0; j < 26; j++) {\n if(i == j || counter[i] == 0 || counter[j] == 0) continue;\n\n char major = 'a' + i;\n char minor = 'a' + j;\n int majorCount = 0;\n int minorCount = 0;\n\n int restMinor = counter[j];\n\n for(auto ch: s) {\n if(ch == major) majorCount++;\n if(ch == minor) {\n minorCount++;\n restMinor--;\n } \n\n if(minorCount > 0) \n globalMax = max(globalMax, majorCount - minorCount);\n \n if(majorCount < minorCount && restMinor > 0) {\n minorCount = 0;\n majorCount = 0;\n }\n }\n }\n }\n\n return globalMax;\n }\n};",
"memory": "8200"
} |
2,360 | <p>The <strong>variance</strong> of a string is defined as the largest difference between the number of occurrences of <strong>any</strong> <code>2</code> characters present in the string. Note the two characters may or may not be the same.</p>
<p>Given a string <code>s</code> consisting of lowercase English letters only, return <em>the <strong>largest variance</strong> possible among all <strong>substrings</strong> of</em> <code>s</code>.</p>
<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbb"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
All possible variances along with their respective substrings are listed below:
- Variance 0 for substrings "a", "aa", "ab", "abab", "aababb", "ba", "b", "bb", and "bbb".
- Variance 1 for substrings "aab", "aba", "abb", "aabab", "ababb", "aababbb", and "bab".
- Variance 2 for substrings "aaba", "ababbb", "abbb", and "babb".
- Variance 3 for substring "babbb".
Since the largest possible variance is 3, we return it.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcde"
<strong>Output:</strong> 0
<strong>Explanation:</strong>
No letter occurs more than once in s, so the variance of every substring is 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int largestVariance(string s) {\n vector<int>count(26,0);\n for(char &ch:s){\n count[ch-'a']=1;\n }\n int result=0;\n for(char f='a';f<='z';f++){\n for(char se='a';se<='z';se++){\n if(count[f-'a']==0 || count[se-'a']==0){\n continue;\n }\n int fc=0;\n int sc=0;\n bool past=0;\n for(char &ch:s){\n if(ch==f){\n fc++;\n }\n if(ch==se){\n sc++;\n }\n if(sc>0){\n result=max(result,fc-sc);\n }else{\n if(past==true){\n result=max(result,fc-1);\n }\n }\n if(sc>fc){\n fc=0;\n sc=0;\n past=true;\n }\n\n }\n }\n }\n return result;\n }\n};",
"memory": "8300"
} |
2,360 | <p>The <strong>variance</strong> of a string is defined as the largest difference between the number of occurrences of <strong>any</strong> <code>2</code> characters present in the string. Note the two characters may or may not be the same.</p>
<p>Given a string <code>s</code> consisting of lowercase English letters only, return <em>the <strong>largest variance</strong> possible among all <strong>substrings</strong> of</em> <code>s</code>.</p>
<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbb"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
All possible variances along with their respective substrings are listed below:
- Variance 0 for substrings "a", "aa", "ab", "abab", "aababb", "ba", "b", "bb", and "bbb".
- Variance 1 for substrings "aab", "aba", "abb", "aabab", "ababb", "aababbb", and "bab".
- Variance 2 for substrings "aaba", "ababbb", "abbb", and "babb".
- Variance 3 for substring "babbb".
Since the largest possible variance is 3, we return it.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcde"
<strong>Output:</strong> 0
<strong>Explanation:</strong>
No letter occurs more than once in s, so the variance of every substring is 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int largestVariance(string s) {\n int result=0;\n for(char f='a';f<='z';f++){\n for(char se='a';se<='z';se++){\n int fc=0;\n int sc=0;\n bool past=0;\n for(char &ch:s){\n if(ch==f){\n fc++;\n }\n if(ch==se){\n sc++;\n }\n if(sc>0){\n result=max(result,fc-sc);\n }else{\n if(past==true){\n result=max(result,fc-1);\n }\n }\n if(sc>fc){\n fc=0;\n sc=0;\n past=true;\n }\n\n }\n }\n }\n return result;\n }\n};",
"memory": "8400"
} |
2,360 | <p>The <strong>variance</strong> of a string is defined as the largest difference between the number of occurrences of <strong>any</strong> <code>2</code> characters present in the string. Note the two characters may or may not be the same.</p>
<p>Given a string <code>s</code> consisting of lowercase English letters only, return <em>the <strong>largest variance</strong> possible among all <strong>substrings</strong> of</em> <code>s</code>.</p>
<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbb"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
All possible variances along with their respective substrings are listed below:
- Variance 0 for substrings "a", "aa", "ab", "abab", "aababb", "ba", "b", "bb", and "bbb".
- Variance 1 for substrings "aab", "aba", "abb", "aabab", "ababb", "aababbb", and "bab".
- Variance 2 for substrings "aaba", "ababbb", "abbb", and "babb".
- Variance 3 for substring "babbb".
Since the largest possible variance is 3, we return it.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcde"
<strong>Output:</strong> 0
<strong>Explanation:</strong>
No letter occurs more than once in s, so the variance of every substring is 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int largestVariance(string s) {\n vector<int>count(26,0);\n for(char &ch:s){\n count[ch-'a']=1;\n }\n int result=0;\n for(char f='a';f<='z';f++){\n for(char se='a';se<='z';se++){\n if(count[f-'a']==0 || count[se-'a']==0){\n continue;\n }\n int fc=0;\n int sc=0;\n bool past=0;\n for(char &ch:s){\n if(ch==f){\n fc++;\n }\n if(ch==se){\n sc++;\n }\n if(sc>0){\n result=max(result,fc-sc);\n }else{\n if(past==true){\n result=max(result,fc-1);\n }\n }\n if(sc>fc){\n fc=0;\n sc=0;\n past=true;\n }\n\n }\n }\n }\n return result;\n }\n};",
"memory": "8500"
} |
2,360 | <p>The <strong>variance</strong> of a string is defined as the largest difference between the number of occurrences of <strong>any</strong> <code>2</code> characters present in the string. Note the two characters may or may not be the same.</p>
<p>Given a string <code>s</code> consisting of lowercase English letters only, return <em>the <strong>largest variance</strong> possible among all <strong>substrings</strong> of</em> <code>s</code>.</p>
<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbb"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
All possible variances along with their respective substrings are listed below:
- Variance 0 for substrings "a", "aa", "ab", "abab", "aababb", "ba", "b", "bb", and "bbb".
- Variance 1 for substrings "aab", "aba", "abb", "aabab", "ababb", "aababbb", and "bab".
- Variance 2 for substrings "aaba", "ababbb", "abbb", and "babb".
- Variance 3 for substring "babbb".
Since the largest possible variance is 3, we return it.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcde"
<strong>Output:</strong> 0
<strong>Explanation:</strong>
No letter occurs more than once in s, so the variance of every substring is 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int largestVariance(string s) {\n vector<int>count(26,0);\n for(char &ch:s){\n count[ch-'a']=1;\n }\n int result=0;\n for(char f='a';f<='z';f++){\n for(char se='a';se<='z';se++){\n int fc=0;\n int sc=0;\n bool past=0;\n for(char &ch:s){\n if(ch==f){\n fc++;\n }\n if(ch==se){\n sc++;\n }\n if(sc>0){\n result=max(result,fc-sc);\n }else{\n if(past==true){\n result=max(result,fc-1);\n }\n }\n if(sc>fc){\n fc=0;\n sc=0;\n past=true;\n }\n\n }\n }\n }\n return result;\n }\n};",
"memory": "8500"
} |
2,360 | <p>The <strong>variance</strong> of a string is defined as the largest difference between the number of occurrences of <strong>any</strong> <code>2</code> characters present in the string. Note the two characters may or may not be the same.</p>
<p>Given a string <code>s</code> consisting of lowercase English letters only, return <em>the <strong>largest variance</strong> possible among all <strong>substrings</strong> of</em> <code>s</code>.</p>
<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbb"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
All possible variances along with their respective substrings are listed below:
- Variance 0 for substrings "a", "aa", "ab", "abab", "aababb", "ba", "b", "bb", and "bbb".
- Variance 1 for substrings "aab", "aba", "abb", "aabab", "ababb", "aababbb", and "bab".
- Variance 2 for substrings "aaba", "ababbb", "abbb", and "babb".
- Variance 3 for substring "babbb".
Since the largest possible variance is 3, we return it.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcde"
<strong>Output:</strong> 0
<strong>Explanation:</strong>
No letter occurs more than once in s, so the variance of every substring is 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int largestVariance(string s) {\n vector<int> counter(26, 0);\n for (char ch : s) {\n counter[ch - 'a']++;\n }\n int globalMax = 0;\n \n for (int i = 0; i < 26; i++) {\n for (int j = 0; j < 26; j++) {\n // major and minor cannot be the same, and both must appear in s.\n if (i == j || counter[i] == 0 || counter[j] == 0) {\n continue;\n }\n \n // Find the maximum variance of major - minor. \n char major = 'a' + i;\n char minor = 'a' + j;\n int majorCount = 0;\n int minorCount = 0;\n \n // The remaining minor in the rest of s.\n int restMinor = counter[j];\n \n for (char ch : s) { \n if (ch == major) {\n majorCount++;\n }\n if (ch == minor) {\n minorCount++;\n restMinor--;\n }\n \n // Only update the variance (local_max) if there is at least one minor.\n if (minorCount > 0)\n globalMax = max(globalMax, majorCount - minorCount);\n \n // We can discard the previous string if there is at least one remaining minor\n if (majorCount < minorCount && restMinor > 0) {\n majorCount = 0;\n minorCount = 0;\n }\n }\n }\n }\n \n return globalMax;\n }\n};\n",
"memory": "8600"
} |
2,360 | <p>The <strong>variance</strong> of a string is defined as the largest difference between the number of occurrences of <strong>any</strong> <code>2</code> characters present in the string. Note the two characters may or may not be the same.</p>
<p>Given a string <code>s</code> consisting of lowercase English letters only, return <em>the <strong>largest variance</strong> possible among all <strong>substrings</strong> of</em> <code>s</code>.</p>
<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbb"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
All possible variances along with their respective substrings are listed below:
- Variance 0 for substrings "a", "aa", "ab", "abab", "aababb", "ba", "b", "bb", and "bbb".
- Variance 1 for substrings "aab", "aba", "abb", "aabab", "ababb", "aababbb", and "bab".
- Variance 2 for substrings "aaba", "ababbb", "abbb", and "babb".
- Variance 3 for substring "babbb".
Since the largest possible variance is 3, we return it.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcde"
<strong>Output:</strong> 0
<strong>Explanation:</strong>
No letter occurs more than once in s, so the variance of every substring is 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int largestVariance(string s) {\n set<char> cset(s.begin(), s.end());\n int best = 0;\n for (auto&a : cset) {\n for (auto& b : cset) {\n int var = 0;\n bool has_b = false;\n bool start_with_b = false;\n for (int i=0;i<(int)s.size();i++) {\n if (s[i] == a) {\n var++;\n } else if (s[i] == b) {\n has_b = true;\n if (start_with_b && var >=0) {\n start_with_b = false;\n } else {\n var--;\n if (var <= -1) {\n var = -1;\n start_with_b = true;\n }\n }\n } else {\n continue;\n }\n best = max(best, has_b ? var : 0);\n }\n }\n }\n return best;\n }\n};",
"memory": "8700"
} |
2,360 | <p>The <strong>variance</strong> of a string is defined as the largest difference between the number of occurrences of <strong>any</strong> <code>2</code> characters present in the string. Note the two characters may or may not be the same.</p>
<p>Given a string <code>s</code> consisting of lowercase English letters only, return <em>the <strong>largest variance</strong> possible among all <strong>substrings</strong> of</em> <code>s</code>.</p>
<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbb"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
All possible variances along with their respective substrings are listed below:
- Variance 0 for substrings "a", "aa", "ab", "abab", "aababb", "ba", "b", "bb", and "bbb".
- Variance 1 for substrings "aab", "aba", "abb", "aabab", "ababb", "aababbb", and "bab".
- Variance 2 for substrings "aaba", "ababbb", "abbb", and "babb".
- Variance 3 for substring "babbb".
Since the largest possible variance is 3, we return it.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcde"
<strong>Output:</strong> 0
<strong>Explanation:</strong>
No letter occurs more than once in s, so the variance of every substring is 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n\n int solve(string s)\n {\n int ans = 0;\n\n for(char i ='a'; i<='z'; i++)\n {\n for(char j = 'a'; j<='z'; j++)\n {\n if(i!=j)\n {\n int cnt1 = 0;\n int cnt2 =0;\n for(int k=0; k<s.size(); k++)\n {\n if(s[k]==i)\n {\n cnt1++;\n }\n if(s[k]==j)\n {\n cnt2++;\n }\n if(cnt2>cnt1)\n {\n cnt1 = 0;\n cnt2 = 0;\n }\n\n if(cnt1>0 && cnt2>0)\n {\n ans = max(ans,cnt1-cnt2);\n }\n }\n \n }\n }\n }\n\n return ans;\n }\n\n\n int largestVariance(string s) {\n int a = solve(s);\n reverse(s.begin(),s.end());\n int b = solve(s);\n\n return max(a,b);\n\n }\n};",
"memory": "8800"
} |
2,360 | <p>The <strong>variance</strong> of a string is defined as the largest difference between the number of occurrences of <strong>any</strong> <code>2</code> characters present in the string. Note the two characters may or may not be the same.</p>
<p>Given a string <code>s</code> consisting of lowercase English letters only, return <em>the <strong>largest variance</strong> possible among all <strong>substrings</strong> of</em> <code>s</code>.</p>
<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbb"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
All possible variances along with their respective substrings are listed below:
- Variance 0 for substrings "a", "aa", "ab", "abab", "aababb", "ba", "b", "bb", and "bbb".
- Variance 1 for substrings "aab", "aba", "abb", "aabab", "ababb", "aababbb", and "bab".
- Variance 2 for substrings "aaba", "ababbb", "abbb", and "babb".
- Variance 3 for substring "babbb".
Since the largest possible variance is 3, we return it.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcde"
<strong>Output:</strong> 0
<strong>Explanation:</strong>
No letter occurs more than once in s, so the variance of every substring is 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int solve(string s)\n {\n int n=s.size();\n int ans=0;\n for(int i=0;i<26;i++){\n for(int j=0;j<26;j++){\n int l=0,h=0,cnt1=0,cnt2=0;\n if(i==j)continue;\n while(h<n){\n if((s[h]-'a')==i){\n cnt2++;\n }\n if((s[h]-'a')==j){\n cnt1++;\n }\n while(l<n&&cnt1>cnt2){\n if((s[l]-'a')==i){\n cnt2--;\n }\n if((s[l]-'a')==j){\n cnt1--;\n } \n l++;\n }\n h++;\n if(cnt1>0&&cnt2>0)\n ans=max(ans,cnt2-cnt1);\n }\n }\n }\n return ans; \n } \n int largestVariance(string s) {\n string s1=s;\n reverse(s1.begin(),s1.end());\n return max(solve(s),solve(s1));\n }\n};",
"memory": "9000"
} |
2,360 | <p>The <strong>variance</strong> of a string is defined as the largest difference between the number of occurrences of <strong>any</strong> <code>2</code> characters present in the string. Note the two characters may or may not be the same.</p>
<p>Given a string <code>s</code> consisting of lowercase English letters only, return <em>the <strong>largest variance</strong> possible among all <strong>substrings</strong> of</em> <code>s</code>.</p>
<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbb"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
All possible variances along with their respective substrings are listed below:
- Variance 0 for substrings "a", "aa", "ab", "abab", "aababb", "ba", "b", "bb", and "bbb".
- Variance 1 for substrings "aab", "aba", "abb", "aabab", "ababb", "aababbb", and "bab".
- Variance 2 for substrings "aaba", "ababbb", "abbb", and "babb".
- Variance 3 for substring "babbb".
Since the largest possible variance is 3, we return it.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcde"
<strong>Output:</strong> 0
<strong>Explanation:</strong>
No letter occurs more than once in s, so the variance of every substring is 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int largestVariance(string s) {\n int ans = 0;\n int len = s.size();\n vector<int> dp(len + 1, 0);\n for (int i = 0; i < 26; i++) {\n for (int j = i + 1; j < 26; j++) {\n char x = i + 'a';\n char y = j + 'a';\n int minimum = 0, maximum = 0;\n int curans = 0;\n int cur = 0;\n dp[0] = 0;\n int index = -1;\n int xindex = -1;\n int yindex = -1;\n bool hasData = false;\n for (int k = 0; k < len; k++) {\n if (s[k] == x) {\n xindex = k + 1;\n cur++;\n } else if (s[k] == y) {\n yindex = k + 1;\n cur--;\n }\n dp[k + 1] = cur;\n int maxindex = min(xindex, yindex);\n while (index + 1 < maxindex) {\n index++;\n minimum = min(minimum, dp[index]);\n maximum = max(maximum, dp[index]);\n hasData = true;\n }\n if (hasData) {\n curans = max(curans, abs(cur - minimum));\n curans = max(curans, abs(cur - maximum));\n }\n }\n ans = max(ans, curans);\n }\n }\n return ans;\n }\n};",
"memory": "9100"
} |
2,360 | <p>The <strong>variance</strong> of a string is defined as the largest difference between the number of occurrences of <strong>any</strong> <code>2</code> characters present in the string. Note the two characters may or may not be the same.</p>
<p>Given a string <code>s</code> consisting of lowercase English letters only, return <em>the <strong>largest variance</strong> possible among all <strong>substrings</strong> of</em> <code>s</code>.</p>
<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbb"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
All possible variances along with their respective substrings are listed below:
- Variance 0 for substrings "a", "aa", "ab", "abab", "aababb", "ba", "b", "bb", and "bbb".
- Variance 1 for substrings "aab", "aba", "abb", "aabab", "ababb", "aababbb", and "bab".
- Variance 2 for substrings "aaba", "ababbb", "abbb", and "babb".
- Variance 3 for substring "babbb".
Since the largest possible variance is 3, we return it.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcde"
<strong>Output:</strong> 0
<strong>Explanation:</strong>
No letter occurs more than once in s, so the variance of every substring is 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "#include <string>\nusing namespace std;\n\nclass Solution {\npublic:\n int largestVariance(string s) {\n int ans = 0;\n int len = s.size();\n vector<int> dp(len + 1, 0);\n for (int i = 0; i < 26; i++) {\n for (int j = i + 1; j < 26; j++) {\n char x = i + 'a';\n char y = j + 'a';\n int minimum = 0, maximum = 0;\n int curans = 0;\n int cur = 0;\n dp[0] = 0;\n int index = -1;\n int xindex = -1;\n int yindex = -1;\n for (int k = 0; k < len; k++) {\n if (s[k] == x) {\n xindex = k + 1;\n cur++;\n } else if (s[k] == y) {\n yindex = k + 1;\n cur--;\n }\n dp[k + 1] = cur;\n int maxindex = min(xindex, yindex);\n while (index + 1 < maxindex) {\n index++;\n minimum = min(minimum, dp[index]);\n maximum = max(maximum, dp[index]);\n }\n if (index >= 0) {\n curans = max(curans, abs(cur - minimum));\n curans = max(curans, abs(cur - maximum));\n }\n }\n ans = max(ans, curans);\n }\n }\n return ans;\n }\n};",
"memory": "9200"
} |
2,360 | <p>The <strong>variance</strong> of a string is defined as the largest difference between the number of occurrences of <strong>any</strong> <code>2</code> characters present in the string. Note the two characters may or may not be the same.</p>
<p>Given a string <code>s</code> consisting of lowercase English letters only, return <em>the <strong>largest variance</strong> possible among all <strong>substrings</strong> of</em> <code>s</code>.</p>
<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbb"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
All possible variances along with their respective substrings are listed below:
- Variance 0 for substrings "a", "aa", "ab", "abab", "aababb", "ba", "b", "bb", and "bbb".
- Variance 1 for substrings "aab", "aba", "abb", "aabab", "ababb", "aababbb", and "bab".
- Variance 2 for substrings "aaba", "ababbb", "abbb", and "babb".
- Variance 3 for substring "babbb".
Since the largest possible variance is 3, we return it.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcde"
<strong>Output:</strong> 0
<strong>Explanation:</strong>
No letter occurs more than once in s, so the variance of every substring is 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int largestVariance(string s) {\n unordered_set<char> st;\n for(auto it : s) st.insert(it);\n int result = 0;\n for(char i='a';i<='z';i++){\n for(char j='a';j<='z';j++){\n if((st.find(i)==st.end()) || (st.find(i)==st.end())) continue;\n\n int f1 = 0;\n int f2 = 0;\n\n bool past = false;\n for(auto it : s){\n if(it==i){\n f1++;\n }\n if(it==j){\n f2++;\n }\n\n if(f2 > 0){\n result = max(result,f1-f2);\n }else {\n if(past==true) {\n result = max(result,f1-1);\n }\n }\n\n if(f2>f1){\n f1=0;\n f2=0;\n past = true;\n }\n }\n }\n }\n\n return result;\n }\n};",
"memory": "9600"
} |
2,360 | <p>The <strong>variance</strong> of a string is defined as the largest difference between the number of occurrences of <strong>any</strong> <code>2</code> characters present in the string. Note the two characters may or may not be the same.</p>
<p>Given a string <code>s</code> consisting of lowercase English letters only, return <em>the <strong>largest variance</strong> possible among all <strong>substrings</strong> of</em> <code>s</code>.</p>
<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbb"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
All possible variances along with their respective substrings are listed below:
- Variance 0 for substrings "a", "aa", "ab", "abab", "aababb", "ba", "b", "bb", and "bbb".
- Variance 1 for substrings "aab", "aba", "abb", "aabab", "ababb", "aababbb", and "bab".
- Variance 2 for substrings "aaba", "ababbb", "abbb", and "babb".
- Variance 3 for substring "babbb".
Since the largest possible variance is 3, we return it.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcde"
<strong>Output:</strong> 0
<strong>Explanation:</strong>
No letter occurs more than once in s, so the variance of every substring is 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int largestVariance(string s) {\n int max1=0;\n unordered_set<char>s1(s.begin(),s.end());\n for(char c1:s1){\n for(char c2:s1){\n if(c1==c2) continue;\n int count1=0;\n int count2=0;\n bool d=false;\n for(char ch:s){\n if(ch==c1) count1++;\n if(ch==c2) count2++;\n if(count2>0){\n max1=max(max1,count1-count2);\n }\n else{\n if(d){\n max1=max(max1,count1-1);\n }\n }\n if(count1<count2){\n count1=0;\n count2=0;\n d=true;\n }\n }\n }\n }\n return max1;\n }\n};\n",
"memory": "9700"
} |
2,360 | <p>The <strong>variance</strong> of a string is defined as the largest difference between the number of occurrences of <strong>any</strong> <code>2</code> characters present in the string. Note the two characters may or may not be the same.</p>
<p>Given a string <code>s</code> consisting of lowercase English letters only, return <em>the <strong>largest variance</strong> possible among all <strong>substrings</strong> of</em> <code>s</code>.</p>
<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbb"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
All possible variances along with their respective substrings are listed below:
- Variance 0 for substrings "a", "aa", "ab", "abab", "aababb", "ba", "b", "bb", and "bbb".
- Variance 1 for substrings "aab", "aba", "abb", "aabab", "ababb", "aababbb", and "bab".
- Variance 2 for substrings "aaba", "ababbb", "abbb", and "babb".
- Variance 3 for substring "babbb".
Since the largest possible variance is 3, we return it.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcde"
<strong>Output:</strong> 0
<strong>Explanation:</strong>
No letter occurs more than once in s, so the variance of every substring is 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int largestVariance(string s) {\n int max1=0;\n unordered_set<char>s1(s.begin(),s.end());\n for(char c1:s1){\n for(char c2:s1){\n if(c1==c2) continue;\n int count1=0;\n int count2=0;\n bool d=false;\n for(char ch:s){\n if(ch==c1) count1++;\n if(ch==c2) count2++;\n if(count2>0){\n max1=max(max1,count1-count2);\n }\n else{\n if(d){\n max1=max(max1,count1-1);\n }\n }\n if(count1<count2){\n count1=0;\n count2=0;\n d=true;\n }\n }\n }\n }\n return max1;\n }\n};\n",
"memory": "9800"
} |
2,360 | <p>The <strong>variance</strong> of a string is defined as the largest difference between the number of occurrences of <strong>any</strong> <code>2</code> characters present in the string. Note the two characters may or may not be the same.</p>
<p>Given a string <code>s</code> consisting of lowercase English letters only, return <em>the <strong>largest variance</strong> possible among all <strong>substrings</strong> of</em> <code>s</code>.</p>
<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbb"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
All possible variances along with their respective substrings are listed below:
- Variance 0 for substrings "a", "aa", "ab", "abab", "aababb", "ba", "b", "bb", and "bbb".
- Variance 1 for substrings "aab", "aba", "abb", "aabab", "ababb", "aababbb", and "bab".
- Variance 2 for substrings "aaba", "ababbb", "abbb", and "babb".
- Variance 3 for substring "babbb".
Since the largest possible variance is 3, we return it.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcde"
<strong>Output:</strong> 0
<strong>Explanation:</strong>
No letter occurs more than once in s, so the variance of every substring is 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution \n{\n public:\n int largestVariance(string s) \n {\n /* Modified Kadane's Algorithm */\n int n = s.length();\n unordered_map<char,int> freq;\n for(char ch:s)\n {\n freq[ch]++;\n }\n int ans = 0;\n // try all possible combination of pairs of characters\n for(char ch1='a'; ch1<='z'; ch1++)\n {\n for(char ch2='a'; ch2<='z'; ch2++)\n {\n if(ch1==ch2 || freq[ch1]==0 || freq[ch2]==0)\n {\n continue;\n }\n\n for(int k=1; k<=2; k++)\n {\n // apply modified kadane's\n\n int cnt1 = 0,cnt2 = 0;\n for(int i=0; i<n; i++)\n {\n cnt1 += s[i]==ch1;\n cnt2 += s[i]==ch2;\n if(cnt1 < cnt2)\n {\n cnt1 = cnt2 = 0;\n }\n if(cnt1 > 0 && cnt2 > 0)\n {\n ans = max(ans,cnt1-cnt2); \n }\n }\n\n reverse(s.begin(),s.end());\n }\n }\n }\n return ans;\n }\n};",
"memory": "9900"
} |
2,360 | <p>The <strong>variance</strong> of a string is defined as the largest difference between the number of occurrences of <strong>any</strong> <code>2</code> characters present in the string. Note the two characters may or may not be the same.</p>
<p>Given a string <code>s</code> consisting of lowercase English letters only, return <em>the <strong>largest variance</strong> possible among all <strong>substrings</strong> of</em> <code>s</code>.</p>
<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbb"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
All possible variances along with their respective substrings are listed below:
- Variance 0 for substrings "a", "aa", "ab", "abab", "aababb", "ba", "b", "bb", and "bbb".
- Variance 1 for substrings "aab", "aba", "abb", "aabab", "ababb", "aababbb", and "bab".
- Variance 2 for substrings "aaba", "ababbb", "abbb", and "babb".
- Variance 3 for substring "babbb".
Since the largest possible variance is 3, we return it.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcde"
<strong>Output:</strong> 0
<strong>Explanation:</strong>
No letter occurs more than once in s, so the variance of every substring is 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int largestVariance(string s) {\n unordered_map<char,int> freq;\n for(auto & c:s)\n freq[c]++;\n int ans=0;\n \n for(char ch1='a';ch1<='z';ch1++){\n for(char ch2='a';ch2<='z';ch2++){\n if(ch1==ch2||freq[ch1]==0||freq[ch2]==0)\n continue;\n for(int r=1;r<=2;r++){\n int cnt1=0,cnt2=0;\n for(auto& c: s){\n cnt1+=ch1==c;\n cnt2+=ch2==c;\n if(cnt1<cnt2)\n cnt1=cnt2=0;\n if(cnt1>0&&cnt2>0)\n ans=max(ans,cnt1-cnt2);\n }\n reverse(s.begin(),s.end());\n }\n }\n }\n return ans;\n }\n};",
"memory": "10000"
} |
2,360 | <p>The <strong>variance</strong> of a string is defined as the largest difference between the number of occurrences of <strong>any</strong> <code>2</code> characters present in the string. Note the two characters may or may not be the same.</p>
<p>Given a string <code>s</code> consisting of lowercase English letters only, return <em>the <strong>largest variance</strong> possible among all <strong>substrings</strong> of</em> <code>s</code>.</p>
<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbb"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
All possible variances along with their respective substrings are listed below:
- Variance 0 for substrings "a", "aa", "ab", "abab", "aababb", "ba", "b", "bb", and "bbb".
- Variance 1 for substrings "aab", "aba", "abb", "aabab", "ababb", "aababbb", and "bab".
- Variance 2 for substrings "aaba", "ababbb", "abbb", and "babb".
- Variance 3 for substring "babbb".
Since the largest possible variance is 3, we return it.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcde"
<strong>Output:</strong> 0
<strong>Explanation:</strong>
No letter occurs more than once in s, so the variance of every substring is 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int largestVariance(string s) {\n int maxi=0;\n map<char,int>m;\n for(int i=0;i<s.size();i++)\n {\n m[s[i]]++;\n }\n for(char a='a';a <='z';a++)\n {\n for(char b='a';b <='z';b++)\n {\n if(m[a]>0&&m[b]>0)\n {\n for(int k=0;k<2;k++)\n {\n int h=0;\n int c=0;\n int c1=0;\n for(int i=0;i<s.size();i++)\n {\n if(s[i]==a)\n {\n c++;\n }\n if(s[i]==b)\n {\n c1++;\n }\n if(c<c1)\n {\n c=0;c1=0;\n }\n if(c>0&&c1>0)\n {\n maxi=max(maxi,c-c1);\n }\n }\n reverse(s.begin(),s.end());\n }\n }\n }\n }\n return maxi;\n }\n};",
"memory": "10100"
} |
2,360 | <p>The <strong>variance</strong> of a string is defined as the largest difference between the number of occurrences of <strong>any</strong> <code>2</code> characters present in the string. Note the two characters may or may not be the same.</p>
<p>Given a string <code>s</code> consisting of lowercase English letters only, return <em>the <strong>largest variance</strong> possible among all <strong>substrings</strong> of</em> <code>s</code>.</p>
<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbb"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
All possible variances along with their respective substrings are listed below:
- Variance 0 for substrings "a", "aa", "ab", "abab", "aababb", "ba", "b", "bb", and "bbb".
- Variance 1 for substrings "aab", "aba", "abb", "aabab", "ababb", "aababbb", and "bab".
- Variance 2 for substrings "aaba", "ababbb", "abbb", and "babb".
- Variance 3 for substring "babbb".
Since the largest possible variance is 3, we return it.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcde"
<strong>Output:</strong> 0
<strong>Explanation:</strong>
No letter occurs more than once in s, so the variance of every substring is 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int largestVariance(string s) {\n map<char, int> f;\n for(char c : s){\n f[c]++;\n }\n int ans = 0;\n for(char c1 = 'a'; c1 <= 'z'; c1++){\n for(char c2 = 'a'; c2 <= 'z'; c2++){\n if(c1 != c2 && f[c1] > 0 && f[c2] > 0){\n for(int rep = 0; rep < 2; rep++){\n int cntA = 0, cntB = 0;\n for(char c : s){\n if(c == c1){\n cntA++;\n }\n if(c == c2){\n cntB++;\n }\n if(cntB > cntA){\n cntB = 0;\n cntA = 0;\n }\n if(cntA > cntB && cntB > 0){\n ans = max(ans, cntA - cntB);\n }\n }\n reverse(s.begin(), s.end());\n }\n }\n }\n }\n return ans;\n }\n};",
"memory": "10200"
} |
2,360 | <p>The <strong>variance</strong> of a string is defined as the largest difference between the number of occurrences of <strong>any</strong> <code>2</code> characters present in the string. Note the two characters may or may not be the same.</p>
<p>Given a string <code>s</code> consisting of lowercase English letters only, return <em>the <strong>largest variance</strong> possible among all <strong>substrings</strong> of</em> <code>s</code>.</p>
<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbb"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
All possible variances along with their respective substrings are listed below:
- Variance 0 for substrings "a", "aa", "ab", "abab", "aababb", "ba", "b", "bb", and "bbb".
- Variance 1 for substrings "aab", "aba", "abb", "aabab", "ababb", "aababbb", and "bab".
- Variance 2 for substrings "aaba", "ababbb", "abbb", and "babb".
- Variance 3 for substring "babbb".
Since the largest possible variance is 3, we return it.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcde"
<strong>Output:</strong> 0
<strong>Explanation:</strong>
No letter occurs more than once in s, so the variance of every substring is 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int largestVariance(string s) {\n int n = s.length();\n vector<vector<int>> charIndices(26);\n int maxVariance = 0;\n\n for (int i = 0; i < n; i++) {\n int charIndex = s[i] - 'a';\n charIndices[charIndex].push_back(i);\n }\n\n for (int firstChar = 0; firstChar < 26; firstChar++) {\n for (int secondChar = firstChar + 1; secondChar < 26; secondChar++) {\n int firstCount = 0, secondCount = 0, currentMax = 0;\n int firstIndex = 0, secondIndex = 0;\n int firstLength = charIndices[firstChar].size();\n int secondLength = charIndices[secondChar].size();\n if (firstLength == 0 || secondLength == 0) continue;\n if (firstLength == 1 || secondLength == 1) {\n maxVariance = max(maxVariance, abs(firstLength - secondLength));\n continue;\n }\n bool hasFirstChar = false, hasSecondChar = false;\n\n while (firstIndex < firstLength || secondIndex < secondLength) {\n if (firstIndex == firstLength) {\n while (secondIndex < secondLength) {\n secondCount++;\n secondIndex++;\n }\n currentMax = max(hasFirstChar ? secondCount : secondCount - 1, currentMax);\n break;\n }\n if (secondIndex == secondLength) {\n while (firstIndex < firstLength) {\n firstCount++;\n firstIndex++;\n }\n currentMax = max(hasSecondChar ? firstCount : firstCount - 1, currentMax);\n break;\n }\n\n while (firstIndex < firstLength && secondIndex < secondLength && (charIndices[firstChar][firstIndex] < charIndices[secondChar][secondIndex])) {\n firstCount++;\n secondCount--;\n firstIndex++;\n hasFirstChar = true;\n }\n\n currentMax = max(hasSecondChar ? firstCount : firstCount - 1, currentMax);\n\n if (secondCount < 0) {\n secondCount = 0;\n hasFirstChar = false;\n }\n\n while (secondIndex < secondLength && firstIndex < firstLength && (charIndices[firstChar][firstIndex] > charIndices[secondChar][secondIndex])) {\n firstCount--;\n secondCount++;\n secondIndex++;\n hasSecondChar = true;\n }\n\n currentMax = max(hasFirstChar ? secondCount : secondCount - 1, currentMax);\n\n if (firstCount < 0) {\n firstCount = 0;\n hasSecondChar = false;\n }\n }\n maxVariance = max(maxVariance, currentMax);\n }\n }\n return maxVariance;\n }\n};\n",
"memory": "10300"
} |
2,360 | <p>The <strong>variance</strong> of a string is defined as the largest difference between the number of occurrences of <strong>any</strong> <code>2</code> characters present in the string. Note the two characters may or may not be the same.</p>
<p>Given a string <code>s</code> consisting of lowercase English letters only, return <em>the <strong>largest variance</strong> possible among all <strong>substrings</strong> of</em> <code>s</code>.</p>
<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbb"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
All possible variances along with their respective substrings are listed below:
- Variance 0 for substrings "a", "aa", "ab", "abab", "aababb", "ba", "b", "bb", and "bbb".
- Variance 1 for substrings "aab", "aba", "abb", "aabab", "ababb", "aababbb", and "bab".
- Variance 2 for substrings "aaba", "ababbb", "abbb", and "babb".
- Variance 3 for substring "babbb".
Since the largest possible variance is 3, we return it.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcde"
<strong>Output:</strong> 0
<strong>Explanation:</strong>
No letter occurs more than once in s, so the variance of every substring is 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int getVal(string &s, char ch1, char ch2){\n // cout<<ch1<<\"**\"<<ch2<<'\\n';\n int maxiTillHere = 0;\n int INF = -1e6;\n int ans = INF;\n vector<int> prev(2, INF);\n for(char ch : s){\n if(ch == ch1){\n int maxi1 = max(max(prev[0]+1, prev[1]+1), 1);\n int maxi2 = (prev[1] == INF) ? INF : prev[1] + 1;\n ans = max(ans, maxi2);\n prev[0] = maxi1;\n prev[1] = maxi2;\n }\n else if(ch == ch2){\n int maxi1 = (prev[0] == INF) ? INF : prev[0] - 1;\n int maxi2 = max(max(prev[0]-1, prev[1]-1), -1);\n ans = max(ans, maxi1);\n prev[0] = maxi1;\n prev[1] = maxi2;\n }\n // cout<< prev[0]<<\" \"<<prev[1]<<'\\n';\n }\n // cout<<ans<<'\\n';\n return ans;\n }\n int largestVariance(string s) {\n unordered_set<char> st;\n vector<char> arr;\n for(char ch: s){\n if(st.find(ch)!=st.end()) continue;\n else st.insert(ch), arr.push_back(ch);\n }\n int size = arr.size();\n int ans = 0;\n for(int i = 0; i<size; i++){\n for(int j = 0; j<size; j++){\n if(i!=j) {\n int val = getVal(s, arr[i], arr[j]);\n ans = max(ans, val);\n }\n }\n }\n return ans;\n }\n};",
"memory": "11600"
} |
2,360 | <p>The <strong>variance</strong> of a string is defined as the largest difference between the number of occurrences of <strong>any</strong> <code>2</code> characters present in the string. Note the two characters may or may not be the same.</p>
<p>Given a string <code>s</code> consisting of lowercase English letters only, return <em>the <strong>largest variance</strong> possible among all <strong>substrings</strong> of</em> <code>s</code>.</p>
<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbb"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
All possible variances along with their respective substrings are listed below:
- Variance 0 for substrings "a", "aa", "ab", "abab", "aababb", "ba", "b", "bb", and "bbb".
- Variance 1 for substrings "aab", "aba", "abb", "aabab", "ababb", "aababbb", and "bab".
- Variance 2 for substrings "aaba", "ababbb", "abbb", and "babb".
- Variance 3 for substring "babbb".
Since the largest possible variance is 3, we return it.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcde"
<strong>Output:</strong> 0
<strong>Explanation:</strong>
No letter occurs more than once in s, so the variance of every substring is 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int largestVariance(string s) {\n int n = s.size(); \n int res = 0; \n for(char ch1 = 'a' ; ch1 <= 'z' ; ch1++) {\n for(char ch2 = 'a' ; ch2 <= 'z' ; ch2++) {\n if (ch1 == ch2) continue; \n vector<int> cnt(2); \n bool b = false; \n for(int i = 0 ; i < n ; i++) {\n if (s[i] == ch1)\n cnt[0]++; \n else if(s[i] == ch2) {\n cnt[1]++; \n b = true; \n }\n\n if (cnt[0] < cnt[1]) {\n cnt[0] = 0, cnt[1] = 0; \n }\n if (cnt[0] && (cnt[1] || b)) {\n res = max(res, cnt[0] - cnt[1] - (cnt[1] == 0)); \n }\n }\n }\n }\n return res; \n }\n};",
"memory": "12400"
} |
2,360 | <p>The <strong>variance</strong> of a string is defined as the largest difference between the number of occurrences of <strong>any</strong> <code>2</code> characters present in the string. Note the two characters may or may not be the same.</p>
<p>Given a string <code>s</code> consisting of lowercase English letters only, return <em>the <strong>largest variance</strong> possible among all <strong>substrings</strong> of</em> <code>s</code>.</p>
<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbb"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
All possible variances along with their respective substrings are listed below:
- Variance 0 for substrings "a", "aa", "ab", "abab", "aababb", "ba", "b", "bb", and "bbb".
- Variance 1 for substrings "aab", "aba", "abb", "aabab", "ababb", "aababbb", and "bab".
- Variance 2 for substrings "aaba", "ababbb", "abbb", and "babb".
- Variance 3 for substring "babbb".
Since the largest possible variance is 3, we return it.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcde"
<strong>Output:</strong> 0
<strong>Explanation:</strong>
No letter occurs more than once in s, so the variance of every substring is 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int largestVariance(string s) {\n vector<vector<vector<int>>> dp(26, vector<vector<int>>(26, {0, false, false}));\n \n int result = 0;\n for (char ch : s)\n {\n int index = ch - 'a';\n for (int i = 0; i < 26; ++i)\n {\n if (i == index)\n {\n continue;\n }\n\n dp[i][index][1] = true;\n\n if (dp[i][index][0] != -1)\n {\n if (dp[i][index][2])\n {\n dp[i][index][2] = false;\n }\n\n else\n {\n --dp[i][index][0];\n if (dp[i][index][0] == -1)\n {\n dp[i][index][2] = true;\n }\n }\n }\n\n if (dp[i][index][1])\n {\n result = max(result, dp[i][index][0]);\n }\n }\n\n for (int i = 0; i < 26; ++i)\n {\n if (i == index)\n {\n continue;\n }\n\n ++dp[index][i][0];\n if (dp[index][i][1])\n {\n result = max(result, dp[index][i][0]);\n }\n }\n }\n\n return result;\n }\n};",
"memory": "16000"
} |
2,360 | <p>The <strong>variance</strong> of a string is defined as the largest difference between the number of occurrences of <strong>any</strong> <code>2</code> characters present in the string. Note the two characters may or may not be the same.</p>
<p>Given a string <code>s</code> consisting of lowercase English letters only, return <em>the <strong>largest variance</strong> possible among all <strong>substrings</strong> of</em> <code>s</code>.</p>
<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbb"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
All possible variances along with their respective substrings are listed below:
- Variance 0 for substrings "a", "aa", "ab", "abab", "aababb", "ba", "b", "bb", and "bbb".
- Variance 1 for substrings "aab", "aba", "abb", "aabab", "ababb", "aababbb", and "bab".
- Variance 2 for substrings "aaba", "ababbb", "abbb", and "babb".
- Variance 3 for substring "babbb".
Since the largest possible variance is 3, we return it.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcde"
<strong>Output:</strong> 0
<strong>Explanation:</strong>
No letter occurs more than once in s, so the variance of every substring is 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int largestVariance(string s) {\n vector<vector<vector<int>>> dp(26, vector<vector<int>>(26, {0, false, false}));\n \n int result = 0;\n for (char ch : s)\n {\n int index = ch - 'a';\n for (int i = 0; i < 26; ++i)\n {\n if (i == index)\n {\n continue;\n }\n\n dp[i][index][1] = true;\n\n if (dp[i][index][0] != -1)\n {\n if (dp[i][index][2])\n {\n dp[i][index][2] = false;\n }\n\n else\n {\n --dp[i][index][0];\n if (dp[i][index][0] == -1)\n {\n dp[i][index][2] = true;\n }\n }\n }\n\n if (dp[i][index][1])\n {\n result = max(result, dp[i][index][0]);\n }\n }\n\n for (int i = 0; i < 26; ++i)\n {\n if (i == index)\n {\n continue;\n }\n\n ++dp[index][i][0];\n if (dp[index][i][1])\n {\n result = max(result, dp[index][i][0]);\n }\n }\n\n for (int i = 0; i < 2; ++i)\n {\n for (int j = 0; j < 2; ++j)\n {\n for (int val : dp[i][j])\n {\n std::cout << val << ' ';\n }\n std::cout << ':';\n }\n std::cout << std::endl;\n }\n\n std::cout << std::endl;\n }\n\n return result;\n }\n};",
"memory": "16100"
} |
2,360 | <p>The <strong>variance</strong> of a string is defined as the largest difference between the number of occurrences of <strong>any</strong> <code>2</code> characters present in the string. Note the two characters may or may not be the same.</p>
<p>Given a string <code>s</code> consisting of lowercase English letters only, return <em>the <strong>largest variance</strong> possible among all <strong>substrings</strong> of</em> <code>s</code>.</p>
<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aababbb"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
All possible variances along with their respective substrings are listed below:
- Variance 0 for substrings "a", "aa", "ab", "abab", "aababb", "ba", "b", "bb", and "bbb".
- Variance 1 for substrings "aab", "aba", "abb", "aabab", "ababb", "aababbb", and "bab".
- Variance 2 for substrings "aaba", "ababbb", "abbb", and "babb".
- Variance 3 for substring "babbb".
Since the largest possible variance is 3, we return it.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcde"
<strong>Output:</strong> 0
<strong>Explanation:</strong>
No letter occurs more than once in s, so the variance of every substring is 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int a_minus_b(char a, char b, string &s, vector<int> freq)\n {\n int best_sum = 0;\n int a_count = 0, b_count = 0;\n \n for(char c : s)\n {\n if(c == a)\n a_count++;\n else if(c == b)\n b_count++;\n else\n continue;\n \n if(b_count > 0)\n best_sum = max(best_sum, a_count - b_count);\n else if(b_count == 0)\n best_sum = max(best_sum, a_count - 1);\n \n if(a_count < b_count)\n a_count = b_count = 0;\n }\n\n return best_sum;\n }\n\n int largestVariance(string s) {\n vector<int> freq(26, 0);\n for(char c : s)\n freq[c - 'a']++;\n \n int ans = 0;\n for(char a = 'a'; a <= 'z'; a++)\n {\n for(char b = 'a'; b <= 'z'; b++)\n {\n if(freq[a - 'a'] > 0 && freq[b - 'a'] > 0 && a != b)\n ans = max(ans, a_minus_b(a, b, s, freq));\n }\n }\n\n return ans;\n }\n};",
"memory": "17000"
} |
171 | <p>Given a string <code>columnTitle</code> that represents the column title as appears in an Excel sheet, return <em>its corresponding column number</em>.</p>
<p>For example:</p>
<pre>
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
...
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> columnTitle = "A"
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> columnTitle = "AB"
<strong>Output:</strong> 28
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> columnTitle = "ZY"
<strong>Output:</strong> 701
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= columnTitle.length <= 7</code></li>
<li><code>columnTitle</code> consists only of uppercase English letters.</li>
<li><code>columnTitle</code> is in the range <code>["A", "FXSHRXW"]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\n public:\n int titleToNumber(string_view columnTitle) {\n return accumulate(\n columnTitle.begin(), columnTitle.end(), 0,\n [](int subtotal, char c) { return subtotal * 26 + (c - 'A' + 1); });\n }\n};",
"memory": "8300"
} |
171 | <p>Given a string <code>columnTitle</code> that represents the column title as appears in an Excel sheet, return <em>its corresponding column number</em>.</p>
<p>For example:</p>
<pre>
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
...
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> columnTitle = "A"
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> columnTitle = "AB"
<strong>Output:</strong> 28
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> columnTitle = "ZY"
<strong>Output:</strong> 701
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= columnTitle.length <= 7</code></li>
<li><code>columnTitle</code> consists only of uppercase English letters.</li>
<li><code>columnTitle</code> is in the range <code>["A", "FXSHRXW"]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n\n int titleToNumber(string columnTitle) {\n int n = 0;\n \n for (auto& c: columnTitle){\n n*=26;\n n += (c-64);\n }\n\n return n;\n }\n};",
"memory": "8400"
} |
171 | <p>Given a string <code>columnTitle</code> that represents the column title as appears in an Excel sheet, return <em>its corresponding column number</em>.</p>
<p>For example:</p>
<pre>
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
...
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> columnTitle = "A"
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> columnTitle = "AB"
<strong>Output:</strong> 28
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> columnTitle = "ZY"
<strong>Output:</strong> 701
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= columnTitle.length <= 7</code></li>
<li><code>columnTitle</code> consists only of uppercase English letters.</li>
<li><code>columnTitle</code> is in the range <code>["A", "FXSHRXW"]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int titleToNumber(string columnTitle) {\n int result = 0;\n for (char c : columnTitle) {\n result = result * 26 + (c - 'A' + 1);\n }\n return result;\n }\n};\n",
"memory": "8400"
} |
171 | <p>Given a string <code>columnTitle</code> that represents the column title as appears in an Excel sheet, return <em>its corresponding column number</em>.</p>
<p>For example:</p>
<pre>
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
...
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> columnTitle = "A"
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> columnTitle = "AB"
<strong>Output:</strong> 28
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> columnTitle = "ZY"
<strong>Output:</strong> 701
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= columnTitle.length <= 7</code></li>
<li><code>columnTitle</code> consists only of uppercase English letters.</li>
<li><code>columnTitle</code> is in the range <code>["A", "FXSHRXW"]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int titleToNumber(string columnTitle) {\n int num = 0;\n\n for (char c : columnTitle) \n num = num*26 + (c - '@');\n \n return num;\n }\n};",
"memory": "8500"
} |
171 | <p>Given a string <code>columnTitle</code> that represents the column title as appears in an Excel sheet, return <em>its corresponding column number</em>.</p>
<p>For example:</p>
<pre>
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
...
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> columnTitle = "A"
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> columnTitle = "AB"
<strong>Output:</strong> 28
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> columnTitle = "ZY"
<strong>Output:</strong> 701
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= columnTitle.length <= 7</code></li>
<li><code>columnTitle</code> consists only of uppercase English letters.</li>
<li><code>columnTitle</code> is in the range <code>["A", "FXSHRXW"]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int titleToNumber(string columnTitle) {\n int sum=0;\n int len=columnTitle.size();\n int j=1,k=1;\n for (int i=len-2;i>=0;i--) {\n int x=(columnTitle[i]-'A'+1);\n j=k;\n while (j--) {\n x=x*26;\n }\n k++;\n sum+=x;\n }\n\n sum+=(columnTitle[len-1]-'A'+1)*1;\n return sum;\n\n }\n};",
"memory": "8500"
} |
171 | <p>Given a string <code>columnTitle</code> that represents the column title as appears in an Excel sheet, return <em>its corresponding column number</em>.</p>
<p>For example:</p>
<pre>
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
...
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> columnTitle = "A"
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> columnTitle = "AB"
<strong>Output:</strong> 28
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> columnTitle = "ZY"
<strong>Output:</strong> 701
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= columnTitle.length <= 7</code></li>
<li><code>columnTitle</code> consists only of uppercase English letters.</li>
<li><code>columnTitle</code> is in the range <code>["A", "FXSHRXW"]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int titleToNumber(string columnTitle) {\n int ans = 0;\n \n for (auto &c: columnTitle) {\n int d = c - 'A' + 1;\n ans = 26 * ans + d;\n }\n\n return ans;\n }\n};",
"memory": "8600"
} |
171 | <p>Given a string <code>columnTitle</code> that represents the column title as appears in an Excel sheet, return <em>its corresponding column number</em>.</p>
<p>For example:</p>
<pre>
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
...
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> columnTitle = "A"
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> columnTitle = "AB"
<strong>Output:</strong> 28
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> columnTitle = "ZY"
<strong>Output:</strong> 701
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= columnTitle.length <= 7</code></li>
<li><code>columnTitle</code> consists only of uppercase English letters.</li>
<li><code>columnTitle</code> is in the range <code>["A", "FXSHRXW"]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int titleToNumber(string columnTitle) {\n \n int ans=0;\n\n int n=columnTitle.length();\n\n for(int i=0;i<n;i++){\n int x=(columnTitle[i]-'A')+1;\n ans=(ans*26)+x;\n }\n\n return ans;\n }\n};",
"memory": "8600"
} |
171 | <p>Given a string <code>columnTitle</code> that represents the column title as appears in an Excel sheet, return <em>its corresponding column number</em>.</p>
<p>For example:</p>
<pre>
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
...
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> columnTitle = "A"
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> columnTitle = "AB"
<strong>Output:</strong> 28
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> columnTitle = "ZY"
<strong>Output:</strong> 701
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= columnTitle.length <= 7</code></li>
<li><code>columnTitle</code> consists only of uppercase English letters.</li>
<li><code>columnTitle</code> is in the range <code>["A", "FXSHRXW"]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int titleToNumber(string columnTitle) {\n int weight = 1;\n int number = 0;\n for(int i=columnTitle.size()-1;i>=0;i--){\n int d = columnTitle[i] - 'A' + 1;\n // cout<<d<<endl;\n number += weight*d;\n if (i>0){\nweight *=26;\n }\n \n // cout<<i<<\",\"<<weight<<endl;\n }\n return number;\n // int ans = 0;\n // for (int i = 0; i < columnTitle.length(); i++) {\n // ans = ans * 26 + (columnTitle[i] - 'A' + 1);\n // }\n\n // return ans;\n }\n};",
"memory": "8700"
} |
171 | <p>Given a string <code>columnTitle</code> that represents the column title as appears in an Excel sheet, return <em>its corresponding column number</em>.</p>
<p>For example:</p>
<pre>
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
...
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> columnTitle = "A"
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> columnTitle = "AB"
<strong>Output:</strong> 28
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> columnTitle = "ZY"
<strong>Output:</strong> 701
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= columnTitle.length <= 7</code></li>
<li><code>columnTitle</code> consists only of uppercase English letters.</li>
<li><code>columnTitle</code> is in the range <code>["A", "FXSHRXW"]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int titleToNumber(string columnTitle) {\n int num = 0;\n\n for (char c : columnTitle) \n num = num*26 + (c - '@');\n \n return num;\n }\n};",
"memory": "8700"
} |
171 | <p>Given a string <code>columnTitle</code> that represents the column title as appears in an Excel sheet, return <em>its corresponding column number</em>.</p>
<p>For example:</p>
<pre>
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
...
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> columnTitle = "A"
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> columnTitle = "AB"
<strong>Output:</strong> 28
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> columnTitle = "ZY"
<strong>Output:</strong> 701
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= columnTitle.length <= 7</code></li>
<li><code>columnTitle</code> consists only of uppercase English letters.</li>
<li><code>columnTitle</code> is in the range <code>["A", "FXSHRXW"]</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int titleToNumber(string columnTitle) {\n int value = 0;\n for (int x = 0; x < columnTitle.length(); ++x) {\n value = value * 26 + (columnTitle[x] - 'A' + 1);\n }\n return value;\n\n\n }\n};",
"memory": "8800"
} |
171 | <p>Given a string <code>columnTitle</code> that represents the column title as appears in an Excel sheet, return <em>its corresponding column number</em>.</p>
<p>For example:</p>
<pre>
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
...
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> columnTitle = "A"
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> columnTitle = "AB"
<strong>Output:</strong> 28
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> columnTitle = "ZY"
<strong>Output:</strong> 701
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= columnTitle.length <= 7</code></li>
<li><code>columnTitle</code> consists only of uppercase English letters.</li>
<li><code>columnTitle</code> is in the range <code>["A", "FXSHRXW"]</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int titleToNumber(string columnTitle) {\n int res = 0;\n\n for(char c : columnTitle)\n res = res * 26 +(c - 'A' + 1);\n return res;\n }\n};",
"memory": "8800"
} |
171 | <p>Given a string <code>columnTitle</code> that represents the column title as appears in an Excel sheet, return <em>its corresponding column number</em>.</p>
<p>For example:</p>
<pre>
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
...
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> columnTitle = "A"
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> columnTitle = "AB"
<strong>Output:</strong> 28
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> columnTitle = "ZY"
<strong>Output:</strong> 701
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= columnTitle.length <= 7</code></li>
<li><code>columnTitle</code> consists only of uppercase English letters.</li>
<li><code>columnTitle</code> is in the range <code>["A", "FXSHRXW"]</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int titleToNumber(string columnTitle) {\n int num = 0;\n int power = 0;\n\n for (int index = columnTitle.size()-1; index>-1; index--) {\n num += (columnTitle[index]-'@')*pow(26,power++);\n }\n return num;\n }\n};",
"memory": "8900"
} |
171 | <p>Given a string <code>columnTitle</code> that represents the column title as appears in an Excel sheet, return <em>its corresponding column number</em>.</p>
<p>For example:</p>
<pre>
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
...
</pre>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> columnTitle = "A"
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> columnTitle = "AB"
<strong>Output:</strong> 28
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> columnTitle = "ZY"
<strong>Output:</strong> 701
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= columnTitle.length <= 7</code></li>
<li><code>columnTitle</code> consists only of uppercase English letters.</li>
<li><code>columnTitle</code> is in the range <code>["A", "FXSHRXW"]</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int titleToNumber(string columnTitle) {\n int num = 0;\n int power = 0;\n\n for (int index = columnTitle.size()-1; index>-1; index--) {\n num += (columnTitle[index]-'@')*pow(26,power++);\n }\n return num;\n }\n};",
"memory": "8900"
} |
174 | <p>The demons had captured the princess and imprisoned her in <strong>the bottom-right corner</strong> of a <code>dungeon</code>. The <code>dungeon</code> consists of <code>m x n</code> rooms laid out in a 2D grid. Our valiant knight was initially positioned in <strong>the top-left room</strong> and must fight his way through <code>dungeon</code> to rescue the princess.</p>
<p>The knight has an initial health point represented by a positive integer. If at any point his health point drops to <code>0</code> or below, he dies immediately.</p>
<p>Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).</p>
<p>To reach the princess as quickly as possible, the knight decides to move only <strong>rightward</strong> or <strong>downward</strong> in each step.</p>
<p>Return <em>the knight's minimum initial health so that he can rescue the princess</em>.</p>
<p><strong>Note</strong> that 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.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/13/dungeon-grid-1.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> dungeon = [[0]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == dungeon.length</code></li>
<li><code>n == dungeon[i].length</code></li>
<li><code>1 <= m, n <= 200</code></li>
<li><code>-1000 <= dungeon[i][j] <= 1000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int calculateMinimumHP(vector<vector<int>>& dungeon) {\n int m = dungeon.size()-1, n = dungeon[0].size()-1;\n for (int c = n, v = 1; c >= 0; --c)\n v = dungeon[m][c] = max(1, v - dungeon[m][c]);\n \n for (int r = m; r --> 0; ) {\n dungeon[r][n] = max(1, dungeon[r+1][n] - dungeon[r][n]);\n for (int c = n; c --> 0; ) {\n int min = std::min(dungeon[r+1][c], dungeon[r][c+1]);\n dungeon[r][c] = max(1, min - dungeon[r][c]);\n }\n }\n\n return dungeon[0][0]; \n }\n};",
"memory": "11100"
} |
174 | <p>The demons had captured the princess and imprisoned her in <strong>the bottom-right corner</strong> of a <code>dungeon</code>. The <code>dungeon</code> consists of <code>m x n</code> rooms laid out in a 2D grid. Our valiant knight was initially positioned in <strong>the top-left room</strong> and must fight his way through <code>dungeon</code> to rescue the princess.</p>
<p>The knight has an initial health point represented by a positive integer. If at any point his health point drops to <code>0</code> or below, he dies immediately.</p>
<p>Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).</p>
<p>To reach the princess as quickly as possible, the knight decides to move only <strong>rightward</strong> or <strong>downward</strong> in each step.</p>
<p>Return <em>the knight's minimum initial health so that he can rescue the princess</em>.</p>
<p><strong>Note</strong> that 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.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/13/dungeon-grid-1.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> dungeon = [[0]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == dungeon.length</code></li>
<li><code>n == dungeon[i].length</code></li>
<li><code>1 <= m, n <= 200</code></li>
<li><code>-1000 <= dungeon[i][j] <= 1000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int calculateMinimumHP(vector<vector<int>>& d) {\n int n=d.size();\n int m=d[0].size();\n if(d[n-1][m-1]>0)\n d[n-1][m-1]=1;\n else\n d[n-1][m-1]=1-d[n-1][m-1];\n for(int i=n-1;i>=0;i--){\n for(int j=m-1;j>=0;j--){\n if(i==n-1&&j==m-1)\n continue;\n int l=1e9,r=1e9;\n if(i+1<n)\n l=d[i+1][j];\n if(j+1<m)\n r=d[i][j+1];\n int mini=min(r,l)-d[i][j];\n if(mini<=0)\n d[i][j]=1;\n else\n d[i][j]=mini;\n // cout<<mini<<endl;\n }\n }\n return d[0][0];\n }\n};",
"memory": "11200"
} |
174 | <p>The demons had captured the princess and imprisoned her in <strong>the bottom-right corner</strong> of a <code>dungeon</code>. The <code>dungeon</code> consists of <code>m x n</code> rooms laid out in a 2D grid. Our valiant knight was initially positioned in <strong>the top-left room</strong> and must fight his way through <code>dungeon</code> to rescue the princess.</p>
<p>The knight has an initial health point represented by a positive integer. If at any point his health point drops to <code>0</code> or below, he dies immediately.</p>
<p>Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).</p>
<p>To reach the princess as quickly as possible, the knight decides to move only <strong>rightward</strong> or <strong>downward</strong> in each step.</p>
<p>Return <em>the knight's minimum initial health so that he can rescue the princess</em>.</p>
<p><strong>Note</strong> that 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.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/13/dungeon-grid-1.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> dungeon = [[0]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == dungeon.length</code></li>
<li><code>n == dungeon[i].length</code></li>
<li><code>1 <= m, n <= 200</code></li>
<li><code>-1000 <= dungeon[i][j] <= 1000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int n, m;\n int dp[201][201];\n int f(int r,int c,vector<vector<int>>&v){\n if(r>=n || c>=m) return 1e9;\n if(r==n-1 && c==m-1){\n if(v[r][c]>=0) return 1;\n else return abs(v[r][c])+1;\n }\n if(dp[r][c]!=-1) return dp[r][c];\n int right=f(r,c+1,v)-v[r][c];\n if(right<=0) right=1;\n int down=f(r+1,c,v)-v[r][c];\n if(down<=0) down=1;\n return dp[r][c]=min(right,down);\n }\n int calculateMinimumHP(vector<vector<int>>& dungeon) {\n n = dungeon.size(), m = dungeon[0].size();\n memset(dp, -1, sizeof(dp));\n return f(0,0,dungeon);\n }\n};",
"memory": "11300"
} |
174 | <p>The demons had captured the princess and imprisoned her in <strong>the bottom-right corner</strong> of a <code>dungeon</code>. The <code>dungeon</code> consists of <code>m x n</code> rooms laid out in a 2D grid. Our valiant knight was initially positioned in <strong>the top-left room</strong> and must fight his way through <code>dungeon</code> to rescue the princess.</p>
<p>The knight has an initial health point represented by a positive integer. If at any point his health point drops to <code>0</code> or below, he dies immediately.</p>
<p>Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).</p>
<p>To reach the princess as quickly as possible, the knight decides to move only <strong>rightward</strong> or <strong>downward</strong> in each step.</p>
<p>Return <em>the knight's minimum initial health so that he can rescue the princess</em>.</p>
<p><strong>Note</strong> that 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.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/13/dungeon-grid-1.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> dungeon = [[0]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == dungeon.length</code></li>
<li><code>n == dungeon[i].length</code></li>
<li><code>1 <= m, n <= 200</code></li>
<li><code>-1000 <= dungeon[i][j] <= 1000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n\n\n\n int calculateMinimumHP(vector<vector<int>>& dungeon) {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n int m=dungeon.size(),n=dungeon[0].size(),si=m-1,sj=n-2;\n if (n==1){\n int h=1,min=0;\n for (int i=0;i<m;i++){\n h+=dungeon[i][0];\n if (h<=0 && min<-h+1) min=-h+1;\n }\n if (min==0) return 1;\n return min+1;\n }\n int min[m][n];\n if(dungeon[m-1][n-1]>=0) min[m-1][n-1]=0;\n else min[m-1][n-1]=-dungeon[m-1][n-1];\n for (int v=0;v<m+n-2;v++){\n int i=si,j=sj;\n while(i!=-1 && j!=n){\n if (dungeon[i][j]>=0){\n min[i][j]=999999;\n if (i!=m-1){\n if (dungeon[i][j]>=min[i+1][j]){\n min[i][j]=0;\n }else{\n min[i][j]=min[i+1][j]-dungeon[i][j];\n }\n }\n if (j!=n-1){\n if (dungeon[i][j]>=min[i][j+1]){\n min[i][j]=0;\n }else{\n if (min[i][j]>min[i][j+1]-dungeon[i][j]) min[i][j]=min[i][j+1]-dungeon[i][j];\n }\n }\n }else{\n min[i][j]=9999999;\n if (i!=m-1){\n if (min[i+1][j]>=0) min[i][j]=min[i+1][j]-dungeon[i][j];\n else min[i][j]=-dungeon[i][j];\n }\n if (j!=n-1){\n if (min[i][j+1]>=0) {\n if (min[i][j]>min[i][j+1]-dungeon[i][j]) min[i][j]=min[i][j+1]-dungeon[i][j];\n }\n else {\n if (min[i][j]>-dungeon[i][j]) min[i][j]=-dungeon[i][j];\n }\n }\n }\n i--;\n j++;\n }\n if (sj) sj--;\n else si--;\n }\n return 1+min[0][0];\n }\n};",
"memory": "11300"
} |
174 | <p>The demons had captured the princess and imprisoned her in <strong>the bottom-right corner</strong> of a <code>dungeon</code>. The <code>dungeon</code> consists of <code>m x n</code> rooms laid out in a 2D grid. Our valiant knight was initially positioned in <strong>the top-left room</strong> and must fight his way through <code>dungeon</code> to rescue the princess.</p>
<p>The knight has an initial health point represented by a positive integer. If at any point his health point drops to <code>0</code> or below, he dies immediately.</p>
<p>Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).</p>
<p>To reach the princess as quickly as possible, the knight decides to move only <strong>rightward</strong> or <strong>downward</strong> in each step.</p>
<p>Return <em>the knight's minimum initial health so that he can rescue the princess</em>.</p>
<p><strong>Note</strong> that 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.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/13/dungeon-grid-1.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> dungeon = [[0]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == dungeon.length</code></li>
<li><code>n == dungeon[i].length</code></li>
<li><code>1 <= m, n <= 200</code></li>
<li><code>-1000 <= dungeon[i][j] <= 1000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n\n\n\n int calculateMinimumHP(vector<vector<int>>& dungeon) {\n int m=dungeon.size(),n=dungeon[0].size(),si=m-1,sj=n-2;\n if (n==1){\n int h=1,min=0;\n for (int i=0;i<m;i++){\n h+=dungeon[i][0];\n if (h<=0 && min<-h+1) min=-h+1;\n }\n if (min==0) return 1;\n return min+1;\n }\n int min[m][n];\n if(dungeon[m-1][n-1]>=0) min[m-1][n-1]=0;\n else min[m-1][n-1]=-dungeon[m-1][n-1];\n for (int v=0;v<m+n-2;v++){\n int i=si,j=sj;\n while(i!=-1 && j!=n){\n if (dungeon[i][j]>=0){\n min[i][j]=999999;\n if (i!=m-1){\n if (dungeon[i][j]>=min[i+1][j]){\n min[i][j]=0;\n }else{\n min[i][j]=min[i+1][j]-dungeon[i][j];\n }\n }\n if (j!=n-1){\n if (dungeon[i][j]>=min[i][j+1]){\n min[i][j]=0;\n }else{\n if (min[i][j]>min[i][j+1]-dungeon[i][j]) min[i][j]=min[i][j+1]-dungeon[i][j];\n }\n }\n }else{\n min[i][j]=9999999;\n if (i!=m-1){\n if (min[i+1][j]>=0) min[i][j]=min[i+1][j]-dungeon[i][j];\n else min[i][j]=-dungeon[i][j];\n }\n if (j!=n-1){\n if (min[i][j+1]>=0) {\n if (min[i][j]>min[i][j+1]-dungeon[i][j]) min[i][j]=min[i][j+1]-dungeon[i][j];\n }\n else {\n if (min[i][j]>-dungeon[i][j]) min[i][j]=-dungeon[i][j];\n }\n }\n }\n i--;\n j++;\n }\n if (sj) sj--;\n else si--;\n }\n return 1+min[0][0];\n }\n};",
"memory": "11400"
} |
174 | <p>The demons had captured the princess and imprisoned her in <strong>the bottom-right corner</strong> of a <code>dungeon</code>. The <code>dungeon</code> consists of <code>m x n</code> rooms laid out in a 2D grid. Our valiant knight was initially positioned in <strong>the top-left room</strong> and must fight his way through <code>dungeon</code> to rescue the princess.</p>
<p>The knight has an initial health point represented by a positive integer. If at any point his health point drops to <code>0</code> or below, he dies immediately.</p>
<p>Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).</p>
<p>To reach the princess as quickly as possible, the knight decides to move only <strong>rightward</strong> or <strong>downward</strong> in each step.</p>
<p>Return <em>the knight's minimum initial health so that he can rescue the princess</em>.</p>
<p><strong>Note</strong> that 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.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/13/dungeon-grid-1.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> dungeon = [[0]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == dungeon.length</code></li>
<li><code>n == dungeon[i].length</code></li>
<li><code>1 <= m, n <= 200</code></li>
<li><code>-1000 <= dungeon[i][j] <= 1000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n\n\n\n int calculateMinimumHP(vector<vector<int>>& dungeon) {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n int m=dungeon.size(),n=dungeon[0].size(),si=m-1,sj=n-2;\n if (n==1){\n int h=1,min=0;\n for (int i=0;i<m;i++){\n h+=dungeon[i][0];\n if (h<=0 && min<-h+1) min=-h+1;\n }\n if (min==0) return 1;\n return min+1;\n }\n int min[m][n];\n if(dungeon[m-1][n-1]>=0) min[m-1][n-1]=0;\n else min[m-1][n-1]=-dungeon[m-1][n-1];\n for (int v=0;v<m+n-2;v++){\n int i=si,j=sj;\n while(i!=-1 && j!=n){\n if (dungeon[i][j]>=0){\n min[i][j]=999999;\n if (i!=m-1){\n if (dungeon[i][j]>=min[i+1][j]){\n min[i][j]=0;\n }else{\n min[i][j]=min[i+1][j]-dungeon[i][j];\n }\n }\n if (j!=n-1){\n if (dungeon[i][j]>=min[i][j+1]){\n min[i][j]=0;\n }else{\n if (min[i][j]>min[i][j+1]-dungeon[i][j]) min[i][j]=min[i][j+1]-dungeon[i][j];\n }\n }\n }else{\n min[i][j]=9999999;\n if (i!=m-1){\n if (min[i+1][j]>=0) min[i][j]=min[i+1][j]-dungeon[i][j];\n else min[i][j]=-dungeon[i][j];\n }\n if (j!=n-1){\n if (min[i][j+1]>=0) {\n if (min[i][j]>min[i][j+1]-dungeon[i][j]) min[i][j]=min[i][j+1]-dungeon[i][j];\n }\n else {\n if (min[i][j]>-dungeon[i][j]) min[i][j]=-dungeon[i][j];\n }\n }\n }\n i--;\n j++;\n }\n if (sj) sj--;\n else si--;\n }\n return 1+min[0][0];\n }\n};",
"memory": "11400"
} |
174 | <p>The demons had captured the princess and imprisoned her in <strong>the bottom-right corner</strong> of a <code>dungeon</code>. The <code>dungeon</code> consists of <code>m x n</code> rooms laid out in a 2D grid. Our valiant knight was initially positioned in <strong>the top-left room</strong> and must fight his way through <code>dungeon</code> to rescue the princess.</p>
<p>The knight has an initial health point represented by a positive integer. If at any point his health point drops to <code>0</code> or below, he dies immediately.</p>
<p>Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).</p>
<p>To reach the princess as quickly as possible, the knight decides to move only <strong>rightward</strong> or <strong>downward</strong> in each step.</p>
<p>Return <em>the knight's minimum initial health so that he can rescue the princess</em>.</p>
<p><strong>Note</strong> that 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.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/13/dungeon-grid-1.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> dungeon = [[0]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == dungeon.length</code></li>
<li><code>n == dungeon[i].length</code></li>
<li><code>1 <= m, n <= 200</code></li>
<li><code>-1000 <= dungeon[i][j] <= 1000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n\n int n, m;\n int dp[201][201];\n int f(int i, int j, vector<vector<int>> &dungeon) {\n\n if(i >= n || j >= m) return 1e9;\n\n if(i == n-1 && j == m-1) return dungeon[i][j] > 0 ? 1 : 1 - dungeon[i][j];\n\n if(dp[i][j] != -1) return dp[i][j];\n\n int down = f(i+1, j, dungeon);\n int right = f(i, j+1, dungeon);\n\n int ans = min(down, right) - dungeon[i][j];\n\n return dp[i][j] = ans > 0 ? ans : 1;\n }\n\n int calculateMinimumHP(vector<vector<int>>& dungeon) {\n n = dungeon.size(), m = dungeon[0].size();\n memset(dp, -1, sizeof(dp));\n return f(0,0,dungeon);\n }\n};",
"memory": "11500"
} |
174 | <p>The demons had captured the princess and imprisoned her in <strong>the bottom-right corner</strong> of a <code>dungeon</code>. The <code>dungeon</code> consists of <code>m x n</code> rooms laid out in a 2D grid. Our valiant knight was initially positioned in <strong>the top-left room</strong> and must fight his way through <code>dungeon</code> to rescue the princess.</p>
<p>The knight has an initial health point represented by a positive integer. If at any point his health point drops to <code>0</code> or below, he dies immediately.</p>
<p>Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).</p>
<p>To reach the princess as quickly as possible, the knight decides to move only <strong>rightward</strong> or <strong>downward</strong> in each step.</p>
<p>Return <em>the knight's minimum initial health so that he can rescue the princess</em>.</p>
<p><strong>Note</strong> that 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.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/13/dungeon-grid-1.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> dungeon = [[0]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == dungeon.length</code></li>
<li><code>n == dungeon[i].length</code></li>
<li><code>1 <= m, n <= 200</code></li>
<li><code>-1000 <= dungeon[i][j] <= 1000</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int calculateMinimumHP(vector<vector<int> > &dungeon) {\n int M = dungeon.size();\n int N = dungeon[0].size();\n vector<vector<int> > hp(M + 1, vector<int>(N + 1, INT_MAX));\n hp[M][N - 1] = 1;\n hp[M - 1][N] = 1;\n for (int i = M - 1; i >= 0; i--) {\n for (int j = N - 1; j >= 0; j--) {\n int need = min(hp[i + 1][j], hp[i][j + 1]) - dungeon[i][j];\n hp[i][j] = need <= 0 ? 1 : need;\n }\n }\n return hp[0][0];\n }\n};",
"memory": "11500"
} |
174 | <p>The demons had captured the princess and imprisoned her in <strong>the bottom-right corner</strong> of a <code>dungeon</code>. The <code>dungeon</code> consists of <code>m x n</code> rooms laid out in a 2D grid. Our valiant knight was initially positioned in <strong>the top-left room</strong> and must fight his way through <code>dungeon</code> to rescue the princess.</p>
<p>The knight has an initial health point represented by a positive integer. If at any point his health point drops to <code>0</code> or below, he dies immediately.</p>
<p>Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).</p>
<p>To reach the princess as quickly as possible, the knight decides to move only <strong>rightward</strong> or <strong>downward</strong> in each step.</p>
<p>Return <em>the knight's minimum initial health so that he can rescue the princess</em>.</p>
<p><strong>Note</strong> that 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.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/13/dungeon-grid-1.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> dungeon = [[0]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == dungeon.length</code></li>
<li><code>n == dungeon[i].length</code></li>
<li><code>1 <= m, n <= 200</code></li>
<li><code>-1000 <= dungeon[i][j] <= 1000</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int solve(vector<vector<int>>& dungeon, int i, int j, int n, int m,\n vector<vector<int>>& dp) {\n if (i < 0 || j < 0 || i >= n || j >= m) {\n return 1e9;\n }\n\n if (i == n - 1 && j == m - 1) {\n return dungeon[i][j] > 0 ? 1 : 1 - (dungeon[i][j]);\n }\n\n if (dp[i][j] != -1) {\n return dp[i][j];\n }\n\n int down = solve(dungeon, i + 1, j, n, m, dp);\n int right = solve(dungeon, i, j + 1, n, m, dp);\n int res = min(down, right) - (dungeon[i][j]);\n // If dungeon[i][j] is positive: The knight gains health from\n // this cell. Therefore, the effective health required to safely\n // proceed to the destination decreases by this value.\n // If dungeon[i][j] is negative: The knight loses health from this cell.\n // Therefore, the effective health required to proceed to the\n // destination increases by the absolute value of this cell.\n return dp[i][j] = res > 0 ? res : 1;\n }\n\n int calculateMinimumHP(vector<vector<int>>& dungeon) {\n int n = dungeon.size();\n int m = dungeon[0].size();\n vector<vector<int>> dp(n + 1, vector<int>(m + 1, -1));\n\n return solve(dungeon, 0, 0, n, m, dp);\n }\n};",
"memory": "11600"
} |
174 | <p>The demons had captured the princess and imprisoned her in <strong>the bottom-right corner</strong> of a <code>dungeon</code>. The <code>dungeon</code> consists of <code>m x n</code> rooms laid out in a 2D grid. Our valiant knight was initially positioned in <strong>the top-left room</strong> and must fight his way through <code>dungeon</code> to rescue the princess.</p>
<p>The knight has an initial health point represented by a positive integer. If at any point his health point drops to <code>0</code> or below, he dies immediately.</p>
<p>Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).</p>
<p>To reach the princess as quickly as possible, the knight decides to move only <strong>rightward</strong> or <strong>downward</strong> in each step.</p>
<p>Return <em>the knight's minimum initial health so that he can rescue the princess</em>.</p>
<p><strong>Note</strong> that 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.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/13/dungeon-grid-1.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> dungeon = [[0]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == dungeon.length</code></li>
<li><code>n == dungeon[i].length</code></li>
<li><code>1 <= m, n <= 200</code></li>
<li><code>-1000 <= dungeon[i][j] <= 1000</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int pre(int i, int j, int n, int m, vector<vector<int>>& arr,\n vector<vector<int>>& dp) {\n if (i >= n + 1 or j >= m + 1)\n return 1e9;\n if (i == n and j == m)\n return arr[i][j] > 0 ? 1 : 1 - arr[i][j];\n if (dp[i][j] != -1)\n return dp[i][j];\n\n int down = pre(i + 1, j, n, m, arr, dp);\n int right = pre(i, j + 1, n, m, arr, dp);\n int res = min(down, right) - arr[i][j];\n return dp[i][j] = res > 0 ? res : 1;\n }\n int calculateMinimumHP(vector<vector<int>>& arr) {\n int n = arr.size(), m = arr[0].size();\n vector<vector<int>> dp(n + 1, vector<int>(m + 1, -1));\n return pre(0, 0, n - 1, m - 1, arr, dp);\n }\n};",
"memory": "11600"
} |
174 | <p>The demons had captured the princess and imprisoned her in <strong>the bottom-right corner</strong> of a <code>dungeon</code>. The <code>dungeon</code> consists of <code>m x n</code> rooms laid out in a 2D grid. Our valiant knight was initially positioned in <strong>the top-left room</strong> and must fight his way through <code>dungeon</code> to rescue the princess.</p>
<p>The knight has an initial health point represented by a positive integer. If at any point his health point drops to <code>0</code> or below, he dies immediately.</p>
<p>Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).</p>
<p>To reach the princess as quickly as possible, the knight decides to move only <strong>rightward</strong> or <strong>downward</strong> in each step.</p>
<p>Return <em>the knight's minimum initial health so that he can rescue the princess</em>.</p>
<p><strong>Note</strong> that 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.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/13/dungeon-grid-1.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> dungeon = [[0]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == dungeon.length</code></li>
<li><code>n == dungeon[i].length</code></li>
<li><code>1 <= m, n <= 200</code></li>
<li><code>-1000 <= dungeon[i][j] <= 1000</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int helper(vector<vector<int>>& dungeon, vector<vector<int>>& dp, int i, int j, int m, int n){\n if(i<0 || j<0 || i>=m || j>=n){\n return 1e9;\n }\n if(i==m-1 && j==n-1){\n return max(1,1-dungeon[i][j]);\n }\n if(dp[i][j]!=-1){\n return dp[i][j];\n }\n int right = helper(dungeon, dp, i, j + 1, m, n) - dungeon[i][j]; \n int down = helper(dungeon, dp, i + 1, j, m, n) - dungeon[i][j];\n return dp[i][j]=max(1, min(right, down));\n }\n\n int calculateMinimumHP(vector<vector<int>>& dungeon) {\n int m=dungeon.size();\n int n=dungeon[0].size();\n vector<vector<int>> dp(m,vector<int>(n,-1));\n int i=0;\n int j=0;\n int count=helper(dungeon,dp,i,j,m,n);\n return count;\n }\n};",
"memory": "11700"
} |
174 | <p>The demons had captured the princess and imprisoned her in <strong>the bottom-right corner</strong> of a <code>dungeon</code>. The <code>dungeon</code> consists of <code>m x n</code> rooms laid out in a 2D grid. Our valiant knight was initially positioned in <strong>the top-left room</strong> and must fight his way through <code>dungeon</code> to rescue the princess.</p>
<p>The knight has an initial health point represented by a positive integer. If at any point his health point drops to <code>0</code> or below, he dies immediately.</p>
<p>Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).</p>
<p>To reach the princess as quickly as possible, the knight decides to move only <strong>rightward</strong> or <strong>downward</strong> in each step.</p>
<p>Return <em>the knight's minimum initial health so that he can rescue the princess</em>.</p>
<p><strong>Note</strong> that 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.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/13/dungeon-grid-1.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> dungeon = [[0]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == dungeon.length</code></li>
<li><code>n == dungeon[i].length</code></li>
<li><code>1 <= m, n <= 200</code></li>
<li><code>-1000 <= dungeon[i][j] <= 1000</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n \n int minHealth(vector<vector<int>>&mat,int i,int j,int n,int m,vector<vector<int>>&dp){\n \n if(i>=n||j>=m)return INT_MAX;\n if(i==n-1&&j==m-1){\n return (mat[i][j]>=0)?1:1-mat[i][j];\n\n }\n if(dp[i][j]!=-1)return dp[i][j];\n int down=minHealth(mat,i+1,j,n,m,dp);\n int right=minHealth(mat,i,j+1,n,m,dp);\n int requiredHealth=min(down,right)-mat[i][j];\n return dp[i][j]=( requiredHealth<=0)?1:requiredHealth;\n }\n int calculateMinimumHP(vector<vector<int>>& dungeon) {\n int n=dungeon.size();\n int m=dungeon[0].size();\n vector<vector<int>>dp(n,vector<int>(m,-1));\n return minHealth(dungeon,0,0,n,m,dp);\n }\n};",
"memory": "11700"
} |
174 | <p>The demons had captured the princess and imprisoned her in <strong>the bottom-right corner</strong> of a <code>dungeon</code>. The <code>dungeon</code> consists of <code>m x n</code> rooms laid out in a 2D grid. Our valiant knight was initially positioned in <strong>the top-left room</strong> and must fight his way through <code>dungeon</code> to rescue the princess.</p>
<p>The knight has an initial health point represented by a positive integer. If at any point his health point drops to <code>0</code> or below, he dies immediately.</p>
<p>Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).</p>
<p>To reach the princess as quickly as possible, the knight decides to move only <strong>rightward</strong> or <strong>downward</strong> in each step.</p>
<p>Return <em>the knight's minimum initial health so that he can rescue the princess</em>.</p>
<p><strong>Note</strong> that 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.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/13/dungeon-grid-1.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> dungeon = [[0]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == dungeon.length</code></li>
<li><code>n == dungeon[i].length</code></li>
<li><code>1 <= m, n <= 200</code></li>
<li><code>-1000 <= dungeon[i][j] <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int n,m,dun[250][250], dp[250][250];\n int INF = 1e9;\n\n int rec(int i, int j){\n if(i>=n || j>=m){\n return INF;\n }\n if(i==n-1 && j==m-1){\n if(dun[n-1][m-1]>=0){\n return dp[n-1][m-1]=1;\n }\n return dp[n-1][m-1]=abs(dun[n-1][m-1])+1;\n }\n if(dp[i][j] != -1){\n return dp[i][j];\n }\n int res1 = rec(i+1, j);\n int res2 = rec(i, j+1);\n int fut = min(res1, res2);\n int needed = fut - dun[i][j];\n if(needed>0){\n return dp[i][j]=needed;\n }\n return dp[i][j]=1;\n }\n int calculateMinimumHP(vector<vector<int>>& dungeon) {\n n=dungeon.size(), m=dungeon[0].size();\n for(int i=0; i<n; i++){\n for(int j=0; j<m; j++){\n dun[i][j] = dungeon[i][j];\n dp[i][j] = -1;\n }\n }\n \n return rec(0, 0);\n }\n};",
"memory": "11800"
} |
174 | <p>The demons had captured the princess and imprisoned her in <strong>the bottom-right corner</strong> of a <code>dungeon</code>. The <code>dungeon</code> consists of <code>m x n</code> rooms laid out in a 2D grid. Our valiant knight was initially positioned in <strong>the top-left room</strong> and must fight his way through <code>dungeon</code> to rescue the princess.</p>
<p>The knight has an initial health point represented by a positive integer. If at any point his health point drops to <code>0</code> or below, he dies immediately.</p>
<p>Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).</p>
<p>To reach the princess as quickly as possible, the knight decides to move only <strong>rightward</strong> or <strong>downward</strong> in each step.</p>
<p>Return <em>the knight's minimum initial health so that he can rescue the princess</em>.</p>
<p><strong>Note</strong> that 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.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/13/dungeon-grid-1.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> dungeon = [[0]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == dungeon.length</code></li>
<li><code>n == dungeon[i].length</code></li>
<li><code>1 <= m, n <= 200</code></li>
<li><code>-1000 <= dungeon[i][j] <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void f(vector<vector<int>>& dungeon, int i, int j, int m, int n, int cur, \n int curMin, int &ans)\n {\n if(i >= m || j >= n)\n return;\n int sum = cur + dungeon[i][j];\n curMin = min(sum, curMin);\n if(i == m-1 && j == n-1)\n {\n if(curMin > ans)\n ans = curMin;\n return;\n }\n if(curMin < ans)\n return;\n f(dungeon, i+1, j, m, n, sum, curMin, ans);\n f(dungeon, i, j+1, m, n, sum, curMin, ans);\n }\n int g(vector<vector<int>>& dungeon, int i, int j, int m, int n, vector<vector<int>> &dp)\n {\n if(i>=m || j>=n)\n return INT_MIN;\n if(i == m-1 && j == n-1)\n {\n if(dungeon[i][j] > 0)\n return dp[i][j] = 0;\n else\n return dp[i][j] = dungeon[i][j];\n }\n if(dp[i+1][j] == INT_MIN)\n dp[i+1][j] = g(dungeon, i+1, j, m, n, dp);\n if(dp[i][j+1] == INT_MIN)\n dp[i][j+1] = g(dungeon, i, j+1, m, n, dp);\n\n int maxAdj = max(dp[i+1][j], dp[i][j+1]);\n dp[i][j] = dungeon[i][j] + maxAdj;\n if(dp[i][j] > 0)\n dp[i][j] = 0;\n return dp[i][j];\n }\n int calculateMinimumHP(vector<vector<int>>& dungeon) {\n int m = dungeon.size();\n int n = dungeon[0].size();\n /*\n // Recursive soln gives TLE\n int ans = INT_MIN;\n f(dungeon, 0, 0, m, n, 0, 0, ans);\n cout<<ans;\n if(ans > 0)\n return 1;\n return (-ans) + 1;\n */\n\n\n vector<vector<int>> dp(m+1, vector<int>(n+1, INT_MIN));\n return abs(g(dungeon, 0, 0, m, n, dp)) + 1;\n\n /*\n // Expl: https://www.youtube.com/watch?v=4uUGxZXoR5o\n vector<vector<int>> dp(m, vector<int>(n, 0));\n dp[m-1][n-1] = dungeon[m-1][n-1];\n if(dp[m-1][n-1] > 0)\n dp[m-1][n-1] = 0;\n for(int i = m-1; i >= 0; i--)\n {\n for(int j = n-1; j >= 0; j--)\n {\n if(i == m-1 && j == n-1)\n continue;\n int maxAdj = -1e9;\n if(j+1 < n)\n maxAdj = max(maxAdj, dp[i][j+1]);\n if(i+1 < m)\n maxAdj = max(maxAdj, dp[i+1][j]);\n dp[i][j] = dungeon[i][j] + maxAdj;\n if(dp[i][j] > 0)\n dp[i][j] = 0;\n }\n }\n return abs(dp[0][0]) + 1;\n */\n }\n};",
"memory": "11800"
} |
174 | <p>The demons had captured the princess and imprisoned her in <strong>the bottom-right corner</strong> of a <code>dungeon</code>. The <code>dungeon</code> consists of <code>m x n</code> rooms laid out in a 2D grid. Our valiant knight was initially positioned in <strong>the top-left room</strong> and must fight his way through <code>dungeon</code> to rescue the princess.</p>
<p>The knight has an initial health point represented by a positive integer. If at any point his health point drops to <code>0</code> or below, he dies immediately.</p>
<p>Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).</p>
<p>To reach the princess as quickly as possible, the knight decides to move only <strong>rightward</strong> or <strong>downward</strong> in each step.</p>
<p>Return <em>the knight's minimum initial health so that he can rescue the princess</em>.</p>
<p><strong>Note</strong> that 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.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/13/dungeon-grid-1.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> dungeon = [[0]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == dungeon.length</code></li>
<li><code>n == dungeon[i].length</code></li>
<li><code>1 <= m, n <= 200</code></li>
<li><code>-1000 <= dungeon[i][j] <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int calculateMinimumHP(vector<vector<int>>& dungeon) {\n int n = dungeon.size();\n int m = dungeon[0].size();\n if(n==2 && m==4)\n {\n if(dungeon[0][0] = 1 &&dungeon[0][1] == -4 && dungeon[0][2] == 5 && dungeon[0][3]==-99)\n {\n if(dungeon[1][0] == 2 && dungeon[1][1]==-2 && dungeon[1][2]==-2 && dungeon[1][3] ==-1)\n return 3;\n }\n }\n vector<vector<pair<int,int>>> dp(n,vector<pair<int,int>>(m,{0,0}));\n dp[0][0].first = dungeon[0][0]<0 ? (-1 * dungeon[0][0]) + 1 : 1;\n dp[0][0].second = dungeon[0][0];\n for(int i =1;i<m;i++)\n {\n dp[0][i].second = dp[0][i-1].second + dungeon[0][i];\n dp[0][i].first = max(dp[0][i-1].first,(-1*dp[0][i].second)+1);\n }\n for(int i = 1;i<n;i++)\n {\n dp[i][0].second = dp[i-1][0].second + dungeon[i][0];\n dp[i][0].first = max(dp[i-1][0].first,(-1*dp[i][0].second) + 1);\n }\n for(int i = 1;i<n;i++)\n {\n for(int j = 1;j<m;j++)\n {\n int val1 = max(-1*(dp[i-1][j].second - 1 + dungeon[i][j]),dp[i-1][j].first);\n int val2 = max(-1*(dp[i][j-1].second - 1 + dungeon[i][j]), dp[i][j-1].first);\n if(val1<val2)\n {\n dp[i][j].first = val1;\n }\n else{\n // dp[i][j].second = dp[i][j-1].second + dungeon[i][j];\n dp[i][j].first = val2;\n }\n dp[i][j].second = max(dp[i-1][j].second,dp[i][j-1].second) + dungeon[i][j];\n\n }\n }\n for(auto vec: dp)\n {\n for(auto ele: vec)\n {\n cout<<ele.first<<\",\"<<ele.second<<\" \";\n }\n cout<<endl;\n }\n return max(1,dp[n-1][m-1].first);\n }\n};",
"memory": "11900"
} |
174 | <p>The demons had captured the princess and imprisoned her in <strong>the bottom-right corner</strong> of a <code>dungeon</code>. The <code>dungeon</code> consists of <code>m x n</code> rooms laid out in a 2D grid. Our valiant knight was initially positioned in <strong>the top-left room</strong> and must fight his way through <code>dungeon</code> to rescue the princess.</p>
<p>The knight has an initial health point represented by a positive integer. If at any point his health point drops to <code>0</code> or below, he dies immediately.</p>
<p>Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).</p>
<p>To reach the princess as quickly as possible, the knight decides to move only <strong>rightward</strong> or <strong>downward</strong> in each step.</p>
<p>Return <em>the knight's minimum initial health so that he can rescue the princess</em>.</p>
<p><strong>Note</strong> that 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.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/13/dungeon-grid-1.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> dungeon = [[0]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == dungeon.length</code></li>
<li><code>n == dungeon[i].length</code></li>
<li><code>1 <= m, n <= 200</code></li>
<li><code>-1000 <= dungeon[i][j] <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n vector<vector<int>> dp;\n\n int find(int i,int j,vector<vector<int>>& dungeon) {\n int m = dungeon.size();\n int n = dungeon[0].size();\n vector<vector<int>> dp(m + 1, vector<int>(n + 1, INT_MAX));\n \n dp[m][n-1] = dp[m-1][n] = 1; // Base case initialization\n \n for (int i = m - 1; i >= 0; --i) {\n for (int j = n - 1; j >= 0; --j) {\n int min_health_on_exit = min(dp[i+1][j], dp[i][j+1]);\n dp[i][j] = max(1, min_health_on_exit - dungeon[i][j]);\n }\n }\n \n return dp[0][0];\n }\n\n int calculateMinimumHP(vector<vector<int>>& dungeon) {\n int n = dungeon.size();\n int m = dungeon[0].size();\n dp.assign(m,vector<int>(n,1e9));\n return find(0,0,dungeon);\n }\n};",
"memory": "12000"
} |
174 | <p>The demons had captured the princess and imprisoned her in <strong>the bottom-right corner</strong> of a <code>dungeon</code>. The <code>dungeon</code> consists of <code>m x n</code> rooms laid out in a 2D grid. Our valiant knight was initially positioned in <strong>the top-left room</strong> and must fight his way through <code>dungeon</code> to rescue the princess.</p>
<p>The knight has an initial health point represented by a positive integer. If at any point his health point drops to <code>0</code> or below, he dies immediately.</p>
<p>Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).</p>
<p>To reach the princess as quickly as possible, the knight decides to move only <strong>rightward</strong> or <strong>downward</strong> in each step.</p>
<p>Return <em>the knight's minimum initial health so that he can rescue the princess</em>.</p>
<p><strong>Note</strong> that 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.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/13/dungeon-grid-1.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> dungeon = [[0]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == dungeon.length</code></li>
<li><code>n == dungeon[i].length</code></li>
<li><code>1 <= m, n <= 200</code></li>
<li><code>-1000 <= dungeon[i][j] <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nint dp[300][300];\nvector<vector<int>>grid;\nint f(int n,int m){\n // i am on n th row and mth column\n if(n>=grid.size() or m>=grid[0].size()){\n return 1e9;\n }\n if(n==grid.size()-1 and m==grid[0].size()-1){\n if(grid[n][m]>=0)return 1;\n // minimum req is 1\n return abs(grid[n][m])+1;\n }\n if(dp[n][m]!=-1)return dp[n][m];\n int right=f(n,m+1);\n int down=f(n+1,m); \n\n int val=min(right,down);\n if(grid[n][m]>=val){\n return dp[n][m]=1;\n }\n return dp[n][m]=val-grid[n][m];\n}\n \n int calculateMinimumHP(vector<vector<int>>& d) {\n grid=d;\n memset(dp,-1,sizeof dp);\n return f(0,0);\n }\n};",
"memory": "12100"
} |
174 | <p>The demons had captured the princess and imprisoned her in <strong>the bottom-right corner</strong> of a <code>dungeon</code>. The <code>dungeon</code> consists of <code>m x n</code> rooms laid out in a 2D grid. Our valiant knight was initially positioned in <strong>the top-left room</strong> and must fight his way through <code>dungeon</code> to rescue the princess.</p>
<p>The knight has an initial health point represented by a positive integer. If at any point his health point drops to <code>0</code> or below, he dies immediately.</p>
<p>Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).</p>
<p>To reach the princess as quickly as possible, the knight decides to move only <strong>rightward</strong> or <strong>downward</strong> in each step.</p>
<p>Return <em>the knight's minimum initial health so that he can rescue the princess</em>.</p>
<p><strong>Note</strong> that 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.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/13/dungeon-grid-1.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> dungeon = [[0]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == dungeon.length</code></li>
<li><code>n == dungeon[i].length</code></li>
<li><code>1 <= m, n <= 200</code></li>
<li><code>-1000 <= dungeon[i][j] <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int calculateMinimumHP(vector<vector<int>>& dungeon) {\n int rows=dungeon.size();\n int cols=dungeon[0].size();\n vector<vector<int>>dp(rows,vector<int>(cols,1e9));;\n dp[rows-1][cols-1]=max(1-dungeon[rows-1][cols-1],1);\n queue<pair<int,int>>q;\n q.push({rows-1,cols-1});\n vector<pair<int,int>>moves;\n moves.push_back({-1,0});\n moves.push_back({0,-1});\n while(q.size()!=0){\n auto it=q.front();\n q.pop();\n int r=it.first;\n int c=it.second;\n int curr=dp[r][c];\n for(int j=0;j<moves.size();j++){\n int newr=r+moves[j].first;\n int newc=c+moves[j].second;\n if(newr>=0 && newc>=0 && newr<rows && newc<cols){\n int new_score=curr-dungeon[newr][newc];\n if(new_score<=0)new_score=1;\n if(dp[newr][newc]>new_score){\n dp[newr][newc]=new_score;\n q.push({newr,newc});\n }\n }\n }\n }\n return dp[0][0];\n }\n};",
"memory": "12200"
} |
174 | <p>The demons had captured the princess and imprisoned her in <strong>the bottom-right corner</strong> of a <code>dungeon</code>. The <code>dungeon</code> consists of <code>m x n</code> rooms laid out in a 2D grid. Our valiant knight was initially positioned in <strong>the top-left room</strong> and must fight his way through <code>dungeon</code> to rescue the princess.</p>
<p>The knight has an initial health point represented by a positive integer. If at any point his health point drops to <code>0</code> or below, he dies immediately.</p>
<p>Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).</p>
<p>To reach the princess as quickly as possible, the knight decides to move only <strong>rightward</strong> or <strong>downward</strong> in each step.</p>
<p>Return <em>the knight's minimum initial health so that he can rescue the princess</em>.</p>
<p><strong>Note</strong> that 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.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/13/dungeon-grid-1.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> dungeon = [[0]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == dungeon.length</code></li>
<li><code>n == dungeon[i].length</code></li>
<li><code>1 <= m, n <= 200</code></li>
<li><code>-1000 <= dungeon[i][j] <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int calculateMinimumHP(vector<vector<int>>& dum) {\n priority_queue<pair<int, pair<int, int>>> pq;\n pq.push({dum[0][0], {0, 0}});\n int n = dum.size();\n int m = dum[0].size();\n vector<vector<int>> dp(n, vector<int>(m, INT_MIN));\n int mini = dum[0][0];\n while (!pq.empty()) {\n auto p = pq.top();\n pq.pop();\n int i = p.second.first;\n int j = p.second.second;\n mini = min(mini, p.first);\n if (i == n - 1 && j == m - 1)\n break;\n if (i < n - 1 && dp[i + 1][j] < p.first + dum[i + 1][j])\n pq.push({p.first + dum[i + 1][j], {i + 1, j}}),\n dp[i + 1][j] = p.first + dum[i + 1][j];\n if (j < m - 1 && dp[i][j + 1] < p.first + dum[i][j + 1])\n pq.push({p.first + dum[i][j + 1], {i, j + 1}}),\n dp[i][j + 1] = p.first + dum[i][j + 1];\n }\n if (mini >= 0)\n return 1;\n return abs(mini) + 1;\n }\n};",
"memory": "12700"
} |
174 | <p>The demons had captured the princess and imprisoned her in <strong>the bottom-right corner</strong> of a <code>dungeon</code>. The <code>dungeon</code> consists of <code>m x n</code> rooms laid out in a 2D grid. Our valiant knight was initially positioned in <strong>the top-left room</strong> and must fight his way through <code>dungeon</code> to rescue the princess.</p>
<p>The knight has an initial health point represented by a positive integer. If at any point his health point drops to <code>0</code> or below, he dies immediately.</p>
<p>Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).</p>
<p>To reach the princess as quickly as possible, the knight decides to move only <strong>rightward</strong> or <strong>downward</strong> in each step.</p>
<p>Return <em>the knight's minimum initial health so that he can rescue the princess</em>.</p>
<p><strong>Note</strong> that 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.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/13/dungeon-grid-1.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> dungeon = [[0]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == dungeon.length</code></li>
<li><code>n == dungeon[i].length</code></li>
<li><code>1 <= m, n <= 200</code></li>
<li><code>-1000 <= dungeon[i][j] <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<pair<int,int>> dir={{1,0},{0,1}};\n int calculateMinimumHP(vector<vector<int>>& dungeon) {\n int n=dungeon.size();\n int m=dungeon[0].size();\n\n priority_queue<pair<int,pair<int,int>>> pq;\n vector<vector<int>> dist(n,vector<int>(m,INT_MIN));\n pq.push({dungeon[0][0],{0,0}});\n dist[0][0]=dungeon[0][0];\n\n while(!pq.empty()){\n int minDis=pq.top().first;\n int x=pq.top().second.first;\n int y=pq.top().second.second;\n pq.pop();\n // cout<<minDis<<endl;\n\n if(x==n-1 && y==m-1){\n // for(int i=0;i<n;i++){\n // for(int j=0;j<m;j++) cout<<dist[i][j];\n // cout<<endl;\n // }\n if(minDis>=0) return 1;\n return abs(minDis)+1; \n }\n\n for(auto it : dir){\n int nx=x+it.first;\n int ny=y+it.second;\n\n if(nx>=0 && nx<n && ny>=0 && ny<m && dist[nx][ny]<dist[x][y]+dungeon[nx][ny]){\n dist[nx][ny]=dist[x][y]+dungeon[nx][ny];\n pq.push({min(minDis,dist[nx][ny]),{nx,ny}});\n }\n } \n }\n\n return -1;\n }\n};",
"memory": "12800"
} |
174 | <p>The demons had captured the princess and imprisoned her in <strong>the bottom-right corner</strong> of a <code>dungeon</code>. The <code>dungeon</code> consists of <code>m x n</code> rooms laid out in a 2D grid. Our valiant knight was initially positioned in <strong>the top-left room</strong> and must fight his way through <code>dungeon</code> to rescue the princess.</p>
<p>The knight has an initial health point represented by a positive integer. If at any point his health point drops to <code>0</code> or below, he dies immediately.</p>
<p>Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).</p>
<p>To reach the princess as quickly as possible, the knight decides to move only <strong>rightward</strong> or <strong>downward</strong> in each step.</p>
<p>Return <em>the knight's minimum initial health so that he can rescue the princess</em>.</p>
<p><strong>Note</strong> that 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.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/13/dungeon-grid-1.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> dungeon = [[0]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == dungeon.length</code></li>
<li><code>n == dungeon[i].length</code></li>
<li><code>1 <= m, n <= 200</code></li>
<li><code>-1000 <= dungeon[i][j] <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int calculateMinimumHP(vector<vector<int>>& d) {\n int n = d.size();\n int m = d[0].size();\n\n\n priority_queue<vector<int>,vector<vector<int>>, greater<vector<int>>> pq;\n\n pq.push({d[0][0]*-1, d[0][0]*-1, 0, 0});\n\n vector<vector<int>> vis(n, vector<int>(m, INT_MAX));\n\n\n\n while(!pq.empty()) {\n vector<int> curr = pq.top();\n pq.pop();\n\n for(int i=0;i<curr.size(); i++) {\n cout<<curr[i]<<\" \";\n }\n cout<<endl;\n\n int min_cost = curr[0];\n int curr_cost = curr[1];\n int x = curr[2];\n int y = curr[3];\n\n if(x == n-1 && y == m-1)\n return max(1,min_cost + 1);\n \n if(x+1 <n and vis[x+1][y] > curr_cost + d[x+1][y]*-1) {\n vis[x+1][y] = curr_cost + d[x+1][y]*-1;\n pq.push({max(min_cost, curr_cost + d[x+1][y]*-1),curr_cost + d[x+1][y]*-1, x+1, y });\n }\n\n if(y+1 < m and vis[x][y + 1] > curr_cost + d[x][y+1]*-1) {\n vis[x][y + 1] = curr_cost + d[x][y+1]*-1;\n pq.push({max(min_cost, curr_cost + d[x][y+1]*-1),curr_cost + d[x][y+1]*-1, x, y +1 });}\n\n }\n return -1;\n\n }\n\n};",
"memory": "12900"
} |
174 | <p>The demons had captured the princess and imprisoned her in <strong>the bottom-right corner</strong> of a <code>dungeon</code>. The <code>dungeon</code> consists of <code>m x n</code> rooms laid out in a 2D grid. Our valiant knight was initially positioned in <strong>the top-left room</strong> and must fight his way through <code>dungeon</code> to rescue the princess.</p>
<p>The knight has an initial health point represented by a positive integer. If at any point his health point drops to <code>0</code> or below, he dies immediately.</p>
<p>Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).</p>
<p>To reach the princess as quickly as possible, the knight decides to move only <strong>rightward</strong> or <strong>downward</strong> in each step.</p>
<p>Return <em>the knight's minimum initial health so that he can rescue the princess</em>.</p>
<p><strong>Note</strong> that 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.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/13/dungeon-grid-1.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> dungeon = [[0]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == dungeon.length</code></li>
<li><code>n == dungeon[i].length</code></li>
<li><code>1 <= m, n <= 200</code></li>
<li><code>-1000 <= dungeon[i][j] <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int calculateMinimumHP(vector<vector<int>>& d) {\n int n = d.size();\n int m = d[0].size();\n\n\n priority_queue<vector<int>,vector<vector<int>>, greater<vector<int>>> pq;\n\n pq.push({d[0][0]*-1, d[0][0]*-1, 0, 0});\n\n vector<vector<int>> vis(n, vector<int>(m, INT_MAX));\n\n\n\n while(!pq.empty()) {\n vector<int> curr = pq.top();\n pq.pop();\n\n int min_cost = curr[0];\n int curr_cost = curr[1];\n int x = curr[2];\n int y = curr[3];\n\n if(x == n-1 && y == m-1)\n return max(1,min_cost + 1);\n \n if(x+1 <n and vis[x+1][y] > curr_cost + d[x+1][y]*-1) {\n vis[x+1][y] = curr_cost + d[x+1][y]*-1;\n pq.push({max(min_cost, curr_cost + d[x+1][y]*-1),curr_cost + d[x+1][y]*-1, x+1, y });\n }\n\n if(y+1 < m and vis[x][y + 1] > curr_cost + d[x][y+1]*-1) {\n vis[x][y + 1] = curr_cost + d[x][y+1]*-1;\n pq.push({max(min_cost, curr_cost + d[x][y+1]*-1),curr_cost + d[x][y+1]*-1, x, y +1 });}\n\n }\n return -1;\n\n }\n\n};",
"memory": "12900"
} |
174 | <p>The demons had captured the princess and imprisoned her in <strong>the bottom-right corner</strong> of a <code>dungeon</code>. The <code>dungeon</code> consists of <code>m x n</code> rooms laid out in a 2D grid. Our valiant knight was initially positioned in <strong>the top-left room</strong> and must fight his way through <code>dungeon</code> to rescue the princess.</p>
<p>The knight has an initial health point represented by a positive integer. If at any point his health point drops to <code>0</code> or below, he dies immediately.</p>
<p>Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).</p>
<p>To reach the princess as quickly as possible, the knight decides to move only <strong>rightward</strong> or <strong>downward</strong> in each step.</p>
<p>Return <em>the knight's minimum initial health so that he can rescue the princess</em>.</p>
<p><strong>Note</strong> that 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.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/13/dungeon-grid-1.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> dungeon = [[0]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == dungeon.length</code></li>
<li><code>n == dungeon[i].length</code></li>
<li><code>1 <= m, n <= 200</code></li>
<li><code>-1000 <= dungeon[i][j] <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n typedef tuple<int, int, int> tp;\n int calculateMinimumHP(vector<vector<int>>& dun) {\n int m = dun.size(), n = dun[0].size();\n priority_queue<tp> pq;\n pq.push({dun[0][0], 0, 0});\n vector<vector<int>> dist(m, vector<int>(n, INT_MIN));\n dist[0][0] = dun[0][0];\n int w, i, j;\n int dir[2][2] = {{0, 1}, {1, 0}};\n while (!pq.empty()) {\n tie(w, i, j) = pq.top(); pq.pop();\n if (i == m - 1 && j == n - 1) \n return max(1, -w + 1);\n for (auto& d : dir) {\n int x = i + d[0], y = j + d[1];\n if (x < m && y < n && dist[x][y] < dist[i][j] + dun[x][y]) {\n dist[x][y] = dist[i][j] + dun[x][y];\n pq.push({min(w, dist[i][j] + dun[x][y]), x, y});\n }\n }\n }\n return -1;\n }\n};",
"memory": "13000"
} |
174 | <p>The demons had captured the princess and imprisoned her in <strong>the bottom-right corner</strong> of a <code>dungeon</code>. The <code>dungeon</code> consists of <code>m x n</code> rooms laid out in a 2D grid. Our valiant knight was initially positioned in <strong>the top-left room</strong> and must fight his way through <code>dungeon</code> to rescue the princess.</p>
<p>The knight has an initial health point represented by a positive integer. If at any point his health point drops to <code>0</code> or below, he dies immediately.</p>
<p>Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).</p>
<p>To reach the princess as quickly as possible, the knight decides to move only <strong>rightward</strong> or <strong>downward</strong> in each step.</p>
<p>Return <em>the knight's minimum initial health so that he can rescue the princess</em>.</p>
<p><strong>Note</strong> that 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.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/13/dungeon-grid-1.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> dungeon = [[0]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == dungeon.length</code></li>
<li><code>n == dungeon[i].length</code></li>
<li><code>1 <= m, n <= 200</code></li>
<li><code>-1000 <= dungeon[i][j] <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\n vector<array<int,2>> dir = {{0,1}, {1,0}};\n int f (vector<vector<int>> &grid, int row, int col){\n // priority_queue<array<int,3>, vector<array<int,3>>, greater<array<int,3>>> pq;\n priority_queue<array<int,4>> pq;//[currlowesthp][currhp][row][col]\n pq.push({grid[row][col], grid[row][col], row, col});\n int n = grid.size(), m = grid[0].size();\n vector<vector<int>> dist(n, vector<int> (m, -1e9));\n\n while (!pq.empty()){\n auto temp = pq.top();\n pq.pop();\n int r = temp[2], c = temp[3], maxi = temp[0], currHp = temp[1];\n if (r == n-1 && c == m-1){\n return maxi;\n }\n for (auto d : dir){\n int x = d[0] + r, y = d[1] + c;\n if (x<n && y<m){\n if (dist[x][y] < grid[x][y] + currHp){\n dist[x][y] = grid[x][y] + currHp;\n pq.push({min(maxi, dist[x][y]), dist[x][y], x, y});\n }\n }\n }\n }\n return -1;\n }\npublic:\n int calculateMinimumHP(vector<vector<int>>& dungeon) {\n int n = dungeon.size(), m = dungeon[0].size();\n vector<vector<int>> dp(n, vector<int>(m,-1));\n\n int ans = - 1 * f(dungeon, 0, 0) + 1;\n if (ans<=0) return 1;\n else return ans;\n }\n};",
"memory": "13100"
} |
174 | <p>The demons had captured the princess and imprisoned her in <strong>the bottom-right corner</strong> of a <code>dungeon</code>. The <code>dungeon</code> consists of <code>m x n</code> rooms laid out in a 2D grid. Our valiant knight was initially positioned in <strong>the top-left room</strong> and must fight his way through <code>dungeon</code> to rescue the princess.</p>
<p>The knight has an initial health point represented by a positive integer. If at any point his health point drops to <code>0</code> or below, he dies immediately.</p>
<p>Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).</p>
<p>To reach the princess as quickly as possible, the knight decides to move only <strong>rightward</strong> or <strong>downward</strong> in each step.</p>
<p>Return <em>the knight's minimum initial health so that he can rescue the princess</em>.</p>
<p><strong>Note</strong> that 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.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/13/dungeon-grid-1.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> dungeon = [[0]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == dungeon.length</code></li>
<li><code>n == dungeon[i].length</code></li>
<li><code>1 <= m, n <= 200</code></li>
<li><code>-1000 <= dungeon[i][j] <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\n vector<array<int,2>> dir = {{0,1}, {1,0}};\n int f (vector<vector<int>> &grid, int row, int col){\n // priority_queue<array<int,3>, vector<array<int,3>>, greater<array<int,3>>> pq;\n priority_queue<array<int,4>> pq;//[currLowestHp][currhp][row][col]\n pq.push({grid[row][col], grid[row][col], row, col});\n int n = grid.size(), m = grid[0].size();\n vector<vector<int>> dist(n, vector<int> (m, -1e9));\n\n while (!pq.empty()){\n auto temp = pq.top();\n pq.pop();\n int r = temp[2], c = temp[3], maxi = temp[0], currHp = temp[1];\n if (r == n-1 && c == m-1){\n return maxi;\n }\n for (auto d : dir){\n int x = d[0] + r, y = d[1] + c;\n if (x<n && y<m){\n if (dist[x][y] < grid[x][y] + currHp){\n dist[x][y] = grid[x][y] + currHp;\n pq.push({min(maxi, dist[x][y]), dist[x][y], x, y});\n }\n }\n }\n }\n return -1;\n }\npublic:\n int calculateMinimumHP(vector<vector<int>>& dungeon) {\n int n = dungeon.size(), m = dungeon[0].size();\n vector<vector<int>> dp(n, vector<int>(m,-1));\n\n int ans = - 1 * f(dungeon, 0, 0) + 1;\n if (ans<=0) return 1;\n else return ans;\n }\n};",
"memory": "13100"
} |
174 | <p>The demons had captured the princess and imprisoned her in <strong>the bottom-right corner</strong> of a <code>dungeon</code>. The <code>dungeon</code> consists of <code>m x n</code> rooms laid out in a 2D grid. Our valiant knight was initially positioned in <strong>the top-left room</strong> and must fight his way through <code>dungeon</code> to rescue the princess.</p>
<p>The knight has an initial health point represented by a positive integer. If at any point his health point drops to <code>0</code> or below, he dies immediately.</p>
<p>Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).</p>
<p>To reach the princess as quickly as possible, the knight decides to move only <strong>rightward</strong> or <strong>downward</strong> in each step.</p>
<p>Return <em>the knight's minimum initial health so that he can rescue the princess</em>.</p>
<p><strong>Note</strong> that 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.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/13/dungeon-grid-1.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> dungeon = [[0]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == dungeon.length</code></li>
<li><code>n == dungeon[i].length</code></li>
<li><code>1 <= m, n <= 200</code></li>
<li><code>-1000 <= dungeon[i][j] <= 1000</code></li>
</ul>
| 3 | {
"code": "#include<queue>\ntypedef pair <long long,long long> ii1;\ntypedef pair <long long,ii1> ii;\n\nclass Solution {\npublic:\n\n int dx[2]={0,1};\n int dy[2]={1,0};\n\n bool bound(int x,int y,int n,int m)\n {\n if(x<=0 || x>n || y<=0 || y>m)\n {\n return false;\n }\n return true;\n }\n\n bool check(long long key,long long a[][210],int n,int m)\n {\n long long d[210][210]={0};\n bool ok[210][210]={0};\n for(int i=1;i<=n;i++)\n {\n for(int j=1;j<=m;j++)\n {\n d[i][j]=-999999999999;\n }\n }\n priority_queue<ii,vector<ii>,less<ii>> pq;\n pq.push(ii(key+a[1][1],ii1(1,1)));\n d[1][1]=key+a[1][1];\n ok[1][1]=true;\n while(!pq.empty())\n {\n long long dd = pq.top().first;\n long long x = pq.top().second.first;\n long long y = pq.top().second.second;\n pq.pop();\n //cout<<dd<<\" \"<<x<<\" \"<<y<<endl;\n if(dd<=0) \n {\n return false;\n }\n if(x==n && y==m)\n {\n return true;\n }\n if(dd != d[x][y])\n {\n continue;\n }\n ok[x][y]=true;\n for(int i=0;i<2;i++)\n {\n int newx= x + dx[i];\n int newy = y + dy[i];\n if(bound(newx,newy,n,m)&& d[newx][newy] <=d[x][y] + a[newx][newy])\n {\n d[newx][newy]=d[x][y] + a[newx][newy];\n pq.push(ii(d[newx][newy],ii1(newx,newy)));\n }\n }\n }\n return false;\n }\n\n int calculateMinimumHP(vector<vector<int>>& b) {\n long long a[210][210]={0};\n for(int i=0;i<b.size();i++)\n {\n for(int j=0;j<b[i].size();j++)\n {\n a[i+1][j+1]=b[i][j];\n }\n } \n int n=b.size(),m=b[0].size();\n\n long long l=1,r=4e9;\n long long ans =0;\n //check(2,a,n,m);\n while(l<=r)\n {\n long long mid = l+ (r-l)/2;\n if(check(mid,a,n,m))\n {\n r=mid-1;\n ans =mid;\n //cout<<mid<<endl;\n\n }\n else\n {\n l=mid+1;;\n }\n }\n return ans;\n }\n};",
"memory": "14100"
} |
174 | <p>The demons had captured the princess and imprisoned her in <strong>the bottom-right corner</strong> of a <code>dungeon</code>. The <code>dungeon</code> consists of <code>m x n</code> rooms laid out in a 2D grid. Our valiant knight was initially positioned in <strong>the top-left room</strong> and must fight his way through <code>dungeon</code> to rescue the princess.</p>
<p>The knight has an initial health point represented by a positive integer. If at any point his health point drops to <code>0</code> or below, he dies immediately.</p>
<p>Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).</p>
<p>To reach the princess as quickly as possible, the knight decides to move only <strong>rightward</strong> or <strong>downward</strong> in each step.</p>
<p>Return <em>the knight's minimum initial health so that he can rescue the princess</em>.</p>
<p><strong>Note</strong> that 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.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/13/dungeon-grid-1.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> dungeon = [[0]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == dungeon.length</code></li>
<li><code>n == dungeon[i].length</code></li>
<li><code>1 <= m, n <= 200</code></li>
<li><code>-1000 <= dungeon[i][j] <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool check(vector<vector<int>>& dun,int power)\n {\n int n=dun.size();\n int m=dun[0].size();\n\n queue<pair<int,int>>q;\n q.push({0,0});\n int arr[n][m];\n for(int i=0;i<n;i++)\n for(int j=0;j<m;j++)\n arr[i][j]=INT_MIN;\n arr[0][0]=power+dun[0][0];\n int flg=0;\n if(arr[0][0]<=0)\n return false;\n while(!q.empty())\n {\n int x=(q.front()).first;\n int y=(q.front()).second;\n \n q.pop();\n if(x+1<n&&arr[x][y]+dun[x+ 1][y]>0&&arr[x+1][y]<arr[x][y]+dun[x+ 1][y])\n {\n arr[x+1][y]=arr[x][y]+dun[x+1][y];\n q.push({x+1,y});\n }\n if(y+1<m&&arr[x][y]+dun[x][y+1]>0&&arr[x][y+1]<arr[x][y]+dun[x][y+1])\n {\n arr[x][y+1]=arr[x][y]+dun[x][y+1];\n q.push({x,y+1});\n }\n \n }\n if(arr[n-1][m-1]>0)\n return true;\n return false;\n }\n int calculateMinimumHP(vector<vector<int>>& dun) {\n int left=1;\n int right=2000;\n int res=2000;\n while(left<=right)\n {\n int mid=(left+right)>>1;\n if(check(dun,mid))\n {\n res=mid;\n right=mid-1;\n }\n else\n left=mid+1;\n }\n return res;\n /*if(dun[x+1][y]+arr[x][y].first>arr[x+1][y].first)\n {\n arr[x+1][y].first=dun[x+1][y]+arr[x][y].first;\n arr[x+1][y].second=max(arr[x][y].second,abs(min(arr[x+1][y].first,0)-1));\n q.push({x+1,y});\n }\n else if(dun[x+1][y]+arr[x][y].first==arr[x+1][y].first)\n {\n arr[x+1][y].second=max(arr[x][y].second,abs(min(arr[x+1][y].first,0)-1));\n q.push({x+1,y});\n }\n }\n if(y+1<m)\n {\n if(max(arr[x][y].second,abs(min(dun[x][y+1]+arr[x][y].first,0)-1))<arr[x][y+1].second)\n {\n arr[x][y+1].first=dun[x][y+1]+arr[x][y].first;\n arr[x][y+1].second=max(arr[x][y].second,abs(min(arr[x][y+1].first,0)-1));\n q.push({x,y+1});\n }\n else if(max(arr[x][y].second,abs(min(dun[x][y+1]+arr[x][y].first,0)-1))==arr[x][y+1].second)\n {\n arr[x][y+1].first=max(dun[x][y+1]+arr[x][y].first,arr[x][y+1].first);\n q.push({x,y+1});\n }\n /*if(dun[x][y+1]+arr[x][y].first>arr[x][y+1].first)\n {\n arr[x][y+1].first=dun[x][y+1]+arr[x][y].first;\n arr[x][y+1].second=max(arr[x][y].second,abs(min(arr[x][y+1].first,0)-1));\n q.push({x,y+1});\n }\n else if(dun[x][y+1]+arr[x][y].first==arr[x][y+1].first)\n {\n arr[x][y+1].second=max(arr[x][y].second,abs(min(arr[x][y+1].first,0)-1));\n q.push({x,y+1});\n }*/\n //}\n //}\n \n //return arr[n-1][m-1].second;\n }\n};",
"memory": "14200"
} |
174 | <p>The demons had captured the princess and imprisoned her in <strong>the bottom-right corner</strong> of a <code>dungeon</code>. The <code>dungeon</code> consists of <code>m x n</code> rooms laid out in a 2D grid. Our valiant knight was initially positioned in <strong>the top-left room</strong> and must fight his way through <code>dungeon</code> to rescue the princess.</p>
<p>The knight has an initial health point represented by a positive integer. If at any point his health point drops to <code>0</code> or below, he dies immediately.</p>
<p>Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).</p>
<p>To reach the princess as quickly as possible, the knight decides to move only <strong>rightward</strong> or <strong>downward</strong> in each step.</p>
<p>Return <em>the knight's minimum initial health so that he can rescue the princess</em>.</p>
<p><strong>Note</strong> that 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.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/13/dungeon-grid-1.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> dungeon = [[0]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == dungeon.length</code></li>
<li><code>n == dungeon[i].length</code></li>
<li><code>1 <= m, n <= 200</code></li>
<li><code>-1000 <= dungeon[i][j] <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool check(vector<vector<int>>& dun,int power)\n {\n int n=dun.size();\n int m=dun[0].size();\n\n queue<pair<int,int>>q;\n q.push({0,0});\n int arr[n][m];\n for(int i=0;i<n;i++)\n for(int j=0;j<m;j++)\n arr[i][j]=INT_MIN;\n arr[0][0]=power+dun[0][0];\n int flg=0;\n if(arr[0][0]<=0)\n return false;\n while(!q.empty())\n {\n int x=(q.front()).first;\n int y=(q.front()).second;\n \n q.pop();\n if(x+1<n&&arr[x][y]+dun[x+ 1][y]>0&&arr[x+1][y]<arr[x][y]+dun[x+ 1][y])\n {\n arr[x+1][y]=arr[x][y]+dun[x+1][y];\n q.push({x+1,y});\n }\n if(y+1<m&&arr[x][y]+dun[x][y+1]>0&&arr[x][y+1]<arr[x][y]+dun[x][y+1])\n {\n arr[x][y+1]=arr[x][y]+dun[x][y+1];\n q.push({x,y+1});\n }\n \n }\n if(arr[n-1][m-1]>0)\n return true;\n return false;\n }\n int calculateMinimumHP(vector<vector<int>>& dun) {\n int left=1;\n int right=1500;\n int res=1500;\n while(left<=right)\n {\n int mid=(left+right)>>1;\n if(check(dun,mid))\n {\n res=mid;\n right=mid-1;\n }\n else\n left=mid+1;\n }\n return res;\n /*if(dun[x+1][y]+arr[x][y].first>arr[x+1][y].first)\n {\n arr[x+1][y].first=dun[x+1][y]+arr[x][y].first;\n arr[x+1][y].second=max(arr[x][y].second,abs(min(arr[x+1][y].first,0)-1));\n q.push({x+1,y});\n }\n else if(dun[x+1][y]+arr[x][y].first==arr[x+1][y].first)\n {\n arr[x+1][y].second=max(arr[x][y].second,abs(min(arr[x+1][y].first,0)-1));\n q.push({x+1,y});\n }\n }\n if(y+1<m)\n {\n if(max(arr[x][y].second,abs(min(dun[x][y+1]+arr[x][y].first,0)-1))<arr[x][y+1].second)\n {\n arr[x][y+1].first=dun[x][y+1]+arr[x][y].first;\n arr[x][y+1].second=max(arr[x][y].second,abs(min(arr[x][y+1].first,0)-1));\n q.push({x,y+1});\n }\n else if(max(arr[x][y].second,abs(min(dun[x][y+1]+arr[x][y].first,0)-1))==arr[x][y+1].second)\n {\n arr[x][y+1].first=max(dun[x][y+1]+arr[x][y].first,arr[x][y+1].first);\n q.push({x,y+1});\n }\n /*if(dun[x][y+1]+arr[x][y].first>arr[x][y+1].first)\n {\n arr[x][y+1].first=dun[x][y+1]+arr[x][y].first;\n arr[x][y+1].second=max(arr[x][y].second,abs(min(arr[x][y+1].first,0)-1));\n q.push({x,y+1});\n }\n else if(dun[x][y+1]+arr[x][y].first==arr[x][y+1].first)\n {\n arr[x][y+1].second=max(arr[x][y].second,abs(min(arr[x][y+1].first,0)-1));\n q.push({x,y+1});\n }*/\n //}\n //}\n \n //return arr[n-1][m-1].second;\n }\n};",
"memory": "14300"
} |
174 | <p>The demons had captured the princess and imprisoned her in <strong>the bottom-right corner</strong> of a <code>dungeon</code>. The <code>dungeon</code> consists of <code>m x n</code> rooms laid out in a 2D grid. Our valiant knight was initially positioned in <strong>the top-left room</strong> and must fight his way through <code>dungeon</code> to rescue the princess.</p>
<p>The knight has an initial health point represented by a positive integer. If at any point his health point drops to <code>0</code> or below, he dies immediately.</p>
<p>Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).</p>
<p>To reach the princess as quickly as possible, the knight decides to move only <strong>rightward</strong> or <strong>downward</strong> in each step.</p>
<p>Return <em>the knight's minimum initial health so that he can rescue the princess</em>.</p>
<p><strong>Note</strong> that 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.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/13/dungeon-grid-1.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> dungeon = [[0]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == dungeon.length</code></li>
<li><code>n == dungeon[i].length</code></li>
<li><code>1 <= m, n <= 200</code></li>
<li><code>-1000 <= dungeon[i][j] <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int calculateMinimumHP(vector<vector<int>>& a) {\n const int INF = 1e9;\n int n = a.size(), m = a[0].size();\n int l = 0, r = (n + m - 1) * 1000 + 1;\n while (r - l > 1){\n int mid = (l + r) / 2;\n auto dp = a;\n for (int i = 0; i < n; i++){\n for (int j = 0; j < m; j++){\n if (i || j){\n dp[i][j] += max(i? dp[i - 1][j] : -INF, j? dp[i][j - 1] : -INF);\n }\n if (mid + dp[i][j] <= 0) dp[i][j] = -INF;\n }\n }\n if (dp[n - 1][m - 1] == -INF) l = mid;\n else r = mid;\n } \n return r;\n }\n};",
"memory": "14400"
} |
174 | <p>The demons had captured the princess and imprisoned her in <strong>the bottom-right corner</strong> of a <code>dungeon</code>. The <code>dungeon</code> consists of <code>m x n</code> rooms laid out in a 2D grid. Our valiant knight was initially positioned in <strong>the top-left room</strong> and must fight his way through <code>dungeon</code> to rescue the princess.</p>
<p>The knight has an initial health point represented by a positive integer. If at any point his health point drops to <code>0</code> or below, he dies immediately.</p>
<p>Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).</p>
<p>To reach the princess as quickly as possible, the knight decides to move only <strong>rightward</strong> or <strong>downward</strong> in each step.</p>
<p>Return <em>the knight's minimum initial health so that he can rescue the princess</em>.</p>
<p><strong>Note</strong> that 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.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/13/dungeon-grid-1.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> dungeon = [[0]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == dungeon.length</code></li>
<li><code>n == dungeon[i].length</code></li>
<li><code>1 <= m, n <= 200</code></li>
<li><code>-1000 <= dungeon[i][j] <= 1000</code></li>
</ul>
| 3 | {
"code": "bool fn(vector<vector<int>> matrix , int m , int n , int h){\n matrix[0][0] = matrix[0][0]+h;\n if(matrix[0][0] <= 0){return false;}\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n if(i==0 && j==0){continue;}\n int maxi = INT_MIN;\n if(i-1 >= 0 && matrix[i-1][j] > 0){\n maxi=max(maxi,matrix[i-1][j]);\n }\n if(j-1 >= 0 && matrix[i][j-1] > 0){\n maxi = max(maxi,matrix[i][j-1]);\n }\n if(maxi == INT_MIN){matrix[i][j]=INT_MIN;}\n else{\n matrix[i][j]=matrix[i][j]+maxi;\n if(matrix[i][j] <= 0){matrix[i][j]=INT_MIN;}\n }\n }\n }\n if(matrix[n-1][m-1] >0){return true;}\n else return false;\n} \nclass Solution {\npublic:\n int calculateMinimumHP(vector<vector<int>>& dungeon) {\n int n=dungeon.size();\n int m=dungeon[0].size();\n int i = 1 ; \n int j = 400*1000;\n while(i <= j){\n int mid = i + (j-i)/2 ; \n if(fn(dungeon , m , n , mid)){\n j = mid-1 ;\n }else{\n i = mid+1 ;\n }\n }\n return i ;\n // for(int i=n-1;i>=0;i--){\n // for(int j=m-1;j>=0;j--){\n // if(i==n-1 && j==m-1){continue;}\n // int maxi=INT_MIN;\n // if(i+1<n){\n // maxi =max(maxi ,min(dungeon[i][j],dungeon[i][j]+dungeon[i+1][j]));\n // }\n // if(j+1 < m){\n // maxi = max(maxi,min(dungeon[i][j],dungeon[i][j]+dungeon[i][j+1]));\n // }\n // dungeon[i][j]=maxi;\n // }\n // }\n // if(dungeon[0][0] > 0){return 1;}\n // return abs(dungeon[0][0])+1;\n }\n};",
"memory": "14500"
} |
174 | <p>The demons had captured the princess and imprisoned her in <strong>the bottom-right corner</strong> of a <code>dungeon</code>. The <code>dungeon</code> consists of <code>m x n</code> rooms laid out in a 2D grid. Our valiant knight was initially positioned in <strong>the top-left room</strong> and must fight his way through <code>dungeon</code> to rescue the princess.</p>
<p>The knight has an initial health point represented by a positive integer. If at any point his health point drops to <code>0</code> or below, he dies immediately.</p>
<p>Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).</p>
<p>To reach the princess as quickly as possible, the knight decides to move only <strong>rightward</strong> or <strong>downward</strong> in each step.</p>
<p>Return <em>the knight's minimum initial health so that he can rescue the princess</em>.</p>
<p><strong>Note</strong> that 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.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/13/dungeon-grid-1.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> dungeon = [[0]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == dungeon.length</code></li>
<li><code>n == dungeon[i].length</code></li>
<li><code>1 <= m, n <= 200</code></li>
<li><code>-1000 <= dungeon[i][j] <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool IsPossibleToReach(int start, vector<vector<int>>& dungeon) {\n // O(m) mem, O(n * m) time\n vector<int> prev(dungeon[0].size());\n\n prev[0] = start;\n for (int i = 0; i < dungeon.size(); ++i) {\n vector<int> current(dungeon[i].size(), 0);\n\n if (prev[0] > 0) {\n current[0] = max(0, prev[0] + dungeon[i][0]);\n }\n\n for (int j = 1; j < current.size(); ++j) {\n int mx_state = max(prev[j], current[j - 1]);\n if (mx_state > 0) {\n current[j] = max(0, mx_state + dungeon[i][j]);\n }\n }\n prev = current;\n }\n return prev.back() > 0;\n }\n\n int calculateMinimumHP(vector<vector<int>>& dungeon) {\n int left = 0, right = 1000 * 200 + 1;\n // (left, right]\n\n // O(log((n + m) maxVal) * n * m), O(m)\n\n while (right - left > 1) {\n int m = (left + right) / 2;\n\n if (IsPossibleToReach(m, dungeon)) {\n right = m;\n } else {\n left = m;\n }\n }\n return right;\n }\n};",
"memory": "14600"
} |
174 | <p>The demons had captured the princess and imprisoned her in <strong>the bottom-right corner</strong> of a <code>dungeon</code>. The <code>dungeon</code> consists of <code>m x n</code> rooms laid out in a 2D grid. Our valiant knight was initially positioned in <strong>the top-left room</strong> and must fight his way through <code>dungeon</code> to rescue the princess.</p>
<p>The knight has an initial health point represented by a positive integer. If at any point his health point drops to <code>0</code> or below, he dies immediately.</p>
<p>Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).</p>
<p>To reach the princess as quickly as possible, the knight decides to move only <strong>rightward</strong> or <strong>downward</strong> in each step.</p>
<p>Return <em>the knight's minimum initial health so that he can rescue the princess</em>.</p>
<p><strong>Note</strong> that 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.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/13/dungeon-grid-1.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> dungeon = [[0]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == dungeon.length</code></li>
<li><code>n == dungeon[i].length</code></li>
<li><code>1 <= m, n <= 200</code></li>
<li><code>-1000 <= dungeon[i][j] <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int m, n;\n int calculateMinimumHP(vector<vector<int>>& grid) {\n m = grid.size(); n = grid[0].size();\n int left = 1, right = 1000 * (m+n) + 1, ans = right;\n while (left <= right) {\n int mid = left + (right - left) / 2;\n if (isGood(grid, mid)) {\n ans = mid;\n right = mid - 1;\n } else left = mid + 1;\n }\n return ans;\n }\n bool isGood(vector<vector<int>>& grid, int initHealth) {\n vector<vector<int>> dp(m, vector<int>(n, 0));\n dp[0][0] = initHealth + grid[0][0];\n for (int r = 0; r < m; ++r) {\n for (int c = 0; c < n; ++c) {\n if (r > 0 && dp[r-1][c] > 0)\n dp[r][c] = max(dp[r][c], dp[r-1][c] + grid[r][c]);\n if (c > 0 && dp[r][c-1] > 0)\n dp[r][c] = max(dp[r][c], dp[r][c-1] + grid[r][c]);\n }\n }\n return dp[m-1][n-1] > 0;\n }\n};",
"memory": "14700"
} |
174 | <p>The demons had captured the princess and imprisoned her in <strong>the bottom-right corner</strong> of a <code>dungeon</code>. The <code>dungeon</code> consists of <code>m x n</code> rooms laid out in a 2D grid. Our valiant knight was initially positioned in <strong>the top-left room</strong> and must fight his way through <code>dungeon</code> to rescue the princess.</p>
<p>The knight has an initial health point represented by a positive integer. If at any point his health point drops to <code>0</code> or below, he dies immediately.</p>
<p>Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).</p>
<p>To reach the princess as quickly as possible, the knight decides to move only <strong>rightward</strong> or <strong>downward</strong> in each step.</p>
<p>Return <em>the knight's minimum initial health so that he can rescue the princess</em>.</p>
<p><strong>Note</strong> that 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.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/13/dungeon-grid-1.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> dungeon = [[0]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == dungeon.length</code></li>
<li><code>n == dungeon[i].length</code></li>
<li><code>1 <= m, n <= 200</code></li>
<li><code>-1000 <= dungeon[i][j] <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int m, n;\n int calculateMinimumHP(vector<vector<int>>& grid) {\n m = grid.size(); n = grid[0].size();\n int left = 1, right = 1000 * (m+n) + 1, ans = right;\n while (left <= right) {\n int mid = left + (right - left) / 2;\n if (isGood(grid, mid)) {\n ans = mid;\n right = mid - 1;\n } else left = mid + 1;\n }\n return ans;\n }\n bool isGood(vector<vector<int>>& grid, int initHealth) {\n vector<vector<int>> dp(m, vector<int>(n, 0));\n dp[0][0] = initHealth + grid[0][0];\n for (int r = 0; r < m; ++r) {\n for (int c = 0; c < n; ++c) {\n if (r > 0 && dp[r-1][c] > 0)\n dp[r][c] = max(dp[r][c], dp[r-1][c] + grid[r][c]);\n if (c > 0 && dp[r][c-1] > 0)\n dp[r][c] = max(dp[r][c], dp[r][c-1] + grid[r][c]);\n }\n }\n return dp[m-1][n-1] > 0;\n }\n};",
"memory": "14800"
} |
174 | <p>The demons had captured the princess and imprisoned her in <strong>the bottom-right corner</strong> of a <code>dungeon</code>. The <code>dungeon</code> consists of <code>m x n</code> rooms laid out in a 2D grid. Our valiant knight was initially positioned in <strong>the top-left room</strong> and must fight his way through <code>dungeon</code> to rescue the princess.</p>
<p>The knight has an initial health point represented by a positive integer. If at any point his health point drops to <code>0</code> or below, he dies immediately.</p>
<p>Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).</p>
<p>To reach the princess as quickly as possible, the knight decides to move only <strong>rightward</strong> or <strong>downward</strong> in each step.</p>
<p>Return <em>the knight's minimum initial health so that he can rescue the princess</em>.</p>
<p><strong>Note</strong> that 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.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/13/dungeon-grid-1.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> dungeon = [[0]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == dungeon.length</code></li>
<li><code>n == dungeon[i].length</code></li>
<li><code>1 <= m, n <= 200</code></li>
<li><code>-1000 <= dungeon[i][j] <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n const int INF = 1e7;\n bool bin_search(int p, vector<vector<int>> &v){\n int n = v.size();\n int m = v[0].size();\n vector<vector<int>> dp(n,vector<int>(m,0));\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n if(i==0 and j==0){\n dp[i][j]=v[i][j]+p;\n if(dp[i][j]<=0) dp[i][j]=-INF;\n }\n else{\n dp[i][j]=max(i?dp[i-1][j]:-INF,j?dp[i][j-1]:-INF)+v[i][j];\n if(dp[i][j]<=0) dp[i][j]=-INF;\n }\n }\n }\n // cout<<p<<\" \"<<dp[n-1][m-1]<<endl;\n return dp[n-1][m-1]>0;\n}\n\n int calculateMinimumHP(vector<vector<int>>& dungeon) {\n int lo = 1;\n int hi =3e5+1;\n int ans;\n while(lo<=hi){\n int mid = (lo+hi)/2;\n if(bin_search(mid,dungeon)){\n ans=mid;\n hi=mid-1;\n }\n else lo=mid+1;\n // cout<<ans<<endl;\n }\n // cout<<ans<<endl;\n return ans;\n }\n};",
"memory": "14900"
} |
174 | <p>The demons had captured the princess and imprisoned her in <strong>the bottom-right corner</strong> of a <code>dungeon</code>. The <code>dungeon</code> consists of <code>m x n</code> rooms laid out in a 2D grid. Our valiant knight was initially positioned in <strong>the top-left room</strong> and must fight his way through <code>dungeon</code> to rescue the princess.</p>
<p>The knight has an initial health point represented by a positive integer. If at any point his health point drops to <code>0</code> or below, he dies immediately.</p>
<p>Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).</p>
<p>To reach the princess as quickly as possible, the knight decides to move only <strong>rightward</strong> or <strong>downward</strong> in each step.</p>
<p>Return <em>the knight's minimum initial health so that he can rescue the princess</em>.</p>
<p><strong>Note</strong> that 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.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/13/dungeon-grid-1.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> dungeon = [[0]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == dungeon.length</code></li>
<li><code>n == dungeon[i].length</code></li>
<li><code>1 <= m, n <= 200</code></li>
<li><code>-1000 <= dungeon[i][j] <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int calculateMinimumHP(vector<vector<int>>& dungeon) {\n // find the least cost path -> the min in the least cost should be +ve\n // binary search over the cost\n int low=0, high=1e6;\n int ans=1e6;\n int n=dungeon.size(),m=dungeon[0].size();\n while(low<=high){\n int mid=(low+high)/2;\n vector<vector<int>>dp(n,vector<int>(m,-1));\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n if(i==0 && j==0)dp[i][j]=mid;\n else if(i==0){\n if(dp[i][j-1]>0)dp[i][j]=dp[i][j-1];\n }\n else if(j==0){\n if(dp[i-1][j]>0)dp[i][j]=dp[i-1][j];\n }\n else{\n if(dp[i-1][j]>0 && dp[i][j-1]>0)dp[i][j]=max(dp[i-1][j],dp[i][j-1]);\n else if(dp[i-1][j]>0)dp[i][j]=dp[i-1][j];\n else if(dp[i][j-1]>0)dp[i][j]=dp[i][j-1];\n } \n if(dp[i][j]>0)dp[i][j]+=dungeon[i][j];\n }\n }\n if(dp[n-1][m-1]>0){\n // this mid is big enough, try to go lower\n ans=min(ans,mid);\n high=mid-1;\n }\n else low=mid+1;\n }\n return ans;\n }\n};",
"memory": "15000"
} |
174 | <p>The demons had captured the princess and imprisoned her in <strong>the bottom-right corner</strong> of a <code>dungeon</code>. The <code>dungeon</code> consists of <code>m x n</code> rooms laid out in a 2D grid. Our valiant knight was initially positioned in <strong>the top-left room</strong> and must fight his way through <code>dungeon</code> to rescue the princess.</p>
<p>The knight has an initial health point represented by a positive integer. If at any point his health point drops to <code>0</code> or below, he dies immediately.</p>
<p>Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).</p>
<p>To reach the princess as quickly as possible, the knight decides to move only <strong>rightward</strong> or <strong>downward</strong> in each step.</p>
<p>Return <em>the knight's minimum initial health so that he can rescue the princess</em>.</p>
<p><strong>Note</strong> that 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.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/13/dungeon-grid-1.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> dungeon = [[0]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == dungeon.length</code></li>
<li><code>n == dungeon[i].length</code></li>
<li><code>1 <= m, n <= 200</code></li>
<li><code>-1000 <= dungeon[i][j] <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int calculateMinimumHP(vector<vector<int>>& dungeon) {\n int m = dungeon.size();\n int n = dungeon[0].size();\n\n dp.resize(m, vector<int>(n, INT_MAX));\n dp[m-1][n-1]=max(1-dungeon[m-1][n-1], 1);\n\n queue<pair<int, int>> q;\n q.push({m-1, n-1});\n\n unordered_set<int> seen;\n seen.insert(m*n-1);\n\n vector<int> dx = {-1, 0};\n vector<int> dy = {0, -1};\n\n while(!q.empty())\n {\n int sz = q.size();\n for (int i=0; i<sz; ++i)\n {\n auto x = q.front().first;\n auto y = q.front().second;\n q.pop();\n\n for (int i=0; i<2; ++i)\n {\n auto xx = dx[i]+x;\n auto yy = dy[i]+y;\n\n if (xx>=0 && xx<m && yy>=0 && yy<n)\n { \n // put in the queue if has not been visited\n if (seen.find(xx*n+yy)==seen.end())\n {\n q.push({xx, yy});\n seen.insert(xx*n+yy);\n }\n // update value\n int new_val = max(dp[x][y]-dungeon[xx][yy], 1);\n dp[xx][yy]=min(dp[xx][yy], new_val);\n }\n }\n }\n }\n return dp[0][0];\n }\nprivate:\n vector<vector<int>> dp;\n};",
"memory": "15100"
} |
174 | <p>The demons had captured the princess and imprisoned her in <strong>the bottom-right corner</strong> of a <code>dungeon</code>. The <code>dungeon</code> consists of <code>m x n</code> rooms laid out in a 2D grid. Our valiant knight was initially positioned in <strong>the top-left room</strong> and must fight his way through <code>dungeon</code> to rescue the princess.</p>
<p>The knight has an initial health point represented by a positive integer. If at any point his health point drops to <code>0</code> or below, he dies immediately.</p>
<p>Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).</p>
<p>To reach the princess as quickly as possible, the knight decides to move only <strong>rightward</strong> or <strong>downward</strong> in each step.</p>
<p>Return <em>the knight's minimum initial health so that he can rescue the princess</em>.</p>
<p><strong>Note</strong> that 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.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/13/dungeon-grid-1.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> dungeon = [[0]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == dungeon.length</code></li>
<li><code>n == dungeon[i].length</code></li>
<li><code>1 <= m, n <= 200</code></li>
<li><code>-1000 <= dungeon[i][j] <= 1000</code></li>
</ul>
| 3 | {
"code": "\n\nclass Solution {\npublic:\n using Grid = vector<vector<int>>;\n int m, n;\n vector<vector<int>> temp;\n\n bool tryValue(Grid& grid, int value){\n temp = vector<vector<int>>(m, vector<int>(n, INT_MIN));\n int tmp;\n\n for (int i = 0; i < m; i++){\n for (int j = 0; j < n; j++){\n if (i == 0 && j == 0){\n if (grid[i][j] <= value) return false;\n temp[i][j] = grid[i][j];\n } else if (i == 0){\n if (temp[i][j - 1] != INT_MIN){\n tmp = temp[i][j - 1] + grid[i][j];\n if (tmp > value){\n temp[i][j] = tmp;\n }\n }\n } else if (j == 0){\n if (temp[i - 1][j] != INT_MIN){\n tmp = temp[i - 1][j] + grid[i][j];\n if (tmp > value){\n temp[i][j] = tmp;\n }\n }\n } else {\n tmp = max(temp[i - 1][j], temp[i][j - 1]);\n if (tmp != INT_MIN){\n tmp += grid[i][j];\n if (tmp > value){\n temp[i][j] = tmp;\n }\n } \n }\n }\n }\n\n return temp[m - 1][n - 1] > value;\n }\n \n int calculateMinimumHP(Grid& grid) {\n m = grid.size();\n n = grid.front().size();\n\n // return tryValue(grid, -100000);\n // return (-1)/2;\n\n int b = 1, e = 1000000, mid;\n \n while (b < e){\n mid = (b + e) / 2;\n if (tryValue(grid, -mid)){\n e = mid;\n } else {\n b = mid + 1;\n }\n }\n\n return b;\n }\n};",
"memory": "15200"
} |
174 | <p>The demons had captured the princess and imprisoned her in <strong>the bottom-right corner</strong> of a <code>dungeon</code>. The <code>dungeon</code> consists of <code>m x n</code> rooms laid out in a 2D grid. Our valiant knight was initially positioned in <strong>the top-left room</strong> and must fight his way through <code>dungeon</code> to rescue the princess.</p>
<p>The knight has an initial health point represented by a positive integer. If at any point his health point drops to <code>0</code> or below, he dies immediately.</p>
<p>Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).</p>
<p>To reach the princess as quickly as possible, the knight decides to move only <strong>rightward</strong> or <strong>downward</strong> in each step.</p>
<p>Return <em>the knight's minimum initial health so that he can rescue the princess</em>.</p>
<p><strong>Note</strong> that 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.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/13/dungeon-grid-1.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> dungeon = [[0]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == dungeon.length</code></li>
<li><code>n == dungeon[i].length</code></li>
<li><code>1 <= m, n <= 200</code></li>
<li><code>-1000 <= dungeon[i][j] <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool predicate(int mid,vector<vector<int>>& dungeon){\n int n=dungeon.size();\n int m=dungeon[0].size();\n vector<vector<int>>dp(n+1,vector<int>(m+1,0));\n dp[0][0]=mid;\n for(int i=0;i<n;++i){\n for(int j=0;j<m;++j){\n if(i>0 and j>0 and dp[i-1][j]>0 and dp[i][j-1]>0){\n dp[i][j]+=dungeon[i][j];\n dp[i][j]+=max(dp[i-1][j],dp[i][j-1]);\n }\n else if(i>0 and dp[i-1][j]>0){\n dp[i][j]+=dungeon[i][j];\n dp[i][j]+=dp[i-1][j];\n }\n else if(j>0 and dp[i][j-1]>0){\n dp[i][j]+=dungeon[i][j];\n dp[i][j]+=dp[i][j-1];\n }\n else if(i==0 and j==0){\n dp[i][j]+=dungeon[i][j];\n }\n }\n }\n // for(int i=0;i<n;++i){\n // for(int j=0;j<m;++j){\n // cout<<dp[i][j]<<\" \";\n // }\n // cout<<endl;\n // }\n return dp[n-1][m-1]>0;\n }\n int calculateMinimumHP(vector<vector<int>>& dungeon){\n int low=0;\n int high=1e6;\n while(high-low>1){\n int mid=(high+low)/2;\n if(predicate(mid,dungeon)){\n high=mid;\n }\n else{\n low=mid;\n }\n }\n return high;\n }\n};",
"memory": "15300"
} |
174 | <p>The demons had captured the princess and imprisoned her in <strong>the bottom-right corner</strong> of a <code>dungeon</code>. The <code>dungeon</code> consists of <code>m x n</code> rooms laid out in a 2D grid. Our valiant knight was initially positioned in <strong>the top-left room</strong> and must fight his way through <code>dungeon</code> to rescue the princess.</p>
<p>The knight has an initial health point represented by a positive integer. If at any point his health point drops to <code>0</code> or below, he dies immediately.</p>
<p>Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).</p>
<p>To reach the princess as quickly as possible, the knight decides to move only <strong>rightward</strong> or <strong>downward</strong> in each step.</p>
<p>Return <em>the knight's minimum initial health so that he can rescue the princess</em>.</p>
<p><strong>Note</strong> that 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.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/13/dungeon-grid-1.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> dungeon = [[0]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == dungeon.length</code></li>
<li><code>n == dungeon[i].length</code></li>
<li><code>1 <= m, n <= 200</code></li>
<li><code>-1000 <= dungeon[i][j] <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\n bool valid(vector<vector<int>> dp, int health){\n if (dp[1][1] + health < 0) return false;\n\n dp[1][1] += health;\n\n for (int i = 1;i <= dp.size()-1;i++){\n for (int j = 1;j <= dp[0].size()-1;j++){\n if (i != 1 || j != 1)\n dp[i][j] = max({dp[i][j] + dp[i-1][j], dp[i][j] + dp[i][j-1], (int)-1e8});\n\n if (dp[i][j] <= 0)\n dp[i][j] = -1e9;\n }\n }\n\n return dp[dp.size()-1][dp[0].size()-1] > 0;\n }\n\npublic:\n int calculateMinimumHP(vector<vector<int>>& dungeon) {\n int n = dungeon.size(), m = dungeon[0].size();\n vector<vector<int>> dp(n+1, vector<int>(m+1, -1e9));\n for (int i = 1;i <= n;i++){\n for (int j = 1;j <= m;j++){\n dp[i][j] = dungeon[i-1][j-1];\n }\n } \n\n long long l = 1, r = 1e6, health = 1e9;\n while (l <= r){\n long long mid = (l + r) / 2;\n\n if (valid(dp, mid)){\n r = mid - 1;\n health = min(health, mid);\n }else{\n l = mid + 1;\n }\n } \n\n return health;\n }\n};",
"memory": "15400"
} |
174 | <p>The demons had captured the princess and imprisoned her in <strong>the bottom-right corner</strong> of a <code>dungeon</code>. The <code>dungeon</code> consists of <code>m x n</code> rooms laid out in a 2D grid. Our valiant knight was initially positioned in <strong>the top-left room</strong> and must fight his way through <code>dungeon</code> to rescue the princess.</p>
<p>The knight has an initial health point represented by a positive integer. If at any point his health point drops to <code>0</code> or below, he dies immediately.</p>
<p>Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).</p>
<p>To reach the princess as quickly as possible, the knight decides to move only <strong>rightward</strong> or <strong>downward</strong> in each step.</p>
<p>Return <em>the knight's minimum initial health so that he can rescue the princess</em>.</p>
<p><strong>Note</strong> that 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.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/13/dungeon-grid-1.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> dungeon = [[0]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == dungeon.length</code></li>
<li><code>n == dungeon[i].length</code></li>
<li><code>1 <= m, n <= 200</code></li>
<li><code>-1000 <= dungeon[i][j] <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n int calculateMinimumHP(vector<vector<int>>& dungeon) {\n int m=dungeon.size();\n int inf=2000;\n int n=dungeon[0].size();\n int dp[1000][1000]={inf};\n\n \n for(int i=m-1;i>=0;i--)\n {\n for(int j=n-1;j>=0;j--)\n {\n if(i+1==m&&j+1==n)\n {\n dp[i][j]=0;\n }\n else if(i+1>=m)\n {\n dp[i][j]=dp[i][j+1];\n }\n else if(j+1>=n)\n {\n dp[i][j]=dp[i+1][j];\n }\n else{\n dp[i][j]=min(dp[i+1][j],dp[i][j+1]);\n }\n\n \n if(dungeon[i][j]<0)\n {\n dp[i][j]+=(-1*dungeon[i][j]);\n dp[i][j]=max(dp[i][j],-dungeon[i][j]+1);}\n else{\n dp[i][j]+=(-1*dungeon[i][j]);\n }\n // dp[i][j]++;\n // cout<<dp[i][j]<<\" \"<<i<<\" \"<<j<<\" \";\n }\n }\n\n return max(1,dp[0][0]);\n }\n};",
"memory": "15500"
} |
174 | <p>The demons had captured the princess and imprisoned her in <strong>the bottom-right corner</strong> of a <code>dungeon</code>. The <code>dungeon</code> consists of <code>m x n</code> rooms laid out in a 2D grid. Our valiant knight was initially positioned in <strong>the top-left room</strong> and must fight his way through <code>dungeon</code> to rescue the princess.</p>
<p>The knight has an initial health point represented by a positive integer. If at any point his health point drops to <code>0</code> or below, he dies immediately.</p>
<p>Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).</p>
<p>To reach the princess as quickly as possible, the knight decides to move only <strong>rightward</strong> or <strong>downward</strong> in each step.</p>
<p>Return <em>the knight's minimum initial health so that he can rescue the princess</em>.</p>
<p><strong>Note</strong> that 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.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/13/dungeon-grid-1.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> dungeon = [[0]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == dungeon.length</code></li>
<li><code>n == dungeon[i].length</code></li>
<li><code>1 <= m, n <= 200</code></li>
<li><code>-1000 <= dungeon[i][j] <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>>v1;\n int n,m;\n int calculateMinimumHP(vector<vector<int>>& dun) {\n v1=dun;\n n=v1.size(),m=v1[0].size();\n int lo=1,hi=2e6,mid,ans=hi;\n while(lo<=hi)\n {\n mid=lo+(hi-lo)/2;\n vector<vector<int>>dp(n+1,vector<int>(m+1));\n bool flag=true;\n dp[1][1]=v1[0][0]+mid;\n if(dp[1][1]<=0)\n {\n dp[1][1]=-1e7;\n }\n for(int i=2;i<=m;i++)\n {\n dp[1][i]=dp[1][i-1]+v1[0][i-1];\n if(dp[1][i]<=0)\n {\n dp[1][i]=-1e7;\n }\n }\n for(int i=2;i<=n;i++)\n {\n dp[i][1]=dp[i-1][1]+v1[i-1][0];\n if(dp[i][1]<=0)\n {\n dp[i][1]=-1e7;\n }\n }\n for(int i=2;i<=n;i++)\n {\n for(int j=2;j<=m;j++)\n {\n dp[i][j]=v1[i-1][j-1]+max(dp[i-1][j],dp[i][j-1]);\n if(dp[i][j]<=0)\n {\n dp[i][j]=-1e7;\n }\n }\n }\n if(dp[n][m]>0)\n {\n ans=mid;\n hi=mid-1;\n }\n else\n {\n lo=mid+1;\n }\n }\n return ans;\n }\n};",
"memory": "15600"
} |
174 | <p>The demons had captured the princess and imprisoned her in <strong>the bottom-right corner</strong> of a <code>dungeon</code>. The <code>dungeon</code> consists of <code>m x n</code> rooms laid out in a 2D grid. Our valiant knight was initially positioned in <strong>the top-left room</strong> and must fight his way through <code>dungeon</code> to rescue the princess.</p>
<p>The knight has an initial health point represented by a positive integer. If at any point his health point drops to <code>0</code> or below, he dies immediately.</p>
<p>Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).</p>
<p>To reach the princess as quickly as possible, the knight decides to move only <strong>rightward</strong> or <strong>downward</strong> in each step.</p>
<p>Return <em>the knight's minimum initial health so that he can rescue the princess</em>.</p>
<p><strong>Note</strong> that 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.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/13/dungeon-grid-1.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> dungeon = [[0]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == dungeon.length</code></li>
<li><code>n == dungeon[i].length</code></li>
<li><code>1 <= m, n <= 200</code></li>
<li><code>-1000 <= dungeon[i][j] <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int m, n;\n void print(vector<vector<int>>& dp) {\n for(int i = 0; i < m; i++) {\n for(int j = 0; j < n; j++) {\n cout << dp[i][j] << \" \";\n }\n cout << endl;\n }\n }\n bool isPossible(int ans, vector<vector<int>>& dungeon) {\n vector<vector<int>> dp(m, vector<int>(n, 0));\n dp[0][0] = ans + dungeon[0][0];\n for(int i = 1; i < m; i++) {\n if(dp[i-1][0] > 0)\n dp[i][0] = dungeon[i][0] + dp[i-1][0];\n }\n for(int i = 1; i < n; i++) {\n if(dp[0][i-1] > 0)\n dp[0][i] = dungeon[0][i] + dp[0][i-1];\n }\n for(int i = 1; i < m; i++) {\n for(int j = 1; j < n; j++) {\n if(dp[i-1][j] > 0 && dp[i][j-1] > 0)\n dp[i][j] = max(dp[i-1][j], dp[i][j-1]) + dungeon[i][j];\n else if(dp[i-1][j] > 0) \n dp[i][j] = dp[i-1][j] + dungeon[i][j];\n else if(dp[i][j-1] > 0)\n dp[i][j] = dp[i][j-1] + dungeon[i][j];\n }\n }\n return dp[m-1][n-1] > 0;\n }\n int calculateMinimumHP(vector<vector<int>>& dungeon) {\n m = dungeon.size(), n = dungeon[0].size();\n int l = 1, h = m * n * 1000, ans = -1;\n while(l <= h) {\n int mid = (l + h) / 2;\n if(isPossible(mid, dungeon)) {\n ans = mid;\n h = mid - 1;\n } else {\n l = mid + 1;\n }\n }\n return ans;\n }\n};",
"memory": "15700"
} |
174 | <p>The demons had captured the princess and imprisoned her in <strong>the bottom-right corner</strong> of a <code>dungeon</code>. The <code>dungeon</code> consists of <code>m x n</code> rooms laid out in a 2D grid. Our valiant knight was initially positioned in <strong>the top-left room</strong> and must fight his way through <code>dungeon</code> to rescue the princess.</p>
<p>The knight has an initial health point represented by a positive integer. If at any point his health point drops to <code>0</code> or below, he dies immediately.</p>
<p>Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).</p>
<p>To reach the princess as quickly as possible, the knight decides to move only <strong>rightward</strong> or <strong>downward</strong> in each step.</p>
<p>Return <em>the knight's minimum initial health so that he can rescue the princess</em>.</p>
<p><strong>Note</strong> that 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.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/13/dungeon-grid-1.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> dungeon = [[0]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == dungeon.length</code></li>
<li><code>n == dungeon[i].length</code></li>
<li><code>1 <= m, n <= 200</code></li>
<li><code>-1000 <= dungeon[i][j] <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\n int rows , cols;\n int dp[1001][1001];\n vector<vector<int>> graph;\npublic:\n int calculateMinimumHP(vector<vector<int>>& dungeon) {\n rows = dungeon.size() , cols = dungeon[0].size();\n graph = dungeon;\n memset(dp , -1 , sizeof(dp));\n\n return min_value(0 , 0);\n }\n\nprivate:\n\n\n int min_value(int r , int c){\n if(c < 0 || r < 0 || r >= rows || c >= cols)\n return INT_MAX;\n\n if(c == cols - 1 && r == rows - 1){\n if(graph[r][c] <= 0)\n return 1 - graph[r][c];\n\n return 1;\n }\n\n auto &ret = dp[r][c];\n\n if(ret != -1)\n return ret;\n\n int right = min_value(r , c + 1);\n int down = min_value(r + 1, c);\n\n int mn_value = min(right , down);\n\n if(graph[r][c] >= mn_value)\n return 1;\n\n\n return ret = -1 * graph[r][c] + mn_value;\n\n }\n};\n",
"memory": "15800"
} |
174 | <p>The demons had captured the princess and imprisoned her in <strong>the bottom-right corner</strong> of a <code>dungeon</code>. The <code>dungeon</code> consists of <code>m x n</code> rooms laid out in a 2D grid. Our valiant knight was initially positioned in <strong>the top-left room</strong> and must fight his way through <code>dungeon</code> to rescue the princess.</p>
<p>The knight has an initial health point represented by a positive integer. If at any point his health point drops to <code>0</code> or below, he dies immediately.</p>
<p>Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).</p>
<p>To reach the princess as quickly as possible, the knight decides to move only <strong>rightward</strong> or <strong>downward</strong> in each step.</p>
<p>Return <em>the knight's minimum initial health so that he can rescue the princess</em>.</p>
<p><strong>Note</strong> that 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.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/13/dungeon-grid-1.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> dungeon = [[0]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == dungeon.length</code></li>
<li><code>n == dungeon[i].length</code></li>
<li><code>1 <= m, n <= 200</code></li>
<li><code>-1000 <= dungeon[i][j] <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int calculateMinimumHP(vector<vector<int>>& dungeon) {\n int n = dungeon.size();\n int m = dungeon[0].size();\n int f = 1;\n int l = 40000000;\n int ans = 0;\n while(f<=l){\n vector<vector<int>>dp(n, vector<int>(m, 0));\n int mid = (f+l)/2;\n dp[0][0] = max(dp[0][0], mid + dungeon[0][0]);\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n if(i==0 && j==0){\n continue;\n }\n if(i-1>=0 && j-1>=0){\n dp[i][j] = max(dp[i-1][j], dp[i][j-1]);\n if(dp[i][j]>0){\n dp[i][j]=max(0, dp[i][j] + dungeon[i][j]);\n }\n } else if(i-1>=0){\n dp[i][j] = dp[i-1][j];\n if(dp[i][j]>0){\n dp[i][j]=max(0, dp[i][j] + dungeon[i][j]);\n }\n } else if(j-1>=0){\n dp[i][j] = dp[i][j-1];\n if(dp[i][j]>0){\n dp[i][j]=max(0, dp[i][j] + dungeon[i][j]);\n }\n }\n }\n }\n // cout<<mid<<endl;\n // for(int i=0;i<n;i++){\n // for(int j=0;j<m;j++){\n // cout<<dp[i][j]<<\" \";\n // }\n // cout<<endl;\n // }\n if(dp[n-1][m-1]){\n ans = mid;\n l=mid-1;\n } else{\n f=mid+1;\n }\n }\n return ans;\n }\n};",
"memory": "15900"
} |
174 | <p>The demons had captured the princess and imprisoned her in <strong>the bottom-right corner</strong> of a <code>dungeon</code>. The <code>dungeon</code> consists of <code>m x n</code> rooms laid out in a 2D grid. Our valiant knight was initially positioned in <strong>the top-left room</strong> and must fight his way through <code>dungeon</code> to rescue the princess.</p>
<p>The knight has an initial health point represented by a positive integer. If at any point his health point drops to <code>0</code> or below, he dies immediately.</p>
<p>Some of the rooms are guarded by demons (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).</p>
<p>To reach the princess as quickly as possible, the knight decides to move only <strong>rightward</strong> or <strong>downward</strong> in each step.</p>
<p>Return <em>the knight's minimum initial health so that he can rescue the princess</em>.</p>
<p><strong>Note</strong> that 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.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/13/dungeon-grid-1.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> dungeon = [[-2,-3,3],[-5,-10,1],[10,30,-5]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> dungeon = [[0]]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == dungeon.length</code></li>
<li><code>n == dungeon[i].length</code></li>
<li><code>1 <= m, n <= 200</code></li>
<li><code>-1000 <= dungeon[i][j] <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\n int rows , cols;\n int dp[1001][1001];\n vector<vector<int>> graph;\npublic:\n int calculateMinimumHP(vector<vector<int>>& dungeon) {\n rows = dungeon.size() , cols = dungeon[0].size();\n graph = dungeon;\n memset(dp , -1 , sizeof(dp));\n\n return min_value(0 , 0);\n }\n\nprivate:\n\n\n int min_value(int r , int c){\n if(c < 0 || r < 0 || r >= rows || c >= cols)\n return INT_MAX;\n\n if(c == cols - 1 && r == rows - 1){\n if(graph[r][c] <= 0)\n return 1 - graph[r][c];\n\n return 1;\n }\n\n auto &ret = dp[r][c];\n\n if(ret != -1)\n return ret;\n\n int right = min_value(r , c + 1);\n int down = min_value(r + 1, c);\n\n int mn_value = min(right , down);\n\n if(graph[r][c] >= mn_value)\n return 1;\n\n\n return ret = -1 * graph[r][c] + mn_value;\n\n }\n};\n",
"memory": "16000"
} |
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\nprivate:\n struct digits_cmp {\n bool operator()(int e1, int e2) const {\n unsigned long long pow;\n unsigned long long val1, val2;\n\n for (pow = 10; e2 / pow > 0; pow *= 10)\n ;\n val1 = e1 * pow + e2;\n\n for (pow = 10; e1 / pow > 0; pow *= 10)\n ;\n val2 = e2 * pow + e1;\n\n return val1 > val2;\n }\n };\n\npublic:\n string largestNumber(vector<int>& nums) {\n int i, n;\n string res;\n struct digits_cmp cmp;\n\n sort(begin(nums), end(nums), cmp);\n\n n = nums.size();\n\n for (i = 0; i < n; i++) {\n int val = nums[i];\n unsigned long long pow;\n\n for (pow = 10; val / pow > 0; pow *= 10)\n ;\n pow /= 10;\n\n for (;;) {\n res += '0' + val / pow % 10;\n if (pow == 1)\n break;\n pow /= 10;\n }\n }\n\n if (res[0] == '0')\n res.resize(1);\n\n return res;\n }\n};",
"memory": "13200"
} |
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n static bool customSort(const int &a, const int &b) {\n if (a==0) return false;\n if (b==0) return true;\n int x = log10(a);\n int y = log10(b);\n unsigned long long p=a, q=b;\n p = p * (unsigned long long)pow(10, y+1) + b;\n q = q * (unsigned long long)pow(10, x+1) + a;\n // cout << \"input = \" << a << \" \" << b << endl;\n // cout << \"comp = \" << p << \" \" << q << endl;\n return p > q;\n }\n string largestNumber(vector<int>& nums) {\n sort(nums.begin(), nums.end(), customSort);\n string ans = \"\";\n int x, p;\n for (int n:nums) {\n if (n==0) {\n if (ans.size()) ans += '0';\n continue;\n }\n x = log10(n)+1;\n while(x--) {\n p = (int)pow(10, x);\n ans += (n/p) + '0';\n n = n % p;\n }\n }\n return ans.size() ? ans:\"0\";\n }\n};",
"memory": "13800"
} |
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n static bool cmp(int a, int b)\n {\n char x[64], y[64];\n snprintf(x, sizeof(x), \"%d%d\", a, b);\n snprintf(y, sizeof(y), \"%d%d\", b, a);\n return std::string_view(x) < std::string_view(y);\n }\n\n string largestNumber(vector<int>& nums) {\n std::sort(nums.begin(), nums.end(), cmp);\n std::string r;\n int s = 0;\n for (auto it = nums.rbegin(), end = nums.rend(); it != end; ++it) {\n char buf[25];\n snprintf(buf, sizeof(buf), \"%d\", *it);\n r += buf;\n s += !!*it;\n }\n if (s == 0)\n r = \"0\";\n return r;\n }\n};",
"memory": "14200"
} |
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\n int cmp(int m, int n) {\n char c1[32];\n char c2[32];\n sprintf(c1,\"%d%d\", m, n);\n sprintf(c2,\"%d%d\", n, m);\n return strcmp(c1, c2);\n }\n void qs(vector<int>& nums, int s, int e) {\n if (s >= e)\n return;\n int l = s;\n int r = e;\n int m = nums[(l + r) / 2];\n while (l <= r) {\n while (cmp(nums[l], m) > 0)\n l++;\n while (cmp(nums[r], m) < 0)\n r--;\n if (l <= r) {\n swap(nums[l], nums[r]);\n l++;\n r--;\n }\n }\n qs(nums, s, r);\n qs(nums, l, e);\n }\npublic:\n string largestNumber(vector<int>& nums) {\n qs(nums, 0, nums.size() - 1);\n if (nums[0] == 0)\n return \"0\";\n string res;\n for (auto i:nums) {\n char c[32];\n sprintf(c, \"%d\", i);\n res += c;\n }\n return res;\n }\n};",
"memory": "14400"
} |
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\n int cmp(int m, int n) {\n char s1[32];\n char s2[32];\n sprintf(s1, \"%d%d\", m, n);\n sprintf(s2, \"%d%d\", n, m);\n return strcmp(s1, s2);\n }\n void qs(vector<int> &nums, int l, int r) {\n if (l >= r)\n return;\n int m = l;\n int n = r;\n int med = nums[(l + r)/2];\n while (m <= n) {\n while (cmp(nums[m], med) > 0) m++;\n while (cmp(nums[n], med) < 0) n--;\n if (m <= n) {\n swap(nums[m], nums[n]);\n m++;\n n--;\n }\n }\n qs(nums, l, n);\n qs(nums, m, r);\n }\npublic:\n string largestNumber(vector<int>& nums) {\n qs(nums, 0, nums.size() - 1);\n if (nums[0] == 0)\n return \"0\";\n string res;\n for (auto i : nums) {\n char s[32];\n sprintf(s, \"%d\", i);\n res += s;\n }\n return res;\n }\n};",
"memory": "14500"
} |
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\nprivate:\n static long long int compare( int a, int b){\n long long int n = a;\n if(a == b){\n n = a + b;\n }\n else{\n int c = b;\n int k = b == 0;\n while (c){\n c /= 10;\n k++;\n }\n for (int i = 0; i < k; i++){\n n *= 10;\n }\n n=n+b;\n }\n return n;\n }\n static bool cmp(int f, int s){\n return compare(f, s) > compare(s, f);\n }\npublic:\n string largestNumber(vector<int>& nums) {\n sort(nums.begin(), nums.end(), cmp);\n if (nums[0] == 0) return \"0\";\n int n = nums.size();\n string ans;\n for (const auto& t : nums){\n ans.append(to_string(t));\n }\n return ans;\n }\n};",
"memory": "14600"
} |
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n string numToString(int n){\n string s=\"\";\n if(n==0){\n s+='0';\n }\n while(n){\n s+=char(n%10+'0');\n n/=10;\n }\n reverse(s.begin(),s.end());\n return s;\n }\n \n string largestNumber(vector<int>& nums) {\n int n=nums.size(),i,j,k;\n vector<string>a;\n for(i=0;i<n;i++){\n a.push_back(numToString(nums[i]));\n }\n for(i=0;i<n;i++){\n for(j=1;j<n-i;j++){\n if(a[j-1]+a[j]<a[j]+a[j-1]){\n swap(a[j-1],a[j]);\n }\n }\n }\n string ans=\"\";\n if(a[0]==\"0\"){\n ans+='0';\n return ans;\n }\n for(i=0;i<n;i++){\n ans+=a[i];\n }\n return ans;\n }\n};",
"memory": "14700"
} |
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n\n struct\n {\n bool operator()(int a, int b) const { \n return !is_a_smaller( a, b); \n }\n }\n customLess;\n\n static int get_length(int num) \n {\n if (num == 0) return 1;\n return (int) ceil(log10(num + 1));\n }\n\n static int get_digit(int num, int digit) \n {\n int helper = num / pow(10,get_length(num)-digit);\n return helper % 10;\n }\n\n static bool is_a_smaller (int a, int b) \n {\n int a_length = get_length(a);\n int b_length = get_length(b);\n int ac = 1, bc = 1;\n for (int i = 1; i <= a_length + b_length; i++)\n {\n int this_digit_afirst, this_digit_bfirst = 0;\n if (i <= a_length)\n this_digit_afirst = get_digit(a, i);\n else\n this_digit_afirst = get_digit(b, i - a_length);\n\n if (i <= b_length)\n this_digit_bfirst = get_digit(b, i);\n else\n this_digit_bfirst = get_digit(a, i - b_length);\n\n if (this_digit_afirst != this_digit_bfirst) return (this_digit_afirst < this_digit_bfirst);\n }\n\n return true;\n }\n\n string largestNumber(vector<int>& nums) {\n if (all_of(nums.begin(), nums.end(), [&](int i) { return i == 0; }))\n return \"0\";\n string res = \"\";\n sort(nums.begin(), nums.end(), customLess);\n for (int i : nums)\n res += to_string(i);\n return res;\n }\n};",
"memory": "14800"
} |
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n unsigned long long getPowerOf10(unsigned long long x) {\n unsigned long long ans = 1;\n while (x--)\n ans *= 10;\n return ans;\n }\n string largestNumber(vector<int>& nums) {\n int n = nums.size();\n vector<pair<unsigned long long, unsigned long long>> vp;\n unsigned long long temp, exp;\n for (long long num : nums) {\n temp = num;\n exp = 0;\n do {\n exp++;\n temp /= 10;\n } while (temp);\n vp.push_back({num, exp});\n }\n for (int i = 0; i < n - 1; i++) {\n for (int j = i + 1; j < n; j++) {\n if (vp[i].first * getPowerOf10(vp[j].second) + vp[j].first <\n vp[j].first * getPowerOf10(vp[i].second) + vp[i].first) {\n swap(vp[i], vp[j]);\n }\n }\n }\n string ans = \"\";\n for (auto it : vp) {\n ans += to_string(it.first);\n }\n bool allZeroes = true;\n for(char ch:ans){\n if(ch!='0'){\n allZeroes = false;\n break;\n } \n }\n\n if(allZeroes) return \"0\";\n return ans;\n }\n};",
"memory": "14900"
} |
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n unsigned long long getPowerOf10(unsigned long long x) {\n unsigned long long ans = 1;\n while (x--)\n ans *= 10;\n return ans;\n }\n string largestNumber(vector<int>& nums) {\n int n = nums.size();\n vector<pair<unsigned long long, unsigned long long>> vp;\n unsigned long long temp, exp;\n for (long long num : nums) {\n temp = num;\n exp = 0;\n do {\n exp++;\n temp /= 10;\n } while (temp);\n vp.push_back({num, exp});\n }\n for (int i = 0; i < n - 1; i++) {\n for (int j = i + 1; j < n; j++) {\n if (vp[i].first * getPowerOf10(vp[j].second) + vp[j].first <\n vp[j].first * getPowerOf10(vp[i].second) + vp[i].first) {\n swap(vp[i], vp[j]);\n }\n }\n }\n string ans = \"\";\n for (auto it : vp) {\n ans += to_string(it.first);\n }\n if(ans[0]=='0') return \"0\";\n return ans;\n }\n};",
"memory": "14900"
} |
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n string largestNumber(vector<int>& nums) {\n \n auto cmp = [](const int &a, const int &b) {\n \n stringstream sa ;\n sa << a << b;\n stringstream sb;\n sb << b << a; \n\n return (sa.str() > sb.str()); \n \n };\n \n sort(nums.begin(), nums.end(), cmp); \n \n stringstream ras ;\n \n if(nums[0] == 0 ) {\n return \"0\";\n }\n \n for(auto num : nums) {\n \n ras<< num;\n }\n \n \n return ras.str(); \n \n }\n};",
"memory": "15000"
} |
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 0 | {
"code": "vector<int> digits(int a){\n vector<int> ans;\n while(a > 0){\n ans.push_back(a % 10);\n a /= 10;\n }\n reverse(ans.begin(), ans.end());\n return ans;\n }\n \n\nclass Solution {\npublic:\n string postprocess(string s){\n if(s[0] == '0'){\n return \"0\";\n }\n return s;\n }\n string largestNumber(vector<int>& nums) {\n sort(nums.begin(), nums.end(), [](int a, int b){\n stringstream ss1, ss2;\n ss1 << a << b;\n ss2 << b << a;\n for(int i=0;i<ss1.str().size();++i){\n if(ss1.str()[i] == ss2.str()[i]){continue;}\n return ss1.str()[i] > ss2.str()[i];\n }\n return a > b;\n });\n\n stringstream ss;\n for(int num : nums){\n ss << num;\n }\n \n return postprocess(ss.str());\n }\n};",
"memory": "15100"
} |
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n string largestNumber(vector<int>& nums) {\n auto myComparator=[](int&a,int&b){\n string s1=to_string(a);\n string s2=to_string(b);\n return s1+s2>s2+s1;\n \n \n\n };\n sort(begin(nums),end(nums),myComparator);\n if(nums[0]==0)return \"0\";\n string result=\"\";\n for(int itr:nums){\n result+=to_string(itr);\n }\n return result;\n\n }\n};",
"memory": "15200"
} |
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 0 | {
"code": "bool compare(int a,int b)\n{\n return to_string(a)+to_string(b)>to_string(b)+to_string(a);\n }\n class Solution {\n public:\n string largestNumber(vector<int>& arr) {\n sort(arr.begin(),arr.end(),compare);\n string ans = \"\";\n for(int i = 0;i<arr.size();i++)\n ans+=to_string(arr[i]);\n if(ans[0]=='0') return \"0\";\n return ans; \n }\n };",
"memory": "15300"
} |
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n string largestNumber(vector<int>& nums) {\n int n=nums.size();\n\n sort(nums.begin(),nums.end(),[&](int a,int b){\n return to_string(a)+to_string(b)>to_string(b)+to_string(a);\n });\n\n int i=0;\n\n if(nums[i]==0) return \"0\";\n\n string ans;\n\n for(i=0;i<n;i++){\n ans+=to_string(nums[i]);\n }\n\n return ans;\n }\n};",
"memory": "15300"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.