id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
210 | <p>There are a total of <code>numCourses</code> courses you have to take, labeled from <code>0</code> to <code>numCourses - 1</code>. You are given an array <code>prerequisites</code> where <code>prerequisites[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that you <strong>must</strong> take course <code>b<sub>i<... | 3 | {
"code": "class Solution {\npublic:\n bool dfs(vector<vector<int>> &graph, vector<bool> &vis, unordered_set<int> &out, int node, vector<int> &ans)\n {\n //if visited? cycle detected. return false\n if(vis[node])\n return false;\n if(out.find(node) != out.end()) //already process... |
210 | <p>There are a total of <code>numCourses</code> courses you have to take, labeled from <code>0</code> to <code>numCourses - 1</code>. You are given an array <code>prerequisites</code> where <code>prerequisites[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that you <strong>must</strong> take course <code>b<sub>i<... | 3 | {
"code": "class Solution {\npublic:\n vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {\n vector<unordered_set<int>> g(numCourses);\n vector<int> degree(numCourses);\n for(auto pre: prerequisites) {\n g[pre[1]].insert(pre[0]);\n degree[pre[0]] +... |
210 | <p>There are a total of <code>numCourses</code> courses you have to take, labeled from <code>0</code> to <code>numCourses - 1</code>. You are given an array <code>prerequisites</code> where <code>prerequisites[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that you <strong>must</strong> take course <code>b<sub>i<... | 3 | {
"code": "class Solution {\npublic:\n vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {\n map<int,list<int>> p_cs;\n map<int,int> c_np;\n list<int> fc;\n int nfc = 0;\n vector<int> order;\n\n for(auto cp : prerequisites){\n int c = cp[... |
210 | <p>There are a total of <code>numCourses</code> courses you have to take, labeled from <code>0</code> to <code>numCourses - 1</code>. You are given an array <code>prerequisites</code> where <code>prerequisites[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that you <strong>must</strong> take course <code>b<sub>i<... | 3 | {
"code": "class Solution {\npublic:\n\n int dfs(map<int, vector<int>> &graph, int curr, vector<int> &topo, vector<int> &vis){\n if(vis[curr]==1)\n {\n return true;\n }\n if(vis[curr]==2)\n {\n return false;\n }\n vis[curr] = 1;\n int an... |
210 | <p>There are a total of <code>numCourses</code> courses you have to take, labeled from <code>0</code> to <code>numCourses - 1</code>. You are given an array <code>prerequisites</code> where <code>prerequisites[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that you <strong>must</strong> take course <code>b<sub>i<... | 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> adj = vector<vector<int>> (2000);\n\n bool checkCycle(int i, vector<int> &vis, vector<int> &cycle){\n //if(vis[i] == 1)return false;\n cycle[i] = 1;\n for(int j : adj[i]){\n if(cycle[j] == 1)return true;\n if(c... |
210 | <p>There are a total of <code>numCourses</code> courses you have to take, labeled from <code>0</code> to <code>numCourses - 1</code>. You are given an array <code>prerequisites</code> where <code>prerequisites[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that you <strong>must</strong> take course <code>b<sub>i<... | 3 | {
"code": "class Solution {\npublic:\n vector<int> findOrder(int numCourses, vector<vector<int>> prerequisites) {\n unordered_map<int, vector<int>> graph;\n for(auto x: prerequisites) {\n int u = x[0];\n int v = x[1];\n graph[v].push_back(u);\n }\n \n ... |
210 | <p>There are a total of <code>numCourses</code> courses you have to take, labeled from <code>0</code> to <code>numCourses - 1</code>. You are given an array <code>prerequisites</code> where <code>prerequisites[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that you <strong>must</strong> take course <code>b<sub>i<... | 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> adj = vector<vector<int>> (2000);\n\n bool checkCycle(int i, vector<int> &vis, vector<int> &cycle){\n if(vis[i] == 1)return false;\n cycle[i] = 1;\n for(int j : adj[i]){\n if(cycle[j] == 1)return true;\n if(che... |
210 | <p>There are a total of <code>numCourses</code> courses you have to take, labeled from <code>0</code> to <code>numCourses - 1</code>. You are given an array <code>prerequisites</code> where <code>prerequisites[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that you <strong>must</strong> take course <code>b<sub>i<... | 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> adj = vector<vector<int>> (2000);\n\n bool checkCycle(int i, vector<int> &vis, vector<int> &cycle){\n if(vis[i] == 1)return false;\n cycle[i] = 1;\n for(int j : adj[i]){\n if(cycle[j] == 1)return true;\n if(che... |
210 | <p>There are a total of <code>numCourses</code> courses you have to take, labeled from <code>0</code> to <code>numCourses - 1</code>. You are given an array <code>prerequisites</code> where <code>prerequisites[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that you <strong>must</strong> take course <code>b<sub>i<... | 3 | {
"code": "class Solution {\npublic:\n void toposort(int node,unordered_map<int,bool> &visited,stack<int> &s,unordered_map<int,list<int>> &adj,unordered_map<int, bool> &onStack,bool &cycle){\n visited[node]=true;\n onStack[node]=true;\n for(auto i:adj[node]){\n if(!visited[i]){\n toposo... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 0 | {
"code": "#pragma GCC optimize(\"Ofast,inline,unroll-loops,fast-math\")\n#pragma GCC target(\"avx,avx2,fma\")\n\nconst long MOD = 1e9 + 7;\nconst long X = 31;\n\nclass Solution {\npublic:\n string shortestPalindrome(const string &s) {\n const int N = s.length();\n if (N < 2) return s;\n long ... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 0 | {
"code": "#pragma GCC optimize(\"Ofast,inline,unroll-loops,fast-math\")\n#pragma GCC target(\"avx,avx2,fma\")\n\nconst long MOD = 1e9 + 7;\nconst long X = 31;\n\nclass Solution {\npublic:\n string shortestPalindrome(const string &s) {\n const int N = s.length();\n if (N < 2) return s;\n long ... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 0 | {
"code": "#pragma GCC optimize(\"Ofast,inline,unroll-loops,fast-math\")\n#pragma GCC target(\"avx,avx2,fma\")\n\nconst long MOD = 1e9 + 7;\nconst long X = 31;\n\nclass Solution {\npublic:\n string shortestPalindrome(const string &s) {\n const int N = s.length();\n if (N < 2) return s;\n long ... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 0 | {
"code": "#pragma GCC optimize(\"Ofast,inline,unroll-loops,fast-math\")\n#pragma GCC target(\"avx,avx2,fma\")\n\nconst long MOD = 1e9 + 7;\nconst long X = 31;\n\nclass Solution {\npublic:\n string shortestPalindrome(const string &s) {\n const int N = s.length();\n if (N < 2) return s;\n long ... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 0 | {
"code": "class Solution {\npublic:\n string shortestPalindrome(string s) {\n\n if (s.size() <= 1)\n return s;\n if (s.size() == 2) {\n if (s.front() == s.back())\n return s;\n s.insert(s.begin(), s.back());\n return s;\n }\n\n ... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 0 | {
"code": "#include <string>\n#include <algorithm>\n\nclass Solution {\npublic:\n std::string shortestPalindrome(const std::string& s) {\n unsigned d = 16777619; // Hash base\n unsigned h = 0; // Hash for the original string\n unsigned rh = 0; // Hash for the reversed string\n unsigned ... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 0 | {
"code": "class Solution {\npublic:\n string shortestPalindrome(string s) {\n long long hashBase = 29;\n long long modValue = 1e9 + 7;\n long long forwardHash = 0, reverseHash = 0, powerValue = 1;\n int palindromeEndIndex = -1;\n\n // Calculate rolling hashes and find the longes... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 0 | {
"code": "#include <string>\n#include <algorithm>\n\nclass Solution {\npublic:\n std::string shortestPalindrome(const std::string& s) {\n unsigned d = 16777619; // Hash base\n unsigned h = 0; // Hash for the original string\n unsigned rh = 0; // Hash for the reversed string\n unsigned ... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 0 | {
"code": "class Solution {\npublic:\n string shortestPalindrome(string s) {\n string reversedString = s;\n // Reverse the original string\n reverse(reversedString.begin(), reversedString.end());\n\n // Iterate through the string to find the longest palindromic prefix\n for (int ... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 0 | {
"code": "class Solution {\npublic:\n string shortestPalindrome(string s) {\n long long hashBase = 29;\n long long modValue = 1e9 + 7;\n long long forwardHash = 0, reverseHash = 0, powerValue = 1;\n int palindromeEndIndex = -1;\n\n // Calculate rolling hashes and find the longes... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 0 | {
"code": "class Solution {\npublic:\n string shortestPalindrome(string s) {\n string reversedString = s;\n // Reverse the original string\n reverse(reversedString.begin(), reversedString.end());\n\n // Iterate through the string to find the longest palindromic prefix\n for (int ... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 0 | {
"code": "class Solution {\npublic:\n string shortestPalindrome(string s) {\n int length = s.length();\n if (length == 0) {\n return s;\n }\n\n // Find the longest palindromic prefix\n int left = 0;\n for (int right = length - 1; right >= 0; right--) {\n ... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 0 | {
"code": "class Solution {\npublic:\n string shortestPalindrome(string s) {\n int n = s.length();\n if (n == 0 || n == 1) {\n return s;\n }\n \n int start = 0;\n for (int end = n - 1; end >= 0; --end) {\n if (s[end] == s[start]) {\n st... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 0 | {
"code": "class Solution {\npublic:\n string shortestPalindrome(string s) {\n int length = s.length();\n if (length == 0) {\n return s;\n }\n\n // Find the longest palindromic prefix\n int left = 0;\n for (int right = length - 1; right >= 0; right--) {\n ... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 0 | {
"code": "class Solution {\npublic:\n string shortestPalindrome(string s) {\n int length = s.length();\n if (length == 0) {\n return s;\n }\n\n // Find the longest palindromic prefix\n int left = 0;\n for (int right = length - 1; right >= 0; right--) {\n ... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 0 | {
"code": "class Solution {\nprivate:\nstring reverse(string s){\n int n=s.length();\n int i=n-1;\n string ans=s;\n while(i>=0){\n ans[n-1-i]=s[i];\n i--;\n }\n return ans;\n}\nbool ispalindrome(string s, int left, int right){\n\n int mid=left+(right-left)/2;\n int i=left;\n w... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 0 | {
"code": "\n\nclass Solution {\npublic:\n string shortestPalindrome(string s) {\n int n = s.size();\n vector<int> fail(n, -1);\n for (int i = 1; i < n; ++i) {\n int j = fail[i - 1];\n while (j != -1 && s[j + 1] != s[i]) {\n j = fail[j];\n }\n ... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 0 | {
"code": "class Solution {\npublic:\n string shortestPalindrome(string s) {\n if(s.size() <= 1) return s;\n int i = 1, prevlps = 0;\n vector<int> lps(s.size() , 0);\n while(i<s.size()){\n if(s[i] == s[prevlps]){\n lps[i] = prevlps+1;\n i++;prevl... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 0 | {
"code": "class Solution {\npublic:\n string shortestPalindrome(string s) {\n string rev = s;\n reverse(rev.begin(), rev.end());\n auto lps = getNext(s);\n int j = 0;\n for(int i = 0; i < rev.size(); ++i){\n while(j > 0 && rev[i] != s[j])\n j = lps[j - ... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 0 | {
"code": "class Solution final {\nprivate:\n template<class Iter, class Func>\n static constexpr void findPalindromes(\n const Iter first,\n const Iter last,\n Func func\n ) noexcept {\n using Difference = iterator_traits<Iter>::difference_type;\n\n if (first == last)\n ... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 0 | {
"code": "class Solution final {\nprivate:\n template<class Iter, class Func>\n static constexpr void findPalindromes(\n const Iter first,\n const Iter last,\n Func func\n ) noexcept {\n using Difference = iterator_traits<Iter>::difference_type;\n\n if (first == last)\n ... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 0 | {
"code": "class Solution {\n vector<int> lpsTable(string p) {\n int n = p.size();\n vector<int> lps(n);\n int i = 1, idx = 0;\n while(i<n) {\n if(p[i] == p[idx]) {\n lps[i] = idx+1;\n i++, idx++;\n } else if(idx>0) {\n ... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 0 | {
"code": "class Solution final {\nprivate:\n template<class Iter, class Func>\n static constexpr void findPalindromes(\n const Iter first,\n const Iter last,\n Func func\n ) noexcept {\n using Difference = iterator_traits<Iter>::difference_type;\n\n if (first == last)\n ... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 0 | {
"code": "class Solution {\npublic:\nint LongestPalindromicPrefix(string str)\n{\n \n // Create temporary string\n string temp = str + '?';\n \n // Reverse the string str\n reverse(str.begin(), str.end());\n \n // Append string str to temp\n temp += str;\n \n // Find the length of string temp\n ... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 0 | {
"code": "// KMP\nclass Solution {\npublic:\n string shortestPalindrome(string p) {\n string newString = p;\n string rs = p;\n reverse(rs.begin(), rs.end());\n newString += '#';\n newString += rs;\n\n int sz = newString.size();\n int pi[sz];\n pi[0] = 0;\n ... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 0 | {
"code": "// KMP\nclass Solution {\npublic:\n string shortestPalindrome(string p) {\n string newString = p;\n string rs = p;\n reverse(rs.begin(), rs.end());\n newString += '#';\n newString += rs;\n\n int sz = newString.size();\n int pi[sz];\n pi[0] = 0;\n ... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 0 | {
"code": "class Solution {\npublic:\n string shortestPalindrome(string s) {\n ios_base::sync_with_stdio(false);\n int n=s.size();\n vector<int> lps(2*n+1);\n int c=0;\n for(int i=0;i<2*n+1;i++)\n {\n int m=2*c-i;\n if(c+lps[c]>i) lps[i]=min(lps[m],c+... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 0 | {
"code": "class Solution {\npublic:\n string shortestPalindrome(string s) {\n int size = s.size();\n\n std::cout << \"kmp: \";\n /* KMP-Table Building Algorithm */\n vector<int> kmp_table(size, 0);\n //std::cout << kmp_table[0] << \",\";\n for (int i = 1; i < size; ++i) {... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 0 | {
"code": "// Author: PrintulVostru\nclass Solution {\npublic:\n string shortestPalindrome(string s) {\n int n = s.size();\n if (n == 0) return \"\";\n\n vector<int> prefix(n, 0);\n prefix[0] = s[0] - 'a';\n for (int i = 1; i < s.size(); i++) \n prefix[i] = (1LL * pref... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 0 | {
"code": "class Solution {\npublic:\n\n string shortestPalindrome(string s) {\n string t = s;\n reverse(t.begin(),t.end()); \n string rev =t;\n if(s == t) return s;\n int m = s.size();\n\n t=s+ '&' + t;\n int n = t.size();\n vector<int> lps(n);\n\n in... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 0 | {
"code": "class Solution {\nprivate:\n // Function to compute the prefix function (KMP table)\n void computePrefixFunction(const std::string &pattern, std::vector<int> &lps) {\n int m = pattern.size();\n lps[0] = 0; // Base case\n int len = 0; // Length of the previous longest prefix suffi... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 0 | {
"code": "class Solution {\nprivate:\n // Function to compute the prefix function (KMP table)\n void computePrefixFunction(const std::string &pattern, std::vector<int> &lps) {\n int m = pattern.size();\n lps[0] = 0; // Base case\n int len = 0; // Length of the previous longest prefix suffi... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 0 | {
"code": "class Solution {\npublic:\n string shortestPalindrome(string s) {\n if (s.empty()) return s;\n \n string rev_s = s;\n reverse(rev_s.begin(), rev_s.end());\n string combined = s + \"#\" + rev_s;\n \n vector<int> z = calculateZ(combined);\n \n ... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 0 | {
"code": "class Solution {\npublic:\n string shortestPalindrome(string s) {\n if (s.empty()) return s;\n \n string rev_s = s;\n reverse(rev_s.begin(), rev_s.end());\n string combined = s + \"#\" + rev_s;\n \n vector<int> z = calculateZ(combined);\n \n ... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 1 | {
"code": "class Solution {\npublic:\n string shortestPalindrome(string s) {\n // Reverse the original string\n string reversedString = string(s.rbegin(), s.rend());\n\n // Combine the original and reversed strings with a separator\n string combinedString = s + \"#\" + reversedString;\n... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 1 | {
"code": "class Solution {\npublic:\n string shortestPalindrome(string s) {\n // Reverse the original string\n string reversedString = string(s.rbegin(), s.rend());\n\n // Combine the original and reversed strings with a separator\n string combinedString = s + \"#\" + reversedString;\n... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 1 | {
"code": "class Solution {\npublic:\n string shortestPalindrome(string s) {\n // Reverse the original string\n string reversedString = string(s.rbegin(), s.rend());\n\n // Combine the original and reversed strings with a separator\n string combinedString = s + \"#\" + reversedString;\n... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 1 | {
"code": "class Solution {\npublic:\n string shortestPalindrome(string s) {\n string rev_s = s;\n reverse(rev_s.begin(), rev_s.end());\n string l = s + \"#\" + rev_s;\n int m = l.size();\n std::vector<int> h(m + 1, 0); // Create a vector of size m+1 initialized to 0\n int... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 2 | {
"code": "class Solution {\npublic:\n string shortestPalindrome(string s) {\n // Return early if the string is null or empty\n if (s.empty()) {\n return s;\n }\n\n // Preprocess the string to handle palindromes uniformly\n string modifiedString = preprocessString(s);\... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 2 | {
"code": "class Solution {\npublic:\n string shortestPalindrome(string s) {\n // Return early if the string is null or empty\n if (s.empty()) {\n return s;\n }\n\n // Preprocess the string to handle palindromes uniformly\n string modifiedString = preprocessString(s);\... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 2 | {
"code": "class Solution {\npublic:\n string shortestPalindrome(string s) {\n if(s.empty()) return s;\n string s1 = s;\n s1+='&';\n string s2 = s;\n reverse(s2.begin(), s2.end());\n s1+=s2;\n int pre =0, suf = 1, n = s1.size();\n vector<int> lcs(n, 0);\n ... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 2 | {
"code": "class Solution{\n public:\n string shortestPalindrome(string s){\n if(s.empty()) return s;\n\n string rev_s =s;\n reverse(rev_s.begin(),rev_s.end());\n\n string combined =s+\"#\" +rev_s;\n\n int n=combined.length();\n\n vector<int> kmp(n,0);\n\n buildK... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 3 | {
"code": "class Solution {\npublic:\n const long long MOD = 1e5 + 3;\n const long long BASE = 26;\n long long pow(long long n, long long x){\n if (x == 0) return 1;\n else {\n long long temp = pow(n, x / 2) % MOD;\n if (x % 2 == 0) return (temp * temp) % MOD;\n ... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 3 | {
"code": "class Solution {\npublic:\n int LPS(string s){\n int n=s.length();\n vector<int>lps(n,0);\n int i=1,len=0;\n while(i<n){\n if(s[i]==s[len]){\n len++;\n lps[i]=len;\n i++;\n }\n else{\n ... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 3 | {
"code": "class Solution {\n vector<int> computeLps(string b){\n int n=b.size();\n vector<int> lps(n);\n lps[0]=0;\n int i=1,len=0;\n while(i<n){\n if(b[i]==b[len]){\n len++;\n lps[i]=len;\n i++;\n }\n ... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 3 | {
"code": "class Solution {\npublic:\n string shortestPalindrome(string s) {\n if(s==\"\")return s;\n string t=s;\n reverse(t.begin(),t.end());\n s+=t;\n reverse(t.begin(),t.end());\n int n=s.size();\n vector<long long> z(n,0);\n z[0]=n;\n long long l=... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 3 | {
"code": "class Solution {\npublic:\n long long lps(string &s) {\n vector<long long> lps(s.size(), 0);\n int i = 0;\n int j = 1;\n int n = s.size();\n \n while (j < n) {\n if (s[i] == s[j]) {\n lps[j] = i + 1;\n i++;\n j++;\n } else {\n i... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 3 | {
"code": "class Manacher\n{\n string t;\n vector<int> p;\n\n int lpsStart, lpsLength;\n\npublic:\n Manacher(string s)\n {\n for (char i : s)\n {\n t += string(\"#\") + i;\n }\n t += string(\"#\");\n\n build();\n }\n\n // p[i] = defines how many lengt... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 3 | {
"code": "class Solution {\n vector<int>computelps(string temp , vector<int>&lps){\n int n = temp.size();\n int length =0;\n int i =1;\n while(i<n){\n if(temp[i]==temp[length]){\n length++;\n lps[i] = length ; \n i++;\n ... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 3 | {
"code": "using ll = long long ;\nclass Solution {\npublic:\n string shortestPalindrome(string s) {\n ll n = s.length();\n if(n < 2) return s;\n string t = s; \n reverse(t.begin(),t.end());\n \n string l = s + \"#\" + t;\n \n \n// int end =... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 3 | {
"code": "class Solution {\npublic:\n string shortestPalindrome(string s) {\n // aacecaaa\n // aaacecaaa\n // abcd\n // dcbabcd\n // longest prefix, which is just mirror of the next substring formed of the same length\n // aacecaaa\n // abcd - dcbabcd\n // \... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 3 | {
"code": "class Solution {\npublic:\n string shortestPalindrome(string s) {\n // aacecaaa\n // aaacecaaa\n // abcd\n // dcbabcd\n // longest prefix, which is just mirror of the next substring formed of the same length\n // aacecaaa\n // abcd - dcbabcd\n // \... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 3 | {
"code": "class Solution {\npublic:\n string shortestPalindrome(string s) {\n string str=s;\n reverse(str.begin(),str.end());\n string s1 = s+\"#\"+str;\n\n int i=0,j=1;\n vector<int> v;\n v.push_back(0);\n while(j<s1.length()){\n if(s1[i]==s1[j]){\n i++;\... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 3 | {
"code": "class Solution {\npublic:\n const long long MOD = 1e5 + 3;\n const long long BASE = 26;\n long long* pow;\n string shortestPalindrome(string s) {\n long long n = s.size();\n vector<long long> pow(n + 1);\n\n pow[0] = 1;\n for (int i = 1; i < n; ++i)\n pow[... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 3 | {
"code": "\n#define ll long long\n\nclass Solution {\npublic:\n ll p = 31;\n ll m = 1e9+9;\n\n vector<ll> p_pow;\n vector<ll> h1, h2;\n\n void compute_power(int n) {\n p_pow.resize(n + 1);\n p_pow[0] = 1;\n for (int i = 1; i <= n; i++) {\n p_pow[i] = (p_pow[i - 1] * p) ... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 3 | {
"code": "#define B 131\n#define P 1000000007\n\ntypedef long long ll;\n\nll back_hash(string&s,vector<ll>&bhash,vector<ll>&p131,int idx){\n int N=s.size()-1;\n ll tmp=(bhash[1]-bhash[idx+1]*p131[idx]%P)%P;\n while(tmp<0)tmp+=P;\n return tmp;\n}\n\n\nclass Solution {\npublic:\n string shortestPalindro... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 3 | {
"code": "class Solution {\npublic:\n vector<int> lpstable(string s){\n int n=s.size();\n vector<int> v;\n v.push_back(0);\n int j=0;\n for(int i=1;i<n;i++){\n j=v[i-1];\n while(j>0 && s[j]!=s[i]){\n j=v[j-1];\n }\n if(s... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 3 | {
"code": "class Solution {\npublic:\n void generate_h( vector<int> &h , string s) { \n h.push_back(-1) ;\n int i = 1 , j = 0 ;\n while ( i < s.size()) { \n if ( s[i] != s[j]) { \n h.push_back(j) ;\n j = h[j] ;\n while ( j >= 0 && s[i] !=... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 3 | {
"code": "class Solution {\npublic:\n void generate_h( vector<int> &h , string s) { \n h.push_back(-1) ;\n int i = 1 , j = 0 ;\n while ( i < s.size()) { \n if ( s[i] != s[j]) { \n h.push_back(j) ;\n j = h[j] ;\n while ( j >= 0 && s[i] !=... |
214 | <p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>
<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>
<p> </p>
<p><strong class="example">Exampl... | 3 | {
"code": "class Solution {\npublic:\n bool checkPal(string& s, int b, int e){\n if (b >= e) return true;\n if (s[b] != s[e]) return false;\n return checkPal(s, b + 1, e - 1);\n }\n\n\n string shortestPalindrome(string s) {\n int n = s.size();\n if (n <= 1) return s;\n ... |
1,814 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>You are initially standing at index <code>0</code>. In one move, you can jump at most <code>k</code> steps forward without going outside the boundaries of the array. That is, you can jump from index <cod... | 1 | {
"code": "class Solution {\npublic:\n int maxResult(vector<int>& nums, int k) {\n int n = nums.size();\n deque<int> q;\n vector<int> dp(n);\n q.push_back(0);\n dp[0] = nums[0];\n for(int i=1; i<n; i++) {\n dp[i] = nums[i] + dp[q.front()];\n while(!q.... |
1,814 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>You are initially standing at index <code>0</code>. In one move, you can jump at most <code>k</code> steps forward without going outside the boundaries of the array. That is, you can jump from index <cod... | 1 | {
"code": "class Solution {\npublic:\n int maxResult(vector<int>& nums, int k) {\n int n=nums.size();\n vector<int> dp(n);\n deque<pair<int,int>>pq;\n for(int i=n-1;i>=0;i--){\n dp[i]=nums[i];\n while(!pq.empty() && (pq.front().second > i+k)) {\n pq.... |
1,814 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>You are initially standing at index <code>0</code>. In one move, you can jump at most <code>k</code> steps forward without going outside the boundaries of the array. That is, you can jump from index <cod... | 1 | {
"code": "class Solution {\npublic:\n int maxResult(vector<int>& nums, int k) {\n int n=nums.size();\n vector<int> dp(n,-1);\n dp[0]=nums[0];\n deque<int> dq;\n dq.push_back(0);\n\n for(int i=1;i<n;i++){\n while (!dq.empty() && dq.front() < i - k ) {\n ... |
1,814 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>You are initially standing at index <code>0</code>. In one move, you can jump at most <code>k</code> steps forward without going outside the boundaries of the array. That is, you can jump from index <cod... | 1 | {
"code": "class Solution {\npublic:\n int maxResult(vector<int>& nums, int k) {\n vector<int> dp(nums.size(), -1);\n deque<int> dq;\n dq.push_back(0);\n dp[0] = nums[0];\n for (int i = 1; i < nums.size(); i++){\n while (!dq.empty() && dq.front() < i - k){\n ... |
1,814 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>You are initially standing at index <code>0</code>. In one move, you can jump at most <code>k</code> steps forward without going outside the boundaries of the array. That is, you can jump from index <cod... | 1 | {
"code": "class Solution {\npublic:\n int maxResult(vector<int>& nums, int k) {\n vector<int> dp;\n dp.resize(nums.size(),0);\n dp[0] = nums[0];\n \n // maxDP store the previous k DP element\n vector<int> tmpDP;\n \n // add first value\n tmpDP.push_ba... |
1,814 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>You are initially standing at index <code>0</code>. In one move, you can jump at most <code>k</code> steps forward without going outside the boundaries of the array. That is, you can jump from index <cod... | 1 | {
"code": "class Solution {\npublic:\n int maxResult(vector<int>& nums, int k) {\n vector<int> dp;\n dp.resize(nums.size(),0);\n dp[0] = nums[0];\n \n // maxDP store the previous k DP element\n vector<int> tmpDP;\n \n // add first value\n tmpDP.push_ba... |
1,814 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>You are initially standing at index <code>0</code>. In one move, you can jump at most <code>k</code> steps forward without going outside the boundaries of the array. That is, you can jump from index <cod... | 1 | {
"code": "#define ppi pair<int,int>\nclass Solution {\npublic:\n int maxResult(vector<int>& v, int k) {\n int n = v.size();\n deque<ppi>dq;\n dq.push_back({v[0] , 0});\n vector<int>dp(n , -1e9); dp[0] = v[0];\n\n for(int i=1;i<n;i++)\n {\n while(!dq.empty() && ... |
1,814 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>You are initially standing at index <code>0</code>. In one move, you can jump at most <code>k</code> steps forward without going outside the boundaries of the array. That is, you can jump from index <cod... | 1 | {
"code": "class Solution {\npublic:\n int maxResult(vector<int>& nums, int k) {\n int n = nums.size();\n deque<pair<int, int>> q; // idx, sum\n vector<int> dp(n);\n dp[0] = nums[0];\n q.push_back(make_pair(0, dp[0]));\n for (int i = 1; i < n; i++) {\n auto fron... |
1,814 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>You are initially standing at index <code>0</code>. In one move, you can jump at most <code>k</code> steps forward without going outside the boundaries of the array. That is, you can jump from index <cod... | 2 | {
"code": "class Solution {\npublic:\n int maxResult(vector<int>& nums, int k) {\n int n = nums.size();\n vector<long long>dp(n, -1e15);\n dp[n-1] = nums[n-1];\n deque<long long>dq;\n dq.push_back(dp[n-1]);\n int j = n-1;\n for(int i =n-2; i>=0; i--){\n d... |
1,814 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>You are initially standing at index <code>0</code>. In one move, you can jump at most <code>k</code> steps forward without going outside the boundaries of the array. That is, you can jump from index <cod... | 2 | {
"code": "class Solution {\npublic:\n int maxResult(vector<int>& nums, int k) {\n int N = nums.size(),\n totalSum = accumulate(nums.begin(), nums.end(), 0);\n vector<int> maxSum(N, 0); \n maxSum[0] = nums[0]; \n\n deque<pair<int, int>> dq; // (value, pos)\n dq.empl... |
1,814 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>You are initially standing at index <code>0</code>. In one move, you can jump at most <code>k</code> steps forward without going outside the boundaries of the array. That is, you can jump from index <cod... | 2 | {
"code": "class Solution {\npublic:\n int maxResult(vector<int>& nums, int k) {\n int n = nums.size();\n vector<int> dp(n, 0);\n dp[n - 1] = nums[n - 1];\n\n auto sorta = [&](int pos1, int pos2) -> bool {\n return dp[pos1] < dp[pos2];\n };\n\n priority_queue<in... |
1,814 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>You are initially standing at index <code>0</code>. In one move, you can jump at most <code>k</code> steps forward without going outside the boundaries of the array. That is, you can jump from index <cod... | 2 | {
"code": "class Solution {\npublic:\n int maxResult(vector<int>& nums, int k) {\n int n = nums.size();\n vector<int> dp(n, 0);\n dp[n - 1] = nums[n - 1];\n\n auto sorta = [&](int pos1, int pos2) -> bool {\n return dp[pos1] < dp[pos2];\n };\n\n priority_queue<in... |
1,814 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>You are initially standing at index <code>0</code>. In one move, you can jump at most <code>k</code> steps forward without going outside the boundaries of the array. That is, you can jump from index <cod... | 2 | {
"code": "const static auto fast = []\n{ \n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return 0;\n}();\n\nclass Solution {\npublic:\n int maxResult(vector<int>& nums, int k) {\n priority_queue<pair<int,int>> heap;\n int cost = nums.back();... |
1,814 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>You are initially standing at index <code>0</code>. In one move, you can jump at most <code>k</code> steps forward without going outside the boundaries of the array. That is, you can jump from index <cod... | 2 | {
"code": "class Solution {\npublic:\n int maxResult(vector<int>& nums, int k) {\n int maxScore = 0;\n priority_queue<pair<int, int>> pq;\n \n for (int i = nums.size() - 1; i >= 0; i--) {\n while (!pq.empty() && pq.top().second > i + k) pq.pop(); \n int temp = p... |
1,814 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>You are initially standing at index <code>0</code>. In one move, you can jump at most <code>k</code> steps forward without going outside the boundaries of the array. That is, you can jump from index <cod... | 2 | {
"code": "class Solution {\npublic:\n int maxResult(vector<int>& nums, int k) {\n const int n = nums.size();\n // deque<int> dq;\n // dq.push_back(0);\n priority_queue<pair<int,int>> pq; // pair {sum at idx, idx}\n\n for (int i=n-1; i>-1; i--) {\n while (pq.size() && ... |
1,814 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>You are initially standing at index <code>0</code>. In one move, you can jump at most <code>k</code> steps forward without going outside the boundaries of the array. That is, you can jump from index <cod... | 2 | {
"code": "class Solution {\npublic:\n int fun(int n, int i, vector<int>& nums, int k, vector<int>& dp) {\n if (i == n - 1)\n return nums[i];\n if (dp[i] != -1)\n return dp[i];\n int maxi = -1e9;\n for (int j = i + 1; j <= min(n - 1, i + k); j++) {\n max... |
1,814 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>You are initially standing at index <code>0</code>. In one move, you can jump at most <code>k</code> steps forward without going outside the boundaries of the array. That is, you can jump from index <cod... | 2 | {
"code": "class Solution {\npublic:\n int maxResult(vector<int>& nums, int k) {\n int n=nums.size();\n int ans=0;\n priority_queue<pair<int,int>>q;\n for(int i=0;i<n;i++){\n while(!q.empty() && i-q.top().second>k) q.pop();\n int val=0;\n if(!q.empty()){... |
1,814 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>You are initially standing at index <code>0</code>. In one move, you can jump at most <code>k</code> steps forward without going outside the boundaries of the array. That is, you can jump from index <cod... | 2 | {
"code": "class Solution {\npublic:\n int maxResult(vector<int>& arr, int k) {\n int ans = arr[0];\n priority_queue<pair<int,int>>pq;\n pq.push({arr[0] , 0});\n for(int i = 1;i<arr.size();i++){\n while((pq.top().second+k)<i) pq.pop();\n ans = arr[i]+pq.top().first... |
1,814 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>You are initially standing at index <code>0</code>. In one move, you can jump at most <code>k</code> steps forward without going outside the boundaries of the array. That is, you can jump from index <cod... | 2 | {
"code": "class Solution {\n // Time complexity O(nlogk); Space complexity O(k);\npublic:\n int maxResult(vector<int>& nums, int k) {\n int n = nums.size();\n priority_queue<pair<int,int>> pq;\n for (int i=0;i<n;i++) {\n while (!pq.empty() && i - pq.top().second > k) \n ... |
1,814 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>You are initially standing at index <code>0</code>. In one move, you can jump at most <code>k</code> steps forward without going outside the boundaries of the array. That is, you can jump from index <cod... | 2 | {
"code": "#define pii pair<int, int>\nclass Solution {\npublic:\n int maxResult(vector<int>& nums, int k)\n {\n int n=nums.size();\n int score[n];\n priority_queue<pii> pq;\n \n for(int i=n-1 ; i>=0 ; i--)\n {\n while(pq.size() && pq.top().second>i+k)\n ... |
1,814 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>You are initially standing at index <code>0</code>. In one move, you can jump at most <code>k</code> steps forward without going outside the boundaries of the array. That is, you can jump from index <cod... | 2 | {
"code": "class Solution {\npublic:\n// iterative O(n^2) solution toh likh lunga\n int maxResult(vector<int>& nums, int k) {\n int n = nums.size();\n int dp[n]; // dp[i] = max score that one can acheive starting from index i and ending at index n-1\n dp[n-1] = nums[n-1];\n \n pr... |
1,814 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>You are initially standing at index <code>0</code>. In one move, you can jump at most <code>k</code> steps forward without going outside the boundaries of the array. That is, you can jump from index <cod... | 2 | {
"code": "class Solution {\npublic:\n int maxResult(vector<int>& nums, int k) {\n int n=nums.size();\n vector<int>dp(n,-1);\n dp[n-1]=nums[n-1];\n priority_queue<pair<int,int>>pq;\n pq.push({dp[n-1],n-1});\n for(int i=n-2;i>=0;i--)\n {\n while(pq.size()>0... |
1,814 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>You are initially standing at index <code>0</code>. In one move, you can jump at most <code>k</code> steps forward without going outside the boundaries of the array. That is, you can jump from index <cod... | 2 | {
"code": "class Solution {\npublic:\n int maxResult(vector<int>& nums, int k) {\n int N = nums.size(),\n totalSum = accumulate(nums.begin(), nums.end(), 0);\n\n vector<int> maxSum(N, totalSum); \n maxSum[0] = nums[0];\n\n auto cmp = [&](pair<int, int> a, pair<int, int> b) {\... |
1,814 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>You are initially standing at index <code>0</code>. In one move, you can jump at most <code>k</code> steps forward without going outside the boundaries of the array. That is, you can jump from index <cod... | 2 | {
"code": "class Solution {\npublic:\n int maxResult(vector<int>& nums, int k) {\n \n //dp[i]= for(int j=i+1;j<=min(n-1,i+k);j++)\n\n //3,-4,7,5,6,7\n //return dp[0]\n //dp[n-1]=nums[n-1];\n int n=nums.size();\n vector<int>dp(n,0);\n\n dp[n-1]=nums[n-1];\... |
1,814 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>You are initially standing at index <code>0</code>. In one move, you can jump at most <code>k</code> steps forward without going outside the boundaries of the array. That is, you can jump from index <cod... | 3 | {
"code": "class Solution {\npublic:\n int maxResult(vector<int>& nums, int k) {\n vector<int>dp(nums.size(),INT_MIN);\n dp[0] = nums[0];\n priority_queue<pair<int,int>>pq;\n pq.push({dp[0],0});\n for(int i=1; i<nums.size(); i++){\n while(pq.size() && i-pq.top().second... |
1,814 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>You are initially standing at index <code>0</code>. In one move, you can jump at most <code>k</code> steps forward without going outside the boundaries of the array. That is, you can jump from index <cod... | 3 | {
"code": "#include <vector>\n#include <queue>\n#include <utility> // For std::pair\nusing namespace std;\n\nclass Solution {\npublic:\n int maxResult(vector<int>& nums, int k) {\n int n = nums.size();\n \n // dp[i] will store the maximum score to reach index i\n vector<int> dp(n, 0);\n... |
1,814 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>You are initially standing at index <code>0</code>. In one move, you can jump at most <code>k</code> steps forward without going outside the boundaries of the array. That is, you can jump from index <cod... | 3 | {
"code": "class Solution {\npublic:\n int maxResult(vector<int>& nums, int k) {\n int n = nums.size();\n priority_queue<pair<long long int,int>>pq;\n pq.emplace(nums[0],0);\n for(int i=1;i<min(k,n);i++){\n // if(pq.top().first>0){\n nums[i]+=pq.top().first;\n ... |
1,814 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>You are initially standing at index <code>0</code>. In one move, you can jump at most <code>k</code> steps forward without going outside the boundaries of the array. That is, you can jump from index <cod... | 3 | {
"code": "class Solution {\npublic:\n int maxResult(vector<int>& nums, int k) {\n map<int, int, std::greater<int>> lastK;\n lastK[nums[0]] = 0;\n for (int i = 1; i < nums.size(); i++) {\n auto itM = lastK.begin();\n cout << \" \" << itM->second << endl;\n nums... |
1,814 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>You are initially standing at index <code>0</code>. In one move, you can jump at most <code>k</code> steps forward without going outside the boundaries of the array. That is, you can jump from index <cod... | 3 | {
"code": "class Solution {\npublic:\n int maxResult(vector<int>& nums, int k) {\n map<int, int, std::greater<int>> lastK;\n lastK[nums[0]] = 0;\n for (int i = 1; i < nums.size(); i++) {\n auto itM = lastK.begin();\n cout << \" \" << itM->second << endl;\n nums... |
1,814 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>You are initially standing at index <code>0</code>. In one move, you can jump at most <code>k</code> steps forward without going outside the boundaries of the array. That is, you can jump from index <cod... | 3 | {
"code": "class Solution {\npublic:\n // int maxResult(vector<int>& nums, int k) {\n // deque<int> lastK;\n // lastK[nums[0]] = 0;\n // for (int i = 1; i < nums.size(); i++) {\n // auto itM = lastK.begin();\n // cout << \" \" << itM->second << endl;\n // nums[... |
1,814 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>You are initially standing at index <code>0</code>. In one move, you can jump at most <code>k</code> steps forward without going outside the boundaries of the array. That is, you can jump from index <cod... | 3 | {
"code": "typedef int ll;\nclass Solution {\npublic:\n int maxResult(vector<int>& a, int k) {\n ll n = a.size();\n\n map<ll, ll> st;\n st[a[n-1]]++;\n ll x = -1;\n ll dp[n];\n dp[n-1] = a[n-1];\n for (int i = n-2; i>=0; i--) {\n if (i+k+1 < n && st.size(... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.