id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
513 | <p>Given the <code>root</code> of a binary tree, return the leftmost value in the last row of the tree.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree1.jpg" style="width: 302px; height: 182px;" />
<pre>
<strong>Input:</strong> root = [2,1,3]
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree2.jpg" style="width: 432px; height: 421px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,null,5,6,null,null,7]
<strong>Output:</strong> 7
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 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<vector<int>> tree;\n void traverse(TreeNode* root, int level){\n if (!root) return;\n\n if (tree.size() < level + 1) tree.resize(level + 1);\n\n tree[level].push_back(root->val);\n\n traverse(root->left, level + 1);\n traverse(root->right, level + 1);\n }\n int findBottomLeftValue(TreeNode* root) {\n traverse(root, 0);\n\n return tree.back().front();\n }\n};",
"memory": "24900"
} |
513 | <p>Given the <code>root</code> of a binary tree, return the leftmost value in the last row of the tree.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree1.jpg" style="width: 302px; height: 182px;" />
<pre>
<strong>Input:</strong> root = [2,1,3]
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree2.jpg" style="width: 432px; height: 421px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,null,5,6,null,null,7]
<strong>Output:</strong> 7
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 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 heightOfTree(TreeNode* root) {\n if(!root)\n return 0;\n return 1+max(heightOfTree(root->left), heightOfTree(root->right));\n }\n int findBottomLeftValue(TreeNode* root) {\n int H=heightOfTree(root);\n vector<vector<int>>levelOrder(H);\n queue<pair<TreeNode*, int>>Q;\n Q.push(make_pair(root,0));\n while(!Q.empty()) {\n pair<TreeNode*, int>currentElement=Q.front();\n TreeNode* currentNode=currentElement.first;\n int currentHeight=currentElement.second;\n Q.pop();\n levelOrder[currentHeight].push_back(currentNode->val);\n if(currentNode->left) {\n Q.push(make_pair(currentNode->left, currentHeight+1));\n }\n if(currentNode->right) {\n Q.push(make_pair(currentNode->right, currentHeight+1));\n }\n }\n return levelOrder[H-1][0];\n }\n};",
"memory": "25000"
} |
513 | <p>Given the <code>root</code> of a binary tree, return the leftmost value in the last row of the tree.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree1.jpg" style="width: 302px; height: 182px;" />
<pre>
<strong>Input:</strong> root = [2,1,3]
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree2.jpg" style="width: 432px; height: 421px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,null,5,6,null,null,7]
<strong>Output:</strong> 7
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 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 heightOfTree(TreeNode* root) {\n if(!root)\n return 0;\n return 1+max(heightOfTree(root->left), heightOfTree(root->right));\n }\n int findBottomLeftValue(TreeNode* root) {\n int H=heightOfTree(root);\n vector<vector<int>>levelOrder(H);\n queue<pair<TreeNode*, int>>Q;\n Q.push(make_pair(root,0));\n while(!Q.empty()) {\n pair<TreeNode*, int>currentElement=Q.front();\n TreeNode* currentNode=currentElement.first;\n int currentHeight=currentElement.second;\n Q.pop();\n levelOrder[currentHeight].push_back(currentNode->val);\n if(currentNode->left) {\n Q.push(make_pair(currentNode->left, currentHeight+1));\n }\n if(currentNode->right) {\n Q.push(make_pair(currentNode->right, currentHeight+1));\n }\n }\n return levelOrder[H-1][0];\n }\n};",
"memory": "25000"
} |
513 | <p>Given the <code>root</code> of a binary tree, return the leftmost value in the last row of the tree.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree1.jpg" style="width: 302px; height: 182px;" />
<pre>
<strong>Input:</strong> root = [2,1,3]
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree2.jpg" style="width: 432px; height: 421px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,null,5,6,null,null,7]
<strong>Output:</strong> 7
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 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:\nvector<vector<int>> v;\nvector<int> b;\n void fun(struct TreeNode* r,int x)\n {\n if (x>=v.size())\n {\n v.push_back(b);\n }\n v[x].push_back(r->val);\n if (r->left!=0)\n {\n fun(r->left,x+1);\n }\n if (r->right!=0)\n {\n fun(r->right,x+1);\n }\n }\n int findBottomLeftValue(TreeNode* root) {\n fun(root,0);\n return v[v.size()-1][0];\n }\n};",
"memory": "25100"
} |
513 | <p>Given the <code>root</code> of a binary tree, return the leftmost value in the last row of the tree.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree1.jpg" style="width: 302px; height: 182px;" />
<pre>
<strong>Input:</strong> root = [2,1,3]
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree2.jpg" style="width: 432px; height: 421px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,null,5,6,null,null,7]
<strong>Output:</strong> 7
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 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 findBottomLeftValue(TreeNode* root) {\n vector<vector<int>> temp;\n queue<TreeNode*> q;\n q.push(root);\n while(!q.empty()){\n int size = q.size();\n vector<int> col;\n for(int i=0;i<size;i++){\n TreeNode* node = q.front();\n q.pop();\n col.push_back(node->val);\n if(node->left) q.push(node->left);\n if(node->right) q.push(node->right);\n }\n if(col.size()!=0) temp.erase(temp.begin(), temp.end());\n temp.push_back(col);\n }\n return temp[temp.size()-1][0];\n }\n};",
"memory": "25200"
} |
513 | <p>Given the <code>root</code> of a binary tree, return the leftmost value in the last row of the tree.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree1.jpg" style="width: 302px; height: 182px;" />
<pre>
<strong>Input:</strong> root = [2,1,3]
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree2.jpg" style="width: 432px; height: 421px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,null,5,6,null,null,7]
<strong>Output:</strong> 7
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 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 findBottomLeftValue(TreeNode* root) {\n \n vector<vector<int>>ans;\n\n if(root == NULL)\n return NULL;\n queue<TreeNode*>q;\n q.push(root);\n q.push(NULL);\n vector<int>v;\n\n while(!q.empty())\n {\n TreeNode* temp = q.front();\n q.pop();\n\n if(temp == NULL)\n {\n ans.push_back(v);\n v.clear();\n if(!q.empty())\n q.push(NULL);\n }\n else\n {\n v.push_back(temp->val);\n\n if(temp->left)\n q.push(temp->left);\n\n if(temp->right)\n q.push(temp->right);\n }\n }\n \n vector<int>vec = ans.back();\n\n return vec[0];\n }\n};",
"memory": "25400"
} |
513 | <p>Given the <code>root</code> of a binary tree, return the leftmost value in the last row of the tree.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree1.jpg" style="width: 302px; height: 182px;" />
<pre>
<strong>Input:</strong> root = [2,1,3]
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree2.jpg" style="width: 432px; height: 421px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,null,5,6,null,null,7]
<strong>Output:</strong> 7
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 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 findBottomLeftValue(TreeNode* root) {\n queue<TreeNode*>q;\n vector<vector<int>>v;\n int ans;\n if(root->left==NULL && root->right==NULL)\n {\n ans=root->val;\n return ans;\n }\n q.push(root);\n q.push(NULL);\n vector<int>v1;\n while(q.size()>1)\n {\n TreeNode* temp=q.front();\n q.pop();\n if(temp==NULL)\n {\n q.push(NULL);\n v.push_back(v1);\n v1.clear();\n }\n else{\n if(temp->left!=NULL)\n q.push(temp->left);\n if(temp->right!=NULL)\n q.push(temp->right);\n v1.push_back(temp->val);}\n }\n v.push_back(v1);\n int n=v.size();\n cout<<n;\n ans=v[n-1][0];\n return ans;\n }\n};",
"memory": "25500"
} |
513 | <p>Given the <code>root</code> of a binary tree, return the leftmost value in the last row of the tree.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree1.jpg" style="width: 302px; height: 182px;" />
<pre>
<strong>Input:</strong> root = [2,1,3]
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree2.jpg" style="width: 432px; height: 421px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,null,5,6,null,null,7]
<strong>Output:</strong> 7
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 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 findBottomLeft(TreeNode* root, std::map<int, int>& levelVal, int currLevel)\n {\n if (root == nullptr)\n return;\n\n if(levelVal.find(currLevel) == levelVal.end())\n levelVal.insert({currLevel,root->val});\n\n findBottomLeft(root->left, levelVal, currLevel+1);\n findBottomLeft(root->right, levelVal, currLevel+1);\n }\n\n int findBottomLeftValue(TreeNode* root) {\n std::map<int, int> levelVal;\n //level and the left most value\n findBottomLeft(root, levelVal, 0);\n auto it = levelVal.rbegin();\n return it->second;\n }\n};",
"memory": "25600"
} |
513 | <p>Given the <code>root</code> of a binary tree, return the leftmost value in the last row of the tree.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree1.jpg" style="width: 302px; height: 182px;" />
<pre>
<strong>Input:</strong> root = [2,1,3]
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree2.jpg" style="width: 432px; height: 421px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,null,5,6,null,null,7]
<strong>Output:</strong> 7
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 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 findBottomLeftValue(TreeNode* root) {\n queue<pair<TreeNode*,int>> q;\n map<int,int> mpp;\n int i;\n q.push({root,0});\n while(!q.empty())\n {\n TreeNode* node=q.front().first;\n int level=q.front().second;\n q.pop();\n if(mpp.find(level)==mpp.end())\n {\n mpp[level]=node->val;\n }\n if(node->left)\n q.push({node->left,level+1});\n if(node->right)\n q.push({node->right,level+1});\n }\n int data;\n for(auto it : mpp)\n {\n int lv=it.first;\n data=it.second;\n }\n return data;\n }\n};",
"memory": "25700"
} |
513 | <p>Given the <code>root</code> of a binary tree, return the leftmost value in the last row of the tree.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree1.jpg" style="width: 302px; height: 182px;" />
<pre>
<strong>Input:</strong> root = [2,1,3]
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree2.jpg" style="width: 432px; height: 421px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,null,5,6,null,null,7]
<strong>Output:</strong> 7
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 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 findBottomLeftValue(TreeNode* root) {\n if(root==NULL) return 0;\n\n queue<TreeNode*>q;\n vector<vector<int>>ans;\n \n q.push(root);\n \n while(!q.empty()){\n int sz=q.size();\n vector<int>level;\n for(int i=0;i<sz;i++){\n TreeNode*node=q.front();\n q.pop();\n level.push_back(node->val);\n if(node->left) q.push(node->left);\n if(node->right) q.push(node->right);\n }\n ans.push_back(level);\n }\n return ans[ans.size()-1][0];\n }\n};",
"memory": "26000"
} |
513 | <p>Given the <code>root</code> of a binary tree, return the leftmost value in the last row of the tree.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree1.jpg" style="width: 302px; height: 182px;" />
<pre>
<strong>Input:</strong> root = [2,1,3]
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree2.jpg" style="width: 432px; height: 421px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,null,5,6,null,null,7]
<strong>Output:</strong> 7
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 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 findBottomLeftValue(TreeNode* root) {\n \n vector<vector<int>> levelWiseNodes = {};\n queue<TreeNode*> q;\n q.push(root);\n\n while(!q.empty()) {\n\n vector<int> currentLevel = {};\n int size = q.size();\n\n for(int i=0; i<size; i++) {\n\n TreeNode* currentNode = q.front();\n q.pop();\n\n if(currentNode->left)\n q.push(currentNode->left);\n if(currentNode->right)\n q.push(currentNode->right);\n\n currentLevel.push_back(currentNode->val);\n\n }\n\n levelWiseNodes.push_back(currentLevel);\n\n }\n\n return levelWiseNodes.back()[0];\n\n }\n};",
"memory": "26100"
} |
513 | <p>Given the <code>root</code> of a binary tree, return the leftmost value in the last row of the tree.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree1.jpg" style="width: 302px; height: 182px;" />
<pre>
<strong>Input:</strong> root = [2,1,3]
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree2.jpg" style="width: 432px; height: 421px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,null,5,6,null,null,7]
<strong>Output:</strong> 7
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 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 findBottomLeftValue(TreeNode* root) {\n vector<vector<int>> v;\n if (root == NULL) {\n return 0;\n }\n queue<TreeNode*> q;\n q.push(root);\n while (!q.empty()) {\n vector<int> level;\n int size = q.size();\n for (int i = 0; i < size; i++) {\n TreeNode* current = q.front();\n q.pop();\n level.push_back(current->val);\n if (current->left != NULL) {\n q.push(current->left);\n }\n if (current->right != NULL) {\n q.push(current->right);\n }\n }\n v.push_back(level);\n }\n vector<int> part = v[v.size() - 1];\n int ans = part[0];\n return ans;\n }\n};",
"memory": "26100"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findRotateSteps(string ring, string key) {\n int n = ring.size(), m = key.size();\n int dp[n][m];\n memset(dp, -1, sizeof(dp));\n for (int i=0;i<n;++i) {\n if(ring[i]!=key[m-1]) continue;\n dp[i][m - 1] = 0;\n }\n for (int j = m - 2; j >= 0; --j) {\n for(int i=0;i<n;++i){\n if(ring[i]!=key[j]) continue;\n int x = 100000;\n for(int k=0;k<n;++k){\n if(dp[k][j+1]==-1) continue;\n int curr = min(abs(i - k), n - abs(i - k));\n x = min(x, curr + dp[k][j+1]);\n }\n dp[i][j]=x;\n }\n }\n int ans=1000000;\n for(int i=0;i<n;++i){\n if(dp[i][0]==-1) continue;\n ans = min(ans,dp[i][0]+min(i, n - i));\n }\n return key.size()+ans;\n }\n};",
"memory": "8816"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n static int findRotateSteps(const string &ring, const string &key) {\n constexpr uint8_t ASIZE = 26, DSIZE = 100;\n const uint8_t r = ring.size(),\n k = key.size();\n uint8_t pos[ASIZE][r+1u]; // todo shorten it\n for (uint8_t j = 0; j < ASIZE; j++)\n pos[j][0] = 0;\n for (uint8_t i = 0; i < r; i++) {\n uint8_t *const p = pos[ring[i]-'a'];\n p[++*p] = i;\n }\n uint16_t moves[2][DSIZE];\n fill(moves[0], moves[0] + DSIZE, USHRT_MAX);\n {\n const uint8_t *const p0 = pos[key[0]-'a'];\n for (uint8_t q = 1; q <= *p0; q++) {\n const uint8_t j = p0[q];\n moves[0][j] = min<uint8_t>(j, r - j) + 1u;\n }\n }\n for (uint8_t i = 1; i < k; i++) {\n const uint8_t *const p = pos[key[i-1u]-'a'],\n *const c = pos[key[i]-'a'];\n fill(moves[i&1u], moves[i&1u] + DSIZE, USHRT_MAX);\n for (uint8_t q = 1; q <= *p; q++) {\n const uint8_t j = p[q];\n for (uint8_t d = 1; d <= *c; d++) {\n const uint8_t e = c[d];\n const uint8_t s = e > j ? e - j : j - e;\n auto &m = moves[i&1u][e];\n m = min<uint16_t>(m, moves[(i&1u)^1u][j] + min<uint8_t>(s, r - s) + 1u);\n }\n }\n }\n return *min_element(moves[(k&1u)^1u], moves[(k&1u)^1u] + r);\n }\n};\n\nauto init = []() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 'c';\n}();",
"memory": "8816"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int dp[100][100]; // curr,prev,key_index\n string ring, key;\n vector<int> adj[26];\n\n int slv(int curr, int keyIndex) {\n if (keyIndex == key.size())return 0;\n int &ret = dp[curr][keyIndex];\n if (~ret)return ret;\n ret = 1e9;\n auto &arr = adj[key[keyIndex]-'a'];\n for (int i = 0; i < arr.size(); ++i) {\n int clockwise = abs(curr - arr[i]);\n int antiClockwise = ring.size() - clockwise;\n ret = min(ret, 1 + slv(arr[i], keyIndex + 1) + clockwise);\n ret = min(ret, 1 + slv(arr[i], keyIndex + 1) + antiClockwise);\n }\n return ret;\n }\n\n int findRotateSteps(string ring, string key) {\n this->ring = ring;\n this->key = key;\n for (int i = 0; i < ring.size(); ++i) {\n adj[ring[i] - 'a'].push_back(i);\n }\n memset(dp, -1, sizeof dp);\n return slv(0, 0);\n }\n};",
"memory": "10048"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int dp[100][100]; // curr,prev,key_index\n string ring, key;\n vector<int> adj[26];\n\n int slv(int curr, int keyIndex) {\n if (keyIndex == key.size())return 0;\n int &ret = dp[curr][keyIndex];\n if (~ret)return ret;\n ret = 1e9;\n auto &arr = adj[key[keyIndex]-'a'];\n for (int i = 0; i < arr.size(); ++i) {\n int clockwise = abs(curr - arr[i]);\n int antiClockwise = ring.size() - clockwise;\n ret = min(ret, 1 + slv(arr[i], keyIndex + 1) + clockwise);\n ret = min(ret, 1 + slv(arr[i], keyIndex + 1) + antiClockwise);\n }\n return ret;\n }\n\n int findRotateSteps(string ring, string key) {\n this->ring = ring;\n this->key = key;\n for (int i = 0; i < ring.size(); ++i) {\n adj[ring[i] - 'a'].push_back(i);\n }\n memset(dp, -1, sizeof dp);\n return slv(0, 0);\n }\n};",
"memory": "10048"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n\n int findMinDist(int center,int ringIdx,int ringLen){\n int distance = abs(center- ringIdx);\n int wrapAroundDist = ringLen - distance;\n return min(distance,wrapAroundDist);\n }\n /*\n int calMinSteps(int center,int ki, int ringLen,string& key,map<char,vector<int>>&mp,vector<vector<int>>&dp){\n if(ki==key.size()) return 0;\n if(dp[center][ki] !=-1) return dp[center][ki];\n int mini= 1e9;\n for(auto &ringIdx:mp[key[ki]]){\n int currDist= findMinDist(center, ringIdx,ringLen);\n mini = min(mini, currDist +calMinSteps(ringIdx,ki+1,ringLen, key, mp,dp) );\n }\n return dp[center][ki]=mini;\n } */\n/*\n // tabulation approach : bottom up approach\n // tc O(keyLen*ringLen)\n int calMinSteps(int ringLen,string& key,map<char,vector<int>>&mp){\n int keyLen= key.size();\n vector<vector<int>>dp(ringLen+1 , vector<int>(keyLen+1,0));\n for(int center =0; center<=ringLen; center++){\n dp[center][keyLen]=0;\n }\n\n for(int ki=keyLen-1; ki>=0; ki--){\n for(int center=ringLen-1; center>=0; center--){\n int mini= 1e9;\n for(auto &ringIdx:mp[key[ki]]){\n int currDist= findMinDist(center, ringIdx,ringLen);\n mini = min(mini, currDist + dp[ringIdx][ki+1] );\n }\n dp[center][ki]=mini;\n }\n }\n return dp[0][0];\n }*/\n int calMinSteps(int ringLen,string& key,map<char,vector<int>>&mp){\n int keyLen= key.size();\n vector<int> next(ringLen+1,1e9),curr(ringLen+1,1e9);\n for(int center =0; center<=ringLen; center++){\n next[center]=0;\n }\n\n for(int ki=keyLen-1; ki>=0; ki--){ \n for(int center=ringLen-1; center>=0; center--){\n int mini= 1e9;\n for(auto &ringIdx:mp[key[ki]]){\n int currDist= findMinDist(center, ringIdx,ringLen);\n mini = min(mini, currDist + next[ringIdx] );\n }\n curr[center]=mini;\n }\n next =curr;\n }\n return next[0]; \n }\n int findRotateSteps(string ring, string key) {\n map<char,vector<int>>mp;\n int ringLen=ring.size();\n int keyLen= key.size();\n // vector<vector<int>>dp(ringLen+1 , vector<int>(keyLen+1,-1));\n for(int i=0; i<ringLen; i++){\n mp[ring[i]].push_back(i);\n }\n // return (int)key.size() + calMinSteps(0,0,ring.size(),key,mp,dp);\n return (int)key.size() + calMinSteps(ringLen,key,mp);\n\n }\n};",
"memory": "11281"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\n unordered_map<char,vector<int>>pos;\n int dp[101][102];\n int solve(int i,int prevpos,string &ring,string &key){\n int n = key.size();\n int m = ring.size();\n if(i>=n)return 0;\n char ch = key[i];\n int ans = 1e9;\n if(dp[i][prevpos+1] != -1)return dp[i][prevpos+1];\n if(prevpos == -1)\n for(auto pos:pos[ch]){\n ans = min(ans,1+ min({pos,m-pos})+ solve(i+1,pos,ring,key));\n }\n else for(auto pos:pos[ch]){\n int diff = abs(pos-prevpos);\n ans = min(ans,1+ min({diff,m-diff})+ solve(i+1,pos,ring,key));\n }\n return dp[i][prevpos+1] = ans;\n }\npublic:\n int findRotateSteps(string ring, string key) {\n int n = ring.size();\n memset(dp,-1,sizeof(dp));\n for(int i =0;i<ring.size();i++){\n pos[ring[i]].push_back(i);\n }\n return solve(0,-1,ring,key);\n }\n};",
"memory": "11281"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findRotateSteps(string ring, string key) {\n unordered_map<char, vector<int>> locs;\n for (int i = 0; i < ring.size(); ++i) {\n char c = ring[i];\n locs[c].push_back(i);\n }\n vector<pair<int, int>> active_pos; // position, distance\n active_pos.emplace_back(0, 0);\n for (char c : key) {\n vector<pair<int, int>> new_pos;\n for (int p : locs[c]) {\n int min_distance = numeric_limits<int>::max();\n for (auto [pos, d] : active_pos) {\n int distance = (ring.size() + pos - p) % ring.size();\n if (distance > ring.size() / 2) {\n distance = ring.size() - distance;\n }\n distance += d;\n if (distance < min_distance) {\n min_distance = distance;\n }\n }\n new_pos.emplace_back(p, min_distance + 1);\n }\n active_pos = move(new_pos);\n }\n int min_steps = numeric_limits<int>::max();\n for (auto [pos, d] : active_pos) {\n if (d < min_steps) {\n min_steps = d;\n }\n }\n return min_steps;\n }\n};",
"memory": "12513"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findRotateSteps(string ring, string key) {\n int dp[ring.size()][key.size()];\n map<char, vector<int>> table;\n vector<int> path;\n\n for(int i=0;i<ring.size();i++){\n table[ring[i]].emplace_back(i);\n if(ring[i] == key[key.size() - 1]){\n dp[i][key.size()-1] = 1;\n }\n }\n\n for(int index = key.size() - 2; index>-1; index--){\n vector<int> pos = table[key[index]];\n vector<int> next_pos = table[key[index+1]];\n for(int i=0;i<pos.size();i++){\n int move_step = INT_MAX, next = 0, cost = INT_MAX;\n for(int j=0;j<next_pos.size();j++){\n int tmp = GetMinRotateNum(pos[i], next_pos[j], ring.size());\n if(cost > (tmp+dp[next_pos[j]][index + 1])){\n move_step = tmp;\n next = next_pos[j];\n cost = tmp+dp[next_pos[j]][index + 1];\n }\n }\n dp[pos[i]][index] = 1 + move_step + dp[next][index + 1];\n }\n }\n\n int ans = -1;\n if(ring[0] != key[0]){\n vector<int> next_pos = table[key[0]]; \n int move_step = INT_MAX, next = -1, cost = INT_MAX;\n for(int i=0;i<next_pos.size();i++){\n int tmp = GetMinRotateNum(0, next_pos[i], ring.size());\n if(cost > (tmp+dp[next_pos[i]][0])){\n move_step = tmp;\n next = next_pos[i];\n cost = tmp+dp[next_pos[i]][0];\n }\n }\n ans = move_step + dp[next][0];\n }else{\n ans = dp[0][0];\n } \n \n return ans;\n }\n\n int GetMinRotateNum(int src, int dst, int length){\n int a = (src - dst + length)%length;\n int b = (dst - src + length)%length;\n return a > b ? b : a;\n }\n};",
"memory": "12513"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 0 | {
"code": "// Time: O(k) ~ O(k * r^2)\n// Space: O(r)\n\nclass Solution {\npublic:\n int findRotateSteps(string ring, string key) {\n unordered_map<char, vector<int>> lookup;\n for (int i = 0; i < ring.size(); ++i) {\n lookup[ring[i]].emplace_back(i);\n }\n \n vector<vector<int>> dp(2, vector<int> (ring.size()));\n for (int i = 1; i <= key.size(); ++i) {\n fill(dp[i % 2].begin(), dp[i % 2].end(), numeric_limits<int>::max());\n for (const auto& j : lookup[key[i - 1]]) {\n for (const auto& k : (i > 1 ? lookup[key[i - 2]] : vector<int>(1))) {\n int min_dist = min((k + ring.size() - j) % ring.size(),\n (j + ring.size() - k) % ring.size()) +\n dp[(i - 1) % 2][k];\n dp[i % 2][j] = min(dp[i % 2][j], min_dist);\n }\n }\n }\n return *min_element(dp[key.size() % 2].begin(), dp[key.size() % 2].end()) + key.size();\n }\n};",
"memory": "13746"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findRotateSteps(std::string ring, std::string key)\n {\n std::unordered_map<char, std::vector<int>> positions;\n for (auto i = 0; i < ring.size(); ++i) {\n positions[ring.at(i)].push_back(i);\n }\n\n // Key and value contain the position and its minimum cost\n std::unordered_map<int, int> dp;\n\n auto n = static_cast<int>(ring.size());\n\n for (const auto current : positions[key.front()]) {\n dp[current] = std::min(current, n - current);\n }\n\n for (const auto k : key) {\n std::unordered_map<int, int> tmp;\n for (const auto& [previous, cost] : dp) {\n for (const auto current : positions[k]) {\n auto min = std::min(std::abs(previous - current), n - std::abs(previous - current));\n if (tmp.contains(current)) {\n tmp[current] = std::min(tmp[current], min + cost);\n } else {\n tmp[current] = min + cost;\n }\n }\n }\n\n dp = std::move(tmp);\n }\n\n return std::min_element(dp.begin(), dp.end(), [] (const auto& x, const auto& y) { return x.second < y.second; })->second + key.size();\n }\n};",
"memory": "18676"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findRotateSteps(std::string ring, std::string key)\n {\n std::unordered_map<char, std::vector<int>> positions;\n for (auto i = 0; i < ring.size(); ++i) {\n positions[ring.at(i)].push_back(i);\n }\n\n // Key and value contain the position and its minimum cost\n std::unordered_map<int, int> dp;\n\n auto n = static_cast<int>(ring.size());\n\n for (const auto current : positions[key.front()]) {\n dp[current] = std::min(current, n - current);\n }\n\n for (const auto k : key) {\n std::unordered_map<int, int> tmp;\n for (const auto& [previous, cost] : dp) {\n for (const auto current : positions[k]) {\n auto min = std::min(std::abs(previous - current), n - std::abs(previous - current));\n if (auto search = tmp.find(current); search != tmp.end()) {\n tmp[current] = std::min(tmp[current], min + cost);\n } else {\n tmp[current] = min + cost;\n }\n }\n }\n\n dp = std::move(tmp);\n }\n\n return std::min_element(dp.begin(), dp.end(), [] (const auto& x, const auto& y) { return x.second < y.second; })->second + key.size();\n }\n};",
"memory": "18676"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int dist(int x, int y, int n){\n int a = max(x,y);\n int b = min(x,y);\n return min(a-b,b+n-a);\n }\n\n int findRotateSteps(string ring, string key) {\n int ans = key.size(), n = ring.size(), m = key.size();\n vector<vector<long long>> dp(m+1,vector<long long>(n,INT_MAX));\n // dp[i][j] = min(dp[i-1][k] + dist(j,k)for all k, ring[k] = key[i-1])\n // dp i j - cost for spelling i chars, when ring position is at j\n dp[0][0] = 0;\n for(int i = 1;i<=m;i++){\n for(int j = 0;j<n;j++){\n // dp[i][j] = INT_MAX;\n if(key[i-1] != ring[j])\n continue;\n // if(i==1 and j>0)\n // break;\n for(int k = 0;k<n;k++)\n // if(ring[k] == key[i-1])\n dp[i][j] = min(dp[i][j], dp[i-1][k] + dist(j,k,n));\n cout << i << ' ' << j << ' ' << dp[i][j] << endl;\n }\n }\n\n return ans + *min_element(dp[m].begin(),dp[m].end());\n }\n};",
"memory": "19908"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int dist(int x, int y, int n){\n int a = max(x,y);\n int b = min(x,y);\n return min(a-b,b+n-a);\n }\n\n int findRotateSteps(string ring, string key) {\n int ans = key.size(), n = ring.size(), m = key.size();\n vector<vector<long long>> dp(m+1,vector<long long>(n,INT_MAX));\n // dp[i][j] = min(dp[i-1][k] + dist(j,k)for all k, ring[k] = key[i-1])\n // dp i j - cost for spelling i chars, when ring position is at j\n dp[0][0] = 0;\n for(int i = 1;i<=m;i++){\n for(int j = 0;j<n;j++){\n // dp[i][j] = INT_MAX;\n if(key[i-1] != ring[j])\n continue;\n // if(i==1 and j>0)\n // break;\n for(int k = 0;k<n;k++)\n // if(ring[k] == key[i-1])\n dp[i][j] = min(dp[i][j], dp[i-1][k] + dist(j,k,n));\n // cout << i << ' ' << j << ' ' << dp[i][j] << endl;\n }\n }\n\n return ans + *min_element(dp[m].begin(),dp[m].end());\n }\n};",
"memory": "19908"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n map<char,vector<int>> mp;\n int n;\n map<int,map<int,int>> dp;\n\n int fn(int pos, string& str,int x) {\n if(x>=str.length())\n return 0;\n if(dp[x][pos] )\n return dp[x][pos];\n int p = INT_MAX;\n for(auto v: mp[str[x]]) {\n p = min(p,1 + min( abs(v-pos) , min(pos + n-v, v+n-pos)) + fn(v,str,x+1));\n }\n return dp[x][pos] = p;\n }\n \n int findRotateSteps(string ring, string key) {\n n = ring.length();\n for(int i=0;i<ring.length();i++) {\n mp[ring[i]].push_back(i);\n }\n\n int pos = 0;\n return fn(pos,key,0);\n }\n};",
"memory": "21141"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\nprivate:\n unordered_map<string, int> memo; // Map directly to an int for memoization\n unordered_map<char, vector<int>> char_positions;\n\n int f(string &ring, string &key, int ring_pos, int key_pos) {\n if (key_pos == key.size()) return 0;\n\n string memo_key = to_string(ring_pos) + \"-\" + to_string(key_pos);\n if (memo.find(memo_key) != memo.end()) {\n return memo[memo_key];\n }\n\n int n = ring.size();\n char current_char = key[key_pos];\n int min_steps = INT_MAX;\n\n for (int target_pos : char_positions[current_char]) {\n int clockwise_steps = abs(target_pos - ring_pos);\n int counterclockwise_steps = n - clockwise_steps;\n int steps = min(clockwise_steps, counterclockwise_steps);\n steps += 1 + f(ring, key, target_pos, key_pos + 1);\n min_steps = min(min_steps, steps);\n }\n\n memo[memo_key] = min_steps;\n return min_steps;\n }\n\npublic:\n int findRotateSteps(string ring, string key) {\n int n = ring.size();\n char_positions.clear();\n memo.clear();\n for (int i = 0; i < n; ++i) {\n char_positions[ring[i]].push_back(i);\n }\n return f(ring, key, 0, 0);\n }\n};",
"memory": "21141"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n\n int recursive(unordered_map<int,vector<int>>& left, unordered_map<int,vector<int>>& right, string &key, int i, int j, int n, vector<vector<int>>& dp){\n if(i==key.length()) return 0;\n\n if(dp[i][j]!=-1) return dp[i][j];\n\n int leftind=left[j][key[i]-'a'];\n int rightind=right[j][key[i]-'a'];\n int leftdis,rightdis;\n if(leftind<=j) leftdis=j-leftind;\n else leftdis=j+(n-leftind);\n\n if(rightind>=j) rightdis=rightind-j;\n else rightdis=(n-j)+rightind;\n\n int way1=leftdis+recursive(left,right,key,i+1,leftind,n,dp);\n int way2=rightdis+recursive(left,right,key,i+1,rightind,n,dp);\n\n\n return dp[i][j]= min(way1,way2);\n }\n int findRotateSteps(string ring, string key) {\n unordered_map<int,vector<int>> left;\n unordered_map<int,vector<int>> right;\n int n=ring.length();\n vector<int> vis1(26,0);\n vector<int> vis2(26,0);\n for(int i=1;i<n;i++){\n vis1[ring[i]-'a']=i;\n }\n for(int i=0;i<n;i++){\n vis1[ring[i]-'a']=i;\n left[i]=vis1;\n }\n\n for(int i=n-1;i>=0;i--){\n vis2[ring[i]-'a']=i;\n }\n\n for(int i=n-1;i>=0;i--){\n vis2[ring[i]-'a']=i;\n right[i]=vis2;\n }\n vector<vector<int>> dp(key.length(), vector<int>(n,-1));\n return key.length()+ recursive(left,right,key,0,0, n, dp); \n }\n};\n/*\n7 ( len of string to spell) + 6 ( for moving)\n*/",
"memory": "22373"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 2 | {
"code": "class Node{\n public:\n char c;\n Node* l;\n Node* r;\n Node(char b) {this->c = b;}\n};\nclass Solution {\npublic:\n vector<Node*> nodels;\n unordered_map<Node*, unordered_map<int, int>> mp;\n int traverse(Node* node, const string& key, int i) {\n if(i >= key.size()) return 0;\n if(node->c == key[i]) return 1 + traverse(node, key, i+1);\n\n if(mp.find(node) == mp.end()) {\n mp[node];\n }\n if(mp[node].find(i) != mp[node].end()) return mp[node][i];\n\n\n Node* templ = node;\n int ctl = 0;\n while(templ->c != key[i]) {\n templ = templ->l;\n ctl++;\n }\n Node* tempr = node;\n int ctr = 0;\n while(tempr->c != key[i]) {\n tempr = tempr->r;\n ctr++;\n }\n\n mp[node][i] = min(ctl+traverse(templ, key, i), ctr+traverse(tempr, key, i));\n return mp[node][i];\n }\n\n int findRotateSteps(string ring, string key) {\n int n = ring.size();\n nodels = vector<Node*>();\n\n for(char c: ring) {\n nodels.push_back(new Node(c));\n nodels.back()->l = nullptr;\n nodels.back()->r = nullptr;\n }\n\n for(int i = 0; i < n-1; i++) {\n nodels[i]->r = nodels[i+1];\n nodels[i+1]->l = nodels[i];\n }\n\n nodels[0]->l = nodels.back();\n nodels.back()->r = nodels[0];\n\n return traverse(nodels[0], key, 0);\n }\n};",
"memory": "22373"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n map<char, vector<int>> m;\n vector<vector<int>> dp;\n int ring_size;\n int f(int index, string key, int n) {\n int minm = INT_MAX;\n if (n == 0) return 0;\n for (auto u: m[key[0]]) {\n if (dp[u][n - 1] == -1) {\n dp[u][n - 1] = f(u, key.substr(1, n - 1), n - 1);\n }\n minm = min(minm, min(abs(u - index), ring_size - abs(u - index)) + dp[u][n - 1]);\n }\n return minm;\n }\n int findRotateSteps(string ring, string key) {\n for (int i = 0; i < ring.size(); i++) {\n m[ring[i]].push_back(i);\n dp.push_back(vector<int>(key.size() + 1, -1));\n }\n ring_size = (int)ring.size();\n return f(0, key, key.size()) + key.size();\n }\n};",
"memory": "23606"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findRotateSteps(string ring, string key) {\n\n int n = ring.size();\n int m = key.size();\n\n struct node {\n char spell;\n int val;\n int lvl;\n int sum;\n };\n\n bool memo[n+1][m+1];\n for (int i=0; i<=n; i++)\n for (int j=0; j<=m; j++)\n memo[i][j] = false;\n\n int step = m;\n queue<node> q;\n memo[0][0] = true;\n q.push(node{ring[0], 0, 0, 0});\n\n while (true) {\n node p = q.front();\n q.pop();\n\n int right = (p.val + 1 + n) % n;\n int left = (p.val - 1 + n) % n;\n int nextKey = p.lvl+1;\n\n\n if (p.lvl == m)\n return step + p.sum;\n if (p.lvl >= m)\n continue;\n\n //Use NextKey\n int arrow = p.lvl;\n if (p.spell == key[p.lvl]){\n // cout << step << \" \" << p.spell << \" {\" << left << \",\" << right << \"} \" << p.sum << endl;;\n \n arrow = nextKey;\n if (!memo[p.val][arrow]){\n memo[p.val][arrow] = true;\n q.push(node{ring[p.val], p.val, arrow, p.sum});\n } \n } else {\n if (!memo[right][arrow]){\n memo[right][arrow] = true;\n q.push(node{ring[right], right, arrow, p.sum+1});\n }\n if (!memo[left][arrow]){\n memo[left][arrow] = true;\n q.push(node{ring[left], left, arrow, p.sum+1});\n }\n }\n }\n }\n};",
"memory": "23606"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findRotateSteps(string ring, string key) {\n\n\n int n = ring.size();\n int m = key.size();\n\n string rings = ring + ring + ring;\n n = rings.size();\n string keys = ring[0] + key;\n m = keys.size();\n vector<vector<int>> v(n, vector<int>(m, INT_MAX));\n v[0][0] = 0;\n v[ring.size()][0] = 0;\n v[ring.size()*2][0] = 0;\n int ans = INT_MAX;\n\n for (int col = 1 ; col < keys.size(); col++) {\n int temp = INT_MAX;\n\n for (int row = 0; row < n; row++) {\n if (rings[row] == keys[col]) {\n int x = INT_MAX;\n int p = 0;\n int k = row;\n do {\n if (v[k][col-1] != INT_MAX) {\n x = v[k][col-1] + p;\n break;\n }\n p++;\n k = (k + 1)%n;\n } while(k != row);\n\n p = 0;\n k = row;\n\n do {\n if (v[k][col-1] != INT_MAX) {\n x = min(v[k][col-1] + p, x);\n break;\n }\n p++;\n k = (k - 1 + n)%n;\n } while(k != row);\n\n\n v[row][col] = min(x, v[row][col]);\n }\n temp = min(temp, v[row][col]);\n }\n ans = temp;\n }\n return ans + key.size();\n \n }\n};",
"memory": "24838"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findRotateSteps(string ring, string key) {\n\n\n int n = ring.size();\n int m = key.size();\n\n string rings = ring + ring + ring;\n n = rings.size();\n string keys = ring[0] + key;\n m = keys.size();\n vector<vector<int>> v(n, vector<int>(m, INT_MAX));\n v[0][0] = 0;\n v[ring.size()][0] = 0;\n v[ring.size()*2][0] = 0;\n int ans = INT_MAX;\n\n for (int col = 1 ; col < keys.size(); col++) {\n int temp = INT_MAX;\n\n for (int row = 0; row < n; row++) {\n if (rings[row] == keys[col]) {\n int x = INT_MAX;\n int p = 0;\n int k = row;\n do {\n if (v[k][col-1] != INT_MAX) {\n x = v[k][col-1] + p;\n break;\n }\n p++;\n k = (k + 1)%n;\n } while(1);\n\n p = 0;\n k = row;\n\n do {\n if (v[k][col-1] != INT_MAX) {\n x = min(v[k][col-1] + p, x);\n break;\n }\n p++;\n k = (k - 1 + n)%n;\n } while(1);\n\n\n v[row][col] = min(x, v[row][col]);\n }\n temp = min(temp, v[row][col]);\n }\n ans = temp;\n }\n return ans + key.size();\n \n }\n};",
"memory": "24838"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findRotateSteps(string ring, string key) {\n int sol = INT_MAX;\n vector<vector<int>> grid(key.size() + 1, vector<int>(ring.size(), INT_MAX));\n queue<pair<int, int>> q;\n q.push({0, 0});\n grid[0][0] = 0;\n while (!q.empty()) {\n int curK = q.front().first;\n int curPos = q.front().second;\n q.pop();\n if (curK == key.size()) {\n sol = min(sol, grid[curK][curPos]);\n continue;\n }\n if (curPos > 0 && grid[curK][curPos - 1] == INT_MAX) {\n grid[curK][curPos - 1] = grid[curK][curPos] + 1;\n q.push({curK, curPos - 1});\n }\n if (curPos < ring.size() - 1 && grid[curK][curPos + 1] == INT_MAX) {\n grid[curK][curPos + 1] = grid[curK][curPos] + 1;\n q.push({curK, curPos + 1});\n }\n if (curPos == 0 && grid[curK][ring.size() - 1] == INT_MAX) {\n grid[curK][ring.size() - 1] = grid[curK][curPos] + 1;\n q.push({curK, ring.size() - 1});\n }\n if (curPos == ring.size() - 1 && grid[curK][0] == INT_MAX) {\n grid[curK][0] = grid[curK][curPos] + 1;\n q.push({curK, 0});\n }\n if (ring[curPos] == key[curK]) {\n grid[curK + 1][curPos] = grid[curK][curPos] + 1;\n q.push({curK + 1, curPos});\n }\n }\n\n return sol;\n}\n\n\n};",
"memory": "26071"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int dis(int i,int j,int n){\n return min(abs(j-i),n-abs(i-j));\n }\n int findRotateSteps(string ring, string key) {\n int dist[26][26]; \n int n=ring.size();\n unordered_map<char,vector<int>> mp;\n for(int i=0;i<ring.size();i++){\n mp[ring[i]].push_back(i);\n }\n set<pair<int,pair<int,int>>> st;\n int m=key.size();\n \n unordered_set<int> vis; \n st.insert({0,{0,0}});\n \n while(!st.empty()){\n auto it=*st.begin();\n st.erase(st.begin());\n int steps=it.first;\n int i=it.second.first;\n int j=it.second.second;\n if(j==m) return steps+m;\n int state=i*100+j;\n if(vis.count(state)) continue;\n vis.insert(state);\n \n for(auto next:mp[key[j]]){\n st.insert({steps+dis(i,next,n),{next,j+1}});\n }\n }\n return 0; \n\n }\n};",
"memory": "26071"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<vector<int>> dp;\n int calc(string &ring, string &key, int ir, int ik) {\n if(ik == key.length()) {\n return 0;\n }\n // if(ring[ir] == key[ik]) {\n // return dp[ir][ik] = calc(ring, key, ir, ik + 1);\n // }\n\n if(dp[ir][ik] != -1) return dp[ir][ik];\n int res = INT_MAX;\n for(int i = 0; i < ring.length(); i++) {\n if(ring[i] == key[ik]) {\n int pa = min(abs(i - ir), int (ring.length() - abs(i - ir))) + calc(ring, key, i, ik + 1);\n res = min(res, pa);\n }\n }\n return dp[ir][ik] = res;\n }\n\n int findRotateSteps(string ring, string key) {\n dp = vector<vector<int>>(105, vector<int>(105, -1));\n return key.length() + calc(ring, key, 0, 0);\n }\n};",
"memory": "27303"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<vector<int>> dp;\n int calc(string &ring, string &key, int ir, int ik) {\n if(ik == key.length()) {\n return 0;\n }\n int res = INT_MAX;\n if(dp[ir][ik] != -1) {\n return dp[ir][ik];\n }\n for(int i = 0; i < ring.length(); i++) {\n if(ring[i] == key[ik]) {\n res = min(res, min(abs(i - ir), int (ring.length() - abs(i - ir))) + calc(ring, key, i, ik+1));\n }\n }\n return dp[ir][ik] = res;\n }\n\n int findRotateSteps(string ring, string key) {\n dp = vector<vector<int>>(105, vector<int>(105, -1));\n return key.length() + calc(ring, key, 0, 0);\n }\n};",
"memory": "27303"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int k;\n int n;\n \n int solve(int idx, int head, const map<char, vector<int>>& m, const string& key,vector<vector<int>>&dp) {\n if (idx >= k) return 0;\n if(dp[idx][head]!=-1) return dp[idx][head];\n int result = INT_MAX;\n for (int pos : m.at(key[idx])) {\n int gap = abs(pos - head);\n result = min(result, gap + solve(idx + 1, pos, m, key,dp));\n result = min(result, (n - gap) + solve(idx + 1, pos, m, key,dp));\n }\n return dp[idx][head]=result;\n }\n\n int findRotateSteps(string ring, string key) {\n k = key.size();\n n = ring.size();\n map<char, vector<int>> m;\n for (int i = 0; i < ring.size(); i++) {\n m[ring[i]].push_back(i);\n }\n vector<vector<int>>dp(101,vector<int>(101,-1));\n return solve(0, 0, m, key,dp) + k;\n }\n};",
"memory": "28536"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findDistance(int n, int start, int target){\n int forward = abs(start - target);\n int backward = n - forward;\n\n return min(forward, backward);\n }\n\n int modifiedDijkstra(string& ring, string& key, unordered_map<char, vector<int>>& adj){\n int ring_len = ring.length();\n int key_len = key.length();\n\n priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> pq; // inner vector -> (cost, ring_index, key_index)\n // set<pair<int, int>> visited;\n vector<vector<bool>> visited(ring_len+1, vector<bool>(key_len, false));\n pq.push({0, 0, 0});\n\n while(!pq.empty()){\n auto item = pq.top();\n int d = item[0];\n int ri = item[1];\n int ki = item[2];\n pq.pop();\n\n if(ki >= key_len) {\n return d;\n }\n\n if(visited[ri][ki]) continue;\n visited[ri][ki] = true;\n // if(visited.find({ri,ki}) != visited.end()) {\n // continue;\n // }\n // visited.insert({ri, ki});\n\n int ch = key[ki];\n\n for(auto& v: adj[ch]){\n int cost = findDistance(ring_len, ri, v);\n int total_cost = d + cost;\n\n pq.push({total_cost, v, ki + 1});\n }\n }\n\n return 0;\n }\n int func(string& ring, string& key, unordered_map<char, vector<int>>& adj){\n int n = ring.length();\n int m = key.length();\n priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> pq;\n pq.push({0, 0, 0});\n\n set<pair<int, int>> visited;\n \n int totalSteps = 0;\n while (!pq.empty()) {\n vector<int> vec = pq.top();\n pq.pop();\n \n totalSteps = vec[0];\n int ringIndex = vec[1];\n int keyIndex = vec[2];\n \n if (keyIndex == m) {\n break;\n }\n \n if (visited.count({ringIndex, keyIndex})) {\n continue;\n }\n\n visited.insert({ringIndex, keyIndex});\n \n for (int nextIndex : adj[key[keyIndex]]) {\n pq.push({totalSteps + findDistance(n, ringIndex, nextIndex), \n nextIndex, keyIndex + 1});\n }\n }\n \n return totalSteps;\n }\n int findRotateSteps(string ring, string key) {\n unordered_map<char, vector<int>> adj;\n for(int i=0; i<ring.length(); i++){\n int ch = ring[i];\n adj[ch].push_back(i);\n }\n int stepsForRotating = modifiedDijkstra(ring, key, adj);\n int stepsForPressing = key.size();\n\n return stepsForRotating + stepsForPressing;\n }\n};",
"memory": "29768"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n map<char, vector<int>> m;\n vector<vector<int>> dp;\n int ring_size;\n int f(int index, string key, int n) {\n int minm = INT_MAX;\n if (n == 0) return 0;\n string key1 = key.substr(1, n - 1);\n for (auto u: m[key[0]]) {\n if (dp[u][n - 1] == -1) {\n dp[u][n - 1] = f(u, key1, n - 1);\n }\n minm = min(minm, min(abs(u - index), ring_size - abs(u - index)) + dp[u][n - 1]);\n }\n return minm;\n }\n int findRotateSteps(string ring, string key) {\n for (int i = 0; i < ring.size(); i++) {\n m[ring[i]].push_back(i);\n dp.push_back(vector<int>(key.size() + 1, -1));\n }\n ring_size = (int)ring.size();\n return f(0, key, key.size()) + key.size();\n }\n};",
"memory": "29768"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findRotateSteps(string ring, string key) {\n queue<pair<pair<int,int>,int>> q;\n q.push({{0,0},0});\n vector<vector<int>> vis(ring.size(),vector<int>(key.size(),0));\n while(!q.empty()){\n int sz = q.size();\n for(int i=0;i<sz;i++){\n int r = q.front().first.first;\n int k = q.front().first.second;\n int steps = q.front().second;\n q.pop();\n if(k==key.size()) return steps;\n if(ring[r]==key[k]){\n q.push({{r,k+1},steps+1});\n }\n int r1 = (r+1)%ring.size();\n if(vis[r1][k]==0){\n vis[r1][k]=1;\n q.push({{r1,k},steps+1});\n }\n int r2 = (r-1+ring.size())%ring.size();\n if(vis[r2][k]==0){\n vis[r2][k]=1;\n q.push({{r2,k},steps+1});\n }\n }\n }\n return -1;\n }\n};",
"memory": "31001"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int countSteps(int ringIndex, int i, int n) {\n int dist = abs(i - ringIndex);\n int wrapAround = n - dist;\n \n return min(dist, wrapAround);\n }\n \n int findRotateSteps(string ring, string key) {\n int n = ring.length();\n int m = key.length();\n \n unordered_map<char, vector<int>> adj; \n for (int i = 0; i < n; i++) {\n char ch = ring[i];\n adj[ch].push_back(i);\n }\n\n priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> pq;\n pq.push({0, 0, 0});\n\n set<pair<int, int>> visited;\n \n int totalSteps = 0;\n while (!pq.empty()) {\n vector<int> vec = pq.top();\n pq.pop();\n \n totalSteps = vec[0];\n int ringIndex = vec[1];\n int keyIndex = vec[2];\n \n if (keyIndex == m) {\n break;\n }\n \n if (visited.count({ringIndex, keyIndex})) {\n continue;\n }\n\n visited.insert({ringIndex, keyIndex});\n \n for (int nextIndex : adj[key[keyIndex]]) {\n pq.push({totalSteps + countSteps(ringIndex, nextIndex, n), \n nextIndex, keyIndex + 1});\n }\n }\n \n return totalSteps + m; \n }\n};\n",
"memory": "31001"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 2 | {
"code": "/* Scroll down to see JAVA code also */\n/*\n MY YOUTUBE VIDEO ON THIS Qn : https://www.youtube.com/watch?v=yUNjMFE1tGo\n Company Tags : GOOGLE\n Leetcode Link : https://leetcode.com/problems/freedom-trail\n DP Solution : https://github.com/MAZHARMIK/Interview_DS_Algo/blob/master/DP/DP%20on%20Strings/Freedom%20Trail.cpp\n*/\n\n/************************************************************ C++ ************************************************/\n//Approach (Using DIjkstra's)\n//T.C : O(nm log(nm)) where n = length of ring, m = length of keyword. n*m = maximum number of pairs we visit is the number of unique possible pairs and push and pop\n //operations will take log(n*m)\n//S.C : O(n*m) pairs stored in heap\nclass Solution {\npublic:\n int countSteps(int ringIndex, int i, int n) {\n int dist = abs(i - ringIndex);\n int wrapAround = n - dist;\n \n return min(dist, wrapAround);\n }\n \n int findRotateSteps(string ring, string key) {\n int n = ring.length();\n int m = key.length();\n \n unordered_map<char, vector<int>> adj; // char --> {indices in ring where char is present}\n for (int i = 0; i < n; i++) {\n char ch = ring[i];\n adj[ch].push_back(i);\n }\n\n priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> pq;\n pq.push({0, 0, 0});\n\n set<pair<int, int>> visited;\n \n int totalSteps = 0;\n while (!pq.empty()) {\n vector<int> vec = pq.top();\n pq.pop();\n \n totalSteps = vec[0];\n int ringIndex = vec[1];\n int keyIndex = vec[2];\n \n if (keyIndex == m) {\n break;\n }\n \n if (visited.count({ringIndex, keyIndex})) {\n continue;\n }\n\n visited.insert({ringIndex, keyIndex});\n \n for (int nextIndex : adj[key[keyIndex]]) {\n pq.push({totalSteps + countSteps(ringIndex, nextIndex, n), \n nextIndex, keyIndex + 1});\n }\n }\n \n return totalSteps + m; //Note : totalSteps is for bringing each character to 12:00 position and then printing each of them will take m steps\n }\n};\n\n",
"memory": "32233"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n \n int countSteps(int ringIdx, int i, int n){\n int dist = abs(i-ringIdx);\n int reverse_dist = n - dist;\n\n return min(dist, reverse_dist);\n }\n int findRotateSteps(string ring, string key) {\n int n = ring.size();\n int m = key.size();\n\n unordered_map<char, vector<int>> adj;\n\n for(int i =0;i < n; i++){\n char ch = ring[i];\n adj[ch].push_back(i);\n }\n\n priority_queue<vector<int> , vector<vector<int> >, greater<vector<int> >>pq;// {steps, ringIndex, keyIndex}\n pq.push({0,0,0});\n\n set<pair<int,int>> visited;\n \n int steps = 0;\n while(!pq.empty()){\n auto top = pq.top();\n pq.pop();\n\n steps = top[0];\n int ringIndex = top[1];\n int keyIndex = top[2];\n int curr_char = key[keyIndex];\n\n if(keyIndex == m){\n break;\n }\n\n if(visited.count({ringIndex, keyIndex})) continue;\n // if not present in the visited\n visited.insert({ringIndex, keyIndex});\n\n for(int & nextIndex: adj[curr_char]){\n int newSteps = countSteps(ringIndex, nextIndex, n)+ steps;\n pq.push({newSteps, nextIndex, keyIndex+1});\n } \n }\n\n return steps+m ;\n }\n};",
"memory": "33466"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\nint countsteps(int i , int j , int n ){\n int dist = abs( i -j);\n int wrap = n- dist;\n return min(dist , wrap);\n}\n int findRotateSteps(string ring, string key) {\n \n int n = ring.length();\n int m = key.length();\n unordered_map<char , list<int>>adj;\n for(int i =0 ; i< n ; i++){\n char ch = ring[i];\n adj[ch].push_back(i);\n }\n //pq ->{steps , ring index , keyindex};\n priority_queue<vector<int>, vector<vector<int>> , greater<vector<int>>>pq;\n pq.push({0 , 0 ,0});\n int ans =0;\n set<pair<int , int>>vis;\n while(!pq.empty()){\n vector<int>v = pq.top();\n pq.pop();\n ans = v[0];\n int ri = v[1];\n int ki = v[2];\n char ch = key[ki];\n \nif(ki == m){\n break;\n}\n if(vis.count({ri , ki})){\n continue;\n }\n vis.insert({ ri , ki});\n for(auto &i : adj[ch]){\n int steps = countsteps(ri , i , n)+ ans;\n\n pq.push({ steps , i , ki+1});\n }\n }\nreturn ans +m;\n\n }\n};",
"memory": "33466"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n unordered_map<char,vector<int>>mp;\n // vector<vector<int>>dp;\n int n;\n\n // int solve(string&ring, string&key,int keyIndex,int prevIndex){\n // if(keyIndex>=key.size()){\n // return 0;\n // }\n // if(dp[keyIndex][prevIndex]!=-1)return dp[keyIndex][prevIndex];\n // char ch = key[keyIndex];\n // int ans = 1e6;\n // for(auto ind:mp[ch]){\n // int diff = abs(ind - prevIndex);\n // int step = min(diff, n - diff); // Min of clockwise and counter-clockwise\n // ans = min(ans,1 + step + solve(ring,key,keyIndex+1,ind));\n // }\n // return dp[keyIndex][prevIndex] = ans;\n // }\n\n int findRotateSteps(string ring, string key) {\n // ring = ring + ring;\n n = ring.size();\n for(int i=0;i<ring.size();i++){\n mp[ring[i]].push_back(i);\n }\n // dp = vector<vector<int>>(key.size(),vector<int>(ring.size(),-1));\n // int keyIndex = 0;\n // return solve(ring,key,keyIndex,0);\n\n // Min-heap priority queue: stores {total_distance, key_index, ring_index}\n priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> pq;\n pq.push({0, 0, 0}); // {current_distance, keyIndex, prevIndex}\n\n // Visited array to keep track of the minimum distance reached for each state\n vector<vector<int>> visited(key.size(), vector<int>(n, INT_MAX));\n\n while (!pq.empty()) {\n auto v = pq.top(); pq.pop();\n int dis = v[0];\n int keyIndex = v[1];\n int prevIndex = v[2];\n\n // If we've reached the end of the key, return the distance\n if (keyIndex >= key.size()) return dis;\n\n // If this state has been visited with a smaller or equal distance, skip it\n if (dis >= visited[keyIndex][prevIndex]) continue;\n \n // Mark this state as visited with the current distance\n visited[keyIndex][prevIndex] = dis;\n\n for (auto ind : mp[key[keyIndex]]) {\n int dis1 = abs(prevIndex - ind);\n int dis2 = n - dis1;\n int newDis = dis + min(dis1, dis2) + 1; // +1 for pressing the button\n pq.push({newDis, keyIndex + 1, ind});\n }\n }\n return 0;\n }\n};",
"memory": "34698"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findRotateSteps(string ring, string key) {\n int ringLen = ring.length();\n int keyLen = key.length();\n \n // HashMap to store the indices of occurrences of each character in the ring\n unordered_map<char, vector<int>> characterIndices;\n for (int i = 0; i < ring.length(); i++) {\n char ch = ring[i];\n characterIndices[ch].push_back(i);\n }\n\n // Initialize the heap (priority queue) with the starting point\n // Each element of the heap is a vector of integers representing:\n // totalSteps, ringIndex, keyIndex\n priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> heap;\n heap.push({0, 0, 0});\n\n // HashSet to keep track of visited states\n unordered_set<string> seen;\n \n // Spell the keyword using the metal dial\n int totalSteps = 0;\n while (!heap.empty()) {\n // Pop the element with the smallest total steps from the heap\n vector<int> state = heap.top();\n heap.pop();\n totalSteps = state[0];\n int ringIndex = state[1];\n int keyIndex = state[2];\n \n // We have spelled the keyword\n if (keyIndex == keyLen) {\n break;\n }\n \n // Continue if we have visited this character from this position in ring before\n string currentState = to_string(ringIndex) + \"-\" + to_string(keyIndex);\n if (seen.count(currentState)) {\n continue;\n }\n\n // Otherwise, add this pair to the visited list\n seen.insert(currentState);\n \n // Add the rest of the occurrences of this character in ring to the heap\n for (int nextIndex : characterIndices[key[keyIndex]]) {\n heap.push({totalSteps + countSteps(ringIndex, nextIndex, ringLen), \n nextIndex, keyIndex + 1});\n }\n }\n \n // Return the total steps and add keyLen to account for \n // pressing the center button for each character in the keyword\n return totalSteps + keyLen;\n }\n\nprivate:\n // Find the minimum steps between two indexes of ring\n int countSteps(int curr, int next, int ringLength) {\n int stepsBetween = abs(curr - next);\n int stepsAround = ringLength - stepsBetween;\n return min(stepsBetween, stepsAround);\n }\n};",
"memory": "34698"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findRotateSteps(string ring, string key) {\n int ringLen = ring.length();\n int keyLen = key.length();\n \n // HashMap to store the indices of occurrences of each character in the ring\n unordered_map<char, vector<int>> characterIndices;\n for (int i = 0; i < ring.length(); i++) {\n char ch = ring[i];\n characterIndices[ch].push_back(i);\n }\n\n // Initialize the heap (priority queue) with the starting point\n // Each element of the heap is a vector of integers representing:\n // totalSteps, ringIndex, keyIndex\n priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> heap;\n heap.push({0, 0, 0});\n\n // HashSet to keep track of visited states\n unordered_set<string> seen;\n \n // Spell the keyword using the metal dial\n int totalSteps = 0;\n while (!heap.empty()) {\n // Pop the element with the smallest total steps from the heap\n vector<int> state = heap.top();\n heap.pop();\n totalSteps = state[0];\n int ringIndex = state[1];\n int keyIndex = state[2];\n \n // We have spelled the keyword\n if (keyIndex == keyLen) {\n break;\n }\n \n // Continue if we have visited this character from this position in ring before\n string currentState = to_string(ringIndex) + \"-\" + to_string(keyIndex);\n if (seen.count(currentState)) {\n continue;\n }\n\n // Otherwise, add this pair to the visited list\n seen.insert(currentState);\n \n // Add the rest of the occurrences of this character in ring to the heap\n for (int nextIndex : characterIndices[key[keyIndex]]) {\n heap.push({totalSteps + countSteps(ringIndex, nextIndex, ringLen), \n nextIndex, keyIndex + 1});\n }\n }\n \n // Return the total steps and add keyLen to account for \n // pressing the center button for each character in the keyword\n return totalSteps + keyLen;\n }\n\nprivate:\n // Find the minimum steps between two indexes of ring\n int countSteps(int curr, int next, int ringLength) {\n int stepsBetween = abs(curr - next);\n int stepsAround = ringLength - stepsBetween;\n return min(stepsBetween, stepsAround);\n }\n};",
"memory": "35931"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 2 | {
"code": "// f(i, j) = minimum number of steps to spell key[j..)\n// assuming we start with a ring rotated clockwise\n// i times (i.e., ring[i] is at 12:00)\n\n//\n// clockwise(i, c) = first occurrence of character c rotating\n// clockwise starting with ring[i] at 12:00\n//\n// anticlockwise(i, c) = first occurrence of character c rotating\n// anticlockwise starting with ring[i] at 12:00\n//\n// f(i, j) = find closest occurrence of key[j] by rotating\n// ring in either direction, and take the one that\n// minimizes f(., j + 1)\n// = 1 + min(f(clockwise(i, c), j + 1), f(anticlockwise(i, c), j + 1))\n// \n// f(i, m) = 0 \n\n// n = |ring|\n// m = |key|\n// Time:\n// - Computing clockwise and anticlockwise takes O(n) time (there are O(1) lowercase English letters)\n// - Computing f takes O(nm) with DP\n// Space: O(n)\n\nostream& operator<<(ostream& out, const vector<unordered_map<char, int>>& v) {\n for (int i = 0; i < v.size(); ++i) {\n out << \"v[\" << i << \"] = { \";\n for (auto kv : v[i]) {\n out << \"(\" << kv.first << \",\" << kv.second << \")\";\n }\n out << endl;\n }\n return out;\n}\n\nclass Solution {\npublic:\n vector<unordered_map<char, int>> computeClockwiseDistances(string& ring) {\n int n = ring.size();\n vector<unordered_map<char, int>> res(n);\n unordered_set<char> alphabet;\n for (int i = 0; i < n; ++i) {\n char c = ring[i];\n if (res[0].count(c) == 0) {\n res[0][c] = i;\n }\n alphabet.insert(c);\n }\n for (int i = n - 1; i >= 1; --i) {\n for (auto c : alphabet) {\n res[i][c] = (ring[i] == c) ? i : res[(i + 1) % n][c];\n }\n }\n return res;\n }\n\n void fix(vector<unordered_map<char, int>>& anticlockwise) {\n reverse(anticlockwise.begin(), anticlockwise.end());\n int n = anticlockwise.size();\n for (int i = 0; i < n; ++i) {\n for (auto kv : anticlockwise[i]) {\n anticlockwise[i][kv.first] = n - kv.second - 1; \n }\n }\n }\n\n int findRotateSteps(string ring, string key) {\n int n = ring.size();\n int m = key.size();\n vector<unordered_map<char, int>> clockwise = computeClockwiseDistances(ring);\n string reversed = ring;\n reverse(reversed.begin(), reversed.end());\n vector<unordered_map<char, int>> anticlockwise = computeClockwiseDistances(reversed);\n fix(anticlockwise);\n vector<int> prev(n);\n vector<int> next(n, 0);\n for (int j = m - 1; j >= 0; --j) {\n prev = next;\n for (int i = 0; i < n; ++i) {\n int cw;\n if (clockwise[i][key[j]] >= i) {\n cw = clockwise[i][key[j]] - i;\n } else {\n cw = n - i + clockwise[i][key[j]];\n }\n int acw;\n if (anticlockwise[i][key[j]] <= i) {\n acw = i - anticlockwise[i][key[j]];\n } else {\n acw = i + n - anticlockwise[i][key[j]];\n }\n next[i] = 1 + std::min(cw + prev[clockwise[i][key[j]]], acw + prev[anticlockwise[i][key[j]]]);\n }\n }\n return next[0];\n }\n};",
"memory": "35931"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 3 | {
"code": "// f(i, j) = minimum number of steps to spell key[j..)\n// assuming we start with a ring rotated clockwise\n// i times (i.e., ring[i] is at 12:00)\n\n//\n// clockwise(i, c) = first occurrence of character c rotating\n// clockwise starting with ring[i] at 12:00\n//\n// anticlockwise(i, c) = first occurrence of character c rotating\n// anticlockwise starting with ring[i] at 12:00\n//\n// f(i, j) = find closest occurrence of key[j] by rotating\n// ring in either direction, and take the one that\n// minimizes f(., j + 1)\n// = 1 + min(f(clockwise(i, c), j + 1), f(anticlockwise(i, c), j + 1))\n// \n// f(i, m) = 0 \n\n// n = |ring|\n// m = |key|\n// Time:\n// - Computing clockwise and anticlockwise takes O(n) time (there are O(1) lowercase English letters)\n// - Computing f takes O(nm) with DP\n// Space: O(n)\n\nostream& operator<<(ostream& out, const vector<unordered_map<char, int>>& v) {\n for (int i = 0; i < v.size(); ++i) {\n out << \"v[\" << i << \"] = { \";\n for (auto kv : v[i]) {\n out << \"(\" << kv.first << \",\" << kv.second << \")\";\n }\n out << endl;\n }\n return out;\n}\n\nclass Solution {\npublic:\n vector<unordered_map<char, int>> computeClockwiseDistances(string& ring) {\n int n = ring.size();\n vector<unordered_map<char, int>> res(n);\n unordered_set<char> alphabet;\n for (int i = 0; i < n; ++i) {\n char c = ring[i];\n if (res[0].count(c) == 0) {\n res[0][c] = i;\n }\n alphabet.insert(c);\n }\n for (int i = n - 1; i >= 1; --i) {\n for (auto c : alphabet) {\n res[i][c] = (ring[i] == c) ? i : res[(i + 1) % n][c];\n }\n }\n return res;\n }\n\n void fix(vector<unordered_map<char, int>>& anticlockwise) {\n reverse(anticlockwise.begin(), anticlockwise.end());\n int n = anticlockwise.size();\n for (int i = 0; i < n; ++i) {\n for (auto kv : anticlockwise[i]) {\n anticlockwise[i][kv.first] = n - kv.second - 1; \n }\n }\n }\n\n int findRotateSteps(string ring, string key) {\n int n = ring.size();\n int m = key.size();\n vector<unordered_map<char, int>> clockwise = computeClockwiseDistances(ring);\n string reversed = ring;\n reverse(reversed.begin(), reversed.end());\n vector<unordered_map<char, int>> anticlockwise = computeClockwiseDistances(reversed);\n fix(anticlockwise);\n\n cout << clockwise << endl;\n cout << anticlockwise << endl;\n\n vector<int> prev(n);\n vector<int> next(n, 0);\n for (int j = m - 1; j >= 0; --j) {\n prev = next;\n for (int i = 0; i < n; ++i) {\n int cw;\n if (clockwise[i][key[j]] >= i) {\n cw = clockwise[i][key[j]] - i;\n } else {\n cw = n - i + clockwise[i][key[j]];\n }\n int acw;\n if (anticlockwise[i][key[j]] <= i) {\n acw = i - anticlockwise[i][key[j]];\n } else {\n acw = i + n - anticlockwise[i][key[j]];\n }\n next[i] = 1 + std::min(cw + prev[clockwise[i][key[j]]], acw + prev[anticlockwise[i][key[j]]]);\n }\n }\n return next[0];\n }\n};",
"memory": "37163"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n typedef pair<int,pair<int,int>> p;\n int minSteps(int ri, int i, int n){\n int dist = abs(ri-i);\n int wrapAround = n-dist;\n return min(dist, wrapAround);\n }\n int findRotateSteps(string ring, string key) {\n unordered_map<char, vector<int>>mp; int m = key.size();\n int n = ring.size();\n for (int i = 0 ; i < n ; i++){\n char c = ring[i];\n mp[c].push_back(i);\n }\n priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>>pq;\n set<vector<int>>st;\n pq.push({0, 0, 0});\n int totSteps = 0;\n while(!pq.empty()){\n auto vec = pq.top(); pq.pop();\n totSteps = vec[0]; int ri = vec[1]; int ki = vec[2]; char currChar = key[ki];\n if (ki == m) break;\n if (st.count({ri, ki})) continue;\n st.insert({ri, ki});\n for (auto &it : mp[currChar]){\n int nIdx = it;\n int nextSteps = minSteps(ri, nIdx, n);\n pq.push({nextSteps+totSteps, nIdx, ki+1});\n }\n }\n return totSteps+m;\n }\n};",
"memory": "37163"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\nprivate:\n int dp[101][101];\n vector< int >indexes[ 26 ];\n int len , key_len;\npublic:\n Solution() {\n ios_base :: sync_with_stdio( false );\n cin.tie( nullptr ) , cout.tie( nullptr );\n memset( dp , -1 , sizeof(dp) );\n }\n int fun(int n , string key , int prev) {\n if( n == key_len ) return 0;\n if( dp[n][prev] != -1 ) return dp[n][prev] ;\n\n int ans = INT_MAX;\n for(int ms : indexes[ key[n]-'a' ]) {\n int clk = (ms - prev + len ) %len ;\n int a_clk = (prev - ms + len ) %len ;\n // cout << key[prev-1] << \" \" << key[n] << \" \" << n << \" : \" << clk << \" \" << a_clk << endl;\n ans = min( ans , min( clk , a_clk) + fun( n+1 , key , ms) );\n }\n return dp[n][prev] = ans+1 ;\n }\n int findRotateSteps(string ring, string key) {\n len = ring.length() , key_len = key.length() ;\n for(int i=1 ; i<=ring.length() ; i++) {\n indexes[ ring[i-1]-'a' ].push_back( i );\n }\n return fun( 0 , key , 1 );\n }\n};",
"memory": "42093"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n // int solve(string ring, string key, int i, int j){\n // int n = ring.length();\n // int m = key.length();\n\n // if(i==n || j==m) return 0;\n // int tans = INT_MAX;\n // if(ring[i] == key[j]){\n // tans = min(tans, 1+solve(ring, key, i, j+1));\n // }\n // else tans = min(tans, 1+solve(ring, key, i+1, j));\n\n // return tans;\n // }\n\n // int solve1(string ring, string key, int i, int j){\n // int n = ring.length();\n // int m = key.length();\n\n // if(i==-n || j==m) return 0;\n // int tans = INT_MAX;\n // if(ring[i] == key[j]){\n // tans = min(tans, 1+solve1(ring, key, i, j+1));\n // }\n // else tans = min(tans, 1+solve1(ring, key, i-1, j));\n\n // return tans;\n // }\n\n // int findRotateSteps(string ring, string key) {\n \n // int n = ring.length();\n // int m = key.length();\n // int cnt = 0;\n // int j=0;\n\n // int ans1 = solve(ring, key, 0, 0);\n // int ans2 = solve1(ring, key, 0, 0);\n\n // return min(ans1, ans2);\n // }\n\n\n int solve(string ring, string key, int i, int j, vector<vector<int>>& dp) {\n int n = ring.length();\n int m = key.length();\n\n if (j == m) return 0; // All characters of 'key' are processed\n\n if (dp[i][j] != -1) return dp[i][j]; // Memoization\n\n int tans = INT_MAX;\n\n // Try rotating right\n for (int k = 0; k < n; ++k) {\n int next = (i + k) % n;\n if (ring[next] == key[j]) {\n tans = min(tans, k + solve(ring, key, next, j + 1, dp));\n break;\n }\n }\n\n // Try rotating left\n for (int k = 0; k < n; ++k) {\n int next = (i - k + n) % n;\n if (ring[next] == key[j]) {\n tans = min(tans, k + solve(ring, key, next, j + 1, dp));\n break;\n }\n }\n return dp[i][j] = tans + 1; // +1 for pressing the button\n}\n\n int findRotateSteps(string ring, string key) {\n int n = ring.length();\n vector<vector<int>> dp(n, vector<int>(key.length(), -1)); // Memoization table\n return solve(ring, key, 0, 0, dp);\n }\n};",
"memory": "43326"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long int solve(string ring,string key,int i,int j,vector<vector<int>>&dp){\n if(i>=key.length()){\n return 0;\n }\n if(dp[i][j]!=-1){\n return dp[i][j];\n }\n //check clockwise\n long long clockwise=0;\n int k=j;\n while(ring[k]!=key[i]){\n k++;\n clockwise++;\n k%=((int)(ring.length()));\n }\n clockwise+=1+solve(ring,key,i+1,k,dp);\n \n //check anticlockwise\n long long antiClockwise=0;\n k=j;\n while(ring[k]!=key[i]){\n k--;\n antiClockwise++;\n k=(k+(int)ring.length())%((int)ring.length());\n }\n antiClockwise+=1+solve(ring,key,i+1,k,dp);\n return dp[i][j]=min(clockwise,antiClockwise);\n }\n int findRotateSteps(string ring, string key) {\n int m=ring.length();\n int n=key.length();\n vector<vector<int>>dp(n,vector<int>(m,-1));\n return solve(ring,key,0,0,dp);\n }\n};",
"memory": "44558"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findRotateSteps(string ring, string key) {\n for (int i = 0; i < ring.size(); i++)\n {\n mMapToCharPos[ring[i]].push_back(i);\n }\n\n return minNumSteps(0, 0, key, ring.size());\n }\n\n int minNumSteps(int starting, int keyNum, string key, int ringSize)\n {\n std::pair<int, int> mapKey = {starting, keyNum};\n if (mMemo.find(mapKey) != mMemo.end())\n return mMemo[mapKey];\n if (keyNum == key.size() - 1)\n {\n int minDistance = std::numeric_limits<int>::max();\n for (auto& option : mMapToCharPos[key[keyNum]])\n {\n int highNum = starting > option ? starting : option;\n int lowNum = starting > option ? option : starting;\n int minToThisOption = min(highNum-lowNum, (lowNum + ringSize - highNum)); \n minDistance = min(minDistance, minToThisOption);\n }\n mMemo[mapKey] = minDistance + 1;\n return minDistance + 1;\n }\n int minDistance = std::numeric_limits<int>::max();\n for (auto& option : mMapToCharPos[key[keyNum]])\n {\n int highNum = starting > option ? starting : option;\n int lowNum = starting > option ? option : starting;\n int minToThisOption = min(highNum-lowNum, (lowNum + ringSize - highNum)) \n + minNumSteps(option, keyNum + 1, key, ringSize);\n minDistance = min(minDistance, minToThisOption);\n }\n mMemo[mapKey] = minDistance + 1;\n return minDistance + 1;\n }\nprivate:\n std::unordered_map<char, std::vector<int>> mMapToCharPos;\n std::map<std::pair<int, int>, int> mMemo;\n\n};",
"memory": "45791"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int f(int i,int p, int n, int m, string key,vector<vector<int>> &id,vector<vector<int>> &dp){\n if(i>=key.length()) return 0;\n if(dp[i][p]!=-1) return dp[i][p];\n int mini = 1e8; \n for(auto x : id[key[i]-'a']){\n int cost = min(abs(p-x),abs(m-abs(p-x))) + 1 + f(i+1,x,n,m,key,id,dp); \n mini = min(mini,cost); \n }\n return dp[i][p] = mini;\n }\n int findRotateSteps(string ring, string key) {\n vector<vector<int>> id(26);\n for(int i=0;i<ring.length();i++){\n id[ring[i]-'a'].push_back(i);\n }\n int n = key.length();\n int m = ring.length();\n vector<vector<int>> dp(n+1,vector<int>(m+1,-1));\n int ans = f(0,0,n,m,key,id,dp);\n return ans;\n }\n};",
"memory": "47023"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\nprivate:\n // Recursive function to perform DFS with memoization\n int dfs(int index, int pos, int n, int m, unordered_map<char, vector<int>>& adj, string key, vector<vector<int>>& dp) {\n // Base Case: If we have processed all characters in the key\n if (index >= m) {\n return 0;\n }\n \n // Check if we have already computed the result for the current position and index\n if (dp[index][pos] != -1) {\n return dp[index][pos]; // Return the memoized result\n }\n \n int min_steps = INT_MAX;\n \n // Iterate over the positions of the current character in the ring\n for (auto it : adj[key[index]]) {\n int steps;\n // Calculate the minimum steps required to move from the current position to 'it'\n if (it >= pos) {\n steps = min(it - pos, pos + n - it);\n } else {\n steps = min(pos - it, it + n - pos);\n }\n // Recursively calculate the minimum steps required for the remaining characters\n min_steps = min(min_steps, (steps + dfs(index + 1, it, n, m, adj, key, dp)));\n }\n \n // Memoize the result and return\n return dp[index][pos] = min_steps + 1; // Add 1 for pressing 'Enter'\n }\n \npublic:\n int findRotateSteps(string ring, string key) {\n int n = ring.size(); // Size of the ring\n int m = key.size(); // Size of the key\n unordered_map<char, vector<int>> adj; // Adjacency list to store positions of characters\n \n // Creating a position map (adjacency list)\n for (int i = 0; i < n; i++) {\n adj[ring[i]].push_back(i);\n }\n \n // Initialize the memoization table with -1\n vector<vector<int>> dp(m + 1, vector<int>(n + 1, -1));\n \n // Start DFS from index 0, position 0\n return dfs(0, 0, n, m, adj, key, dp);\n }\n};",
"memory": "48256"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\nprivate:\n // int dp[101][101];\n vector< int >indexes[ 26 ];\n int len , key_len;\npublic:\n Solution() {\n ios_base :: sync_with_stdio( false );\n cin.tie( nullptr ) , cout.tie( nullptr );\n // memset( dp , -1 , sizeof(dp) );\n }\n int fun(int n , string key , int prev ,vector< vector<int>> &dp) {\n if( n == key_len ) return 0;\n if( dp[n][prev] != -1 ) return dp[n][prev] ;\n\n int ans = INT_MAX;\n for(int ms : indexes[ key[n]-'a' ]) {\n int clk = (ms - prev + len ) %len ;\n int a_clk = (prev - ms + len ) %len ;\n // cout << key[prev-1] << \" \" << key[n] << \" \" << n << \" : \" << clk << \" \" << a_clk << endl;\n ans = min( ans , min( clk , a_clk) + fun( n+1 , key , ms ,dp) );\n }\n return dp[n][prev] = ans+1 ;\n }\n int findRotateSteps(string ring, string key) {\n len = ring.length() , key_len = key.length() ;\n for(int i=1 ; i<=ring.length() ; i++) {\n indexes[ ring[i-1]-'a' ].push_back( i );\n }\n vector< vector<int>> dp( key.length()+1 , vector<int>( ring.length()+1 , -1));\n return fun( 0 , key , 1 , dp);\n }\n};",
"memory": "49488"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\nprivate:\n unordered_map<char, vector<int>> adj; \n int dfs(int index, int pos, int n, int m,string key, vector<vector<int>>& dp) {\n \n if (index >= m) {\n return 0;\n }\n \n if (dp[index][pos] != -1) {\n return dp[index][pos];\n }\n \n int min_steps = INT_MAX;\n \n \n for (auto it : adj[key[index]]) {\n int d1=abs(it-pos);\n int d2=abs(d1-n);\n int steps=min(d1,d2);\n \n min_steps = min(min_steps, (steps + dfs(index + 1, it, n, m, key, dp)));\n }\n \n \n return dp[index][pos] = min_steps + 1;\n }\n \npublic:\n int findRotateSteps(string ring, string key) {\n int n = ring.size(); \n int m = key.size(); \n \n for (int i = 0; i < n; i++) {\n adj[ring[i]].push_back(i);\n }\n \n vector<vector<int>> dp(m + 1, vector<int>(n + 1, -1));\n\n return dfs(0, 0, n, m, key, dp);\n }\n};",
"memory": "50721"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int n;\n vector<vector<int>> minSteps;\n vector<vector<int>> cache;\n unordered_map<char, vector<int>> potential_idxs;\n int findRotateSteps(string ring, string key) {\n // we only have two decisions: clockwise or anti-clockwise\n // in the string, this is really left or right from curr position, with a looparound\n\n\n // build a table of distances of char to another char for easy reference\n // remember all distances have +1 to press the button\n // minSteps[from][to] contains num steps incurred to transition\n n = ring.length();\n minSteps.resize(n, vector<int>(n, 1));\n for (int i=0; i<n; i++) {\n for (int j=i; j<n; j++) {\n minSteps[i][j] = 1 + min(i + n - j, j - i); // CW or anti-CW\n minSteps[j][i] = minSteps[i][j];\n }\n }\n\n for (int i=0; i<n; i++) {\n potential_idxs[ring[i]].push_back(i);\n }\n\n cache.resize(key.size(), vector<int>(n,-1));\n int min_steps_to_finish = INT_MAX;\n // find starting cost for initial turn\n for (int j : potential_idxs[key[0]]) {\n int starting_cost = minSteps[0][j] + dfs(j, 0, key);\n min_steps_to_finish = min(min_steps_to_finish, starting_cost);\n }\n return min_steps_to_finish;\n\n }\n\n int dfs(int i, int idx, string key) {\n // from idx to idx + 1\n // base case, button press accounted for in transition to this state \n if (idx == key.size() - 1) return 0;\n if (cache[idx][i] != -1) return cache[idx][i];\n // check best of CW or anti-CW\n int min_steps_to_finish = INT_MAX;\n char next_char = key[idx+1];\n for (int j : potential_idxs[next_char]) {\n int steps_to_finish = minSteps[i][j] + dfs(j, idx+1, key);\n min_steps_to_finish = min(min_steps_to_finish, steps_to_finish);\n }\n\n cache[idx][i] = min_steps_to_finish;\n return min_steps_to_finish;\n }\n};",
"memory": "51953"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n pair<int,int> fclock(string ring, char target, int ind) {\n int n = ring.length();\n int ans = 0;\n int j = ind;\n int count1 = 0;\n while(ring[j]!=target)\n {\n count1++;\n j = (j+1)%n;\n }\n count1++;\n return {count1, j};\n }\n\n pair<int,int> fanti(string ring, char target, int ind) {\n int n = ring.length();\n int k = ind;\n int count2 = 0;\n while(ring[k]!=target)\n {\n count2++;\n k--;\n if(k==-1) k = n-1; \n }\n count2++;\n return {count2, k};\n }\n\n int fun(int i, int j, string ring, string key,vector<vector<int>> &dp)\n {\n if(i>=key.length()) return 0;\n if(dp[i][j]!=-1) return dp[i][j];\n int clock = INT_MAX;\n pair<int,int> pclock;\n pclock = fclock(ring, key[i],j);\n clock = pclock.first + fun(i+1, pclock.second,ring,key,dp);\n int anti = INT_MAX;\n pair<int,int> panti;\n panti = fanti(ring,key[i],j);\n anti = panti.first + fun(i+1, panti.second,ring,key,dp);\n\n return dp[i][j] = min(clock,anti);\n }\n int findRotateSteps(string ring, string key) {\n vector<vector<int>> dp(key.length(), vector<int>(ring.length(),-1));\n int ans = fun(0,0,ring, key,dp);\n return ans;\n }\n};",
"memory": "53186"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n // i = current state of key ring, index of string\n int solve(string ring, string key, int i, int j, vector<vector<int>>& dp){\n int rs = ring.size();\n int k = key.size();\n // base case\n if (j == k){\n return 0;\n }\n\n if (dp[i][j]!= -1){\n return dp[i][j];\n }\n\n int ans = INT_MAX;\n // no turn\n if (ring[i] == key[j]){\n ans = 1 + solve(ring, key, i, j+1, dp);\n } else {\n int ind = 0;\n int right = INT_MAX;\n int left = INT_MAX;\n // search right\n for (int r = 1; r < rs; r++){\n ind = (i+r)%rs;\n if (ring[ind] == key[j]){\n right = r + solve(ring, key, ind, j+1, dp) + 1;\n break;\n }\n }\n // search left;\n for (int l = 1; l < rs; l++){\n ind = (i-l+rs)%rs;\n if (ring[ind] == key[j]){\n left = l + solve(ring, key, ind, j+1, dp) + 1;\n break;\n }\n }\n ans = min(left, right);\n }\n dp[i][j] = ans;\n return ans;\n }\n\n int findRotateSteps(string ring, string key) {\n int rs = ring.size();\n int ks = key.size();\n vector<vector<int>> dp(rs, vector<int>(ks, -1));\n return solve(ring, key, 0, 0, dp);\n }\n};",
"memory": "54418"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n // i = current state of key ring, index of string\n int solve(string ring, string key, int i, int j, vector<vector<int>>& dp){\n int rs = ring.size();\n int k = key.size();\n // base case\n if (j == k){\n return 0;\n }\n\n if (dp[i][j]!= -1){\n return dp[i][j];\n }\n\n int ans = INT_MAX;\n // no turn\n if (ring[i] == key[j]){\n ans = 1 + solve(ring, key, i, j+1, dp);\n } else {\n int ind = 0;\n int right = INT_MAX;\n int left = INT_MAX;\n // search right\n for (int r = 1; r < rs; r++){\n ind = (i+r)%rs;\n if (ring[ind] == key[j]){\n right = r + solve(ring, key, ind, j+1, dp) + 1;\n break;\n }\n }\n // search left;\n for (int l = 1; l < rs; l++){\n ind = (i-l+rs)%rs;\n if (ring[ind] == key[j]){\n left = l + solve(ring, key, ind, j+1, dp) + 1;\n break;\n }\n }\n ans = min(left, right);\n }\n dp[i][j] = ans;\n return ans;\n }\n\n int findRotateSteps(string ring, string key) {\n int rs = ring.size();\n int ks = key.size();\n vector<vector<int>> dp(rs, vector<int>(ks, -1));\n return solve(ring, key, 0, 0, dp);\n }\n};",
"memory": "54418"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nint dp[101][101];\n int countstep(int ri, int i, int n) {\n int clkw = abs(i - ri);\n int anticlw = n - clkw;\n return min(clkw, anticlw);\n }\n int solve(int ri, int ki, string ring, string key) {\n if (ki == key.size()) {\n return 0;\n }\n if(dp[ri][ki]!=-1){\n return dp[ri][ki];\n }\n int mini = INT_MAX;\n for (int i = 0; i < ring.size(); i++) {\n if (ring[i] == key[ki]) {\n int totalcount = countstep(ri, i, ring.length()) + 1 +\n solve(i, ki + 1, ring, key);\n\n mini = min(mini, totalcount);\n }\n }\n return dp[ri][ki] = mini;\n }\n int findRotateSteps(string ring, string key) {\n memset(dp,-1,sizeof(dp));\n return solve(0, 0, ring, key);\n }\n};",
"memory": "55651"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int dp[101][101];\n int solve(string ring, string key,int ke,int i)\n {\n if(i==key.size())\n return 0;\n if(dp[ke][i]!=-1)\n return dp[ke][i];\n int n=ring.size();\n int cnt=n,j=ke,jj=ke;\n int ans=INT_MAX;\n while(cnt--)\n {\n j=j%n;\n if(ring[j]==key[i])\n ans=min(ans,min(jj-ke,n-jj+ke)+solve(ring,key,j,i+1));\n j++;\n jj++;\n }\n return dp[ke][i]=ans;\n }\n int findRotateSteps(string ring, string key) {\n \n memset(dp,-1,sizeof(dp));\n return solve(ring,key,0,0)+key.size();\n }\n};",
"memory": "56883"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\n\n int solve(string &ring,string &key,int i,int j,int dir,vector<vector<vector<int>>>&dp){\n if(j>=key.size())return 0;\n if(i==-1)i=ring.size()-1;\n if(i==ring.size())i=0;\n if(dp[i][j][dir]!=-1)return dp[i][j][dir];\n\n int w1=INT_MAX,w2=INT_MAX;\n if(ring[i]!=key[j]){\n // go to left if dir==2\n\n // go to right if dir ==1\n if(dir==0){\n w1=1+solve(ring,key,i-1,j,2,dp);\n w2=1+solve(ring,key,i+1,j,1,dp);\n }\n else{\n if(dir==2){\n w1=1+solve(ring,key,i-1,j,dir,dp);\n }\n else w2=1+solve(ring,key,i+1,j,dir,dp);\n }\n\n return dp[i][j][dir]=min(w1,w2);\n }\n else{\n // 2 choice \n // choice 1 : accept and move to either left or ring\n int w3=solve(ring,key,i-1,j+1,2,dp);\n int w4=solve(ring,key,i+1,j+1,1,dp);\n int w5=solve(ring,key,i,j+1,0,dp);\n if(j+1<key.size())w3++,w4++;\n\n // Choice 2 reject\n // if(dir==0){\n // w1=1+solve(ring,key,i-1,j,2);\n // w2=1+solve(ring,key,i+1,j,1);\n // }\n // else{\n // if(dir==2){\n // w1=1+solve(ring,key,i-1,j,dir);\n // }\n // else w2=1+solve(ring,key,i+1,j,dir);\n // }\n return dp[i][j][dir]=min(w3,min(w4,w5));\n }\n }\npublic:\n int findRotateSteps(string ring, string key) {\n int ans=key.size();\n\n vector<vector<vector<int>>>dp(ring.size(),vector<vector<int>>(key.size(),vector<int>(3,-1)));\n // calc\n ans+=solve(ring,key,0,0,0,dp);\n\n return ans;\n }\n};",
"memory": "58116"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int f(int pos, int ind, string ring, vector<vector<int>>&dp, string key){\n if(ind == key.size()){\n return 0;\n }\n if(dp[ind][pos]!=-1) return dp[ind][pos];\n if(ring[pos]==key[ind]){\n return f(pos, ind+1, ring, dp, key)+1;\n }\n int tmp = pos, cnt=0;\n int n=ring.size();\n while(ring[tmp]!=key[ind]){\n tmp = (tmp+1)%n;\n cnt++;\n }\n int ans = f(tmp, ind+1, ring, dp, key)+1+cnt;\n tmp = pos;\n cnt = 0;\n while(ring[tmp]!=key[ind]){\n tmp = (tmp-1+n)%n;\n cnt++;\n }\n return dp[ind][pos]=min(ans, f(tmp, ind+1, ring, dp, key)+1+cnt);\n }\n int findRotateSteps(string ring, string key) {\n vector<vector<int>>dp(key.size()+1,vector<int>(ring.size()+1,-1));\n return f(0,0,ring,dp,key); \n }\n};",
"memory": "59348"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n int n , m;\n\n int minSteps(string & ring , string & key , int i , int j, int direction, vector<vector<vector<int>>> & dp)\n {\n if(j >= m) return 0;\n if(i >= n) i = 0;\n if(i < 0) i = n-1;\n\n if(dp[i][j][direction == 1] != -1) return dp[i][j][direction == 1];\n\n\n if(ring[i] == key[j]) \n {\n return dp[i][j][direction == 1] = min({\n minSteps(ring , key , i + 1 , j + 1, 1, dp) + 1 ,\n minSteps(ring , key , i - 1, j + 1, -1, dp) + 1 ,\n minSteps(ring , key , i , j + 1, 1 , dp), \n minSteps(ring, key , i , j + 1 , -1, dp)\n }) + 1;\n }\n\n return dp[i][j][direction == 1] = minSteps(ring, key, i + direction , j , direction, dp) + 1;\n }\n\n int findRotateSteps(string ring, string key) {\n n = ring.size() , m = key.size();\n vector<vector<vector<int>>> dp (n , vector<vector<int>> (m , vector<int> (2 , -1)));\n\n return min(minSteps(ring, key, 0 , 0 , 1 , dp) , minSteps(ring, key , 0 , 0 , -1, dp));\n\n }\n};",
"memory": "60581"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 3 | {
"code": "#include<bits/stdc++.h>\nclass Solution {\npublic:\n int dp[1001][1001];\n int solve(string ring,string key,int i,int j,int dp[1001][1001]){\n int ans = 1e9;\n int n = key.size();\n int m = ring.size();\n if(i>=n){\n return 0;\n }\n if(dp[i][j] != -1){\n return dp[i][j];\n }\n for(int k = 0;k<ring.size();k++){\n if(ring[k] == key[i]){\n int t = min(abs(k-j),m-abs(k-j));\n ans = min(ans,1+t+solve(ring,key,i+1,k,dp));\n }\n }\n return dp[i][j] = ans;\n }\n int findRotateSteps(string ring, string key) {\n memset(dp,-1,sizeof(dp));\n return solve(ring,key,0,0,dp);\n }\n};",
"memory": "61813"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int vini(int ind, string& ring, string& key, int rp, int c,\n vector<vector<vector<int>>>& dp) {\n int n = ring.length();\n if (ind == key.length())\n return 0;\n if (dp[ind][rp][c] != -1)\n return dp[ind][rp][c];\n if (key[ind] == ring[rp])\n return vini(ind + 1, ring, key, rp, 0, dp) + 1;\n\n int cwise = 1e9, acwise = 1e9;\n\n if (c == 1 || c == 0) {\n cwise = vini(ind, ring, key, (rp - 1 + n) % n, 1, dp) + 1;\n }\n\n if (c == 2 || c == 0) {\n acwise = vini(ind, ring, key, (rp + 1) % n, 2, dp) + 1;\n }\n\n return dp[ind][rp][c] = min(cwise, acwise);\n }\n\n int findRotateSteps(string ring, string key) {\n int n = key.length(), m = ring.length();\n vector<vector<vector<int>>> dp(n+1, vector<vector<int>>(m+1, vector<int>(3, -1)));\n return vini(0, ring, key, 0, 0, dp);\n }\n};\n",
"memory": "63046"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findRightShortestPath(int ringPos, int keyPos, string ring, string key) {\n int n = ring.size();\n int pathCount = 0;\n while(pathCount <= n && (ring[(ringPos + pathCount) % n] != key[keyPos])) {\n pathCount++;\n }\n return pathCount;\n }\n\n int findLeftShortestPath(int ringPos, int keyPos, string ring, string key) {\n int n = ring.size();\n int pathCount = 0;\n while(pathCount <= n && (ring[(n + ringPos - pathCount) % n] != key[keyPos])) {\n pathCount++;\n }\n return pathCount;\n }\n\n int findRotateStepsDP(int ringPos, int keyPos, string ring, string key, vector<vector<int>>& memo) {\n int n = ring.size();\n int m = key.size();\n if (keyPos >= m) {\n return 0;\n }\n if (memo[ringPos][keyPos] != -1) {\n return memo[ringPos][keyPos];\n }\n int leftDistance = findLeftShortestPath(ringPos, keyPos, ring, key);\n int rightDistance = findRightShortestPath(ringPos, keyPos, ring, key);\n int shortestPath = min(\n findRotateStepsDP((ringPos + rightDistance) % n, keyPos + 1, ring, key, memo) + rightDistance + 1,\n findRotateStepsDP((n + ringPos - leftDistance) % n, keyPos + 1, ring, key, memo) + leftDistance + 1\n );\n memo[ringPos][keyPos] = shortestPath;\n return shortestPath;\n }\n\n int findRotateSteps(string ring, string key) {\n vector<vector<int>> dp(ring.size(), vector<int> (key.size(), -1));\n return findRotateStepsDP(0, 0, ring, key, dp);\n }\n};",
"memory": "64278"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 3 | {
"code": "class Solution \n{\npublic:\n int method(int i1,int i2, string ring, string key,int m,int n, vector<vector<int>>&dp)\n {\n if(i2==n)\n return 0;\n if(dp[i1][i2]!=-1)\n return dp[i1][i2];\n int ans = INT_MAX;\n for(int i=0;i<m;i++)\n {\n if(ring[i]==key[i2])\n {\n int count = min(abs(i1-i),m-abs(i1-i));\n ans = min(ans, count + method(i, i2+1, ring, key, m, n,dp));\n }\n }\n return dp[i1][i2]=ans;\n }\n int findRotateSteps(string ring, string key)\n {\n int i1=0,i2=0;\n int n = key.size();\n int m = ring.size();\n vector<vector<int>>dp(m, vector<int>(n,-1));\n return method(i1,i2,ring, key,m,n,dp)+n;\n }\n};",
"memory": "65511"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n int solve(string ring,string key,int n,int m,int i,int j,vector<vector<int>> &dp){\n if(j==m) return 0;\n if(dp[i][j]!=-1) return dp[i][j];\n int ans=INT_MAX;\n for(int k=0;k<n;k++){\n if(ring[k]==key[j]){\n int diff=abs(k-i);\n int steps=min(diff,n-diff);\n ans=min(ans,steps+solve(ring,key,n,m,k,j+1,dp));\n }\n }\n return dp[i][j] = ans;\n }\n\n int findRotateSteps(string ring, string key) {\n int n=ring.length();\n int m=key.length();\n vector<vector<int>> dp(n,vector<int>(m,-1));\n return solve(ring, key,n,m,0,0,dp)+m;\n\n }\n};",
"memory": "66743"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 3 | {
"code": "class Solution \n{\npublic:\n int method(int i1,int i2, string ring, string key,int m,int n, vector<vector<int>>&dp)\n {\n if(i2==n)\n return 0;\n if(dp[i1][i2]!=-1)\n return dp[i1][i2];\n int ans = INT_MAX;\n for(int i=0;i<m;i++)\n {\n if(ring[i]==key[i2])\n {\n int count = min(abs(i1-i),m-abs(i1-i));\n ans = min(ans, count + method(i, i2+1, ring, key, m, n,dp));\n }\n }\n return dp[i1][i2]=ans;\n }\n int findRotateSteps(string ring, string key)\n {\n int i1=0,i2=0;\n int n = key.size();\n int m = ring.size();\n vector<vector<int>>dp(m, vector<int>(n,-1));\n return method(i1,i2,ring, key,m,n,dp)+n;\n }\n};",
"memory": "67976"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 3 | {
"code": "class Solution \n{\npublic:\n int method(int i1,int i2, string ring, string key,int m,int n, vector<vector<int>>&dp)\n {\n if(i2==n)\n return 0;\n if(dp[i1][i2]!=-1)\n return dp[i1][i2];\n int ans = INT_MAX;\n for(int i=0;i<m;i++)\n {\n if(ring[i]==key[i2])\n {\n int count = min(abs(i1-i),m-abs(i1-i));\n ans = min(ans, count + method(i, i2+1, ring, key, m, n,dp));\n }\n }\n return dp[i1][i2]=ans;\n }\n int findRotateSteps(string ring, string key)\n {\n int i1=0,i2=0;\n int n = key.size();\n int m = ring.size();\n vector<vector<int>>dp(m, vector<int>(n,-1));\n return method(i1,i2,ring, key,m,n,dp)+n;\n }\n};",
"memory": "69208"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int dist(int next,int curr,int ringSize){\n int a=abs(curr-next);\n int b=ringSize-a;\n return min(a,b);\n }\n int solve(int ringIndex,int keyIndex,string ring, string key,vector<vector<int>>&t){\n if(keyIndex>=key.size()){\n return 0;\n }\n if(t[ringIndex][keyIndex]!=-1){\n return t[ringIndex][keyIndex];\n }\n int mini=INT_MAX;\n for(int i=0;i<ring.size();i++){\n if(ring[i]==key[keyIndex]){\n int total=dist(i,ringIndex,ring.size())+1+solve(i,keyIndex+1,ring,key,t);\n mini=min(mini,total);\n }\n }\n return t[ringIndex][keyIndex] =mini;\n\n }\n int findRotateSteps(string ring, string key) {\n vector<vector<int>>t(ring.size()+1,vector<int>(key.size()+1,-1));\n return solve(0,0,ring,key,t);\n }\n};",
"memory": "70441"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int solve(string ring, string key, int ptr, int index, int n, int m, vector<vector<int>>&dp){\n //base\n if(index >= m) return 0;\n if(dp[ptr][index]!= -1) return dp[ptr][index];\n int ans =1e9;\n // choice\n for(int i=0; i<n; ++i){\n if(ring[i] == key[index]){\n ans = min(ans, min(abs(i- ptr), n-abs(i-ptr)) + 1 + solve(ring, key, i, index+1, n, m, dp));\n }\n }\n return dp[ptr][index]= ans;\n }\n int findRotateSteps(string ring, string key) {\n int n= ring.length(), m= key.length();\n int ptr=0, index=0;\n vector<vector<int>>dp(n+1, vector<int>(m+1, -1));\n return solve(ring, key, ptr, index, n, m,dp);\n }\n};",
"memory": "71673"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findRotateSteps(string ring, string key) {\n int n = ring.size(), m = key.size();\n vector<vector<int>> v(26);\n vector<vector<int>> memo(n, vector<int>(m));\n for (int i = 0; i < n; ++i) v[ring[i] - 'a'].push_back(i);\n return helper(ring, key, 0, 0, v, memo);\n }\n\n int helper(string ring, string key, int x, int y, vector<vector<int>>&v, vector<vector<int>>& memo) {\n if (y == key.size()) return 0;\n if (memo[x][y]) return memo[x][y];\n int res = INT_MAX, n = ring.size();\n for (int k : v[key[y] - 'a']) {\n int diff = abs(x - k);\n int step = min(diff, n - diff);\n res = min(res, step + helper(ring, key, k, y + 1, v, memo));\n }\n return memo[x][y] = res + 1;\n }\n};",
"memory": "72906"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findRotateSteps(string ring, string key) {\n unordered_map<char, vector<int>> m;\n vector<vector<int>> memo(ring.size(), vector<int>(key.size()));\n for (int i = 0; i < ring.size(); i++) m[ring[i]].push_back(i);\n return helper(m, ring, key, 0, 0, memo);\n }\n\n int helper(unordered_map<char, vector<int>>& m, string ring, string key, int i, int j, vector<vector<int>>& memo) {\n if (j == key.size()) return 0;\n if (memo[i][j] > 0) return memo[i][j];\n int res = INT_MAX;\n for (int pos : m[key[j]]) {\n int diff = abs(i - pos);\n int step = min(diff, (int)ring.size() - diff);\n res = min(res, step + helper(m, ring, key, pos, j + 1, memo));\n }\n return memo[i][j] = res + 1;\n }\n};",
"memory": "74138"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minStepsToRotate(int i, int j,string ring, string key, \n unordered_map<char, vector<int>> &ringIndex, vector<vector<int>> &dp){\n if(j>=key.length()) return 0;\n \n if(dp[i][j]!=-1) return dp[i][j];\n\n int steps = INT_MAX, n=ring.size();\n\n for(auto ind:ringIndex[key[j]]){\n int dist = min(abs(ind-i), n-abs(ind-i));\n steps = min(steps , dist + \n minStepsToRotate(ind, j+1, ring, key, ringIndex, dp));\n }\n return dp[i][j] = steps+1;\n }\n\n int findRotateSteps(string ring, string key) {\n unordered_map<char, vector<int>> ringIndex;\n int n=ring.size(), m=key.size();\n vector<vector<int>> dp(n, vector<int>(m,-1));\n for(int i=0;i<n;i++) ringIndex[ring[i]].push_back(i);\n return minStepsToRotate(0,0,ring,key, ringIndex, dp);\n }\n};",
"memory": "75371"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findRotateSteps(string ring, string key) {\n unordered_map<int, unordered_map<int, int>> bestSteps;\n return tryLock(0, 0, ring, key, INT_MAX, bestSteps);\n }\n\nprivate:\n // Find the minimum steps between two indexes of ring\n int countSteps(int curr, int next, int ringLength) {\n int stepsBetween = abs(curr - next);\n int stepsAround = ringLength - stepsBetween;\n return min(stepsBetween, stepsAround);\n }\n\n int tryLock(int ringIndex, int keyIndex, string ring, string key, int minSteps,\n unordered_map<int, unordered_map<int, int>>& bestSteps) {\n // If we have already calculated this sub-problem, return the result\n if (bestSteps[ringIndex][keyIndex]) {\n return bestSteps[ringIndex][keyIndex];\n }\n // If we reach the end of the key, it has been spelled\n if (keyIndex == key.length()) {\n return 0;\n }\n // For each occurrence of the character at key_index of key in ring\n // Calculate and save the minimum steps to that character from the ringIndex of ring\n for (int charIndex = 0; charIndex < ring.length(); charIndex++) {\n if (ring[charIndex] == key[keyIndex]) {\n int totalSteps = countSteps(ringIndex, charIndex, ring.length()) + 1\n + tryLock(charIndex, keyIndex + 1, ring, key, INT_MAX, bestSteps);\n minSteps = min(minSteps, totalSteps);\n bestSteps[ringIndex][keyIndex] = minSteps;\n }\n }\n return minSteps;\n }\n};",
"memory": "76603"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int check(string ring, string key, int ind, int start,vector<vector<int>> &dp){\n if(ind == key.size()){\n return 0;\n }\n if(dp[ind][start]!=-1){\n return dp[ind][start];\n }\n int ans = 1000000000;\n for(int i = 0; i<ring.size();i++){\n if(ring[i] == key[ind]){\n cout<<\"hi\"<<endl;\n int count = ring.size()-abs(start-i);\n if(abs(start-i)<count){\n count=abs(start-i);\n }\n cout<<count<<endl;\n ans = min(ans, count+check(ring,key,ind+1,i,dp));\n }\n }\n return dp[ind][start] = ans;\n }\n int findRotateSteps(string ring, string key) {\n vector<vector<int>> dp(key.size()+1,vector<int>(ring.size()+1,-1));\n return check(ring,key,0,0,dp)+key.size();\n }\n};",
"memory": "77836"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int func(int i,int k, string ring ,string key,vector<vector<int>> &dp) {\n if(i >= ring.size())return 1e6;\n if(k >= key.size())return 0;\n if(ring[i] == key[k]) return dp[i][k]=1 + func(i, k + 1, ring, key, dp);\n int n = ring.size();\n int mini = 1e6;\n if(dp[i][k]!= -1) return dp[i][k];\n for(int index = 0; index<ring.size(); index++) {\n if((index == i ) || ring[index] != key[k]) continue;\n cout<<ring.size() - abs(index- i )<<\" \";\n int l = 1 + min(abs(index-i),n -abs(index-i))+ func(index, k+1, ring, key, dp);\n mini = min(mini, l); \n }\n return dp[i][k] =mini;\n }\n int findRotateSteps(string ring, string key) {\n vector<vector<int>> dp(ring.size() + 1, vector<int> (key.size() + 1, -1));\n return func(0,0,ring, key, dp );\n }\n};",
"memory": "82766"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n \n vector<vector<int>> dp;\n \n int m,n;\n \n int help(string ring, string key, int indx, int ptr)\n {\n if(indx == n)\n {\n return 0;\n }\n \n if(dp[indx][ptr] != -1)\n {\n return dp[indx][ptr];\n }\n \n int ans = INT_MAX;\n \n for(int i=0; i<m; i++)\n {\n if(ring[i] == key[indx])\n {\n int x = abs(ptr - i);\n int steps = min(x, m - x);\n \n \n ans = min(ans, steps+help(ring,key,indx+1,i));\n }\n }\n \n return dp[indx][ptr] = ans+1; //..\n \n // // Store the result with an additional step for pressing\n }\n \n int findRotateSteps(string ring, string key) \n {\n m = ring.size(), n = key.size();\n \n dp.assign(101,vector<int>(101,-1));\n \n return help(ring,key,0,0);\n \n //https://www.youtube.com/watch?v=L1T6pd2etRI\n \n }\n};\n/*At the beginning (before any rotation), the start position is the initial position of the ring, which is typically the first character of the ring. So, when ptr is 0, it means the first character of the ring is aligned with the start position.\nptr represents the current index in the ring where the ring is currently aligned. For example, if ptr is 2, it means that the character at index 2 of ring is aligned with the start position. */",
"memory": "83998"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n \n vector<vector<int>> dp;\n \n int m,n;\n \n int help(string ring, string key, int indx, int ptr)\n {\n if(indx == n)\n {\n return 0;\n }\n \n if(dp[indx][ptr] != -1)\n {\n return dp[indx][ptr];\n }\n \n int ans = INT_MAX;\n \n for(int i=0; i<m; i++)\n {\n if(ring[i] == key[indx])\n {\n int x = abs(ptr - i);\n int steps = min(x, m - x);\n \n \n ans = min(ans, steps+help(ring,key,indx+1,i));\n }\n }\n \n return dp[indx][ptr] = ans+1; //..\n \n // // Store the result with an additional step for pressing\n }\n \n int findRotateSteps(string ring, string key) \n {\n m = ring.size(), n = key.size();\n \n dp.assign(101,vector<int>(101,-1));\n \n return help(ring,key,0,0);\n \n //https://www.youtube.com/watch?v=L1T6pd2etRI\n \n }\n};\n/*At the beginning (before any rotation), the start position is the initial position of the ring, which is typically the first character of the ring. So, when ptr is 0, it means the first character of the ring is aligned with the start position.\nptr represents the current index in the ring where the ring is currently aligned. For example, if ptr is 2, it means that the character at index 2 of ring is aligned with the start position. */",
"memory": "85231"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n map<pair<int,int>,int>dp;\n\n int solve(map<char,vector<int>>&clock, string key, int index, int pos, int n) {\n if(index==key.size())\n return 0;\n if(dp.find({index,pos})!=dp.end())\n return dp[{index,pos}];\n auto v = clock[key[index]];\n int ans = INT_MAX;\n for(int i = 0;i<v.size();i++) {\n int count = abs(v[i]-pos);\n int temp = count+1+solve(clock,key,index+1,v[i],n);\n ans = min(ans,temp);\n count = n-abs(v[i]-pos);\n temp = count+1+solve(clock,key,index+1,v[i],n);\n ans = min(ans,temp);\n }\n return dp[{index,pos}] = ans;\n }\n\n int findRotateSteps(string ring, string key) {\n int n = ring.length();\n map<char,vector<int>>clock;\n for(int i = 0;i<n;i++) {\n clock[ring[i]].push_back(i);\n }\n return solve(clock,key,0,0,n);\n }\n};",
"memory": "86463"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findRotateSteps(string ring, string key) {\n int n = ring.size();\n int m = key.size();\n vector<vector<int>> visited(n, vector<int>(m + 1, INT_MAX)); // Track minimum cost to reach (position, key_index)\n unordered_map<char, vector<int>> char_to_index; // Map each character to all its positions in ring\n\n // Preprocessing: map characters to their positions in the ring\n for (int i = 0; i < n; ++i) {\n char_to_index[ring[i]].push_back(i);\n }\n\n // BFS queue: (current_position, key_index, cost)\n queue<tuple<int, int, int>> q;\n q.push({0, 0, 0});\n visited[0][0] = 0;\n\n int min_steps = INT_MAX;\n\n while (!q.empty()) {\n auto [pos, key_idx, cost] = q.front();\n q.pop();\n\n // If we've spelled the entire key, update the minimum steps\n if (key_idx == m) {\n min_steps = min(min_steps, cost);\n continue;\n }\n\n char target_char = key[key_idx];\n \n // Explore all positions of the current target character\n for (int next_pos : char_to_index[target_char]) {\n // Calculate the rotation cost\n int steps = min(abs(next_pos - pos), n - abs(next_pos - pos)) + 1; // +1 for pressing the button\n int new_cost = cost + steps;\n\n // Only proceed if this is a cheaper way to reach this state\n if (new_cost < visited[next_pos][key_idx + 1]) {\n visited[next_pos][key_idx + 1] = new_cost;\n q.push({next_pos, key_idx + 1, new_cost});\n }\n }\n }\n\n return min_steps;\n }\n};\n",
"memory": "87696"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nint step(int k,int i,string ring){\n int dist = abs(i-k);\n return min(dist,(int)ring.size()-dist);\n}\nint solve(string ring ,string key,int i,int j,vector<vector<int>>&dp){\n if(j==key.size())return 0;\n if(dp[i][j]!=-1)return dp[i][j];\n int ans=INT_MAX;\n for(int k=0;k<ring.size();k++){\n if(ring[k]==key[j]){\n int cnt = 1+ step(k,i,ring);\n ans=min(ans,cnt+solve(ring,key,k,j+1,dp));\n }\n }\n return dp[i][j] = ans;\n}\n int findRotateSteps(string ring, string key) {\n vector<vector<int>>dp(ring.size(),vector<int>(key.size(),-1));\n return solve(ring,key,0,0,dp);\n }\n};",
"memory": "88928"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n int countSteps(int ring_idx, int key_idx, string ring) {\n int n = ring.size();\n int step1 = abs(key_idx - ring_idx);\n int step2 = n - step1;\n return min(step1, step2);\n }\n\n int solve(vector<vector<int>> &dp, int ring_idx, int key_idx, string ring, string key) {\n if(key_idx == key.size()) return 0;\n\n if(dp[ring_idx][key_idx]!=-1) return dp[ring_idx][key_idx];\n\n int res = INT_MAX;\n for(int i=0; i<ring.size(); i++) {\n if(ring[i] == key[key_idx]) {\n int steps = 1 + countSteps(ring_idx, i, ring) + solve(dp, i, key_idx+1, ring, key);\n res = min(res, steps);\n }\n }\n return dp[ring_idx][key_idx] = res;\n }\n\n int findRotateSteps(string ring, string key) {\n vector<vector<int>> dp(ring.size(), vector<int>(key.size(), -1));\n return solve(dp, 0, 0, ring, key);\n }\n};",
"memory": "90161"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\n public:\n int findRotateSteps(string ring, string key) {\n return dfs(ring, key, 0, {}) + key.length();\n }\n\n private:\n // Returns the number of rotates of ring to match key[index..n).\n int dfs(const string& ring, const string& key, int index,\n unordered_map<string, int>&& mem) {\n if (index == key.length())\n return 0;\n // Add the index to prevent duplication.\n const string hashKey = ring + to_string(index);\n if (const auto it = mem.find(hashKey); it != mem.cend())\n return it->second;\n\n int ans = INT_MAX;\n\n // For each ring[i] == key[index], we rotate the ring to match the ring[i]\n // with the key[index], then recursively match the newRing with the\n // key[index + 1..n).\n for (size_t i = 0; i < ring.length(); ++i)\n if (ring[i] == key[index]) {\n const int minRotates = min(i, ring.length() - i);\n const string& newRing = ring.substr(i) + ring.substr(0, i);\n const int remainingRotates =\n dfs(newRing, key, index + 1, std::move(mem));\n ans = min(ans, minRotates + remainingRotates);\n }\n\n return mem[hashKey] = ans;\n }\n};",
"memory": "91393"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\n public:\n int findRotateSteps(string ring, string key) {\n return dfs(ring, key, 0, {}) + key.length();\n }\n\n private:\n // Returns the number of rotates of ring to match key[index..n).\n int dfs(const string& ring, const string& key, int index,\n unordered_map<string, int>&& mem) {\n if (index == key.length())\n return 0;\n // Add the index to prevent duplication.\n const string hashKey = ring + to_string(index);\n if (const auto it = mem.find(hashKey); it != mem.cend())\n return it->second;\n\n int ans = INT_MAX;\n\n // For each ring[i] == key[index], we rotate the ring to match the ring[i]\n // with the key[index], then recursively match the newRing with the\n // key[index + 1..n).\n for (size_t i = 0; i < ring.length(); ++i)\n if (ring[i] == key[index]) {\n const int minRotates = min(i, ring.length() - i);\n const string& newRing = ring.substr(i) + ring.substr(0, i);\n const int remainingRotates =\n dfs(newRing, key, index + 1, std::move(mem));\n ans = min(ans, minRotates + remainingRotates);\n }\n\n return mem[hashKey] = ans;\n }\n};",
"memory": "92626"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\n public:\n int findRotateSteps(string ring, string key) {\n return dfs(ring, key, 0, {}) + key.length();\n }\n\n private:\n // Returns the number of rotates of ring to match key[index..n).\n int dfs(const string& ring, const string& key, int index,\n unordered_map<string, int>&& mem) {\n if (index == key.length())\n return 0;\n // Add the index to prevent duplication.\n const string hashKey = ring + to_string(index);\n if (const auto it = mem.find(hashKey); it != mem.cend())\n return it->second;\n\n int ans = INT_MAX;\n\n // For each ring[i] == key[index], we rotate the ring to match the ring[i]\n // with the key[index], then recursively match the newRing with the\n // key[index + 1..n).\n for (size_t i = 0; i < ring.length(); ++i)\n if (ring[i] == key[index]) {\n const int minRotates = min(i, ring.length() - i);\n const string& newRing = ring.substr(i) + ring.substr(0, i);\n const int remainingRotates =\n dfs(newRing, key, index + 1, std::move(mem));\n ans = min(ans, minRotates + remainingRotates);\n }\n\n return mem[hashKey] = ans;\n }\n};",
"memory": "92626"
} |
514 | <p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
<ol>
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
<pre>
<strong>Input:</strong> ring = "godding", key = "gd"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
Also, we need 1 more step for spelling.
So the final output is 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> ring = "godding", key = "godding"
<strong>Output:</strong> 13
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= ring.length, key.length <= 100</code></li>
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\n public:\n int findRotateSteps(string ring, string key) {\n return dfs(ring, key, 0, {}) + key.length();\n }\n\n private:\n // Returns the number of rotates of ring to match key[index..n).\n int dfs(const string& ring, const string& key, int index,\n unordered_map<string, int>&& mem) {\n if (index == key.length())\n return 0;\n // Add the index to prevent duplication.\n const string hashKey = ring + to_string(index);\n if (const auto it = mem.find(hashKey); it != mem.cend())\n return it->second;\n\n int ans = INT_MAX;\n\n // For each ring[i] == key[index], we rotate the ring to match the ring[i]\n // with the key[index], then recursively match the newRing with the\n // key[index + 1..n).\n for (size_t i = 0; i < ring.length(); ++i)\n if (ring[i] == key[index]) {\n const int minRotates = min(i, ring.length() - i);\n const string& newRing = ring.substr(i) + ring.substr(0, i);\n const int remainingRotates =\n dfs(newRing, key, index + 1, std::move(mem));\n ans = min(ans, minRotates + remainingRotates);\n }\n\n return mem[hashKey] = ans;\n }\n};",
"memory": "93858"
} |
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>
| 0 | {
"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 }\n queue<TreeNode*> q;\n q.push(root);\n\n while(!q.empty()){\n int maxi = INT_MIN;\n int size = q.size();\n\n for(int i=0;i<size;i++){\n TreeNode* temp = q.front();\n q.pop();\n maxi = max(maxi,temp->val);\n if(temp->left){\n q.push(temp->left);\n }\n if(temp->right){\n q.push(temp->right);\n }\n }\n ans.push_back(maxi);\n\n }\n root->left = NULL;\n root->right = NULL;\n return ans;\n }\n};",
"memory": "14900"
} |
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>
| 0 | {
"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 findlevel(TreeNode *root)\n {\n if(root == NULL)\n return 0;\n\n return 1+max(findlevel(root->left),findlevel(root->right));\n\n }\n void findlargest(TreeNode *root, vector<int> &sol, int level)\n {\n if(root==NULL)\n return;\n\n if(sol[level]<root->val)\n sol[level]=root->val;\n\n findlargest(root->left,sol,level+1);\n findlargest(root->right,sol,level+1);\n }\n vector<int> largestValues(TreeNode* root) \n {\n int level = findlevel(root);\n vector<int> sol(level,INT_MIN);\n if(root==NULL)\n return sol;\n\n findlargest(root,sol,0);\n return sol;\n }\n};",
"memory": "20500"
} |
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>
| 0 | {
"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 */\n\n\n\nclass Solution {\npublic:\n vector<int> largestValues(TreeNode* root) {\n vector<int> ans;\n inorder(root, 0, ans);\n return ans;\n }\n\n void inorder(TreeNode* node, int level, vector<int>& ans) {\n if (node == nullptr) return;\n if (level >= ans.size()) {\n ans.push_back(node->val);\n } else {\n ans[level] = max(ans[level], node->val);\n }\n inorder(node->left, level+1, ans);\n inorder(node->right, level+1, ans); \n }\n};",
"memory": "20600"
} |
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>
| 0 | {
"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<TreeNode*> que;\n vector<int> result;\n if(root!=NULL) que.push(root);\n while(!que.empty()){\n int size = que.size();\n int tmp_max = INT_MIN;\n for(int i=0;i<size;i++){\n auto node = que.front();\n que.pop();\n tmp_max = tmp_max>node->val?tmp_max:node->val;\n \n if(node->left) que.push(node->left);\n if(node->right) que.push(node->right);\n }\n result.push_back(tmp_max);\n }\n return result;\n }\n};",
"memory": "20700"
} |
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>
| 0 | {
"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 vector<int> largestValues(TreeNode* root) {\n helper(root,0);\n return ans;\n }\n\n void helper(TreeNode* root, int level){\n if(!root) return;\n if(level >= ans.size()){\n ans.push_back(root->val);\n }\n else{\n ans[level] = max(root->val, ans[level]);\n }\n helper(root->left,level+1);\n helper(root->right, level+1);\n }\n};",
"memory": "20700"
} |
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>
| 0 | {
"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<TreeNode*> q;\n vector<int> ans;\n q.push(root);\n if(root==NULL) return ans;\n while(!q.empty())\n {\n int s = q.size();\n int maxi = INT_MIN;\n for(int i=0;i<s;i++)\n {\n TreeNode* node = q.front();\n q.pop();\n maxi = max(maxi,node->val);\n if(node->left) q.push(node->left);\n if(node->right) q.push(node->right);\n }\n ans.push_back(maxi);\n }\n return ans;\n }\n};",
"memory": "20800"
} |
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>
| 0 | {
"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> BFS(TreeNode *root)\n {\n vector<int> ans;\n queue<TreeNode *> q;\n\n q.push(root);\n\n while(!q.empty())\n {\n int size = q.size();\n int maxVal = INT_MIN;\n\n for(int i = 0; i < size; i++)\n {\n TreeNode *curr = q.front();\n q.pop();\n\n if(maxVal <= curr->val)\n maxVal = curr->val;\n\n if(curr->left)\n q.push(curr->left);\n if(curr->right)\n q.push(curr->right);\n }\n\n ans.push_back(maxVal);\n }\n return ans;\n }\n\n vector<int> largestValues(TreeNode* root) {\n if(!root)\n return {};\n \n return BFS(root);\n }\n};",
"memory": "20800"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.