id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
515 | <p>Given the <code>root</code> of a binary tree, return <em>an array of the largest value in each row</em> of the tree <strong>(0-indexed)</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/08/21/largest_e1.jpg" style="width: 300px; height: 172px;" />
<pre>
<strong>Input:</strong> root = [1,3,2,5,3,null,9]
<strong>Output:</strong> [1,3,9]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2,3]
<strong>Output:</strong> [1,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree will be in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></li>
</ul>
| 1 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> largestValues(TreeNode* root) {\n vector<int> ans;\n if(root == NULL)\n return ans;\n queue<TreeNode*> q;\n \n q.push(root);\n while(q.empty() == false) {\n int sz = q.size();\n int maxi = INT_MIN;\n for(int i=0;i<sz;i++) {\n TreeNode* curr = q.front();\n maxi = max(maxi, curr->val);\n q.pop();\n if(curr->left != NULL) {\n q.push(curr->left);\n }\n if(curr->right != NULL) {\n q.push(curr->right);\n }\n }\n ans.push_back(maxi);\n }\n return ans;\n }\n};",
"memory": "20900"
} |
515 | <p>Given the <code>root</code> of a binary tree, return <em>an array of the largest value in each row</em> of the tree <strong>(0-indexed)</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/08/21/largest_e1.jpg" style="width: 300px; height: 172px;" />
<pre>
<strong>Input:</strong> root = [1,3,2,5,3,null,9]
<strong>Output:</strong> [1,3,9]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2,3]
<strong>Output:</strong> [1,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree will be in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></li>
</ul>
| 1 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> largestValues(TreeNode* root) {\n vector<int>ans;\n if(root==NULL) return ans;\n queue<TreeNode*>q;\n q.push(root);\n while(!q.empty()){\n int m=q.size();\n int maxval=INT_MIN;\n while(m){\n TreeNode* temp= q.front();\n maxval=max(maxval, temp->val);\n if(temp->left) q.push(temp->left);\n if(temp->right) q.push(temp->right);\n q.pop();\n m--;\n }\n ans.push_back(maxval);\n } \n return ans;\n }\n};",
"memory": "20900"
} |
515 | <p>Given the <code>root</code> of a binary tree, return <em>an array of the largest value in each row</em> of the tree <strong>(0-indexed)</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/08/21/largest_e1.jpg" style="width: 300px; height: 172px;" />
<pre>
<strong>Input:</strong> root = [1,3,2,5,3,null,9]
<strong>Output:</strong> [1,3,9]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2,3]
<strong>Output:</strong> [1,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree will be in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></li>
</ul>
| 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> largestValues(TreeNode* root) {\n vector<int> ans;\n queue<TreeNode*> q;\n if(root) q.push(root);\n while(!q.empty()) {\n int mx = INT_MIN, n = q.size();\n for(int i=0;i<n;i++) {\n TreeNode* t = q.front();\n q.pop();\n mx = max(mx, t->val);\n if(t->left) q.push(t->left);\n if(t->right) q.push(t->right);\n }\n ans.push_back(mx);\n }\n return ans;\n }\n};",
"memory": "21000"
} |
515 | <p>Given the <code>root</code> of a binary tree, return <em>an array of the largest value in each row</em> of the tree <strong>(0-indexed)</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/08/21/largest_e1.jpg" style="width: 300px; height: 172px;" />
<pre>
<strong>Input:</strong> root = [1,3,2,5,3,null,9]
<strong>Output:</strong> [1,3,9]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2,3]
<strong>Output:</strong> [1,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree will be in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></li>
</ul>
| 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> largestValues(TreeNode* root) {\n vector<int> res;\n if(!root) return res;\n queue<TreeNode*> q;\n q.push(root);\n while(!q.empty()){\n int size = q.size();\n int levelMax = INT_MIN;\n while(size--) {\n TreeNode* front = q.front(); q.pop();\n if(front->left) q.push(front->left);\n if(front->right) q.push(front->right);\n levelMax = max(levelMax, front->val);\n }\n res.push_back(levelMax);\n }\n return res;\n }\n};",
"memory": "21000"
} |
515 | <p>Given the <code>root</code> of a binary tree, return <em>an array of the largest value in each row</em> of the tree <strong>(0-indexed)</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/08/21/largest_e1.jpg" style="width: 300px; height: 172px;" />
<pre>
<strong>Input:</strong> root = [1,3,2,5,3,null,9]
<strong>Output:</strong> [1,3,9]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2,3]
<strong>Output:</strong> [1,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree will be in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></li>
</ul>
| 3 | {
"code": "class Solution {\n public:\n vector<int> largestValues(TreeNode* root) {\n if (root == nullptr)\n return {};\n\n vector<int> ans;\n queue<TreeNode*> q{{root}};\n\n while (!q.empty()) {\n int mx = INT_MIN;\n for (int sz = q.size(); sz > 0; --sz) {\n TreeNode* node = q.front();\n q.pop();\n mx = max(mx, node->val);\n if (node->left)\n q.push(node->left);\n if (node->right)\n q.push(node->right);\n }\n ans.push_back(mx);\n }\n\n return ans;\n }\n};",
"memory": "21100"
} |
515 | <p>Given the <code>root</code> of a binary tree, return <em>an array of the largest value in each row</em> of the tree <strong>(0-indexed)</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/08/21/largest_e1.jpg" style="width: 300px; height: 172px;" />
<pre>
<strong>Input:</strong> root = [1,3,2,5,3,null,9]
<strong>Output:</strong> [1,3,9]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2,3]
<strong>Output:</strong> [1,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree will be in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> largestValues(TreeNode* root) {\n\n if(root == NULL) return vector<int>();\n queue<pair<TreeNode*,int>> q;\n q.push({root,0});\n int maxi = INT_MIN, curr_lev = -1;\n vector<int> ans;\n while(!q.empty()) {\n int lev = q.front().second;\n TreeNode* node = q.front().first;\n q.pop();\n if(lev != curr_lev) {\n if(curr_lev != -1) ans.push_back(maxi);\n curr_lev = lev;\n maxi = node->val;\n }\n maxi = max(maxi, node->val);\n if(node->left != NULL) {q.push({node->left, lev+1});}\n if(node->right != NULL) {q.push({node->right, lev+1});}\n }\n ans.push_back(maxi);\n return ans;\n \n \n }\n};",
"memory": "21100"
} |
515 | <p>Given the <code>root</code> of a binary tree, return <em>an array of the largest value in each row</em> of the tree <strong>(0-indexed)</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/08/21/largest_e1.jpg" style="width: 300px; height: 172px;" />
<pre>
<strong>Input:</strong> root = [1,3,2,5,3,null,9]
<strong>Output:</strong> [1,3,9]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2,3]
<strong>Output:</strong> [1,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree will be in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\nvoid lot(TreeNode* root,int maxm ,vector<int> &ans,\n queue<TreeNode*> q){\n q.push(root);\n q.push(NULL);\n while(!q.empty()){\n TreeNode* front;\n front=q.front();\n q.pop();\n if(front!=NULL){\n if(front->left!=NULL){\n q.push(front->left);\n }\n if(front->right!=NULL){\n q.push(front->right);\n }\n maxm=max(front->val,maxm);\n }\n else{\n ans.push_back(maxm);\n maxm=INT_MIN;\n if(q.empty()){\n break;\n }\n else{\n q.push(NULL);\n }\n }\n }\n return;\n}\n vector<int> largestValues(TreeNode* root) {\n vector<int> ans;\n if(root==NULL){\n return ans;\n }\n queue<TreeNode*> q;\n int maxm=INT_MIN;\n lot(root,maxm,ans,q);\n return ans;\n }\n};",
"memory": "21200"
} |
515 | <p>Given the <code>root</code> of a binary tree, return <em>an array of the largest value in each row</em> of the tree <strong>(0-indexed)</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/08/21/largest_e1.jpg" style="width: 300px; height: 172px;" />
<pre>
<strong>Input:</strong> root = [1,3,2,5,3,null,9]
<strong>Output:</strong> [1,3,9]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2,3]
<strong>Output:</strong> [1,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree will be in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\nvoid lot(TreeNode* root,int maxm ,vector<int> &ans,\n queue<TreeNode*> q){\n q.push(root);\n q.push(NULL);\n while(!q.empty()){\n TreeNode* front;\n front=q.front();\n q.pop();\n if(front!=NULL){\n if(front->left!=NULL){\n q.push(front->left);\n }\n if(front->right!=NULL){\n q.push(front->right);\n }\n maxm=max(front->val,maxm);\n }\n else{\n ans.push_back(maxm);\n maxm=INT_MIN;\n if(q.empty()){\n break;\n }\n else{\n q.push(NULL);\n }\n }\n }\n return;\n}\n vector<int> largestValues(TreeNode* root) {\n vector<int> ans;\n if(root==NULL){\n return ans;\n }\n queue<TreeNode*> q;\n int maxm=INT_MIN;\n lot(root,maxm,ans,q);\n return ans;\n }\n};",
"memory": "21200"
} |
515 | <p>Given the <code>root</code> of a binary tree, return <em>an array of the largest value in each row</em> of the tree <strong>(0-indexed)</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/08/21/largest_e1.jpg" style="width: 300px; height: 172px;" />
<pre>
<strong>Input:</strong> root = [1,3,2,5,3,null,9]
<strong>Output:</strong> [1,3,9]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2,3]
<strong>Output:</strong> [1,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree will be in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> largestValues(TreeNode* root) {\n vector<int> result;\n if (root == nullptr) return result; // If root is null, return empty result\n \n queue<TreeNode*> q;\n q.push(root);\n \n while (!q.empty()) {\n int size = q.size();\n vector<int> currentLevel; // Store all node values of the current level\n \n for (int i = 0; i < size; ++i) {\n TreeNode* node = q.front();\n q.pop();\n \n // Store the current node's value in the current level vector\n currentLevel.push_back(node->val);\n \n // Add children to the queue for the next level\n if (node->left) q.push(node->left);\n if (node->right) q.push(node->right);\n }\n \n // Find the max value in the current level and store it in result\n int maxVal = *max_element(currentLevel.begin(), currentLevel.end());\n result.push_back(maxVal);\n }\n \n return result;\n }\n};\n",
"memory": "21300"
} |
515 | <p>Given the <code>root</code> of a binary tree, return <em>an array of the largest value in each row</em> of the tree <strong>(0-indexed)</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/08/21/largest_e1.jpg" style="width: 300px; height: 172px;" />
<pre>
<strong>Input:</strong> root = [1,3,2,5,3,null,9]
<strong>Output:</strong> [1,3,9]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2,3]
<strong>Output:</strong> [1,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree will be in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int depth(TreeNode* root, int d) {\n if (!root) return d;\n\n d++;\n\n if (!root->left && !root->right) return d;\n\n return max(depth(root->left, d), depth(root->right, d));\n }\n\n int maxDepth(TreeNode* root) {\n return depth(root, 0);\n }\n\n void bfs(TreeNode* root, vector<vector<int>>& ans, int level) {\n if (!root) return;\n\n level++;\n\n ans[level].push_back(root->val);\n\n if (!root->left && !root->right) return;\n\n if (root->left) bfs(root->left, ans, level);\n if (root->right) bfs(root->right, ans, level);\n }\n\n vector<int> largestValues(TreeNode* root) {\n vector<vector<int>> ans(maxDepth(root), vector<int>{});\n\n bfs(root, ans, -1);\n\n vector<int> f;\n\n for (auto arr: ans) f.push_back(*max_element(arr.begin(), arr.end()));\n\n return f;\n }\n};",
"memory": "21400"
} |
515 | <p>Given the <code>root</code> of a binary tree, return <em>an array of the largest value in each row</em> of the tree <strong>(0-indexed)</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/08/21/largest_e1.jpg" style="width: 300px; height: 172px;" />
<pre>
<strong>Input:</strong> root = [1,3,2,5,3,null,9]
<strong>Output:</strong> [1,3,9]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2,3]
<strong>Output:</strong> [1,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree will be in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> largestValues(TreeNode* root) {\n queue<pair<TreeNode*,int>>q;\n map<int,int>hash;\n vector<int>res;\n if(root==NULL)return res;\n q.push({root,0});\n while(q.size()){\n auto it=q.front();\n q.pop();\n TreeNode* head=it.first;\n int lvl=it.second;\n if(hash.find(lvl)==hash.end())hash[lvl]=INT_MIN;\n hash[lvl]=max(hash[lvl],head->val);\n if(head->left!=NULL){\n q.push({head->left,lvl+1});\n }\n if(head->right!=NULL){\n q.push({head->right,lvl+1});\n }\n }\n for(auto it:hash){\n res.push_back(it.second);\n }\n return res;\n }\n};",
"memory": "21500"
} |
515 | <p>Given the <code>root</code> of a binary tree, return <em>an array of the largest value in each row</em> of the tree <strong>(0-indexed)</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/08/21/largest_e1.jpg" style="width: 300px; height: 172px;" />
<pre>
<strong>Input:</strong> root = [1,3,2,5,3,null,9]
<strong>Output:</strong> [1,3,9]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2,3]
<strong>Output:</strong> [1,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree will be in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> largestValues(TreeNode* root) {\n vector<int>v;\n vector<vector<int>>ans;\n queue<TreeNode*>q;\n q.push(root);\n if(q.front()==nullptr){\n return v;\n }\n while(1){\n int n=q.size();\n if(n==0){\n break;\n }\n vector<int>temp;\n while(n!=0){\n TreeNode* node=q.front();\n temp.push_back(node->val);\n q.pop();\n if(node->left!=nullptr){\n q.push(node->left);\n }\n if(node->right!=nullptr){\n q.push(node->right);\n }\n n--;\n }\n ans.push_back(temp);\n }\n for(int i=0;i<ans.size();i++){\n int a=*max_element(ans[i].begin(),ans[i].end());\n v.push_back(a);\n }\n return v;\n }\n};",
"memory": "21600"
} |
515 | <p>Given the <code>root</code> of a binary tree, return <em>an array of the largest value in each row</em> of the tree <strong>(0-indexed)</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/08/21/largest_e1.jpg" style="width: 300px; height: 172px;" />
<pre>
<strong>Input:</strong> root = [1,3,2,5,3,null,9]
<strong>Output:</strong> [1,3,9]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2,3]
<strong>Output:</strong> [1,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree will be in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> largestValues(TreeNode* root) {\n vector<int> fin;\n vector<vector<int>> ans;\n if(!root) return {};\n queue<TreeNode*> q;\n q.push(root);\n while(!q.empty()){\n int size=q.size();\n vector<int> temp;\n for(int i=0;i<size;i++){\n TreeNode* ele=q.front();\n q.pop();\n if(ele->left!=nullptr) q.push(ele->left);\n if(ele->right!=nullptr) q.push(ele->right);\n temp.push_back(ele->val);\n }\n ans.push_back(temp);\n }\n for(auto it:ans){\n int maxi=INT_MIN;\n for(auto i:it){\n maxi=max(maxi,i);\n }\n fin.push_back(maxi);\n }\n return fin;\n }\n};",
"memory": "21700"
} |
515 | <p>Given the <code>root</code> of a binary tree, return <em>an array of the largest value in each row</em> of the tree <strong>(0-indexed)</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/08/21/largest_e1.jpg" style="width: 300px; height: 172px;" />
<pre>
<strong>Input:</strong> root = [1,3,2,5,3,null,9]
<strong>Output:</strong> [1,3,9]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2,3]
<strong>Output:</strong> [1,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree will be in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void levelorder(TreeNode* root,vector<vector<int>>&arr){\n if(root==NULL){\n return;\n }\n queue<TreeNode*>q;\n q.push(root);\n while(!q.empty()){\n int size=q.size();\n vector<int>level;\n for(int i=0;i<size;i++){\n TreeNode* node=q.front();\n q.pop();\n if(node->left){\n q.push(node->left);\n }\n if(node->right){\n q.push(node->right);\n }\n level.push_back(node->val);\n }\n arr.push_back(level);\n }\n\n }\n\n vector<int> largestValues(TreeNode* root) {\n vector<vector<int>>arr;\n levelorder(root,arr);\n vector<int>ans;\n for(auto it:arr){\n ans.push_back(*max_element(it.begin(),it.end()));\n }\n return ans;\n }\n};",
"memory": "21800"
} |
515 | <p>Given the <code>root</code> of a binary tree, return <em>an array of the largest value in each row</em> of the tree <strong>(0-indexed)</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/08/21/largest_e1.jpg" style="width: 300px; height: 172px;" />
<pre>
<strong>Input:</strong> root = [1,3,2,5,3,null,9]
<strong>Output:</strong> [1,3,9]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2,3]
<strong>Output:</strong> [1,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree will be in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> ans;\n priority_queue<int> pq;\n\n int getHeight(TreeNode* root) {\n if (root == nullptr) return 0;\n int leftHeight = getHeight(root->left);\n int rightHeight = getHeight(root->right);\n return max(leftHeight, rightHeight) + 1;\n }\n\n void LargestElement(TreeNode* root, int curr, int level) {\n if (root == nullptr) return;\n\n \n if (curr == level) {\n pq.push(root->val);\n }\n\n\n LargestElement(root->left, curr + 1, level);\n LargestElement(root->right, curr + 1, level);\n }\n\n vector<int> largestValues(TreeNode* root) {\n \n int n = getHeight(root);\n for(int i = 1; i <= n; i++){\n // Clear the priority queue for the new level\n pq = priority_queue<int>();\n\n LargestElement(root, 1, i);\n\n if (!pq.empty()) {\n ans.push_back(pq.top());\n }\n }\n return ans;\n }\n};\n",
"memory": "21900"
} |
515 | <p>Given the <code>root</code> of a binary tree, return <em>an array of the largest value in each row</em> of the tree <strong>(0-indexed)</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/08/21/largest_e1.jpg" style="width: 300px; height: 172px;" />
<pre>
<strong>Input:</strong> root = [1,3,2,5,3,null,9]
<strong>Output:</strong> [1,3,9]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2,3]
<strong>Output:</strong> [1,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree will be in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n \n void helper_function(vector<int> &vec, TreeNode* root, int level){\n if (root){\n while(vec.size()<=level) vec.push_back(INT_MIN);\n vec[level] = max(vec[level], root->val);\n helper_function(vec, root->left, level+1);\n helper_function(vec, root->right, level+1);\n }\n }\n \n vector<int> largestValues(TreeNode* root) {\n vector<int> vec;\n helper_function(vec, root, 0);\n return vec;\n \n }\n};",
"memory": "22000"
} |
515 | <p>Given the <code>root</code> of a binary tree, return <em>an array of the largest value in each row</em> of the tree <strong>(0-indexed)</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/08/21/largest_e1.jpg" style="width: 300px; height: 172px;" />
<pre>
<strong>Input:</strong> root = [1,3,2,5,3,null,9]
<strong>Output:</strong> [1,3,9]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2,3]
<strong>Output:</strong> [1,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree will be in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> largestValues(TreeNode* root) {\n vector<int>ans;\n queue<TreeNode* >q;\n if(!root){\n return ans;\n }\n q.push(root);\n \n priority_queue<int> pq;\n while(q.size()){\n int n = q.size();\n for(int i = 0; i < n ; i++){\n pq.push(q.front()->val);\n if(q.front()->left){\n q.push(q.front()->left);\n }\n if(q.front()->right){\n q.push(q.front()->right);\n }\n q.pop();\n }\n ans.push_back(pq.top());\n while(pq.size())\n {\n pq.pop();\n } \n \n }\n return ans;\n }\n};",
"memory": "22100"
} |
515 | <p>Given the <code>root</code> of a binary tree, return <em>an array of the largest value in each row</em> of the tree <strong>(0-indexed)</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/08/21/largest_e1.jpg" style="width: 300px; height: 172px;" />
<pre>
<strong>Input:</strong> root = [1,3,2,5,3,null,9]
<strong>Output:</strong> [1,3,9]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2,3]
<strong>Output:</strong> [1,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree will be in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n void solve(TreeNode* root,int depth,map<int,int> &mpp){\n\n if(!root){\n return;\n }\n\n if(mpp.find(depth)==mpp.end()){\n mpp[depth]=root->val;\n }\n else{\n mpp[depth]=max(mpp[depth],root->val);\n }\n\n solve(root->left,depth+1,mpp);\n solve(root->right,depth+1,mpp);\n }\n\n vector<int> largestValues(TreeNode* root) {\n\n map<int,int> mpp;\n\n solve(root,0,mpp);\n\n vector<int> ans;\n\n for(auto &it : mpp){\n ans.push_back(it.second);\n }\n\n return ans;\n }\n};",
"memory": "22200"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int longestPalindromeSubseq(string s) {\n string t = s;\n reverse(s.begin(), s.end());\n return lcs(s,t);\n }\n int lcs(string s, string t)\n {\n string temp;\n if (t.length() < s.length())\n {\n // swap the strings(should simply be a re-assigning of pointers)\n temp = s;\n s = t;\n t = temp;\n }\n int s_len = s.length();\n int t_len = t.length();\n int dp[2][t_len]; \n dp[0][0] = (s[0] == t[0]) ? 1 : 0;\n int j;\n for (j = 1; j < t_len ; j++)\n {\n dp[0][j] = (s[0] == t[j]) ? 1 : dp[0][j-1]; // Either match on your own, or ask if you've matched before\n }\n if (s_len > 1) dp[1][0] = (s[1] == t[0]) ? 1 : dp[0][0];\n for (int i = 1; i < s_len ; i++)\n {\n // cout << \"Current index 'i' is \" << i << endl;\n dp[1][0] = dp[0][0];\n if (s[i] == t[0])\n {\n dp[1][0] = 1;\n }\n for (j = 1; j < t_len ; j++)\n {\n // cout << \"Current index 'j' is \" << j << endl;\n if (s[i] == t[j])\n { // match\n dp[1][j] = 1 + dp[0][j-1];\n // cout << \"They match! Value of f(\" << i << \",\" << j << \"): \" << dp[1][j] << endl;\n }else\n {\n dp[1][j] = max(dp[1][j-1] , dp[0][j]);\n // cout << \"They don't match! Value of f(\" << i << \",\" << j << \"): \" << dp[1][j] << \" Computed from f(\" << i << \",\" << j-1 << \"): \" << dp[1][j-1] << \" and f(\" << i-1 << \",\" << j << \"): \" << dp[0][j] << endl;\n }\n }\n for (j = 0 ; j < t_len ; j++)\n {\n dp[0][j] = dp[1][j];\n }\n }\n return dp[0][t_len-1];\n }\n};",
"memory": "7700"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int longestPalindromeSubseq(string s) {\n int m = s.size();\n\n int tab[3][m];\n for (int i = 0; i < m; i++) {\n tab[0][i] = 0;\n tab[1][i] = 1;\n }\n int _2, _1, _0;\n for (int gap = 1; gap < m; gap++) {\n _2 = (gap + 1) % 3, _1 = (gap + 3) % 3, _0 = (gap - 1) % 3;\n for (int i = 0, j = gap; j < m; i++, j++) {\n if (s[i] == s[j]) {\n tab[_2][j] = tab[_0][j - 1] + 2;\n } else {\n tab[_2][j] = max(tab[_1][j], tab[_1][j - 1]);\n }\n }\n }\n return tab[m % 3][m - 1];\n }\n};",
"memory": "7800"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int longestPalindromeSubseq(string s) {\n int m = s.size();\n\n int tab[3][m];\n for (int i = 0; i < m; i++) {\n tab[0][i] = 0;\n tab[1][i] = 1;\n }\n int _2, _1, _0;\n for (int gap = 1; gap < m; gap++) {\n _2 = (gap + 1) % 3, _1 = (gap + 3) % 3, _0 = (gap - 1) % 3;\n for (int i = 0, j = gap; j < m; i++, j++) {\n if (s[i] == s[j]) {\n tab[_2][j] = tab[_0][j - 1] + 2;\n } else {\n tab[_2][j] = max(tab[_1][j], tab[_1][j - 1]);\n }\n }\n }\n return tab[m % 3][m - 1];\n }\n};",
"memory": "7800"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int longestPalindromeSubseq(string s) {\n std::array<std::vector<int>, 2> v;\n v[0] = (std::vector(s.size() + 1, 0));\n v[1] = (std::vector(s.size() + 1, -1));\n for (int o = 0; o < s.size(); ++o) {\n for (int ii = 0; ii < s.size() - o; ++ii) {\n v[1][ii] = std::exchange(v[0][ii], std::max({v[0][ii], v[0][ii + 1], v[1][ii + 1] + 2 * static_cast<int>(s[ii] == s[ii + o])}));\n } \n }\n\n return v[0][0];\n }\n};",
"memory": "7900"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\nint solveTab(string& a, string &b){\n vector<int> curr(b.length()+1, 0);\n vector<int> next(b.length()+1, 0);\n\n for(int i = a.length()-1; i>=0; i--){\n for(int j = b.length()-1; j>=0; j--){\n int ans = 0;\n if(a[i] == b[j]){\n ans = 1 + next[j+1];\n }\n else{\n ans = max(next[j], curr[j+1]);\n }\n curr[j] = ans;\n }\n next = curr;\n }\n return next[0];\n}\n int longestPalindromeSubseq(string s) {\n string revs = s;\n reverse(s.begin(), s.end());\n\n return solveTab(revs, s);\n }\n};",
"memory": "8000"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\n\n int solve(int start, int end, string& s, vector<vector<int>>& dp) {\n\n if(start == end)\n return 1;\n\n if(start > end)\n return 0;\n\n if(dp[start][end] != -1)\n return dp[start][end];\n\n int ans;\n\n if(s[start] == s[end])\n ans = 2 + solve(start+1, end-1, s, dp);\n else{\n\n ans = max(solve(start+1, end, s, dp), solve(start, end-1, s, dp));\n }\n\n return dp[start][end] = ans;\n }\npublic:\n int longestPalindromeSubseq(string s) {\n\n int n = s.length();\n\n vector<int> curr (n+1, 0);\n vector<int> next (n+1, 0);\n\n for(int start = n-1; start >= 0; start--){\n\n curr[start] = 1;\n\n for(int end = start+1; end < n; end++) {\n\n int ans;\n if(s[start] == s[end]){\n ans = 2 + next[end-1];\n }\n else{\n ans = max(next[end], curr[end-1]);\n }\n\n curr[end] = ans;\n }\n next = curr;\n }\n \n return next[n-1];\n }\n};",
"memory": "8000"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int solveUsingTabSO(string &a, string&b)\n {\n vector<int>next(a.size()+1,0);\n vector<int>curr(a.size()+1,0);\n \n for(int l = b.size()-1;l>=0;l--)\n {\n for(int k = a.size()-1;k>=0;k--)\n \n {\n int ans=0;\n if(a[k]==b[l])\n {\n ans = 1 + next[k+1];\n }\n else\n {\n ans = 0 + max(next[k],curr[k+1]);\n }\n\n curr[k]=ans;\n }\n next=curr;\n }\n return next[0];\n }\n int longestPalindromeSubseq(string s) {\n string a = s;\n reverse(a.begin(),a.end());\n int ans = solveUsingTabSO(s,a); // refer Longest common subsequnce leetcode question\n return ans;\n }\n};",
"memory": "8100"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int longestPalindromeSubseq(string s) {\n int n=s.length();\n string rev=s;\n reverse(rev.begin(),rev.end());\n vector<int> prev(n+1,0);\n vector<int> curr(n+1,0);\n for(int i=1;i<=n;i++){\n for(int j=1;j<=n;j++){\n if(s[i-1]==rev[j-1]){\n curr[j]=1+prev[j-1];\n }\n else{\n curr[j]=max(prev[j],curr[j-1]);\n }\n }\n prev=curr;\n }\n return prev[n];\n }\n};",
"memory": "8100"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\nprivate:\n int longestCommonSubsequence(string s1, string s2) {\n int n = s1.size();\n int m = s2.size();\n // vector<vector<int>> dp(n+1 , vector<int>(m+1 , 0));\n // return func(n, m , s1 , s2 , dp);\n vector<int> prev(m + 1, 0), curr(m + 1, 0);\n for (int ind2 = 0; ind2 <= m; ind2++)\n prev[ind2] = 0;\n // for(int ind1 = 0 ; ind1<= n ; ind1++) dp[ind1][0] = 0;\n for (int ind1 = 1; ind1 <= n; ind1++) {\n for (int ind2 = 1; ind2 <= m; ind2++) {\n if (s1[ind1 - 1] == s2[ind2 - 1])\n curr[ind2] = 1 + prev[ind2 - 1];\n else\n curr[ind2] = max(prev[ind2], curr[ind2 - 1]);\n }\n prev = curr;\n }\n return prev[m];\n }\n\npublic:\n int longestPalindromeSubseq(string s) {\n string t = s;\n reverse(s.begin() , s.end());\n return longestCommonSubsequence(s,t);\n }\n};",
"memory": "8200"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\nprivate:\n int longestCommonSubsequence(string s1, string s2) {\n int n = s1.size();\n int m = s2.size();\n // vector<vector<int>> dp(n+1 , vector<int>(m+1 , 0));\n // return func(n, m , s1 , s2 , dp);\n vector<int> prev(m + 1, 0), curr(m + 1, 0);\n for (int ind2 = 0; ind2 <= m; ind2++)\n prev[ind2] = 0;\n // for(int ind1 = 0 ; ind1<= n ; ind1++) dp[ind1][0] = 0;\n for (int ind1 = 1; ind1 <= n; ind1++) {\n for (int ind2 = 1; ind2 <= m; ind2++) {\n if (s1[ind1 - 1] == s2[ind2 - 1])\n curr[ind2] = 1 + prev[ind2 - 1];\n else\n curr[ind2] = max(prev[ind2], curr[ind2 - 1]);\n }\n prev = curr;\n }\n return prev[m];\n }\n\npublic:\n int longestPalindromeSubseq(string s) {\n string t = s;\n reverse(s.begin() , s.end());\n return longestCommonSubsequence(s,t);\n }\n};",
"memory": "8200"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int tabu(string &s1, string &s2, int n){\n vector<vector<int>> dp(n+1, vector<int>(n+1, 0));\n for(int i=1; i<=n; i++){\n for(int j=1; j<=n; j++){\n if(s1[i-1] == s2[j-1]) dp[i][j] = 1+dp[i-1][j-1];\n else dp[i][j] = max(dp[i-1][j], dp[i][j-1]);\n }\n }\n return dp[n][n];\n }\n int space(string &s1, string &s2, int n){\n vector<int> prv(n+1, 0), cur(n+1, 0);\n for(int i=1; i<=n; i++){\n for(int j=1; j<=n; j++){\n if(s1[i-1] == s2[j-1]) cur[j] = 1 + prv[j-1];\n else cur[j] = max(prv[j], cur[j-1]);\n }\n prv = cur;\n }\n return prv[n];\n }\n int memo(string &s1, string &s2, int i, int j, vector<vector<int>> &dp){\n if(i<0 || j<0) return 0;\n if(dp[i][j] != -1) return dp[i][j];\n if(s1[i] == s2[j]) return dp[i][j] = 1 + memo(s1, s2, i-1, j-1, dp);\n return dp[i][j] = max(memo(s1, s2, i-1, j, dp), memo(s1, s2, i, j-1, dp));\n }\n int longestPalindromeSubseq(string s) {\n int n = s.size();\n string s2 = s; reverse(s2.begin(), s2.end());\n // vector<vector<int>> dp(n, vector<int> (n, -1));\n // return memo(s, s2, n-1, n-1, dp);\n // return tabu(s, s2, n);\n return space(s, s2, n); \n }\n};",
"memory": "8300"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\nprivate:\n int solveSpaceOpt(string a, string b) {\n vector<int> curr(b.length() + 1, 0);\n vector<int> next(b.length() + 1, 0);\n\n for (int i = a.length() - 1; i >= 0; i--) {\n for (int j = b.length() - 1; j >= 0; j--) {\n int ans = 0;\n if (a[i] == b[j]) {\n ans = 1 + next[j + 1];\n }\n else {\n ans = max(next[j], curr[j + 1]);\n }\n curr[j] = ans;\n }\n next = curr;\n }\n return next[0];\n }\n\npublic:\n int longestPalindromeSubseq(string s) {\n string revStr = s;\n reverse(revStr.begin(), revStr.end());\n\n return solveSpaceOpt(s, revStr);\n }\n};",
"memory": "8300"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int space(string text1, string text2){\n //vector<vector<int>>dp(text1.length()+1 , vector<int>(text2.length()+1 , 0));\n vector<int>curr(text2.length()+1,0);\n vector<int>next(text2.length()+1,0);\n\n for(int i = text1.length()-1 ; i>=0 ; i--){\n for(int j = text2.length()-1 ; j>= 0 ; j--){\n int ans = 0;\n if(text1[i] == text2[j]){\n ans = 1+next[j+1];\n }\n else{\n ans = max(next[j] , curr[j+1]);\n }\n curr[j] = ans;\n \n }\n next = curr;\n }\n return next[0];\n }\n int longestPalindromeSubseq(string s) {\n string first = s;\n reverse(s.begin() , s.end());\n string second = s;\n return space(first,second);\n }\n};",
"memory": "8400"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int f(int ind1, int ind2, string s, string t)\n {\n if(ind1 < 0 or ind2 < 0)return 0;\n if(s[ind1] == t[ind2])return 1 + f(ind1-1, ind2-1, s, t);\n return max(f(ind1-1,ind2,s,t),f(ind1,ind2-1,s,t));\n }\n int longestPalindromeSubseq(string s) {\n string t = s;\n reverse(t.begin(),t.end());\n // return f(s.size()-1, s.size()-1,s,t);\n int n = s.size();\n vector<int> prev(n+1,0), curr(n+1,0);\n // vector<vector<int>> dp(n+1,vector<int>(n+1,0));\n for(int ind1 = 1; ind1 <= n; ind1++)\n {\n for(int ind2 = 1; ind2 <= n; ind2++)\n {\n if(s[ind1-1] == t[ind2-1])curr[ind2] = 1 + prev[ind2-1];\n else curr[ind2] = max(prev[ind2],curr[ind2-1]);\n }\n prev = curr;\n }\n return prev[n];\n }\n};",
"memory": "8400"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "\nint solve(string text1, string text2) {\n int i1 = text1.length(), j1 = text2.length();\n vector<int> prev(i1 + j1 + 1, 0), cur(i1 + j1 + 1, 0);\n for (int i = 1; i <= i1; i++) {\n for (int j = 1; j <= j1; j++) {\n if (text1[i - 1] == text2[j - 1])\n cur[j] = 1 + prev[j - 1];\n else\n cur[j] = max(prev[j], cur[j - 1]);\n }\n prev = cur;\n }\n return prev[j1];\n}\n\nclass Solution {\npublic:\n int longestPalindromeSubseq(string s) {\n string reverseS = s;\n reverse(s.begin(), s.end());\n return solve(s, reverseS);\n }\n};",
"memory": "8500"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n\nint f(string s, string t) {\n int n = s.size();\n int m = t.size();\n \n vector<int> prev(m+1, 0), cur(m+1, 0); // Only two 1D arrays for space optimization\n \n for (int i = 1; i <= n; i++) {\n for (int j = 1; j <= m; j++) {\n if (s[i-1] == t[j-1]) {\n cur[j] = 1 + prev[j-1];\n } else {\n cur[j] = max(prev[j], cur[j-1]);\n }\n }\n prev = cur; // Move current row to previous row for the next iteration\n }\n \n return prev[m];\n}\n\nint lcs(string s, string t){\n int n=s.size();\n int m=t.size();\n vector<int> cur(m+1);\n vector<int> prev(m+1);\n return f(s,t);\n}\n int longestPalindromeSubseq(string s) {\n int n=s.size();\n string t=s;\n reverse(t.begin(),t.end());\n return lcs(s,t);\n }\n};",
"memory": "8600"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "// class Solution {\n// public:\n// int longestPalindromeSubseq(string &s) {\n\n// vector<vector<int>> dp(s.size(), vector<int>(s.size(), 0));\n \n// for (int i = s.size() - 1; i >= 0; i--) {\n\n// dp[i][i] = 1;\n\n// for (int j = i + 1; j < s.size(); j++) {\n\n// int lengthIfMatch = 0, lengthIfSkipStart = 0, lengthIfSkipEnd = 0;\n \n// if (s[i] == s[j]) {\n// lengthIfMatch = 2 + dp[i + 1][j - 1];\n// } \n// else {\n// lengthIfSkipStart = dp[i + 1][j];\n// lengthIfSkipEnd = dp[i][j - 1];\n// }\n// dp[i][j] = max({lengthIfMatch, lengthIfSkipStart, lengthIfSkipEnd});\n// }\n// }\n// return dp[0][s.size() - 1];\n// }\n// };\n\n\n\n\nclass Solution {\npublic:\n int longestPalindromeSubseq(const string &s) {\n\n vector<int> curr(1001, 0);\n vector<int> next(1001, 0);\n \n for (int start = s.size() - 1; start >= 0; start--) {\n\n curr[start] = 1; // Single character is always a palindrome of length 1\n\n for (int end = start + 1; end < s.size(); end++) {\n \n int lengthIfMatch = 0, lengthIfSkipStart = 0, lengthIfSkipEnd = 0;\n \n // If characters at start and end indices are the same\n if (s[start] == s[end]) {\n // Characters are the same\n lengthIfMatch = 2 + next[end - 1];\n } \n else { \n lengthIfSkipStart = next[end];\n\n lengthIfSkipEnd = curr[end - 1];\n }\n curr[end] = max({lengthIfMatch, lengthIfSkipStart, lengthIfSkipEnd});\n }\n next = curr;\n }\n // The answer is stored in next[s.size() - 1], which covers the entire string\n return next[s.size() - 1];\n }\n};\n",
"memory": "8700"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "// class Solution {\n// public:\n// int longestPalindromeSubseq(string &s) {\n\n// vector<vector<int>> dp(s.size(), vector<int>(s.size(), 0));\n \n// for (int i = s.size() - 1; i >= 0; i--) {\n\n// dp[i][i] = 1;\n\n// for (int j = i + 1; j < s.size(); j++) {\n\n// int lengthIfMatch = 0, lengthIfSkipStart = 0, lengthIfSkipEnd = 0;\n \n// if (s[i] == s[j]) {\n// lengthIfMatch = 2 + dp[i + 1][j - 1];\n// } \n// else {\n// lengthIfSkipStart = dp[i + 1][j];\n// lengthIfSkipEnd = dp[i][j - 1];\n// }\n// dp[i][j] = max({lengthIfMatch, lengthIfSkipStart, lengthIfSkipEnd});\n// }\n// }\n// return dp[0][s.size() - 1];\n// }\n// };\n\n\n\n\nclass Solution {\npublic:\n int longestPalindromeSubseq(const string &s) {\n\n vector<int> curr(1001, 0);\n vector<int> next(1001, 0);\n \n for (int start = s.size() - 1; start >= 0; start--) {\n\n curr[start] = 1; // Single character is always a palindrome of length 1\n\n for (int end = start + 1; end < s.size(); end++) {\n \n int lengthIfMatch = 0, lengthIfSkipStart = 0, lengthIfSkipEnd = 0;\n \n // If characters at start and end indices are the same\n if (s[start] == s[end]) {\n // Characters are the same\n lengthIfMatch = 2 + next[end - 1];\n } \n else { \n lengthIfSkipStart = next[end];\n\n lengthIfSkipEnd = curr[end - 1];\n }\n curr[end] = max({lengthIfMatch, lengthIfSkipStart, lengthIfSkipEnd});\n }\n next = curr;\n }\n // The answer is stored in next[s.size() - 1], which covers the entire string\n return next[s.size() - 1];\n }\n};\n",
"memory": "8800"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "// class Solution {\n// public:\n// int longestPalindromeSubseq(string &s) {\n\n// vector<vector<int>> dp(s.size(), vector<int>(s.size(), 0));\n \n// for (int i = s.size() - 1; i >= 0; i--) {\n\n// dp[i][i] = 1;\n\n// for (int j = i + 1; j < s.size(); j++) {\n\n// int lengthIfMatch = 0, lengthIfSkipStart = 0, lengthIfSkipEnd = 0;\n \n// if (s[i] == s[j]) {\n// lengthIfMatch = 2 + dp[i + 1][j - 1];\n// } \n// else {\n// lengthIfSkipStart = dp[i + 1][j];\n// lengthIfSkipEnd = dp[i][j - 1];\n// }\n// dp[i][j] = max({lengthIfMatch, lengthIfSkipStart, lengthIfSkipEnd});\n// }\n// }\n// return dp[0][s.size() - 1];\n// }\n// };\n\n\n\n\nclass Solution {\npublic:\n int longestPalindromeSubseq(const string &s) {\n\n vector<int> curr(1001, 0);\n vector<int> next(1001, 0);\n \n for (int start = s.size() - 1; start >= 0; start--) {\n\n curr[start] = 1; // Single character is always a palindrome of length 1\n\n for (int end = start + 1; end < s.size(); end++) {\n \n int lengthIfMatch = 0, lengthIfSkipStart = 0, lengthIfSkipEnd = 0;\n \n // If characters at start and end indices are the same\n if (s[start] == s[end]) {\n // Characters are the same\n lengthIfMatch = 2 + next[end - 1];\n } \n else { \n lengthIfSkipStart = next[end];\n\n lengthIfSkipEnd = curr[end - 1];\n }\n curr[end] = max({lengthIfMatch, lengthIfSkipStart, lengthIfSkipEnd});\n }\n next = curr;\n }\n // The answer is stored in next[s.size() - 1], which covers the entire string\n return next[s.size() - 1];\n }\n};\n",
"memory": "9000"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n\n int lcs(string s1, string s2) {\n int n=s1.size();\n int m=s2.size();\n\n // vector<vector<int>> dp(n+1,vector<int>(m+1,0));\n vector<int> prev(m+1,0);\n vector<int> cur(m+1,0);\n\n\n for(int i=1;i<=n;i++){\n for(int j=1;j<=m;j++){\n if(s1[i-1]==s2[j-1]){\n cur[j]=1+prev[j-1];\n\n }\n else cur[j] = max(prev[j] , cur[j-1]);\n }\n prev=cur;\n \n }\n return prev[m];\n }\n\n int longestPalindromeSubseq(string s) {\n stack <int> st;\n string t=\"\";\n for(auto it:s){\n st.push(it);\n\n }\n\n while(!st.empty()){\n t+=st.top();\n st.pop();\n }\n\n return lcs(s,t);\n \n }\n};",
"memory": "9700"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\n\n const int cap = 1001;\n\npublic:\n int longestPalindromeSubseq(string s) {\n\n using u16=unsigned short;\n\n u16 dp[cap][cap];\n memset(dp, 0, sizeof(dp));\n\n if (s.size() == 1)\n return 1;\n\n auto m=s.size();\n auto t=s;\n reverse(t.begin(), t.end());\n\n for (int i=1; i<m+1; i++) {\n for (int j=1; j<m+1; j++) {\n if (s[i-1]==t[j-1]) {\n dp[i][j] = dp[i-1][j-1] + 1;\n } else {\n dp[i][j] = max(dp[i-1][j], dp[i][j-1]);\n }\n }\n }\n \n return dp[m][m];\n }\n};",
"memory": "10100"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int longestPalindromeSubseq(string s) {\n string r = s;\n reverse(s.begin(), s.end());\n return lcs(s, r);\n }\n\n int lcs(string a, string b) {\n short m[1001][1001] = {};\n for (auto i = 0; i < a.size(); ++i)\n for (auto j = 0; j < b.size(); ++j)\n m[i + 1][j + 1] = a[i] == b[j] ? m[i][j] + 1 : max(m[i + 1][j], m[i][j + 1]);\n return m[a.size()][b.size()];\n}\n};",
"memory": "10200"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "int dp[1001][1001];\nclass Solution {\npublic:\n int longestPalindromeSubseq(string s) {\n int n = s.size();\n //vector<vector<int>> dp(n + 1, vector<int>(n + 1));\n for (int i = 1; i <= n; i++) {\n for (int j = 1; j <= n; j++) {\n dp[i][j] = max({dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1] + (s[i - 1] == s[n - j])});\n }\n }\n return dp[n][n];\n }\n};",
"memory": "11600"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "int dp[1001][1001];\nclass Solution {\npublic:\n int util(string& s, int i, int j) {\n if (i > j) {\n return 0;\n }\n if (i == j) {\n return 1;\n }\n if (dp[i][j] != -1) return dp[i][j]; \n if (s[i] == s[j]) {\n dp[i][j] = 2 + util(s, i + 1, j - 1);\n } else {\n dp[i][j] = max(util(s, i + 1, j), util(s, i, j - 1));\n }\n return dp[i][j];\n }\n int longestPalindromeSubseq(string s) {\n memset(dp, -1, sizeof(dp));\n int n = s.size();\n return util(s, 0, n - 1);\n }\n};",
"memory": "11700"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "#include <cstring>\n\nint memo[1000][1000];\n\nclass Solution {\n string s;\n\n int compute(int i, int j) {\n if (i >= j) return i == j;\n \n int& res = memo[i][j];\n if (res != -1) return res;\n\n if (s[i] == s[j]) return res = 2 + compute(i + 1, j - 1);\n return res = max(compute(i + 1, j), compute(i, j - 1));\n }\n\npublic:\n \n int longestPalindromeSubseq(string s) {\n int n = s.size();\n this->s = move(s);\n memset(memo, -1, sizeof(memo));\n return compute(0, n - 1);\n }\n};",
"memory": "11800"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int longestPalindromeSubseq(string s) {\n int n = s.length();\n int dp[n][n];\n memset(dp,0,sizeof(dp));\n \n for(int i=0;i<n;i++){\n dp[i][i] = 1;\n }\n \n for(int l=2;l<=n;l++){\n for(int i=0;i<=n-l;i++){\n int j = i+l-1;\n if(s[i]==s[j] && l==2){\n dp[i][j] = 2; \n }\n else if(s[i]==s[j]){\n dp[i][j] = 2 + dp[i+1][j-1];\n }else{\n dp[i][j] = max(dp[i][j-1],dp[i+1][j]);\n }\n }\n }\n \n return dp[0][n-1];\n }\n};",
"memory": "11900"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int longestPalindromeSubseq(string s) {\n string t = s;\n reverse(t.begin(), t.end());\n\n int n = s.size();\n\n int dp[n+1][n+1];\n for(int i=0; i<n; i++) dp[0][i] = 0;\n for(int i=0; i<n; i++) dp[i][0] = 0;\n\n for(int i=1; i<=n; i++) {\n for(int j=1; j<=n; j++) {\n if(s[i-1] == t[j-1]) {\n dp[i][j] = 1 + dp[i-1][j-1];\n }\n else{\n dp[i][j] = max(dp[i-1][j], dp[i][j-1]);\n }\n }\n }\n\n return dp[n][n];\n\n }\n};",
"memory": "12000"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int n,m;\n int dp[1001][1001];\n int rec(int i,int j,string &text1,string &text2){\n // pruning\n if(i>=n || j>=m) return 0;\n // basecase\n\n // cache check\n if(dp[i][j]!=-1) return dp[i][j]; \n // compute\n int ans = 0;\n if(text1[i]==text2[j]) ans = max(ans,1+rec(i+1,j+1,text1,text2));\n else ans = max(rec(i+1,j,text1,text2),rec(i,j+1,text1,text2));\n \n // save and return\n dp[i][j] = ans;\n return ans;\n }\n int longestPalindromeSubseq(string text2) {\n string text1 = text2;\n reverse(text2.begin(),text2.end());\n this->n = text1.size();\n this->m = text2.size();\n memset(dp,-1,sizeof(dp));\n int ans = rec(0,0,text1,text2);\n return ans;\n }\n};",
"memory": "12400"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int dp[1001][1001];\n int solve(int i, int j, string& s){\n\n if(i>j){\n return 0;\n }\n if(i==j){\n return 1;\n }\n\n if(dp[i][j] != -1){\n return dp[i][j];\n }\n \n if(s[i] == s[j]){\n return dp[i][j] = 2 + solve(i+1, j-1, s);\n }\n\n int take_i = solve(i, j-1, s);\n int take_j = solve(i+1, j, s);\n\n return dp[i][j] = max(take_i, take_j);\n }\n int longestPalindromeSubseq(string s) {\n memset(dp, -1, sizeof(dp));\n int n = s.size();\n return solve(0, n-1, s);\n }\n};",
"memory": "12400"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int dp[1001][1001];\n int solve(int i, int j, string& s1, string& s2){\n\n if(i==s1.length() || j == s2.length()){\n return 0;\n }\n if(dp[i][j] != -1){\n return dp[i][j];\n }\n\n if(s1[i] == s2[j]){\n return dp[i][j] = 1 + solve(i+1, j+1, s1, s2);\n }\n\n int take1 = solve(i, j+1, s1, s2);\n int take2 = solve(i+1, j, s1, s2);\n\n return dp[i][j] = max(take1, take2);\n }\n int longestPalindromeSubseq(string s) {\n memset(dp, -1, sizeof(dp));\n int n = s.length();\n string s2 = \"\";\n\n for(int i=n-1; i>=0; i--){\n s2 += s[i];\n }\n\n return solve(0, 0, s, s2);\n }\n};",
"memory": "12500"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\n int dp[1001][1001]; \n\nprivate:\n int LCS(string& s, string& rev_s, int n, int m) {\n if (n == 0 || m == 0) {\n return 0;\n }\n if (dp[n][m] != -1) {\n return dp[n][m]; \n }\n if (s[n - 1] == rev_s[m - 1]) {\n return dp[n][m] = 1 + LCS(s, rev_s, n - 1, m - 1);\n } else {\n return dp[n][m] = max(LCS(s, rev_s, n, m - 1),\n LCS(s, rev_s, n - 1, m));\n }\n }\n\npublic:\n int longestPalindromeSubseq(string s) {\n memset(dp, -1, sizeof(dp));\n string rev_s = s; \n reverse(rev_s.begin(), rev_s.end()); \n int n = s.size();\n return LCS(s, rev_s, n, n); \n }\n};\n",
"memory": "12500"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\n int dp[1005][1005];\n vector<int>places[26];\n string s;\n int searchindex(int num, int end){\n int l = 0, r = places[num].size(), g = (l+r)/2;\n while(l + 1 < r){\n g = (l+r)/2;\n if(places[num][g] > end) r = g;\n else l = g;\n }\n return places[num][l];\n }\n\n int findans( int start, int end){\n if(start > end)return 0;\n if(dp[start][end] != -1)return dp[start][end];\n int ans = 1;\n int num = s[start]-'a';\n int l = 0, r = places[num].size(), g;\n while(l + 1 < r){\n g = (l+r)/2;\n if(places[num][g] > end) r = g;\n else l = g;\n }\n int last = places[num][l];\n\n if(last > start){\n ans = max(ans, 2 + findans(start+1, last -1));\n }\n\n ans = max(ans, findans(start+1, end));\n dp[start][end] = ans;\n return ans;\n }\npublic:\n int longestPalindromeSubseq(string s1) {\n s = s1;\n for(int i=0;i<1005;i++){\n for(int j=0;j<1005;j++)dp[i][j] = -1;\n }\n for(int i=0;i<s.size();i++){\n places[s[i]-'a'].push_back(i);\n }\n return findans(0, s.size()-1);\n }\n};",
"memory": "12600"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int dp[1001][1001];\n // int solve(int ind1,int ind2,string &s, string &t){\n // if(ind1<0 || ind2<0) return 0;\n // if(s[ind1]==t[ind2]) return 1+solve(ind1-1,ind2-1,s,t);\n // if(dp[ind1][ind2]!=-1) return dp[ind1][ind2];\n // return dp[ind1][ind2]=max(solve(ind1-1,ind2,s,t),solve(ind1,ind2-1,s,t));\n // }\n int longestPalindromeSubseq(string s) {\n int n=s.size();\n string t=s;\n reverse(t.begin(),t.end());\n memset(dp,0,sizeof(dp));\n vector<int>prev(n+1,0);\n vector<int>curr(n+1,0);\n for(int i=1;i<=n;i++){\n for(int j=1;j<=n;j++){\n if(s[i-1]==t[j-1]) curr[j]=1+prev[j-1];\n else curr[j]=max(prev[j],curr[j-1]);\n }\n prev=curr;\n }\n \n return prev[n];\n }\n};",
"memory": "12700"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "int dp[1001][1001];\nclass Solution {\npublic:\n int ans(int i,int j,string& s)\n {\n if(i>j)return 0;\n if(i==j)return 1;\n if(dp[i][j]!=-1)return dp[i][j];\n if(s[i]==s[j])\n return dp[i][j]=max({ans(i+1,j-1,s)+2,ans(i+1,j,s),ans(i,j-1,s)});\n return dp[i][j]=max({ans(i,j-1,s),ans(i+1,j,s)});\n }\n int longestPalindromeSubseq(string s) {\n \n int n=s.length();\n for(int i=0;i<n;i++)\n for(int j=0;j<n;j++)\n dp[i][j]=-1;\n return ans(0,n-1,s);\n }\n};",
"memory": "12900"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "int dp[1001][1001];\nclass Solution {\npublic:\n int ans(int i,int j,string& s)\n {\n if(i>j)return 0;\n if(i==j)return 1;\n if(dp[i][j]!=-1)return dp[i][j];\n if(s[i]==s[j])\n return dp[i][j]=max({ans(i+1,j-1,s)+2,ans(i+1,j,s),ans(i,j-1,s)});\n return dp[i][j]=max({ans(i,j-1,s),ans(i+1,j,s)});\n }\n int longestPalindromeSubseq(string s) {\n \n int n=s.length();\n for(int i=0;i<n;i++)\n for(int j=0;j<n;j++)\n dp[i][j]=-1;\n return ans(0,n-1,s);\n }\n};",
"memory": "12900"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "int dp[1001][1001];\nstring s,t;\nint rec(int i,int j){\n\n if(i< 0 || j < 0) return 0;\n if(dp[i][j]!=-1) return dp[i][j];\n\n int ans = 0;\n if(s[i]==t[j]){\n ans = max(ans,1 + rec(i-1,j-1));\n }\n ans = max({ans,rec(i,j-1),rec(i-1,j)});\n\n return dp[i][j]=ans;\n}\nclass Solution {\npublic:\n int longestPalindromeSubseq(string p) {\n s = p;\n reverse(p.begin(),p.end());\n t = p;\n memset(dp,-1,sizeof(dp));\n int n = s.size();\n return rec(n-1,n-1);\n }\n};",
"memory": "13200"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n\n int longestPalindromeSubseq(string s) {\n int n = s.size();\n int dp[1001][1001];\n memset(dp, -1,sizeof(dp));\n function<int(int,int)> find = [&](int i, int j)->int {\n if (i>j) return 0;\n int& res = dp[i][j];\n if (res != -1) return res;\n int ans= 0;\n if (s[i] == s[j]) {\n if (i==j) {\n ans += 1;\n } else {\n ans = max(ans, 2 + find(i+1, j-1));\n }\n }\n // ko chon vi tri i, chon vi tri j\n ans = max(ans, find(i+1, j));\n // chon i, ko chon j\n ans = max(ans, find(i, j-1));\n return res = ans;\n };\n return find(0, n-1);\n }\n};",
"memory": "13300"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int dp[1001][1001];\n int f(int i, int j, string& s){\n if(i == j) return 1;\n if(i > j) return 0;\n\n if(dp[i][j] != -1) return dp[i][j];\n\n int take = INT_MIN;\n if(s[i] == s[j]){\n take = 2 + f(i+1,j-1,s);\n }\n int skipi = f(i+1,j,s);\n int skipj = f(i,j-1,s);\n return dp[i][j] = max({take,skipi,skipj});\n }\n int longestPalindromeSubseq(string s) {\n int n = s.size();\n memset(dp,-1,sizeof(dp));\n return f(0,n-1,s);\n }\n};",
"memory": "13500"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\n private:\nint dp[1001][1001];\nint m,n;\nstring s1,s2;\n\nint rec(int i, int j){\n // pruning\n\n // basecase\n if(i == m || j == n){\n return 0;\n }\n\n // cache check\n if(dp[i][j] != -1){\n return dp[i][j];\n }\n\n // transition\n int ans = 0;\n if(s1[i] == s2[j]){\n ans = max(ans, 1+rec(i+1,j+1));\n }\n else {\n ans = max({ans, rec(i+1,j), rec(i,j+1)});\n }\n\n // save and return\n return dp[i][j] = ans;\n}\npublic:\n int longestPalindromeSubseq(string s) {\n s1 = s;\n reverse(s.begin(),s.end());\n s2 = s; \n n = s.size();\n m = n;\n\n memset(dp,-1,sizeof(dp));\n\n return rec(0,0);\n }\n};",
"memory": "13600"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\n private:\nint dp[1001][1001];\nint m,n;\nstring s1,s2;\n\nint rec(int i, int j){\n // pruning\n\n // basecase\n if(i == m || j == n){\n return 0;\n }\n\n // cache check\n if(dp[i][j] != -1){\n return dp[i][j];\n }\n\n // transition\n int ans = 0;\n if(s1[i] == s2[j]){\n ans = max(ans, 1+rec(i+1,j+1));\n }\n else {\n ans = max({ans, rec(i+1,j), rec(i,j+1)});\n }\n\n // save and return\n return dp[i][j] = ans;\n}\npublic:\n int longestPalindromeSubseq(string s) {\n s1 = s;\n reverse(s.begin(),s.end());\n s2 = s; \n n = s.size();\n m = n;\n\n memset(dp,-1,sizeof(dp));\n\n return rec(0,0);\n }\n};",
"memory": "13700"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\n int dp[1001][1001];\n int solve(string &s, string &s1,int index1,int index2){\n if(index1 >= s.size() || index2 >= s1.size()){\n return 0;\n }\n if(dp[index1][index2]!=-1) return dp[index1][index2];\n int a = INT_MIN;\n if(s[index1]==s1[index2]){\n a = 1+ solve(s,s1,index1+1,index2+1);\n }\n int b = solve(s,s1,index1+1,index2);\n int c = solve(s,s1,index1,index2+1);\n\n\n return dp[index1][index2]=max({a,b,c});\n }\npublic:\n int longestPalindromeSubseq(string s) {\n string s1 = s;\n reverse(s1.begin(),s1.end());\n memset(dp,-1,sizeof(dp));\n return solve(s,s1,0,0);\n \n }\n};",
"memory": "13800"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "int pd[3005][3005];\n\nint LCS(string a, string b)\n{\n for(int i = 1; i <= a.size(); i++)\n for(int j = 1; j <= b.size(); j++)\n if(a[i-1] == b[j-1])\n pd[i][j] = pd[i-1][j-1] + 1;\n else\n pd[i][j] = max(pd[i][j-1], pd[i-1][j]);\n return pd[a.size()][b.size()];\n}\n\nclass Solution {\npublic:\n int longestPalindromeSubseq(string s) {\n string b=s;\n reverse(b.begin(), b.end());\n int lcs = LCS(s, b);\n return lcs;\n \n }\n};",
"memory": "15900"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n #define ll long long\n int longestPalindromeSubseq(string s) {\n ll n=s.size();\n if(n==1)return 1;\n if(n==2){\n return (s[0]==s[1])?2:1;\n }\n ll ans = 1;\n ll dp[n][n];\n for(ll i=0;i<n;i++){\n dp[i][i]=1;\n if(i<n-1){\n dp[i][i+1] = (s[i]==s[i+1])?2:1;\n ans = max(ans,dp[i][i+1]);\n }\n }\n for(ll l=3;l<=n;l++){\n for(ll i=0;i+l-1<n;i++){\n dp[i][i+l-1] = max(dp[i][i+l-2],dp[i+1][i+l-1]);\n if(s[i]==s[i+l-1]){\n dp[i][i+l-1] = max(dp[i][i+l-1],dp[i+1][i+l-2]+2);\n }\n ans = max(ans,dp[i][i+l-1]);\n }\n }\n return ans;\n }\n};",
"memory": "16500"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution \n{\npublic:\n typedef long long ll;\n ll dp[1001][1001];\n int longestPalindromeSubseq(string s) \n {\n ll n = s.size();\n string r = s;\n reverse(s.begin(), s.end());\n \n\n for(ll i = 1; i <= n; i++)\n {\n for(ll j = 1; j <= n; j++)\n {\n if(s[i-1] == r[j-1])\n dp[i][j] = 1+dp[i-1][j-1];\n\n else\n dp[i][j] = max(dp[i][j-1], dp[i-1][j]);\n }\n }\n\n return dp[n][n];\n }\n};",
"memory": "16600"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\nint dp[1000][1000];\n Solution(){\n memset(dp,-1,sizeof(dp));\n }\n int LCS(string text1,string text2,int m,int n){\n if(n==0||m==0){\n return 0;\n }\n if(dp[m][n]!=-1){\n return dp[m][n];\n }\n if(text1[m-1]==text2[n-1]){\n return dp[m][n]=1+LCS(text1,text2,m-1,n-1);\n }\n else{\n //either you take last character of 1st and dont take of 2nd \n //or either you take last character of 2nd and dont take of 1st\n int a=LCS(text1,text2,m-1,n);\n int b=LCS(text1,text2,m,n-1);\n return dp[m][n]=max(a,b);\n }\n }\n int longestPalindromeSubseq(string s) {\n string s1=s;\n reverse(s.begin(),s.end());\n string s2=s;\n //return LCS(s1,s2,s.size(),s.size());\n //find the lcs of both it will be same we are reversing string \n //lps of 1st strig will be same as lpos of second which is also lcs of both\n int n=s.size();\n int m=s.size();\n int dp[n+1][m+1];//dp meaning store lcs of strting n elements of str1 and m of str2\n for(int i=0;i<=n;i++){\n for(int j=0;j<=m;j++){\n if(i==0||j==0){\n dp[i][j]=0;\n }\n else if(s1[i-1]==s2[j-1]){\n dp[i][j]=1+dp[i-1][j-1];\n }\n else{\n dp[i][j]=max(dp[i][j-1],dp[i-1][j]);\n }\n }\n }\n return dp[n][m];\n }\n};",
"memory": "16700"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\nint t[1001][1001];\n// int LCS(string & s, string & a, int n, int m){\n// if(n==0 || m==0) return 0;\n// if(t[n][m]!=-1) return t[n][m];\n// if(s[n-1]==a[m-1]){\n// return t[n][m]= 1+ LCS(s,a,n-1,m-1);\n// }else{\n// return t[n][m]= max(LCS(s,a,n-1,m),LCS(s,a,n,m-1));\n// }\n// }\n\n int LCS(string text1, string text2) {\n int n=text1.size();\n int m=text2.size();\n int t[n+1][m+1];\n \n for(int i=0;i<n+1;i++){\n for(int j=0;j<m+1;j++){\n if(i==0 || j==0){\n t[i][j]=0;\n }\n }\n }\n\n // if(n==0 || m==0) return 0;\n // if(t[n][m]!=-1) return t[n][m];\n for(int i=1;i<n+1;i++){\n for(int j=1;j<m+1;j++){\n\n if(text1[i-1]==text2[j-1]) {\n t[i][j]= 1+ t[i-1][j-1];\n }\n else{\n t[i][j]= max(t[i-1][j], t[i][j-1]);\n }\n\n\n }\n }\n \n return t[n][m];\n \n }\n \n\n int longestPalindromeSubseq(string s) {\n memset(t,-1,sizeof(t));\n int n=s.size();\n string reversed(s.rbegin(), s.rend());\n // string a= reverse(s.begin(),s.end());\n // int m= a.size();\n return LCS(s,reversed);\n\n \n }\n};",
"memory": "16800"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "\nclass Solution {\npublic:\n int dp[1500][1500] = {0};\n int longestPalindromeSubseq(string s) {\n int i, j, k, len, l, r, bracket, ok;\n string ans = \"\", ret = \"\";\n for (i=0; i<s.size(); i++){\n dp[i][i]=1;\n }\n for (i=1; i<s.size(); i++){\n dp[i-1][i]=1+(s[i-1]==s[i]);\n }\n for (i=2; i<s.size(); i++){\n for (j=0; (j+i)<s.size(); j++){\n //j = start\n //i+1 = length (e.g. 0->2, i=2 but length is 3)\n k = j+i; //j is end, inclusive\n dp[j][k] = (s[j]==s[k])*2 + dp[j+1][k-1];\n //cout<<j<<\" \"<<k<<\" \"<<dp[j][k]<<\"\\n\";\n dp[j][k] = max(dp[j][k], dp[j+1][k]);\n //cout<<j<<\" \"<<k<<\" \"<<dp[j][k]<<\"\\n\";\n dp[j][k] = max(dp[j][k], dp[j][k-1]);\n //cout<<j<<\" \"<<k<<\" \"<<dp[j][k]<<\"\\n\";\n }\n }\n /*\n cout<<\"ans \"<<dp[0][s.size()-1]<<\"\\n\";;\n len = dp[0][s.size()-1];\n\n l = 0;\n r = s.size()-1;\n while (l < r){\n ok = 0;\n bracket = r - l; //exactly 1 smaller than l..r inclusive\n cout<<\"l \"<<l<<\" r \"<<r<<\" bracket \"<<bracket<<\"\\n\";\n for (i=l; i+bracket-1<=r; i++){\n cout<<\"check \"<<i<<\" \"<<(i+bracket-1)<<\" \"<<dp[i][i+bracket-1]<<\"\\n\";\n if (dp[i][i+bracket-1] == len){\n l = i;\n r = i+bracket-1;\n bracket--;\n ok = 1;\n break;\n }\n }\n if (!ok){\n ret += s[l];\n cout<<\"add \"<<s[l]<<\" \"<<s[r]<<\"\\n\";\n len -= 2;\n l++;\n r--;\n }\n }\n //bracket is supposed to be 0 now\n ans = ret;\n reverse(ret.begin(), ret.end());\n if (len){\n ans += s[l];\n }\n ans += ret;\n */\n \n return dp[0][s.size()-1];\n\n }\n};",
"memory": "17800"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int longestPalindromeSubseq(string s) {\n string s2;\n \n int n=s.size();\n int m=s.size();\n for(int i=n-1;i>=0;i--){\n s2 = s2 + s[i];\n }\n \n vector<int>prev(m+1,0);\n vector<int>curr(m+1,0);\n \n for(int i=1;i<=n;i++){\n for(int j=1;j<=m;j++){\n if(s[i-1] == s2[j-1]){\n curr[j] = 1 + prev[j-1];\n }\n else{\n curr[j] = max(prev[j], curr[j-1]);\n }\n }\n prev = curr;\n }\n\n return prev[m];\n \n }\n};",
"memory": "18000"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int longestPalindromeSubseq(string s) {\n string t = \"\";\n int n = s.size();\n for(int i = n-1; i >= 0; i--){\n t = t + s[i];\n }\n\n vector<int> prev(n+1, 0), curr(n+1, 0);\n\n for(int j = 0; j <= n; j++) prev[j] = 0;\n\n for(int i = 1; i <= n; i++){\n for(int j = 1; j <= n; j++){\n // match\n if(s[i-1] == t[j-1]){\n curr[j] = 1 + prev[j-1];\n }\n else{\n // not match\n curr[j] = 0 + max(prev[j], curr[j-1]);\n }\n }\n prev = curr;\n }\n\n return prev[n];\n }\n};",
"memory": "18100"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\nint longestCommonSubsequence(string t1, string t2) {\n \n int m=t1.length();\n int n=t2.length(); \n vector<int>prev(n+1,0);\n vector<int>curr(n+1,0);\n for(int i=1;i<=m;i++)\n {\n for(int j=1;j<=n;j++)\n {\n if(t1[i-1]==t2[j-1])\n curr[j]=1+prev[j-1];\n else\n curr[j]=max(prev[j],curr[j-1]);\n }\n prev=curr;\n }\n return curr[n];\n }\n int longestPalindromeSubseq(string s) {\n string s1=\"\";\n for(int i=0;i<s.length();i++)\n s1=s[i]+s1;\n return longestCommonSubsequence(s1,s);\n }\n};",
"memory": "18200"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int lpsR(string s,string t,int ind1,int ind2){\n if(ind1==0 || ind2==0){\n return 0;\n }\n if(s[ind1-1]==t[ind2-1]){\n return 1+lpsR(s,t,ind1-1,ind2-1);\n }\n return max(lpsR(s,t,ind1-1,ind2),lpsR(s,t,ind1,ind2-1));\n }\n int lpsM(string s,string t,int ind1,int ind2,vector<vector<int>> &dp){\n if(ind1==0 || ind2==0){\n return 0;\n }\n if(dp[ind1][ind2]!=-1){\n return dp[ind1][ind2];\n }\n if(s[ind1-1]==t[ind2-1]){\n return dp[ind1][ind2]=1+lpsM(s,t,ind1-1,ind2-1,dp);\n }\n return dp[ind1][ind2]=max(lpsM(s,t,ind1-1,ind2,dp),lpsM(s,t,ind1,ind2-1,dp));\n }\n int lpsT(string s,string t){\n vector<vector<int>> dp(s.size()+1,vector<int>(t.size()+1,0));\n for(int ind1=1;ind1<=s.size();ind1++){\n for(int ind2=1;ind2<=t.size();ind2++){\n if(s[ind1-1]==t[ind2-1]){\n dp[ind1][ind2]=1+dp[ind1-1][ind2-1];\n }\n else{\n dp[ind1][ind2]=max(dp[ind1-1][ind2],dp[ind1][ind2-1]);\n }\n }\n }\n return dp[s.size()][t.size()];\n }\n int lpsO(string s,string t){\n vector<int> dp(t.size()+1,0),temp(t.size()+1,0);\n for(int ind1=1;ind1<=s.size();ind1++){\n for(int ind2=1;ind2<=t.size();ind2++){\n if(s[ind1-1]==t[ind2-1]){\n temp[ind2]=1+dp[ind2-1];\n }\n else{\n temp[ind2]=max(dp[ind2],temp[ind2-1]);\n }\n }\n dp=temp;\n }\n return dp[t.size()];\n }\n int longestPalindromeSubseq(string s) {\n string t=\"\";\n for(int i=0;i<s.size();i++){\n t=s[i]+t;\n }\n\n // return lpsR(s,t,s.size(),t.size());\n\n // vector<vector<int>> dp(s.size()+1,vector<int>(t.size()+1,-1));\n // return lpsM(s,t,s.size(),t.size(),dp);\n\n // return lpsT(s,t);\n \n return lpsO(s,t);\n }\n};",
"memory": "18300"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\n static const int mx=1001;\n int dp[mx][mx];\n pair<int,int> nxt[mx][mx];\n int solve(int i,int j,string &s)\n {\n if(i>j) return 0;\n if(i==j) return dp[i][j]=1;\n else if(dp[i][j]!=-1) return dp[i][j];\n if(s[i]==s[j]){\n nxt[i][j]={i+1,j-1};\n return 2+solve(i+1,j-1,s);\n }else{\n int val1,val2;\n val1=solve(i+1,j,s);\n val2=solve(i,j-1,s);\n if(val1>=val2){\n nxt[i][j]={i+1,j};\n return dp[i][j]=val1;\n }else{\n nxt[i][j]={i,j-1};\n return dp[i][j]=val2;\n }\n }\n }\npublic:\n int longestPalindromeSubseq(string s) {\n int n=s.size();\n s=\"$\"+s;\n for(int i=1;i<=n;i++){\n for(int j=1;j<=n;j++) dp[i][j]=-1;\n }\n\n return solve(1,n,s);\n }\n}; ",
"memory": "21300"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int longestPalindromeSubseq(string s) {\n string t=\"\";\n int dp[s.size()+1][s.size()+1];\n for(int i=0;i<s.size();i++){\n t=t+s[s.size()-1-i];\n dp[0][i]=0;\n dp[i][0]=0;\n }dp[0][s.size()]=0;\n dp[s.size()][0]=0;\n for(int i=1;i<=s.size();i++){\n for(int j=1;j<=s.size();j++){\n if(s[i-1]==t[j-1]){\n dp[i][j]=1+dp[i-1][j-1];\n }else{\n dp[i][j]=max(dp[i-1][j],dp[i][j-1]);\n }\n }\n }return dp[s.size()][s.size()];\n\n }\n};",
"memory": "22200"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\n int f[2000][2000];\npublic:\n int longestPalindromeSubseq(string s) {\n int n=s.length();\n string s_re=\"\";\n for(int i=n-1;i>=0;i--){\n s_re+=s[i];\n }\n cout<<s_re<<endl;\n for(int i=1;i<=n;i++){\n for(int j=1;j<=n;j++){\n if(s[i-1]==s_re[j-1]){\n f[i][j]=f[i-1][j-1]+1;\n }else{\n f[i][j]=max(f[i-1][j],f[i][j-1]);\n }\n }\n }\n return f[n][n];\n }\n};",
"memory": "25600"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int longestPalindromeSubseq(string s) {\n string t = s ;\n int n = s.length() ;\n reverse(t.begin() , t.end()) ;\n if(s == t) return n;\n vector<vector<int>> dp(n+1 , vector<int>(n+1,0)) ;\n\n // using tabulation\n \n for(int i=1 ; i<=n ; i++) {\n for(int j=1 ; j<=n ; j++) {\n if(s[i-1] == t[j-1]) {\n dp[i][j] = 1 + dp[i-1][j-1] ;\n } else {\n dp[i][j] = max(dp[i-1][j] , dp[i][j-1]) ;\n }\n }\n }\n\n return dp[n][n] ;\n }\n};",
"memory": "34700"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int longestPalindromeSubseq(string s) {\n string st=\"\";\n int n=s.length();\n for(int i=n-1;i>=0;i--)\n {\n st+=s[i];\n }\n if(s==st)\n {\n return s.length();\n }\n vector<vector<int>>dp(n+1,vector<int>(n+1,-1));\n for(int i=0;i<=n;i++)\n {\n dp[i][0]=0;\n }\n for(int j=0;j<=n;j++)\n {\n dp[0][j]=0;\n }\n\n for(int i=1;i<=n;i++)\n {\n for(int j=1;j<=n;j++)\n {\n if(s[i-1]==st[j-1])\n {\n dp[i][j]=1+dp[i-1][j-1];\n }\n else\n {\n dp[i][j]=max(dp[i-1][j],dp[i][j-1]);\n }\n }\n }\n return dp[n][n];\n }\n};",
"memory": "34800"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\nint longestCommonSubsequence(string s1, string s2) {\n if(s1==s2)return s1.size();\n int n1=s1.size();\n int n2=s2.size();\n vector<vector<int>>dp(n1+1,vector<int>(n2+1,0));\n for(int i=1;i<=n1;i++){\n for(int j=1;j<=n2;j++){\n if(s1[i-1]==s2[j-1]){\n dp[i][j]=1+dp[i-1][j-1];\n }\n else{\n dp[i][j]=max(dp[i-1][j],dp[i][j-1]);\n }\n }\n }\n return dp[n1][n2];\n }\n int longestPalindromeSubseq(string s) {\n string s2=s;\n reverse(s2.begin(),s2.end());\n return longestCommonSubsequence(s,s2);\n }\n};",
"memory": "34900"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "// class Solution {\n// public:\n// int maxCommon(int ind1, int ind2, string& str1, string& str2){\n// if(ind1 < 0 || ind2 < 0)\n// return 0;\n\n// if(str1[ind1] == str2[ind2])\n// return 1 + maxCommon(ind1-1, ind2-1, str1, str2);\n \n// int pickL = maxCommon(ind1-1, ind2, str1, str2);\n// int pickR = maxCommon(ind1, ind2-1, str1, str2);\n// return max(pickL, pickR);\n// }\n// int longestPalindromeSubseq(string s) {\n// string text2 = s;\n// reverse(text2.begin(), text2.end());\n \n// if(s == text2)\n// return s.length();\n\n// int l1 = s.size(), l2 = text2.size();\n// return maxCommon(l1-1, l2-1, s, text2);\n// } \n// };\n\n\n\n\n\n\n\n\nclass Solution {\npublic:\n int maxCommon(int ind1, int ind2, string& str1, string& str2, vector<vector<int>>& dp){\n if(ind1 < 0 || ind2 < 0)\n return 0;\n\n if(dp[ind1][ind2] != -1)\n return dp[ind1][ind2];\n\n if(str1[ind1] == str2[ind2])\n return dp[ind1][ind2] = 1 + maxCommon(ind1-1, ind2-1, str1, str2, dp);\n \n int pickL = maxCommon(ind1-1, ind2, str1, str2, dp);\n int pickR = maxCommon(ind1, ind2-1, str1, str2, dp);\n return dp[ind1][ind2] = max(pickL, pickR);\n }\n int longestPalindromeSubseq(string text1) {\n string text2 = text1;\n reverse(text2.begin(), text2.end());\n if(text1 == text2)\n return text1.length();\n\n int l1 = text1.size(), l2 = text2.size();\n vector<vector<int>> dp(l1, vector<int>(l2, -1));\n\n return maxCommon(l1-1, l2-1, text1, text2, dp);\n } \n};\n\n\n\n\n\n// --SHIFTING OF INDEX --- MEMOIZATION\n// class Solution {\n// public:\n// int maxCommon(int ind1, int ind2, string& str1, string& str2, vector<vector<int>>& dp){\n// if(ind1 == 0 || ind2 == 0)\n// return 0;\n\n// if(dp[ind1][ind2] != -1)\n// return dp[ind1][ind2];\n\n// if(str1[ind1-1] == str2[ind2-1])\n// return dp[ind1][ind2] = 1 + maxCommon(ind1-1, ind2-1, str1, str2, dp);\n \n// int pickL = maxCommon(ind1-1, ind2, str1, str2, dp);\n// int pickR = maxCommon(ind1, ind2-1, str1, str2, dp);\n// return dp[ind1][ind2] = max(pickL, pickR);\n// }\n// int longestCommonSubsequence(string text1, string text2) {\n// if(text1 == text2)\n// return text1.length();\n\n// int l1 = text1.size(), l2 = text2.size();\n// vector<vector<int>> dp(l1+1, vector<int>(l2+1, -1));\n\n// return maxCommon(l1, l2, text1, text2, dp);\n// } \n// };\n\n\n\n\n\n\n// class Solution {\n// public:\n// int longestCommonSubsequence(string str1, string str2) {\n// if(str1 == str2)\n// return str1.length();\n\n// int l1 = str1.size(), l2 = str2.size();\n// vector<vector<int>> dp(l1+1, vector<int>(l2+1, 0));\n\n// for(int i = 0; i <= l1; i++)\n// dp[i][0] = 0;\n\n// for(int i = 0; i <= l2; i++)\n// dp[0][i] = 0;\n\n// for(int ind1 = 1; ind1 <= l1; ind1++){\n// for(int ind2 = 1; ind2 <= l2; ind2++){\n// if(str1[ind1-1] == str2[ind2-1])\n// dp[ind1][ind2] = 1 + dp[ind1-1][ind2-1];\n// else\n// dp[ind1][ind2] = max(dp[ind1-1][ind2], dp[ind1][ind2-1]);\n// }\n// }\n// return dp[l1][l2];\n// } \n// };\n\n\n\n// class Solution {\n// public:\n// int longestCommonSubsequence(string str1, string str2) {\n// if(str1 == str2)\n// return str1.length();\n\n// int l1 = str1.size(), l2 = str2.size();\n// vector<int>prev (l2+1, 0);\n// vector<int>cur (l2+1, 0);\n\n// for(int i = 0; i <= l2; i++)\n// prev[i] = 0;\n\n// for(int ind1 = 1; ind1 <= l1; ind1++){\n// for(int ind2 = 1; ind2 <= l2; ind2++){\n// if(str1[ind1-1] == str2[ind2-1])\n// cur[ind2] = 1 + prev[ind2-1];\n// else\n// cur[ind2] = max(prev[ind2], cur[ind2-1]);\n// }\n// prev = cur;\n// }\n// return prev[l2];\n// } \n// };",
"memory": "35000"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "// class Solution {\n// public:\n// int maxCommon(int ind1, int ind2, string& str1, string& str2){\n// if(ind1 < 0 || ind2 < 0)\n// return 0;\n\n// if(str1[ind1] == str2[ind2])\n// return 1 + maxCommon(ind1-1, ind2-1, str1, str2);\n \n// int pickL = maxCommon(ind1-1, ind2, str1, str2);\n// int pickR = maxCommon(ind1, ind2-1, str1, str2);\n// return max(pickL, pickR);\n// }\n// int longestPalindromeSubseq(string s) {\n// string text2 = s;\n// reverse(text2.begin(), text2.end());\n \n// if(s == text2)\n// return s.length();\n\n// int l1 = s.size(), l2 = text2.size();\n// return maxCommon(l1-1, l2-1, s, text2);\n// } \n// };\n\n\n\n\n\n\n\n\n// class Solution {\n// public:\n// int maxCommon(int ind1, int ind2, string& str1, string& str2, vector<vector<int>>& dp){\n// if(ind1 < 0 || ind2 < 0)\n// return 0;\n\n// if(dp[ind1][ind2] != -1)\n// return dp[ind1][ind2];\n\n// if(str1[ind1] == str2[ind2])\n// return dp[ind1][ind2] = 1 + maxCommon(ind1-1, ind2-1, str1, str2, dp);\n \n// int pickL = maxCommon(ind1-1, ind2, str1, str2, dp);\n// int pickR = maxCommon(ind1, ind2-1, str1, str2, dp);\n// return dp[ind1][ind2] = max(pickL, pickR);\n// }\n// int longestPalindromeSubseq(string text1) {\n// string text2 = text1;\n// reverse(text2.begin(), text2.end());\n \n// if(text1 == text2)\n// return text1.length();\n\n// int l1 = text1.size(), l2 = text2.size();\n// vector<vector<int>> dp(l1, vector<int>(l2, -1));\n\n// return maxCommon(l1-1, l2-1, text1, text2, dp);\n// } \n// };\n\n\n\n\n\n\n// --SHIFTING OF INDEX --- MEMOIZATION\nclass Solution {\npublic:\n int maxCommon(int ind1, int ind2, string& str1, string& str2, vector<vector<int>>& dp){\n if(ind1 == 0 || ind2 == 0)\n return 0;\n\n if(dp[ind1][ind2] != -1)\n return dp[ind1][ind2];\n\n if(str1[ind1-1] == str2[ind2-1])\n return dp[ind1][ind2] = 1 + maxCommon(ind1-1, ind2-1, str1, str2, dp);\n \n int pickL = maxCommon(ind1-1, ind2, str1, str2, dp);\n int pickR = maxCommon(ind1, ind2-1, str1, str2, dp);\n return dp[ind1][ind2] = max(pickL, pickR);\n }\n int longestPalindromeSubseq(string text1) {\n string text2 = text1;\n reverse(text2.begin(), text2.end());\n \n if(text1 == text2)\n return text1.length();\n\n int l1 = text1.size(), l2 = text2.size();\n vector<vector<int>> dp(l1+1, vector<int>(l2+1, -1));\n\n return maxCommon(l1, l2, text1, text2, dp);\n } \n};\n\n\n\n\n\n\n// class Solution {\n// public:\n// int longestCommonSubsequence(string str1, string str2) {\n// if(str1 == str2)\n// return str1.length();\n\n// int l1 = str1.size(), l2 = str2.size();\n// vector<vector<int>> dp(l1+1, vector<int>(l2+1, 0));\n\n// for(int i = 0; i <= l1; i++)\n// dp[i][0] = 0;\n\n// for(int i = 0; i <= l2; i++)\n// dp[0][i] = 0;\n\n// for(int ind1 = 1; ind1 <= l1; ind1++){\n// for(int ind2 = 1; ind2 <= l2; ind2++){\n// if(str1[ind1-1] == str2[ind2-1])\n// dp[ind1][ind2] = 1 + dp[ind1-1][ind2-1];\n// else\n// dp[ind1][ind2] = max(dp[ind1-1][ind2], dp[ind1][ind2-1]);\n// }\n// }\n// return dp[l1][l2];\n// } \n// };\n\n\n\n// class Solution {\n// public:\n// int longestCommonSubsequence(string str1, string str2) {\n// if(str1 == str2)\n// return str1.length();\n\n// int l1 = str1.size(), l2 = str2.size();\n// vector<int>prev (l2+1, 0);\n// vector<int>cur (l2+1, 0);\n\n// for(int i = 0; i <= l2; i++)\n// prev[i] = 0;\n\n// for(int ind1 = 1; ind1 <= l1; ind1++){\n// for(int ind2 = 1; ind2 <= l2; ind2++){\n// if(str1[ind1-1] == str2[ind2-1])\n// cur[ind2] = 1 + prev[ind2-1];\n// else\n// cur[ind2] = max(prev[ind2], cur[ind2-1]);\n// }\n// prev = cur;\n// }\n// return prev[l2];\n// } \n// };",
"memory": "35000"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int solve(string &text1, string &text2,int i,int j,vector<vector<int>>&dp){\n if(i<0||j<0)return 0;\n if(dp[i][j]!=-1)return dp[i][j];\n int ans=0;\n int pick=0;\n if(text1[i]==text2[j]){\n pick=1+solve(text1,text2,i-1,j-1,dp);\n\n }\n int nopick=max(solve(text1,text2,i-1,j,dp),solve(text1,text2,i,j-1,dp));\n ans=max(pick,nopick);\n return dp[i][j]=ans;\n }\n \n int longestCommonSubsequence(string text1, string text2) {\n int n=text1.size();\n int m=text2.size();\n if(text1==text2){\n return n;\n }\n vector<vector<int>>dp(n,vector<int>(m,-1));\n int ans=solve(text1,text2,n-1,m-1,dp);\n return ans;\n \n }\n int longestPalindromeSubseq(string s) {\n string r=s;\n reverse(s.begin(),s.end());\n return longestCommonSubsequence(r,s);\n \n }\n};",
"memory": "35100"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "#include<string>\n#include<algorithm>\nclass Solution {\n int longestCommonSubsequence(string text1, string text2) {\n int length1 = text1.size(), length2 = text2.size();\n // vector<vector<int>> dp(length1+1, vector<int>(length2+1, -1));\n\n vector<int> prev(length2+1), curr(length2+1);\n\n // Base case\n for(int i=0;i<length1 + 1; i++){\n prev[0] = 0;\n }\n string s = \"\";\n\n // Main code\n for(int i=1; i<length1+1; i++){\n for(int j =1; j<length2+1;j++){\n if(text1[i-1] == text2[j-1]){\n curr[j] = 1 + prev[j-1];\n\n //printing string\n s+=text1[i-1];\n\n }\n else{\n curr[j] = max(prev[j],curr[j-1]);\n }\n }\n prev = curr;\n }\n // cout<<s<<endl;\n return prev[length2];\n }\npublic:\n int longestPalindromeSubseq(string s) {\n string s2 =s;\n reverse(s2.begin(),s2.end());\n return longestCommonSubsequence(s, s2);\n }\n};\n",
"memory": "36800"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int longestPalindromeSubseq(string s) {\n const int n = s.size();\n\n vector<int> last2(n, 0), last(n, 0);\n for(int i=0 ; i<n ; ++i) last[i] = 1;\n\n for(int d=1, i, j ; d<n ; ++d) {\n i = 0, j = d;\n vector<int> lps(n-d);\n\n while(i < n && j < n) {\n lps[i] = (s[i] == s[j]) ? (2 + last2[i+1]) : max(last[i], last[i+1]);\n\n ++i, ++j;\n }\n\n last2 = last;\n last = lps;\n }\n\n return last[0];\n }\n};",
"memory": "39400"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int longestPalindromeSubseq(string s) {\n const int n = s.size();\n\n vector<int> last2(n, 0), last(n, 1);\n\n for(int d=1, i, j ; d<n ; ++d) {\n i = 0, j = d;\n vector<int> lps(n-d);\n\n while(i < n && j < n) {\n lps[i] = (s[i] == s[j]) ? (2 + last2[i+1]) : max(last[i], last[i+1]);\n\n ++i, ++j;\n }\n\n last2 = last;\n last = lps;\n }\n\n return last[0];\n }\n};",
"memory": "39400"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int dp[1000][10000];\n\n int palin(string &s, int i, int j){\n if(i>j) return 0;\n if(i==j) return 1;\n\n if(dp[i][j]!=-1)\n return dp[i][j];\n\n if(s[i]==s[j]){\n return 2+palin(s, i+1, j-1);\n }\n int a = palin(s, i+1, j);\n int b = palin(s, i, j-1);\n\n return dp[i][j] = max(a,b);\n }\n\n int longestPalindromeSubseq(string &s) {\n int n = s.size();\n memset(dp, -1, sizeof(dp));\n // dp.resize(n+1, vector<int>(n+1, -1));\n return palin(s, 0, n-1);\n }\n};",
"memory": "51800"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int dp[1001][10001];\n int f(int i, int j, string&s, string&reverse) {\n \n if(i == s.size() or j == s.size()) return 0;\n\n if(dp[i][j]!=-1) return dp[i][j];\n int equal = -1e9, notequal = -1e9;\n if(s[i] == reverse[j]) {\n return dp[i][j] = 1 + f(i+1, j+1, s, reverse);\n } else {\n return dp[i][j] = max(f(i, j+1, s, reverse),f(i+1, j, s, reverse)); \n }\n }\n int longestPalindromeSubseq(string s) {\n memset(dp, -1, sizeof(dp));\n string reverseString = s;\n reverse(reverseString.begin(), reverseString.end());\n //cout<<reverseString<<\" \"<<s;\n return f(0, 0, s, reverseString);\n }\n};",
"memory": "52000"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int dp[1003][10003];\n int lcs(string &s1,string&s2,int n,int m)\n {\n if(n==0 || m == 0)\n return 0;\n \n if(dp[n][m]!= -1)\n return dp[n][m];\n \n if(s1[n-1] == s2[m-1])\n dp[n][m] = 1+lcs(s1,s2,n-1,m-1);\n \n else if(s1[n-1] != s2[m-1])\n dp[n][m] = max(lcs(s1,s2,n-1,m),lcs(s1,s2,n,m-1));\n \n return dp[n][m];\n }\n int longestPalindromeSubseq(string s) {\n string s2 = s;\n reverse(s.begin(),s.end());\n int n = s.size();\n memset(dp,-1,sizeof(dp));\n return lcs(s,s2,n,n);\n \n }\n};",
"memory": "52100"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int dp[3001][3001];\n\n int lcs(string x, string y, int n, int m)\n {\n if(n==0 || m==0)\n return 0;\n if(dp[n][m]!=-1)\n return dp[n][m];\n if(x[n-1]==y[m-1])\n {\n return dp[n][m] = 1+lcs(x,y,n-1,m-1);\n }\n else\n return dp[n][m] = max(lcs(x,y,n-1,m), lcs(x,y,n,m-1));\n }\n int longestPalindromeSubseq(string s) {\n int n = s.size();\n string x = s;\n string y = \"\";\n for(int i=0; i<n; i++)\n y=s[i]+y;\n for (int i = 0; i < n+1; i++)\n {\n dp[i][0] = 0;\n }\n for (int j = 0; j < n+1; j++)\n {\n dp[0][j]=0;\n }\n for (int i = 1; i < n+1; i++)\n {\n for (int j = 1; j < n+1; j++)\n {\n if(x[i-1] == y[j-1])\n dp[i][j] = 1+dp[i-1][j-1];\n else\n dp[i][j] = max(dp[i-1][j],dp[i][j-1]);\n }\n \n }\n return dp[n][n];\n }\n};",
"memory": "57500"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int dp[3001][3001];\n int longestPalindromeSubseq(string s) {\n int n = s.size();\n string x = s;\n string y = \"\";\n for(int i=0; i<n; i++)\n y=s[i]+y;\n for (int i = 0; i < n+1; i++)\n {\n dp[i][0] = 0;\n }\n for (int j = 0; j < n+1; j++)\n {\n dp[0][j]=0;\n }\n for (int i = 1; i < n+1; i++)\n {\n for (int j = 1; j < n+1; j++)\n {\n if(x[i-1] == y[j-1])\n dp[i][j] = 1+dp[i-1][j-1];\n else\n dp[i][j] = max(dp[i-1][j],dp[i][j-1]);\n }\n \n }\n return dp[n][n];\n }\n};",
"memory": "57500"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int dp[3001][3001];\n\n int lcs(string x, string y, int n, int m)\n {\n if(n==0 || m==0)\n return 0;\n if(dp[n][m]!=-1)\n return dp[n][m];\n if(x[n-1]==y[m-1])\n {\n return dp[n][m] = 1+lcs(x,y,n-1,m-1);\n }\n else\n return dp[n][m] = max(lcs(x,y,n-1,m), lcs(x,y,n,m-1));\n }\n int longestPalindromeSubseq(string s) {\n int n = s.size();\n string x = s;\n string y = \"\";\n for(int i=0; i<n; i++)\n y=s[i]+y;\n for (int i = 0; i < n+1; i++)\n {\n dp[i][0] = 0;\n }\n for (int j = 0; j < n+1; j++)\n {\n dp[0][j]=0;\n }\n for (int i = 1; i < n+1; i++)\n {\n for (int j = 1; j < n+1; j++)\n {\n if(x[i-1] == y[j-1])\n dp[i][j] = 1+dp[i-1][j-1];\n else\n dp[i][j] = max(dp[i-1][j],dp[i][j-1]);\n }\n \n }\n return dp[n][n];\n }\n};",
"memory": "57600"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int longestPalindromeSubseq(string s) {\n string s1=s;\n int n=s.size();\n reverse(s1.begin(),s1.end());\n map<char,vector<int>>mp;\n for(int i=0; i<n; i++){\n mp[s1[i]].push_back(i);\n }\n vector<int>ans;\n for(int i=0; i<n; i++){\n if(mp.find(s[i])!=mp.end()){\n vector<int>r=mp[s[i]];\n for(int j=r.size()-1; j>=0; j--){\n int lo=lower_bound(ans.begin(),ans.end(),r[j])-ans.begin();\n if(lo==(int)ans.size())ans.push_back(r[j]);\n else ans[lo]=r[j];\n }\n }\n }\n return ans.size();\n }\n};",
"memory": "58500"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int longestPalindromeSubseq(const string& s) {\n vector<int> cache(s.size() * s.size(), -1);\n \n int r = 0;\n for (int i = 0; i < s.size(); ++i) {\n r = max({ r, pal_size(s, i, i, cache), pal_size(s, i, i+1, cache) });\n }\n\n return r;\n }\n\n int pal_size(const string& s, int i, int j, vector<int>& cache) {\n if (i < 0 || i >= s.size() || j < 0 || j >= s.size())\n return 0;\n \n auto& cached = cache[i * s.size() + j];\n if (cached != -1) return cached;\n\n if (s[i] == s[j]) {\n return cached = 1 + (i != j) + pal_size(s, i-1, j+1, cache);\n }\n\n return cached = max(\n pal_size(s, i-1, j, cache),\n pal_size(s, i, j+1, cache)\n );\n }\n};",
"memory": "61700"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int longestPalindromeSubseq(string s) {\n int size = s.length();\n auto dp = new int*[size]();\n\n for (int i = 0; i < size; i++) {\n dp[i] = new int[size]{ 0 }; \n dp[i][i] = 1;\n }\n\n for (int j = 0; j < size; j++) {\n for (int i = j - 1; i >= 0; i--) {\n if (s[i] == s[j]) {\n dp[i][j] = dp[i + 1][j - 1] + 2;\n } else {\n dp[i][j] = max(dp[i][j - 1], dp[i + 1][j]);\n }\n }\n }\n\n return dp[0][size - 1];\n }\n};",
"memory": "73600"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int solve(string s, int i, int j, vector<vector<int>>& dp){\n \n if(j < i){\n return 0;\n }\n if(i == j){\n return 1;\n }\n\n if(dp[i][j] != -1) return dp[i][j];\n\n if(s[i] == s[j]){\n return dp[i][j] = 2 + solve(s, i+1, j-1, dp);\n }\n\n int nexti = solve(s, i+1, j, dp);\n int lastj = solve(s, i, j-1, dp);\n return dp[i][j] = max(nexti, lastj);\n }\n int longestPalindromeSubseq(string s) {\n int n = s.size();\n // vector<vector<int>> dp(n, vector<int>(n, -1));\n // return solve(s, 0, n-1, dp);\n\n // vector<vector<int>> dp(n, vector<int>(n, 0));\n // for(int i = n-1; i >= 0; i--){\n // for(int j = 0; j < n; j++){\n // if(i > j) continue;\n // if(i == j) {\n // dp[i][j] = 1;\n // continue;\n // }\n // if(s[i] == s[j]){\n // dp[i][j] = 2 + dp[i+1][j-1];\n // }\n // else{\n // int nexti = dp[i+1][j];\n // int lastj = dp[i][j-1];\n // dp[i][j] = max(nexti, lastj);\n // }\n // }\n // }\n // return dp[0][n-1];\n\n vector<int> dp(n, 0);\n for(int i = n-1; i >= 0; i--){\n vector<int> temp(n, 0);\n for(int j = 0; j < n; j++){\n if(i > j) continue;\n if(i == j) {\n temp[j] = 1;\n continue;\n }\n if(s[i] == s[j]){\n temp[j] = 2 + dp[j-1];\n }\n else{\n int nexti = dp[j];\n int lastj = temp[j-1];\n temp[j] = max(nexti, lastj);\n }\n }\n dp = temp;\n }\n return dp[n-1];\n }\n};",
"memory": "73700"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int longestPalindromeSubseq(string s) {\n string w=s;\n reverse(s.begin(),s.end());\n int m=s.size();\n vector<int>dp(m+1, 0);\n for(int i=0;i<m+1;i++){\n dp[0]=0;\n }\n for(int i=0;i<m+1;i++){\n dp[i]=0;\n }\n for(int i=1;i<m+1;i++){\n vector<int>curr(m+1);\n for(int j=1;j<m+1;j++){\n if(s[i-1]==w[j-1]){\n curr[j]= 1+dp[j-1];\n }else{\n curr[j]=max(dp[j],curr[j-1]);\n } \n }\n dp=curr;\n }\n return dp[m];\n }\n};",
"memory": "73800"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int longestCommonSubsequence(string text1, string text2) {\n\n int n=text1.size(),m=text2.size();\n vector<int> prev(m+1,0);\n \n\n for(int idx1=1;idx1<=n;idx1++){\n vector<int> curr(m+1,0);\n for(int idx2=1;idx2<=m;idx2++){\n if(text1[idx1-1]==text2[idx2-1])\n curr[idx2]=1+prev[idx2-1];\n else curr[idx2]=max(prev[idx2],curr[idx2-1]);\n \n }\n prev = curr;\n }\n return prev[m];\n \n }\n int longestPalindromeSubseq(string s) {\n string t=s;\n reverse(t.begin(),t.end());\n return longestCommonSubsequence(s,t);\n \n }\n};",
"memory": "73900"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int longestPalindromeSubseq(string s) {\n string w=s;\n reverse(s.begin(),s.end());\n int m=s.size();\n vector<int>dp(m+1, 0);\n for(int i=0;i<m+1;i++){\n dp[0]=0;\n }\n for(int i=0;i<m+1;i++){\n dp[i]=0;\n }\n for(int i=1;i<m+1;i++){\n vector<int>curr(m+1);\n for(int j=1;j<m+1;j++){\n if(s[i-1]==w[j-1]){\n curr[j]= 1+dp[j-1];\n }else{\n curr[j]=max(dp[j],curr[j-1]);\n } \n }\n dp=curr;\n }\n return dp[m];\n }\n};",
"memory": "74000"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int longestPalindromeSubseq(string s) {\n \n string t=s;\n reverse(s.begin(),s.end());\n int n=s.length();\n\tint m=t.length();\n\t//Write your code here\n\tint **dp=new int*[n+1];\n\tfor(int i=0;i<=n;i++)\n\t{\n\t\tdp[i]=new int[m+1];\n\n\t\tfor(int j=0;j<=m;j++)\n\t\t{\n\t\t\tdp[i][j]=0;\n\t\t}\n\t}\n\t\n\n\tfor(int i=1;i<=n;i++)\n\t{\n\t\tfor(int j=1;j<=m;j++)\n\t\t{\n\t\t\tif(s[i-1]==t[j-1]){\n\t\t\t\tdp[i][j]=dp[i-1][j-1]+1;\n\t\t\t}\n\n\t\t\telse{ \n\t\t\tdp[i][j]=max(dp[i-1][j],dp[i][j-1]);}\n\t\t}\n\t}\n\n\treturn dp[n][m];\n \n }\n};",
"memory": "74000"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\nint solve(int i1,int i2,string &t1,string &t2,vector<vector<int>>&dp){\n // if(i1==0 && i2==0) return (t1[i1]==t2[i2]);\n if(i1<0 || i2<0) return 0;\n if(dp[i1][i2]!=-1) return dp[i1][i2];\n if(t1[i1]==t2[i2]){\n return dp[i1][i2]=1+solve(i1-1,i2-1,t1,t2,dp);\n }\n else{\n return dp[i1][i2]=max(solve(i1-1,i2,t1,t2,dp),solve(i1,i2-1,t1,t2,dp));\n \n }\n\n}\n int longestPalindromeSubseq(string t1) {\n string t2=t1;\n reverse(t2.begin(),t2.end());\n int n=t1.size();\n int m=t2.size();\n vector<int>prev(m+1, 0);\n for(int i1=1;i1<=n;i1++){\n vector<int>curr(m+1, 0);\n for(int i2=1;i2<=m;i2++){\n if(t1[i1-1]==t2[i2-1]){\n curr[i2]=1+prev[i2-1];\n }\n else{\n curr[i2]=max(prev[i2],curr[i2-1]);\n \n }\n }\n prev=curr;\n }\n return prev[m];\n }\n};",
"memory": "74100"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int lcs(string s1, string s2)\n {\n int n = s1.size();\n int m = s2.size();\n\n vector<int> dp(m+1, 0);\n for(int i=1;i<=n;i++)\n {\n vector<int> temp(m+1, 0);\n for(int j=1;j<=m;j++)\n {\n if(s1[i-1] == s2[j-1]) temp[j] = 1 + dp[j-1];\n else temp[j] = max(temp[j-1], dp[j]);\n }\n dp = temp;\n }\n\n return dp[m];\n }\n int longestPalindromeSubseq(string s) {\n string t = s;\n reverse(t.begin(), t.end());\n\n return lcs(s, t);\n }\n};",
"memory": "74100"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int helper(int i, int j, string s1, string s2, vector<vector<int>>& dp)\n {\n if(i == 0 || j == 0) return 0;\n\n if(dp[i][j] != -1) return dp[i][j];\n\n if(s1[i-1] == s2[j-1]) return dp[i][j] = 1 + helper(i-1, j-1, s1, s2, dp);\n return dp[i][j] = max(helper(i, j-1, s1, s2, dp), helper(i-1, j, s1, s2, dp));\n }\n int lcs(string s1, string s2)\n {\n int n = s1.size();\n int m = s2.size();\n\n //return helper(n, m, s1, s2, dp);\n\n vector<int> dp(m+1, 0);\n for(int i=1;i<=n;i++)\n {\n vector<int> temp(m+1, 0);\n for(int j=1;j<=m;j++)\n {\n if(s1[i-1] == s2[j-1]) temp[j] = 1 + dp[j-1];\n else temp[j] = max(temp[j-1], dp[j]);\n }\n dp = temp;\n }\n\n return dp[m];\n }\n int longestPalindromeSubseq(string s) {\n string t = s;\n reverse(t.begin(), t.end());\n\n return lcs(s, t);\n }\n};",
"memory": "74200"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int longestPalindromeSubseq(string s) {\n /*\n \n \n */\n int n = s.size();\n int res = 0;\n vector<vector<int>> dp(n, vector<int>(n, 0));\n for(int i = n-1; i>=0; i--) {\n for(int j = i; j<n; j++) {\n if(i==j) dp[i][j] = 1;\n else if(i+1==j) dp[i][j] = s.at(i)==s.at(j)? 2 : 1;\n else if(s.at(i)==s.at(j)) dp[i][j] = 2 + dp[i+1][j-1];\n else dp[i][j] = max(dp[i+1][j], dp[i][j-1]);\n \n res = max(res, dp[i][j]);\n }\n }\n return res;\n }\n};",
"memory": "74300"
} |
516 | <p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "bbbab"
<strong>Output:</strong> 4
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "cbbd"
<strong>Output:</strong> 2
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 1000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 0 | {
"code": "#include <vector>\n#include <string>\n#include <algorithm>\n\nclass Solution {\npublic:\n int longestPalindromeSubseq(const std::string& s) {\n int n = s.length();\n std::vector<std::vector<int>> dp(n, std::vector<int>(n, 0));\n\n // Base case: substrings of length 1 are palindromes of length 1\n for (int i = 0; i < n; ++i) {\n dp[i][i] = 1;\n }\n\n // Fill the table for substrings of length 2 to n\n for (int len = 2; len <= n; ++len) { // length of the substring\n for (int i = 0; i <= n - len; ++i) {\n int j = i + len - 1; // ending index of the substring\n if (s[i] == s[j]) {\n dp[i][j] = dp[i + 1][j - 1] + 2;\n } else {\n dp[i][j] = std::max(dp[i + 1][j], dp[i][j - 1]);\n }\n }\n }\n\n return dp[0][n - 1];\n }\n};\n",
"memory": "74300"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.