id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
222
<p>Given the <code>root</code> of a <strong>complete</strong> binary tree, return the number of the nodes in the tree.</p> <p>According to <strong><a href="http://en.wikipedia.org/wiki/Binary_tree#Types_of_binary_trees" target="_blank">Wikipedia</a></strong>, every level, except possibly the last, is completely filled in a complete binary tree, and all nodes in the last level are as far left as possible. It can have between <code>1</code> and <code>2<sup>h</sup></code> nodes inclusive at the last level <code>h</code>.</p> <p>Design an algorithm that runs in less than&nbsp;<code data-stringify-type="code">O(n)</code>&nbsp;time complexity.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/14/complete.jpg" style="width: 372px; height: 302px;" /> <pre> <strong>Input:</strong> root = [1,2,3,4,5,6] <strong>Output:</strong> 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> 0 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5 * 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 5 * 10<sup>4</sup></code></li> <li>The tree is guaranteed to be <strong>complete</strong>.</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 n=1;\n void bb(TreeNode* root){\n if(!root){n--;return;}\n n++;\n bb(root->left);\n n++;\n bb(root->right);\n root->left=NULL;\n root->right=NULL;\n }\n int countNodes(TreeNode* root) {\n bb(root);\n return n;\n }\n};", "memory": "19800" }
222
<p>Given the <code>root</code> of a <strong>complete</strong> binary tree, return the number of the nodes in the tree.</p> <p>According to <strong><a href="http://en.wikipedia.org/wiki/Binary_tree#Types_of_binary_trees" target="_blank">Wikipedia</a></strong>, every level, except possibly the last, is completely filled in a complete binary tree, and all nodes in the last level are as far left as possible. It can have between <code>1</code> and <code>2<sup>h</sup></code> nodes inclusive at the last level <code>h</code>.</p> <p>Design an algorithm that runs in less than&nbsp;<code data-stringify-type="code">O(n)</code>&nbsp;time complexity.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/14/complete.jpg" style="width: 372px; height: 302px;" /> <pre> <strong>Input:</strong> root = [1,2,3,4,5,6] <strong>Output:</strong> 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> 0 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5 * 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 5 * 10<sup>4</sup></code></li> <li>The tree is guaranteed to be <strong>complete</strong>.</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 speed = []() {\n ios::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n return 0;\n }();\n int n=1;\n void bb(TreeNode* root){\n if(!root){n--;return;}\n n++;\n bb(root->left);\n n++;\n bb(root->right);\n root->left=NULL;\n root->right=NULL;\n }\n int countNodes(TreeNode* root) {\n bb(root);\n return n;\n }\n};", "memory": "19800" }
222
<p>Given the <code>root</code> of a <strong>complete</strong> binary tree, return the number of the nodes in the tree.</p> <p>According to <strong><a href="http://en.wikipedia.org/wiki/Binary_tree#Types_of_binary_trees" target="_blank">Wikipedia</a></strong>, every level, except possibly the last, is completely filled in a complete binary tree, and all nodes in the last level are as far left as possible. It can have between <code>1</code> and <code>2<sup>h</sup></code> nodes inclusive at the last level <code>h</code>.</p> <p>Design an algorithm that runs in less than&nbsp;<code data-stringify-type="code">O(n)</code>&nbsp;time complexity.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/14/complete.jpg" style="width: 372px; height: 302px;" /> <pre> <strong>Input:</strong> root = [1,2,3,4,5,6] <strong>Output:</strong> 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> 0 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5 * 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 5 * 10<sup>4</sup></code></li> <li>The tree is guaranteed to be <strong>complete</strong>.</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 {\nvoid preOrder(TreeNode* root ){\n TreeNode* curr = root ;\n while(curr != NULL){\n \n while(curr->left){\n if(curr->left){\n TreeNode* pred = curr ->left ;\n \n while(pred->right)\n pred = pred->right ;\n pred->right = curr->right;\n curr->right = curr->left ;\n curr->left = nullptr ;\n }\n }\n curr = curr->right ;\n }\n}\npublic:\n int countNodes(TreeNode* root) {\n int cnt =0 ;\n preOrder(root );\n while(root != NULL){\n cnt++ ;\n root = root->right;\n }\n return cnt ;\n \n }\n};", "memory": "21500" }
222
<p>Given the <code>root</code> of a <strong>complete</strong> binary tree, return the number of the nodes in the tree.</p> <p>According to <strong><a href="http://en.wikipedia.org/wiki/Binary_tree#Types_of_binary_trees" target="_blank">Wikipedia</a></strong>, every level, except possibly the last, is completely filled in a complete binary tree, and all nodes in the last level are as far left as possible. It can have between <code>1</code> and <code>2<sup>h</sup></code> nodes inclusive at the last level <code>h</code>.</p> <p>Design an algorithm that runs in less than&nbsp;<code data-stringify-type="code">O(n)</code>&nbsp;time complexity.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/14/complete.jpg" style="width: 372px; height: 302px;" /> <pre> <strong>Input:</strong> root = [1,2,3,4,5,6] <strong>Output:</strong> 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> 0 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5 * 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 5 * 10<sup>4</sup></code></li> <li>The tree is guaranteed to be <strong>complete</strong>.</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 void number(TreeNode* root,int& count){\n if(root==NULL){\n return;\n }\n count++;\n number(root->left,count);\n number(root->right,count);\n\n }\n int countNodes(TreeNode* root) {\n int count=0;\n number(root,count);\n return count;\n\n }\n};", "memory": "29000" }
222
<p>Given the <code>root</code> of a <strong>complete</strong> binary tree, return the number of the nodes in the tree.</p> <p>According to <strong><a href="http://en.wikipedia.org/wiki/Binary_tree#Types_of_binary_trees" target="_blank">Wikipedia</a></strong>, every level, except possibly the last, is completely filled in a complete binary tree, and all nodes in the last level are as far left as possible. It can have between <code>1</code> and <code>2<sup>h</sup></code> nodes inclusive at the last level <code>h</code>.</p> <p>Design an algorithm that runs in less than&nbsp;<code data-stringify-type="code">O(n)</code>&nbsp;time complexity.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/14/complete.jpg" style="width: 372px; height: 302px;" /> <pre> <strong>Input:</strong> root = [1,2,3,4,5,6] <strong>Output:</strong> 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> 0 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5 * 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 5 * 10<sup>4</sup></code></li> <li>The tree is guaranteed to be <strong>complete</strong>.</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 countNodes(TreeNode* root) {\n if (root == nullptr)\n return 0;\n return 1 + countNodes(root->left) + countNodes(root->right);\n \n }\n};", "memory": "29100" }
222
<p>Given the <code>root</code> of a <strong>complete</strong> binary tree, return the number of the nodes in the tree.</p> <p>According to <strong><a href="http://en.wikipedia.org/wiki/Binary_tree#Types_of_binary_trees" target="_blank">Wikipedia</a></strong>, every level, except possibly the last, is completely filled in a complete binary tree, and all nodes in the last level are as far left as possible. It can have between <code>1</code> and <code>2<sup>h</sup></code> nodes inclusive at the last level <code>h</code>.</p> <p>Design an algorithm that runs in less than&nbsp;<code data-stringify-type="code">O(n)</code>&nbsp;time complexity.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/14/complete.jpg" style="width: 372px; height: 302px;" /> <pre> <strong>Input:</strong> root = [1,2,3,4,5,6] <strong>Output:</strong> 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> 0 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5 * 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 5 * 10<sup>4</sup></code></li> <li>The tree is guaranteed to be <strong>complete</strong>.</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 {\nprivate:\n int count = 0;\npublic:\n int countNodes(TreeNode* root) {\n traverse(root);\n return count;\n }\n\n void traverse(TreeNode* root) {\n if (root == nullptr) {\n return;\n }\n\n count ++;\n traverse(root->left);\n traverse(root->right);\n }\n};", "memory": "29200" }
222
<p>Given the <code>root</code> of a <strong>complete</strong> binary tree, return the number of the nodes in the tree.</p> <p>According to <strong><a href="http://en.wikipedia.org/wiki/Binary_tree#Types_of_binary_trees" target="_blank">Wikipedia</a></strong>, every level, except possibly the last, is completely filled in a complete binary tree, and all nodes in the last level are as far left as possible. It can have between <code>1</code> and <code>2<sup>h</sup></code> nodes inclusive at the last level <code>h</code>.</p> <p>Design an algorithm that runs in less than&nbsp;<code data-stringify-type="code">O(n)</code>&nbsp;time complexity.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/14/complete.jpg" style="width: 372px; height: 302px;" /> <pre> <strong>Input:</strong> root = [1,2,3,4,5,6] <strong>Output:</strong> 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> 0 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5 * 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 5 * 10<sup>4</sup></code></li> <li>The tree is guaranteed to be <strong>complete</strong>.</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 countNodes(TreeNode* root) {\n int height = 0;\n if(root == NULL) return 0;\n if(root->left == NULL and root->right == NULL) return 1;\n TreeNode* x = root;\n while(x->left != NULL)\n {\n x = x->left;\n height++;\n }\n int low = 0;\n int high = (1 << height) - 1;\n while(low <= high)\n {\n int mid = (low + high) / 2;\n TreeNode* start = root;\n int zz = mid;\n int count = height;\n while(count--)\n {\n if(zz & (1 << (height-1))) start = start->right;\n else start = start->left;\n zz = zz << 1;\n }\n if(start == NULL) high = mid - 1;\n else low = mid + 1;\n }\n cout << high;\n return (1<<height) + high;\n }\n};", "memory": "29200" }
222
<p>Given the <code>root</code> of a <strong>complete</strong> binary tree, return the number of the nodes in the tree.</p> <p>According to <strong><a href="http://en.wikipedia.org/wiki/Binary_tree#Types_of_binary_trees" target="_blank">Wikipedia</a></strong>, every level, except possibly the last, is completely filled in a complete binary tree, and all nodes in the last level are as far left as possible. It can have between <code>1</code> and <code>2<sup>h</sup></code> nodes inclusive at the last level <code>h</code>.</p> <p>Design an algorithm that runs in less than&nbsp;<code data-stringify-type="code">O(n)</code>&nbsp;time complexity.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/14/complete.jpg" style="width: 372px; height: 302px;" /> <pre> <strong>Input:</strong> root = [1,2,3,4,5,6] <strong>Output:</strong> 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> 0 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5 * 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 5 * 10<sup>4</sup></code></li> <li>The tree is guaranteed to be <strong>complete</strong>.</li> </ul>
0
{ "code": "//somdeday he will say \"you did well\";\n\n/**\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 countNodes(TreeNode* root) {\n if(root == nullptr)\n return 0;\n int count = countNodes(root->left) + countNodes(root->right) +1;\n return count;\n }\n};", "memory": "29300" }
222
<p>Given the <code>root</code> of a <strong>complete</strong> binary tree, return the number of the nodes in the tree.</p> <p>According to <strong><a href="http://en.wikipedia.org/wiki/Binary_tree#Types_of_binary_trees" target="_blank">Wikipedia</a></strong>, every level, except possibly the last, is completely filled in a complete binary tree, and all nodes in the last level are as far left as possible. It can have between <code>1</code> and <code>2<sup>h</sup></code> nodes inclusive at the last level <code>h</code>.</p> <p>Design an algorithm that runs in less than&nbsp;<code data-stringify-type="code">O(n)</code>&nbsp;time complexity.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/14/complete.jpg" style="width: 372px; height: 302px;" /> <pre> <strong>Input:</strong> root = [1,2,3,4,5,6] <strong>Output:</strong> 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> 0 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5 * 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 5 * 10<sup>4</sup></code></li> <li>The tree is guaranteed to be <strong>complete</strong>.</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 exists(TreeNode* root){\n if(root==NULL){\n return 0;\n }\n int left = exists(root->left);\n int right = exists(root->right);\n return left+right+1;\n }\n int countNodes(TreeNode* root) {\n return exists(root);\n }\n};", "memory": "29300" }
222
<p>Given the <code>root</code> of a <strong>complete</strong> binary tree, return the number of the nodes in the tree.</p> <p>According to <strong><a href="http://en.wikipedia.org/wiki/Binary_tree#Types_of_binary_trees" target="_blank">Wikipedia</a></strong>, every level, except possibly the last, is completely filled in a complete binary tree, and all nodes in the last level are as far left as possible. It can have between <code>1</code> and <code>2<sup>h</sup></code> nodes inclusive at the last level <code>h</code>.</p> <p>Design an algorithm that runs in less than&nbsp;<code data-stringify-type="code">O(n)</code>&nbsp;time complexity.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/14/complete.jpg" style="width: 372px; height: 302px;" /> <pre> <strong>Input:</strong> root = [1,2,3,4,5,6] <strong>Output:</strong> 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> 0 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5 * 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 5 * 10<sup>4</sup></code></li> <li>The tree is guaranteed to be <strong>complete</strong>.</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 void inorder(TreeNode *root,int *count){\n if(root==NULL) return;\n inorder(root->left,count);\n (*count)++;\n inorder(root->right,count);\n }\n\n int countNodes(TreeNode* root) {\n if(root==NULL) return 0;\n if(root->left==NULL&&root->right==NULL) return 1;\n // queue<TreeNode*>q;\n // vector<TreeNode*>ans;\n // q.push(root);\n // while(!q.empty()){\n // auto topNode=q.front();\n // q.pop();\n // ans.push_back(topNode);\n // if(topNode->left) q.push(topNode->left);\n // if(topNode->right) q.push(topNode->right);\n // }\n // return ans.size();\n int count=0;\n inorder(root,&count);\n return count;\n }\n};", "memory": "29400" }
222
<p>Given the <code>root</code> of a <strong>complete</strong> binary tree, return the number of the nodes in the tree.</p> <p>According to <strong><a href="http://en.wikipedia.org/wiki/Binary_tree#Types_of_binary_trees" target="_blank">Wikipedia</a></strong>, every level, except possibly the last, is completely filled in a complete binary tree, and all nodes in the last level are as far left as possible. It can have between <code>1</code> and <code>2<sup>h</sup></code> nodes inclusive at the last level <code>h</code>.</p> <p>Design an algorithm that runs in less than&nbsp;<code data-stringify-type="code">O(n)</code>&nbsp;time complexity.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/14/complete.jpg" style="width: 372px; height: 302px;" /> <pre> <strong>Input:</strong> root = [1,2,3,4,5,6] <strong>Output:</strong> 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> 0 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5 * 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 5 * 10<sup>4</sup></code></li> <li>The tree is guaranteed to be <strong>complete</strong>.</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 void inorder(TreeNode *root,int *count){\n if(root==NULL) return;\n inorder(root->left,count);\n (*count)++;\n inorder(root->right,count);\n }\n\n int countNodes(TreeNode* root) {\n if(root==NULL) return 0;\n if(root->left==NULL&&root->right==NULL) return 1;\n int count=0;\n inorder(root,&count);\n return count;\n }\n};", "memory": "29400" }
222
<p>Given the <code>root</code> of a <strong>complete</strong> binary tree, return the number of the nodes in the tree.</p> <p>According to <strong><a href="http://en.wikipedia.org/wiki/Binary_tree#Types_of_binary_trees" target="_blank">Wikipedia</a></strong>, every level, except possibly the last, is completely filled in a complete binary tree, and all nodes in the last level are as far left as possible. It can have between <code>1</code> and <code>2<sup>h</sup></code> nodes inclusive at the last level <code>h</code>.</p> <p>Design an algorithm that runs in less than&nbsp;<code data-stringify-type="code">O(n)</code>&nbsp;time complexity.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/14/complete.jpg" style="width: 372px; height: 302px;" /> <pre> <strong>Input:</strong> root = [1,2,3,4,5,6] <strong>Output:</strong> 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> 0 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5 * 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 5 * 10<sup>4</sup></code></li> <li>The tree is guaranteed to be <strong>complete</strong>.</li> </ul>
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int countNodes(TreeNode* root) {\n if (root == nullptr)\n return 0;\n return 1 + countNodes(root->left) + countNodes(root->right);\n }\n};", "memory": "29500" }
222
<p>Given the <code>root</code> of a <strong>complete</strong> binary tree, return the number of the nodes in the tree.</p> <p>According to <strong><a href="http://en.wikipedia.org/wiki/Binary_tree#Types_of_binary_trees" target="_blank">Wikipedia</a></strong>, every level, except possibly the last, is completely filled in a complete binary tree, and all nodes in the last level are as far left as possible. It can have between <code>1</code> and <code>2<sup>h</sup></code> nodes inclusive at the last level <code>h</code>.</p> <p>Design an algorithm that runs in less than&nbsp;<code data-stringify-type="code">O(n)</code>&nbsp;time complexity.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/14/complete.jpg" style="width: 372px; height: 302px;" /> <pre> <strong>Input:</strong> root = [1,2,3,4,5,6] <strong>Output:</strong> 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> 0 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5 * 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 5 * 10<sup>4</sup></code></li> <li>The tree is guaranteed to be <strong>complete</strong>.</li> </ul>
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n// yeh apan ne khud se kiya hai lekin isme apan ne sum lekar return kiya hai void function bana ke \n int le(TreeNode* node)\n {\n if(node==NULL) return 0;\n int lh = le(node->left);\n return 1+lh;\n }\n int re(TreeNode* node)\n {\n if(node==NULL) return 0;\n int rh = re(node->right);\n return 1+rh;\n }\n\n\n void count(TreeNode * node, int & sum )\n { if(node==NULL) return;\n int rh = re(node);\n \n int lh = le(node);\n \n\n if(lh==rh){\n sum+=pow(2,lh)-1;\n return;}\n else\n {\n sum+=1;\n }\n count(node->left,sum);\n count(node->right,sum);\n\n }\n int countNodes(TreeNode* root) {\n int sum =0;\n count(root,sum);\n return sum;\n }\n};", "memory": "29500" }
222
<p>Given the <code>root</code> of a <strong>complete</strong> binary tree, return the number of the nodes in the tree.</p> <p>According to <strong><a href="http://en.wikipedia.org/wiki/Binary_tree#Types_of_binary_trees" target="_blank">Wikipedia</a></strong>, every level, except possibly the last, is completely filled in a complete binary tree, and all nodes in the last level are as far left as possible. It can have between <code>1</code> and <code>2<sup>h</sup></code> nodes inclusive at the last level <code>h</code>.</p> <p>Design an algorithm that runs in less than&nbsp;<code data-stringify-type="code">O(n)</code>&nbsp;time complexity.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/14/complete.jpg" style="width: 372px; height: 302px;" /> <pre> <strong>Input:</strong> root = [1,2,3,4,5,6] <strong>Output:</strong> 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> 0 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5 * 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 5 * 10<sup>4</sup></code></li> <li>The tree is guaranteed to be <strong>complete</strong>.</li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n// \n int le(TreeNode* node)\n {\n if(node==NULL) return 0;\n int lh = le(node->left);\n return 1+lh;\n }\n\n int re(TreeNode* node)\n {\n if(node==NULL) return 0;\n int rh = re(node->right);\n return 1+rh;\n }\n\n\n \n // ab dekh yanha pe simple sa concept aa gaya jaise ki tu void me & value me bharte jata tha ab dhyan rakh yanha pe ushi jagah pe janha pr tu sum me add kar rha tha wanha par function ko return kar dena hai bas wamha tu 1 add kar rha tha tanha 1 add ka ke function ko hi return kar do easy hai kuch questions karega to samjha aa jayega\n\n \n \n int countNodes(TreeNode* root) {\n if(root==NULL) return 0;\n int lh = le(root);\n int rh = re(root);\n if(lh!=rh) return 1+countNodes(root->left)+countNodes(root->right);\n else return (1<<lh)-1;\n\n }\n};", "memory": "29600" }
222
<p>Given the <code>root</code> of a <strong>complete</strong> binary tree, return the number of the nodes in the tree.</p> <p>According to <strong><a href="http://en.wikipedia.org/wiki/Binary_tree#Types_of_binary_trees" target="_blank">Wikipedia</a></strong>, every level, except possibly the last, is completely filled in a complete binary tree, and all nodes in the last level are as far left as possible. It can have between <code>1</code> and <code>2<sup>h</sup></code> nodes inclusive at the last level <code>h</code>.</p> <p>Design an algorithm that runs in less than&nbsp;<code data-stringify-type="code">O(n)</code>&nbsp;time complexity.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/14/complete.jpg" style="width: 372px; height: 302px;" /> <pre> <strong>Input:</strong> root = [1,2,3,4,5,6] <strong>Output:</strong> 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> 0 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5 * 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 5 * 10<sup>4</sup></code></li> <li>The tree is guaranteed to be <strong>complete</strong>.</li> </ul>
3
{ "code": "\n\n/**\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 heightMax(TreeNode* root) {\n return root == nullptr ? 0 : 1 + heightMax(root->left);\n }\n\n int heightMin(TreeNode* root) {\n return root == nullptr ? 0 : 1 + heightMin(root->right);\n }\n\n string toBinary(int n) {\n string r;\n while(n != 0) {\n r = (n % 2 == 0 ? \"0\" : \"1\" ) + r; \n n /= 2;\n }\n cout << r << endl;\n return r;\n }\n\n int countNodes(TreeNode* root) {\n int l = heightMax(root), r = heightMin(root);\n if (l == r) {\n return pow(2, l) - 1;\n }\n // else, do binary search to find where is the \"splitting point\"\n int minNode = pow(2, r), maxNode = pow(2, l) - 2;\n while(maxNode > minNode) {\n int avr = ((minNode + maxNode) / 2) + 1;\n string avrBin = toBinary(avr); \n int len = avrBin.length();\n TreeNode* curr = root;\n for (int i = 1; i < len; i++) {\n if (avrBin[i] == '0') {\n curr = curr -> left;\n } else {\n curr = curr -> right;\n }\n }\n if (curr == nullptr) {\n // number of nodes is small\n maxNode = avr - 1;\n } else {\n minNode = avr;\n }\n }\n return maxNode;\n }\n};", "memory": "29700" }
222
<p>Given the <code>root</code> of a <strong>complete</strong> binary tree, return the number of the nodes in the tree.</p> <p>According to <strong><a href="http://en.wikipedia.org/wiki/Binary_tree#Types_of_binary_trees" target="_blank">Wikipedia</a></strong>, every level, except possibly the last, is completely filled in a complete binary tree, and all nodes in the last level are as far left as possible. It can have between <code>1</code> and <code>2<sup>h</sup></code> nodes inclusive at the last level <code>h</code>.</p> <p>Design an algorithm that runs in less than&nbsp;<code data-stringify-type="code">O(n)</code>&nbsp;time complexity.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/14/complete.jpg" style="width: 372px; height: 302px;" /> <pre> <strong>Input:</strong> root = [1,2,3,4,5,6] <strong>Output:</strong> 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> 0 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5 * 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 5 * 10<sup>4</sup></code></li> <li>The tree is guaranteed to be <strong>complete</strong>.</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 countNodes(TreeNode* root) {\n if(root==NULL) return 0;\n queue<TreeNode*> Q;\n int ans = 0;\n Q.push(root);\n while(!Q.empty()){\n int n = Q.size();\n ans+=n;\n while(n--){\n TreeNode* tempNode = Q.front();\n Q.pop();\n if(tempNode->left) Q.push(tempNode->left);\n if(tempNode->right) Q.push(tempNode->right);\n }\n }\n return ans;\n }\n};", "memory": "29800" }
222
<p>Given the <code>root</code> of a <strong>complete</strong> binary tree, return the number of the nodes in the tree.</p> <p>According to <strong><a href="http://en.wikipedia.org/wiki/Binary_tree#Types_of_binary_trees" target="_blank">Wikipedia</a></strong>, every level, except possibly the last, is completely filled in a complete binary tree, and all nodes in the last level are as far left as possible. It can have between <code>1</code> and <code>2<sup>h</sup></code> nodes inclusive at the last level <code>h</code>.</p> <p>Design an algorithm that runs in less than&nbsp;<code data-stringify-type="code">O(n)</code>&nbsp;time complexity.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/14/complete.jpg" style="width: 372px; height: 302px;" /> <pre> <strong>Input:</strong> root = [1,2,3,4,5,6] <strong>Output:</strong> 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> 0 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5 * 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 5 * 10<sup>4</sup></code></li> <li>The tree is guaranteed to be <strong>complete</strong>.</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 countNodes(TreeNode* root) \n {\n if(!root) return 0;\n\n queue<TreeNode*>q;\n int count = 1;\n q.push(root);\n while(!q.empty())\n {\n int size = q.size();\n for(int i = 0 ; i<size ; i++)\n {\n TreeNode* node = q.front();\n q.pop();\n if(node->left)\n {\n count++;\n q.push(node->left);\n }\n if(node->right)\n {\n count++;\n q.push(node->right);\n }\n }\n }\n return count;\n }\n};", "memory": "29900" }
222
<p>Given the <code>root</code> of a <strong>complete</strong> binary tree, return the number of the nodes in the tree.</p> <p>According to <strong><a href="http://en.wikipedia.org/wiki/Binary_tree#Types_of_binary_trees" target="_blank">Wikipedia</a></strong>, every level, except possibly the last, is completely filled in a complete binary tree, and all nodes in the last level are as far left as possible. It can have between <code>1</code> and <code>2<sup>h</sup></code> nodes inclusive at the last level <code>h</code>.</p> <p>Design an algorithm that runs in less than&nbsp;<code data-stringify-type="code">O(n)</code>&nbsp;time complexity.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/14/complete.jpg" style="width: 372px; height: 302px;" /> <pre> <strong>Input:</strong> root = [1,2,3,4,5,6] <strong>Output:</strong> 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> 0 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5 * 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 5 * 10<sup>4</sup></code></li> <li>The tree is guaranteed to be <strong>complete</strong>.</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 countNodes(TreeNode* root) \n {\n if( root== nullptr ){\n return 0 ;\n\n }\n\n int count =0 ;\n \n\n queue<TreeNode*>q;\n\n q.push( root );\n \n while(!q.empty()){\n TreeNode* node= q.front();\n count++;\n q.pop();\n if(node->left != nullptr ){\n q.push(node->left );\n\n }\n\n if(node->right != nullptr){\n\n q.push(node->right);\n\n }\n\n }\n\n return count ;\n\n }\n};", "memory": "30000" }
2,148
<p>There are <code>n</code> <strong>availabe </strong>seats and <code>n</code> students <strong>standing</strong> in a room. You are given an array <code>seats</code> of length <code>n</code>, where <code>seats[i]</code> is the position of the <code>i<sup>th</sup></code> seat. You are also given the array <code>students</code> of length <code>n</code>, where <code>students[j]</code> is the position of the <code>j<sup>th</sup></code> student.</p> <p>You may perform the following move any number of times:</p> <ul> <li>Increase or decrease the position of the <code>i<sup>th</sup></code> student by <code>1</code> (i.e., moving the <code>i<sup>th</sup></code> student from position&nbsp;<code>x</code>&nbsp;to <code>x + 1</code> or <code>x - 1</code>)</li> </ul> <p>Return <em>the <strong>minimum number of moves</strong> required to move each student to a seat</em><em> such that no two students are in the same seat.</em></p> <p>Note that there may be <strong>multiple</strong> seats or students in the <strong>same </strong>position at the beginning.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> seats = [3,1,5], students = [2,7,4] <strong>Output:</strong> 4 <strong>Explanation:</strong> The students are moved as follows: - The first student is moved from position 2 to position 1 using 1 move. - The second student is moved from position 7 to position 5 using 2 moves. - The third student is moved from position 4 to position 3 using 1 move. In total, 1 + 2 + 1 = 4 moves were used. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> seats = [4,1,5,9], students = [1,3,2,6] <strong>Output:</strong> 7 <strong>Explanation:</strong> The students are moved as follows: - The first student is not moved. - The second student is moved from position 3 to position 4 using 1 move. - The third student is moved from position 2 to position 5 using 3 moves. - The fourth student is moved from position 6 to position 9 using 3 moves. In total, 0 + 1 + 3 + 3 = 7 moves were used. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> seats = [2,2,6,6], students = [1,3,2,6] <strong>Output:</strong> 4 <strong>Explanation:</strong> Note that there are two seats at position 2 and two seats at position 6. The students are moved as follows: - The first student is moved from position 1 to position 2 using 1 move. - The second student is moved from position 3 to position 6 using 3 moves. - The third student is not moved. - The fourth student is not moved. In total, 1 + 3 + 0 + 0 = 4 moves were used. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == seats.length == students.length</code></li> <li><code>1 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= seats[i], students[j] &lt;= 100</code></li> </ul>
0
{ "code": "#pragma GCC optimize(\"O3,unroll-loops\")\n#pragma GCC target(\"avx2,bmi,bmi2,lzcnt,popcnt\")\n\nstatic const bool Booster = [](){\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return true;\n}();\n\nvoid parse_input_and_solve(std::ofstream& out, std::string& s, std::string& t) {\n s.erase(s.begin());\n s.pop_back();\n t.erase(t.begin());\n t.pop_back();\n \n vector<int> v1, v2;\n string w;\n \n istringstream iss1(s);\n while(getline(iss1, w, ',')) v1.push_back(stoi(w));\n \n istringstream iss2(t);\n while(getline(iss2, w, ',')) v2.push_back(stoi(w));\n \n sort(v1.begin(), v1.end());\n sort(v2.begin(), v2.end());\n \n int c = 0, n = v1.size();\n for(int i=0; i<n; i++) c += abs(v1[i] - v2[i]);\n \n out << c << endl;\n}\n\nbool Solve = []() {\n std::ofstream out(\"user.out\");\n std::string s, t;\n while (std::getline(std::cin, s) && std::getline(std::cin, t)) {\n parse_input_and_solve(out, s, t);\n }\n out.flush();\n exit(0);\n return true;\n}();\n\nclass Solution {\npublic:\n int minMovesToSeat(vector<int>& seats, vector<int>& students) {\n int res = 0;\n sort(seats.begin(), seats.end());\n sort(students.begin(), students.end());\n for(int i = 0; i < seats.size(); i++)\n res += abs(seats[i] - students[i]);\n return res;\n }\n};", "memory": "10400" }
2,148
<p>There are <code>n</code> <strong>availabe </strong>seats and <code>n</code> students <strong>standing</strong> in a room. You are given an array <code>seats</code> of length <code>n</code>, where <code>seats[i]</code> is the position of the <code>i<sup>th</sup></code> seat. You are also given the array <code>students</code> of length <code>n</code>, where <code>students[j]</code> is the position of the <code>j<sup>th</sup></code> student.</p> <p>You may perform the following move any number of times:</p> <ul> <li>Increase or decrease the position of the <code>i<sup>th</sup></code> student by <code>1</code> (i.e., moving the <code>i<sup>th</sup></code> student from position&nbsp;<code>x</code>&nbsp;to <code>x + 1</code> or <code>x - 1</code>)</li> </ul> <p>Return <em>the <strong>minimum number of moves</strong> required to move each student to a seat</em><em> such that no two students are in the same seat.</em></p> <p>Note that there may be <strong>multiple</strong> seats or students in the <strong>same </strong>position at the beginning.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> seats = [3,1,5], students = [2,7,4] <strong>Output:</strong> 4 <strong>Explanation:</strong> The students are moved as follows: - The first student is moved from position 2 to position 1 using 1 move. - The second student is moved from position 7 to position 5 using 2 moves. - The third student is moved from position 4 to position 3 using 1 move. In total, 1 + 2 + 1 = 4 moves were used. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> seats = [4,1,5,9], students = [1,3,2,6] <strong>Output:</strong> 7 <strong>Explanation:</strong> The students are moved as follows: - The first student is not moved. - The second student is moved from position 3 to position 4 using 1 move. - The third student is moved from position 2 to position 5 using 3 moves. - The fourth student is moved from position 6 to position 9 using 3 moves. In total, 0 + 1 + 3 + 3 = 7 moves were used. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> seats = [2,2,6,6], students = [1,3,2,6] <strong>Output:</strong> 4 <strong>Explanation:</strong> Note that there are two seats at position 2 and two seats at position 6. The students are moved as follows: - The first student is moved from position 1 to position 2 using 1 move. - The second student is moved from position 3 to position 6 using 3 moves. - The third student is not moved. - The fourth student is not moved. In total, 1 + 3 + 0 + 0 = 4 moves were used. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == seats.length == students.length</code></li> <li><code>1 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= seats[i], students[j] &lt;= 100</code></li> </ul>
0
{ "code": "#pragma GCC optimize(\"O3,unroll-loops\")\n#pragma GCC target(\"avx2,bmi,bmi2,lzcnt,popcnt\")\n\nstatic const bool Booster = [](){\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return true;\n}();\n\nvoid parse_input_and_solve(std::ofstream& out, std::string& s, std::string& t) {\n s.erase(s.begin());\n s.pop_back();\n t.erase(t.begin());\n t.pop_back();\n \n vector<int> v1, v2;\n string w;\n \n istringstream iss1(s);\n while(getline(iss1, w, ',')) v1.push_back(stoi(w));\n \n istringstream iss2(t);\n while(getline(iss2, w, ',')) v2.push_back(stoi(w));\n \n sort(v1.begin(), v1.end());\n sort(v2.begin(), v2.end());\n \n int c = 0, n = v1.size();\n for(int i=0; i<n; i++) c += abs(v1[i] - v2[i]);\n \n out << c << endl;\n}\n\nbool Solve = []() {\n std::ofstream out(\"user.out\");\n std::string s, t;\n while (std::getline(std::cin, s) && std::getline(std::cin, t)) {\n parse_input_and_solve(out, s, t);\n }\n out.flush();\n exit(0);\n return true;\n}();\n\nclass Solution {\npublic:\n int minMovesToSeat(vector<int>& seats, vector<int>& students) {\n sort(seats.begin(), seats.end()); \n sort(students.begin(), students.end()); \n int cnt=0;\n\n for(int i=0;i<seats.size();i++){\n cnt += abs(seats[i] - students[i]);\n\n }\n return cnt;\n }\n};", "memory": "10500" }
2,148
<p>There are <code>n</code> <strong>availabe </strong>seats and <code>n</code> students <strong>standing</strong> in a room. You are given an array <code>seats</code> of length <code>n</code>, where <code>seats[i]</code> is the position of the <code>i<sup>th</sup></code> seat. You are also given the array <code>students</code> of length <code>n</code>, where <code>students[j]</code> is the position of the <code>j<sup>th</sup></code> student.</p> <p>You may perform the following move any number of times:</p> <ul> <li>Increase or decrease the position of the <code>i<sup>th</sup></code> student by <code>1</code> (i.e., moving the <code>i<sup>th</sup></code> student from position&nbsp;<code>x</code>&nbsp;to <code>x + 1</code> or <code>x - 1</code>)</li> </ul> <p>Return <em>the <strong>minimum number of moves</strong> required to move each student to a seat</em><em> such that no two students are in the same seat.</em></p> <p>Note that there may be <strong>multiple</strong> seats or students in the <strong>same </strong>position at the beginning.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> seats = [3,1,5], students = [2,7,4] <strong>Output:</strong> 4 <strong>Explanation:</strong> The students are moved as follows: - The first student is moved from position 2 to position 1 using 1 move. - The second student is moved from position 7 to position 5 using 2 moves. - The third student is moved from position 4 to position 3 using 1 move. In total, 1 + 2 + 1 = 4 moves were used. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> seats = [4,1,5,9], students = [1,3,2,6] <strong>Output:</strong> 7 <strong>Explanation:</strong> The students are moved as follows: - The first student is not moved. - The second student is moved from position 3 to position 4 using 1 move. - The third student is moved from position 2 to position 5 using 3 moves. - The fourth student is moved from position 6 to position 9 using 3 moves. In total, 0 + 1 + 3 + 3 = 7 moves were used. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> seats = [2,2,6,6], students = [1,3,2,6] <strong>Output:</strong> 4 <strong>Explanation:</strong> Note that there are two seats at position 2 and two seats at position 6. The students are moved as follows: - The first student is moved from position 1 to position 2 using 1 move. - The second student is moved from position 3 to position 6 using 3 moves. - The third student is not moved. - The fourth student is not moved. In total, 1 + 3 + 0 + 0 = 4 moves were used. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == seats.length == students.length</code></li> <li><code>1 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= seats[i], students[j] &lt;= 100</code></li> </ul>
0
{ "code": "#pragma GCC optimize(\"O3,unroll-loops\")\n#pragma GCC target(\"avx2,bmi,bmi2,lzcnt,popcnt\")\n\nstatic const bool Booster = []() {\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return true;\n}();\n\nvoid parse_input_and_solve(std::ofstream& out, std::string& s, std::string& t) {\n s.erase(s.begin());\n s.pop_back();\n t.erase(t.begin());\n t.pop_back();\n\n vector<int> v1, v2;\n string w;\n\n istringstream iss1(s);\n while (getline(iss1, w, ','))\n v1.push_back(stoi(w));\n\n istringstream iss2(t);\n while (getline(iss2, w, ','))\n v2.push_back(stoi(w));\n\n sort(v1.begin(), v1.end());\n sort(v2.begin(), v2.end());\n\n int c = 0, n = v1.size();\n for (int i = 0; i < n; i++)\n c += abs(v1[i] - v2[i]);\n\n out << c << endl;\n}\n\nbool Solve = []() {\n std::ofstream out(\"user.out\");\n std::string s, t;\n while (std::getline(std::cin, s) && std::getline(std::cin, t)) {\n parse_input_and_solve(out, s, t);\n }\n out.flush();\n exit(0);\n return true;\n}();\n\nclass Solution {\npublic:\n int minMovesToSeat(vector<int>& seats, vector<int>& students) {\n int res = 0;\n sort(seats.begin(), seats.end());\n sort(students.begin(), students.end());\n for (int i = 0; i < seats.size(); i++)\n res += abs(seats[i] - students[i]);\n return res;\n }\n};", "memory": "10600" }
2,148
<p>There are <code>n</code> <strong>availabe </strong>seats and <code>n</code> students <strong>standing</strong> in a room. You are given an array <code>seats</code> of length <code>n</code>, where <code>seats[i]</code> is the position of the <code>i<sup>th</sup></code> seat. You are also given the array <code>students</code> of length <code>n</code>, where <code>students[j]</code> is the position of the <code>j<sup>th</sup></code> student.</p> <p>You may perform the following move any number of times:</p> <ul> <li>Increase or decrease the position of the <code>i<sup>th</sup></code> student by <code>1</code> (i.e., moving the <code>i<sup>th</sup></code> student from position&nbsp;<code>x</code>&nbsp;to <code>x + 1</code> or <code>x - 1</code>)</li> </ul> <p>Return <em>the <strong>minimum number of moves</strong> required to move each student to a seat</em><em> such that no two students are in the same seat.</em></p> <p>Note that there may be <strong>multiple</strong> seats or students in the <strong>same </strong>position at the beginning.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> seats = [3,1,5], students = [2,7,4] <strong>Output:</strong> 4 <strong>Explanation:</strong> The students are moved as follows: - The first student is moved from position 2 to position 1 using 1 move. - The second student is moved from position 7 to position 5 using 2 moves. - The third student is moved from position 4 to position 3 using 1 move. In total, 1 + 2 + 1 = 4 moves were used. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> seats = [4,1,5,9], students = [1,3,2,6] <strong>Output:</strong> 7 <strong>Explanation:</strong> The students are moved as follows: - The first student is not moved. - The second student is moved from position 3 to position 4 using 1 move. - The third student is moved from position 2 to position 5 using 3 moves. - The fourth student is moved from position 6 to position 9 using 3 moves. In total, 0 + 1 + 3 + 3 = 7 moves were used. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> seats = [2,2,6,6], students = [1,3,2,6] <strong>Output:</strong> 4 <strong>Explanation:</strong> Note that there are two seats at position 2 and two seats at position 6. The students are moved as follows: - The first student is moved from position 1 to position 2 using 1 move. - The second student is moved from position 3 to position 6 using 3 moves. - The third student is not moved. - The fourth student is not moved. In total, 1 + 3 + 0 + 0 = 4 moves were used. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == seats.length == students.length</code></li> <li><code>1 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= seats[i], students[j] &lt;= 100</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minMovesToSeat(vector<int>& seats, vector<int>& students) {\n int count=0;\n sort(seats.begin(), seats.end());\n sort(students.begin(), students.end());\n for(int i=0;i<students.size();i++){\n count += abs(seats[i] - students[i]);\n }\n return count;\n }\n};", "memory": "21000" }
2,148
<p>There are <code>n</code> <strong>availabe </strong>seats and <code>n</code> students <strong>standing</strong> in a room. You are given an array <code>seats</code> of length <code>n</code>, where <code>seats[i]</code> is the position of the <code>i<sup>th</sup></code> seat. You are also given the array <code>students</code> of length <code>n</code>, where <code>students[j]</code> is the position of the <code>j<sup>th</sup></code> student.</p> <p>You may perform the following move any number of times:</p> <ul> <li>Increase or decrease the position of the <code>i<sup>th</sup></code> student by <code>1</code> (i.e., moving the <code>i<sup>th</sup></code> student from position&nbsp;<code>x</code>&nbsp;to <code>x + 1</code> or <code>x - 1</code>)</li> </ul> <p>Return <em>the <strong>minimum number of moves</strong> required to move each student to a seat</em><em> such that no two students are in the same seat.</em></p> <p>Note that there may be <strong>multiple</strong> seats or students in the <strong>same </strong>position at the beginning.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> seats = [3,1,5], students = [2,7,4] <strong>Output:</strong> 4 <strong>Explanation:</strong> The students are moved as follows: - The first student is moved from position 2 to position 1 using 1 move. - The second student is moved from position 7 to position 5 using 2 moves. - The third student is moved from position 4 to position 3 using 1 move. In total, 1 + 2 + 1 = 4 moves were used. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> seats = [4,1,5,9], students = [1,3,2,6] <strong>Output:</strong> 7 <strong>Explanation:</strong> The students are moved as follows: - The first student is not moved. - The second student is moved from position 3 to position 4 using 1 move. - The third student is moved from position 2 to position 5 using 3 moves. - The fourth student is moved from position 6 to position 9 using 3 moves. In total, 0 + 1 + 3 + 3 = 7 moves were used. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> seats = [2,2,6,6], students = [1,3,2,6] <strong>Output:</strong> 4 <strong>Explanation:</strong> Note that there are two seats at position 2 and two seats at position 6. The students are moved as follows: - The first student is moved from position 1 to position 2 using 1 move. - The second student is moved from position 3 to position 6 using 3 moves. - The third student is not moved. - The fourth student is not moved. In total, 1 + 3 + 0 + 0 = 4 moves were used. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == seats.length == students.length</code></li> <li><code>1 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= seats[i], students[j] &lt;= 100</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minMovesToSeat(vector<int>& seats, vector<int>& students) {\n sort (seats.begin(), seats.end());\n sort (students.begin(), students.end());\n int ans = 0;\n\n for (int i = 0; i < seats.size(); i++) {\n ans += abs(seats[i] - students[i]);\n }\n\n return ans;\n }\n};", "memory": "21100" }
2,148
<p>There are <code>n</code> <strong>availabe </strong>seats and <code>n</code> students <strong>standing</strong> in a room. You are given an array <code>seats</code> of length <code>n</code>, where <code>seats[i]</code> is the position of the <code>i<sup>th</sup></code> seat. You are also given the array <code>students</code> of length <code>n</code>, where <code>students[j]</code> is the position of the <code>j<sup>th</sup></code> student.</p> <p>You may perform the following move any number of times:</p> <ul> <li>Increase or decrease the position of the <code>i<sup>th</sup></code> student by <code>1</code> (i.e., moving the <code>i<sup>th</sup></code> student from position&nbsp;<code>x</code>&nbsp;to <code>x + 1</code> or <code>x - 1</code>)</li> </ul> <p>Return <em>the <strong>minimum number of moves</strong> required to move each student to a seat</em><em> such that no two students are in the same seat.</em></p> <p>Note that there may be <strong>multiple</strong> seats or students in the <strong>same </strong>position at the beginning.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> seats = [3,1,5], students = [2,7,4] <strong>Output:</strong> 4 <strong>Explanation:</strong> The students are moved as follows: - The first student is moved from position 2 to position 1 using 1 move. - The second student is moved from position 7 to position 5 using 2 moves. - The third student is moved from position 4 to position 3 using 1 move. In total, 1 + 2 + 1 = 4 moves were used. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> seats = [4,1,5,9], students = [1,3,2,6] <strong>Output:</strong> 7 <strong>Explanation:</strong> The students are moved as follows: - The first student is not moved. - The second student is moved from position 3 to position 4 using 1 move. - The third student is moved from position 2 to position 5 using 3 moves. - The fourth student is moved from position 6 to position 9 using 3 moves. In total, 0 + 1 + 3 + 3 = 7 moves were used. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> seats = [2,2,6,6], students = [1,3,2,6] <strong>Output:</strong> 4 <strong>Explanation:</strong> Note that there are two seats at position 2 and two seats at position 6. The students are moved as follows: - The first student is moved from position 1 to position 2 using 1 move. - The second student is moved from position 3 to position 6 using 3 moves. - The third student is not moved. - The fourth student is not moved. In total, 1 + 3 + 0 + 0 = 4 moves were used. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == seats.length == students.length</code></li> <li><code>1 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= seats[i], students[j] &lt;= 100</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minMovesToSeat(vector<int>& seats, vector<int>& students) {\n sort(seats.begin(),seats.end());\n sort(students.begin(),students.end());\n int ans=0;\n int moves=0;\n int n=seats.size();\n for(int i=0;i<n;i++){\n int diffrence=0;\n if(seats[i]>students[i]){\n diffrence=seats[i]-students[i];\n moves+=diffrence;\n }\n else if(seats[i]<students[i]){\n diffrence=students[i]-seats[i];\n moves+=diffrence;\n }\n else{\n moves+=diffrence;\n }\n }\n return moves;\n }\n};", "memory": "21200" }
2,148
<p>There are <code>n</code> <strong>availabe </strong>seats and <code>n</code> students <strong>standing</strong> in a room. You are given an array <code>seats</code> of length <code>n</code>, where <code>seats[i]</code> is the position of the <code>i<sup>th</sup></code> seat. You are also given the array <code>students</code> of length <code>n</code>, where <code>students[j]</code> is the position of the <code>j<sup>th</sup></code> student.</p> <p>You may perform the following move any number of times:</p> <ul> <li>Increase or decrease the position of the <code>i<sup>th</sup></code> student by <code>1</code> (i.e., moving the <code>i<sup>th</sup></code> student from position&nbsp;<code>x</code>&nbsp;to <code>x + 1</code> or <code>x - 1</code>)</li> </ul> <p>Return <em>the <strong>minimum number of moves</strong> required to move each student to a seat</em><em> such that no two students are in the same seat.</em></p> <p>Note that there may be <strong>multiple</strong> seats or students in the <strong>same </strong>position at the beginning.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> seats = [3,1,5], students = [2,7,4] <strong>Output:</strong> 4 <strong>Explanation:</strong> The students are moved as follows: - The first student is moved from position 2 to position 1 using 1 move. - The second student is moved from position 7 to position 5 using 2 moves. - The third student is moved from position 4 to position 3 using 1 move. In total, 1 + 2 + 1 = 4 moves were used. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> seats = [4,1,5,9], students = [1,3,2,6] <strong>Output:</strong> 7 <strong>Explanation:</strong> The students are moved as follows: - The first student is not moved. - The second student is moved from position 3 to position 4 using 1 move. - The third student is moved from position 2 to position 5 using 3 moves. - The fourth student is moved from position 6 to position 9 using 3 moves. In total, 0 + 1 + 3 + 3 = 7 moves were used. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> seats = [2,2,6,6], students = [1,3,2,6] <strong>Output:</strong> 4 <strong>Explanation:</strong> Note that there are two seats at position 2 and two seats at position 6. The students are moved as follows: - The first student is moved from position 1 to position 2 using 1 move. - The second student is moved from position 3 to position 6 using 3 moves. - The third student is not moved. - The fourth student is not moved. In total, 1 + 3 + 0 + 0 = 4 moves were used. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == seats.length == students.length</code></li> <li><code>1 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= seats[i], students[j] &lt;= 100</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minMovesToSeat(vector<int>& seats, vector<int>& students) {\n sort(seats.begin(), seats.end());\n sort(students.begin(), students.end());\n int ans = 0;\n for (int i = 0; i < seats.size(); ++i) {\n ans += abs(seats[i] - students[i]);\n }\n return ans;\n }\n};", "memory": "21200" }
2,148
<p>There are <code>n</code> <strong>availabe </strong>seats and <code>n</code> students <strong>standing</strong> in a room. You are given an array <code>seats</code> of length <code>n</code>, where <code>seats[i]</code> is the position of the <code>i<sup>th</sup></code> seat. You are also given the array <code>students</code> of length <code>n</code>, where <code>students[j]</code> is the position of the <code>j<sup>th</sup></code> student.</p> <p>You may perform the following move any number of times:</p> <ul> <li>Increase or decrease the position of the <code>i<sup>th</sup></code> student by <code>1</code> (i.e., moving the <code>i<sup>th</sup></code> student from position&nbsp;<code>x</code>&nbsp;to <code>x + 1</code> or <code>x - 1</code>)</li> </ul> <p>Return <em>the <strong>minimum number of moves</strong> required to move each student to a seat</em><em> such that no two students are in the same seat.</em></p> <p>Note that there may be <strong>multiple</strong> seats or students in the <strong>same </strong>position at the beginning.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> seats = [3,1,5], students = [2,7,4] <strong>Output:</strong> 4 <strong>Explanation:</strong> The students are moved as follows: - The first student is moved from position 2 to position 1 using 1 move. - The second student is moved from position 7 to position 5 using 2 moves. - The third student is moved from position 4 to position 3 using 1 move. In total, 1 + 2 + 1 = 4 moves were used. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> seats = [4,1,5,9], students = [1,3,2,6] <strong>Output:</strong> 7 <strong>Explanation:</strong> The students are moved as follows: - The first student is not moved. - The second student is moved from position 3 to position 4 using 1 move. - The third student is moved from position 2 to position 5 using 3 moves. - The fourth student is moved from position 6 to position 9 using 3 moves. In total, 0 + 1 + 3 + 3 = 7 moves were used. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> seats = [2,2,6,6], students = [1,3,2,6] <strong>Output:</strong> 4 <strong>Explanation:</strong> Note that there are two seats at position 2 and two seats at position 6. The students are moved as follows: - The first student is moved from position 1 to position 2 using 1 move. - The second student is moved from position 3 to position 6 using 3 moves. - The third student is not moved. - The fourth student is not moved. In total, 1 + 3 + 0 + 0 = 4 moves were used. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == seats.length == students.length</code></li> <li><code>1 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= seats[i], students[j] &lt;= 100</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minMovesToSeat(vector<int>& seats, vector<int>& students) {\n sort(seats.begin(),seats.end());\n sort(students.begin(),students.end());\n int ans=0;\n int moves=0;\n int n=seats.size();\n for(int i=0;i<n;i++){\n int diffrence=0;\n if(seats[i]>students[i]){\n diffrence=seats[i]-students[i];\n moves+=diffrence;\n }\n else if(seats[i]<students[i]){\n diffrence=students[i]-seats[i];\n moves+=diffrence;\n }\n else{\n moves+=diffrence;\n }\n }\n return moves;\n }\n};", "memory": "21300" }
2,148
<p>There are <code>n</code> <strong>availabe </strong>seats and <code>n</code> students <strong>standing</strong> in a room. You are given an array <code>seats</code> of length <code>n</code>, where <code>seats[i]</code> is the position of the <code>i<sup>th</sup></code> seat. You are also given the array <code>students</code> of length <code>n</code>, where <code>students[j]</code> is the position of the <code>j<sup>th</sup></code> student.</p> <p>You may perform the following move any number of times:</p> <ul> <li>Increase or decrease the position of the <code>i<sup>th</sup></code> student by <code>1</code> (i.e., moving the <code>i<sup>th</sup></code> student from position&nbsp;<code>x</code>&nbsp;to <code>x + 1</code> or <code>x - 1</code>)</li> </ul> <p>Return <em>the <strong>minimum number of moves</strong> required to move each student to a seat</em><em> such that no two students are in the same seat.</em></p> <p>Note that there may be <strong>multiple</strong> seats or students in the <strong>same </strong>position at the beginning.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> seats = [3,1,5], students = [2,7,4] <strong>Output:</strong> 4 <strong>Explanation:</strong> The students are moved as follows: - The first student is moved from position 2 to position 1 using 1 move. - The second student is moved from position 7 to position 5 using 2 moves. - The third student is moved from position 4 to position 3 using 1 move. In total, 1 + 2 + 1 = 4 moves were used. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> seats = [4,1,5,9], students = [1,3,2,6] <strong>Output:</strong> 7 <strong>Explanation:</strong> The students are moved as follows: - The first student is not moved. - The second student is moved from position 3 to position 4 using 1 move. - The third student is moved from position 2 to position 5 using 3 moves. - The fourth student is moved from position 6 to position 9 using 3 moves. In total, 0 + 1 + 3 + 3 = 7 moves were used. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> seats = [2,2,6,6], students = [1,3,2,6] <strong>Output:</strong> 4 <strong>Explanation:</strong> Note that there are two seats at position 2 and two seats at position 6. The students are moved as follows: - The first student is moved from position 1 to position 2 using 1 move. - The second student is moved from position 3 to position 6 using 3 moves. - The third student is not moved. - The fourth student is not moved. In total, 1 + 3 + 0 + 0 = 4 moves were used. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == seats.length == students.length</code></li> <li><code>1 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= seats[i], students[j] &lt;= 100</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minMovesToSeat(vector<int>& seats, vector<int>& students) {\n int sum=0;\n int n=seats.size();\n sort(seats.begin(),seats.end());\n sort(students.begin(),students.end());\n for(int i=0;i<n;i++){\n if(seats[i]>students[i]){\n sum=sum+(seats[i]-students[i]);\n }\n else\n sum=sum+(students[i]-seats[i]);\n \n }\n return sum;\n }\n};", "memory": "21300" }
2,148
<p>There are <code>n</code> <strong>availabe </strong>seats and <code>n</code> students <strong>standing</strong> in a room. You are given an array <code>seats</code> of length <code>n</code>, where <code>seats[i]</code> is the position of the <code>i<sup>th</sup></code> seat. You are also given the array <code>students</code> of length <code>n</code>, where <code>students[j]</code> is the position of the <code>j<sup>th</sup></code> student.</p> <p>You may perform the following move any number of times:</p> <ul> <li>Increase or decrease the position of the <code>i<sup>th</sup></code> student by <code>1</code> (i.e., moving the <code>i<sup>th</sup></code> student from position&nbsp;<code>x</code>&nbsp;to <code>x + 1</code> or <code>x - 1</code>)</li> </ul> <p>Return <em>the <strong>minimum number of moves</strong> required to move each student to a seat</em><em> such that no two students are in the same seat.</em></p> <p>Note that there may be <strong>multiple</strong> seats or students in the <strong>same </strong>position at the beginning.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> seats = [3,1,5], students = [2,7,4] <strong>Output:</strong> 4 <strong>Explanation:</strong> The students are moved as follows: - The first student is moved from position 2 to position 1 using 1 move. - The second student is moved from position 7 to position 5 using 2 moves. - The third student is moved from position 4 to position 3 using 1 move. In total, 1 + 2 + 1 = 4 moves were used. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> seats = [4,1,5,9], students = [1,3,2,6] <strong>Output:</strong> 7 <strong>Explanation:</strong> The students are moved as follows: - The first student is not moved. - The second student is moved from position 3 to position 4 using 1 move. - The third student is moved from position 2 to position 5 using 3 moves. - The fourth student is moved from position 6 to position 9 using 3 moves. In total, 0 + 1 + 3 + 3 = 7 moves were used. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> seats = [2,2,6,6], students = [1,3,2,6] <strong>Output:</strong> 4 <strong>Explanation:</strong> Note that there are two seats at position 2 and two seats at position 6. The students are moved as follows: - The first student is moved from position 1 to position 2 using 1 move. - The second student is moved from position 3 to position 6 using 3 moves. - The third student is not moved. - The fourth student is not moved. In total, 1 + 3 + 0 + 0 = 4 moves were used. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == seats.length == students.length</code></li> <li><code>1 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= seats[i], students[j] &lt;= 100</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int minMovesToSeat(vector<int>& seats, vector<int>& students) {\n sort(seats.begin(),seats.end());\n sort(students.begin(),students.end());\n int ans=0;\n int moves=0;\n int n=seats.size();\n for(int i=0;i<n;i++){\n int diffrence=0;\n if(seats[i]>students[i]){\n diffrence=seats[i]-students[i];\n moves+=diffrence;\n }\n else if(seats[i]<students[i]){\n diffrence=students[i]-seats[i];\n moves+=diffrence;\n }\n else{\n moves+=diffrence;\n }\n }\n return moves;\n }\n};", "memory": "21400" }
2,148
<p>There are <code>n</code> <strong>availabe </strong>seats and <code>n</code> students <strong>standing</strong> in a room. You are given an array <code>seats</code> of length <code>n</code>, where <code>seats[i]</code> is the position of the <code>i<sup>th</sup></code> seat. You are also given the array <code>students</code> of length <code>n</code>, where <code>students[j]</code> is the position of the <code>j<sup>th</sup></code> student.</p> <p>You may perform the following move any number of times:</p> <ul> <li>Increase or decrease the position of the <code>i<sup>th</sup></code> student by <code>1</code> (i.e., moving the <code>i<sup>th</sup></code> student from position&nbsp;<code>x</code>&nbsp;to <code>x + 1</code> or <code>x - 1</code>)</li> </ul> <p>Return <em>the <strong>minimum number of moves</strong> required to move each student to a seat</em><em> such that no two students are in the same seat.</em></p> <p>Note that there may be <strong>multiple</strong> seats or students in the <strong>same </strong>position at the beginning.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> seats = [3,1,5], students = [2,7,4] <strong>Output:</strong> 4 <strong>Explanation:</strong> The students are moved as follows: - The first student is moved from position 2 to position 1 using 1 move. - The second student is moved from position 7 to position 5 using 2 moves. - The third student is moved from position 4 to position 3 using 1 move. In total, 1 + 2 + 1 = 4 moves were used. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> seats = [4,1,5,9], students = [1,3,2,6] <strong>Output:</strong> 7 <strong>Explanation:</strong> The students are moved as follows: - The first student is not moved. - The second student is moved from position 3 to position 4 using 1 move. - The third student is moved from position 2 to position 5 using 3 moves. - The fourth student is moved from position 6 to position 9 using 3 moves. In total, 0 + 1 + 3 + 3 = 7 moves were used. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> seats = [2,2,6,6], students = [1,3,2,6] <strong>Output:</strong> 4 <strong>Explanation:</strong> Note that there are two seats at position 2 and two seats at position 6. The students are moved as follows: - The first student is moved from position 1 to position 2 using 1 move. - The second student is moved from position 3 to position 6 using 3 moves. - The third student is not moved. - The fourth student is not moved. In total, 1 + 3 + 0 + 0 = 4 moves were used. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == seats.length == students.length</code></li> <li><code>1 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= seats[i], students[j] &lt;= 100</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int minMovesToSeat(vector<int>& seats, vector<int>& students) {\n \n sort(seats.begin(), seats.end());\n sort(students.begin(), students.end());\n\n int ans=0;\n\n for(int i=0;i<seats.size(); i++){\n ans+= abs(seats[i]- students[i]);\n }\n\n return ans;\n }\n};", "memory": "21400" }
2,149
<p>There are <code>n</code> pieces arranged in a line, and each piece is colored either by <code>&#39;A&#39;</code> or by <code>&#39;B&#39;</code>. You are given a string <code>colors</code> of length <code>n</code> where <code>colors[i]</code> is the color of the <code>i<sup>th</sup></code> piece.</p> <p>Alice and Bob are playing a game where they take <strong>alternating turns</strong> removing pieces from the line. In this game, Alice moves<strong> first</strong>.</p> <ul> <li>Alice is only allowed to remove a piece colored <code>&#39;A&#39;</code> if <strong>both its neighbors</strong> are also colored <code>&#39;A&#39;</code>. She is <strong>not allowed</strong> to remove pieces that are colored <code>&#39;B&#39;</code>.</li> <li>Bob is only allowed to remove a piece colored <code>&#39;B&#39;</code> if <strong>both its neighbors</strong> are also colored <code>&#39;B&#39;</code>. He is <strong>not allowed</strong> to remove pieces that are colored <code>&#39;A&#39;</code>.</li> <li>Alice and Bob <strong>cannot</strong> remove pieces from the edge of the line.</li> <li>If a player cannot make a move on their turn, that player <strong>loses</strong> and the other player <strong>wins</strong>.</li> </ul> <p>Assuming Alice and Bob play optimally, return <code>true</code><em> if Alice wins, or return </em><code>false</code><em> if Bob wins</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> colors = &quot;AAABABB&quot; <strong>Output:</strong> true <strong>Explanation:</strong> A<u>A</u>ABABB -&gt; AABABB Alice moves first. She removes the second &#39;A&#39; from the left since that is the only &#39;A&#39; whose neighbors are both &#39;A&#39;. Now it&#39;s Bob&#39;s turn. Bob cannot make a move on his turn since there are no &#39;B&#39;s whose neighbors are both &#39;B&#39;. Thus, Alice wins, so return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> colors = &quot;AA&quot; <strong>Output:</strong> false <strong>Explanation:</strong> Alice has her turn first. There are only two &#39;A&#39;s and both are on the edge of the line, so she cannot move on her turn. Thus, Bob wins, so return false. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> colors = &quot;ABBBBBBBAAA&quot; <strong>Output:</strong> false <strong>Explanation:</strong> ABBBBBBBA<u>A</u>A -&gt; ABBBBBBBAA Alice moves first. Her only option is to remove the second to last &#39;A&#39; from the right. ABBBB<u>B</u>BBAA -&gt; ABBBBBBAA Next is Bob&#39;s turn. He has many options for which &#39;B&#39; piece to remove. He can pick any. On Alice&#39;s second turn, she has no more pieces that she can remove. Thus, Bob wins, so return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;=&nbsp;colors.length &lt;= 10<sup>5</sup></code></li> <li><code>colors</code>&nbsp;consists of only the letters&nbsp;<code>&#39;A&#39;</code>&nbsp;and&nbsp;<code>&#39;B&#39;</code></li> </ul>
0
{ "code": "\n\n\n\nint _ = [](){ std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); return 0; }();\n\nclass Solution {\npublic:\n bool winnerOfGame(string &colors) {\n int const n = colors.length();\n int a = 0, b = 0, count = 0;\n int left = 0, right = 0;\n while(left < n) {\n while(right < n and colors[right] == colors[left]) { ++right; }\n count = (right - left - 2) * ((right - left - 2) > 0);\n a = a + count * (colors[left] == 'A');\n b = b + count * (colors[left] == 'B');\n left = right;\n }\n return a > b;\n }\n};", "memory": "12700" }
2,149
<p>There are <code>n</code> pieces arranged in a line, and each piece is colored either by <code>&#39;A&#39;</code> or by <code>&#39;B&#39;</code>. You are given a string <code>colors</code> of length <code>n</code> where <code>colors[i]</code> is the color of the <code>i<sup>th</sup></code> piece.</p> <p>Alice and Bob are playing a game where they take <strong>alternating turns</strong> removing pieces from the line. In this game, Alice moves<strong> first</strong>.</p> <ul> <li>Alice is only allowed to remove a piece colored <code>&#39;A&#39;</code> if <strong>both its neighbors</strong> are also colored <code>&#39;A&#39;</code>. She is <strong>not allowed</strong> to remove pieces that are colored <code>&#39;B&#39;</code>.</li> <li>Bob is only allowed to remove a piece colored <code>&#39;B&#39;</code> if <strong>both its neighbors</strong> are also colored <code>&#39;B&#39;</code>. He is <strong>not allowed</strong> to remove pieces that are colored <code>&#39;A&#39;</code>.</li> <li>Alice and Bob <strong>cannot</strong> remove pieces from the edge of the line.</li> <li>If a player cannot make a move on their turn, that player <strong>loses</strong> and the other player <strong>wins</strong>.</li> </ul> <p>Assuming Alice and Bob play optimally, return <code>true</code><em> if Alice wins, or return </em><code>false</code><em> if Bob wins</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> colors = &quot;AAABABB&quot; <strong>Output:</strong> true <strong>Explanation:</strong> A<u>A</u>ABABB -&gt; AABABB Alice moves first. She removes the second &#39;A&#39; from the left since that is the only &#39;A&#39; whose neighbors are both &#39;A&#39;. Now it&#39;s Bob&#39;s turn. Bob cannot make a move on his turn since there are no &#39;B&#39;s whose neighbors are both &#39;B&#39;. Thus, Alice wins, so return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> colors = &quot;AA&quot; <strong>Output:</strong> false <strong>Explanation:</strong> Alice has her turn first. There are only two &#39;A&#39;s and both are on the edge of the line, so she cannot move on her turn. Thus, Bob wins, so return false. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> colors = &quot;ABBBBBBBAAA&quot; <strong>Output:</strong> false <strong>Explanation:</strong> ABBBBBBBA<u>A</u>A -&gt; ABBBBBBBAA Alice moves first. Her only option is to remove the second to last &#39;A&#39; from the right. ABBBB<u>B</u>BBAA -&gt; ABBBBBBAA Next is Bob&#39;s turn. He has many options for which &#39;B&#39; piece to remove. He can pick any. On Alice&#39;s second turn, she has no more pieces that she can remove. Thus, Bob wins, so return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;=&nbsp;colors.length &lt;= 10<sup>5</sup></code></li> <li><code>colors</code>&nbsp;consists of only the letters&nbsp;<code>&#39;A&#39;</code>&nbsp;and&nbsp;<code>&#39;B&#39;</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool winnerOfGame(std::string& colors) {\n auto left1 = colors.begin();\n auto left2 = colors.begin();\n int alica = 0;\n int bob = 0;\n bool ans = false;\n while (left1 != colors.end() || left2 != colors.end()) {\n while (left1 != colors.end()) {\n if (*left1 == 'A')\n alica++;\n else\n alica = 0;\n left1++;\n if (alica == 3) {\n ans = true;\n alica--;\n break;\n }\n }\n\n while (left2 != colors.end()) {\n if (*left2 == 'B')\n bob++;\n else\n bob = 0;\n left2++;\n if (bob == 3) {\n ans = false;\n bob--;\n break;\n }\n }\n }\n return ans;\n }\n};", "memory": "12800" }
2,149
<p>There are <code>n</code> pieces arranged in a line, and each piece is colored either by <code>&#39;A&#39;</code> or by <code>&#39;B&#39;</code>. You are given a string <code>colors</code> of length <code>n</code> where <code>colors[i]</code> is the color of the <code>i<sup>th</sup></code> piece.</p> <p>Alice and Bob are playing a game where they take <strong>alternating turns</strong> removing pieces from the line. In this game, Alice moves<strong> first</strong>.</p> <ul> <li>Alice is only allowed to remove a piece colored <code>&#39;A&#39;</code> if <strong>both its neighbors</strong> are also colored <code>&#39;A&#39;</code>. She is <strong>not allowed</strong> to remove pieces that are colored <code>&#39;B&#39;</code>.</li> <li>Bob is only allowed to remove a piece colored <code>&#39;B&#39;</code> if <strong>both its neighbors</strong> are also colored <code>&#39;B&#39;</code>. He is <strong>not allowed</strong> to remove pieces that are colored <code>&#39;A&#39;</code>.</li> <li>Alice and Bob <strong>cannot</strong> remove pieces from the edge of the line.</li> <li>If a player cannot make a move on their turn, that player <strong>loses</strong> and the other player <strong>wins</strong>.</li> </ul> <p>Assuming Alice and Bob play optimally, return <code>true</code><em> if Alice wins, or return </em><code>false</code><em> if Bob wins</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> colors = &quot;AAABABB&quot; <strong>Output:</strong> true <strong>Explanation:</strong> A<u>A</u>ABABB -&gt; AABABB Alice moves first. She removes the second &#39;A&#39; from the left since that is the only &#39;A&#39; whose neighbors are both &#39;A&#39;. Now it&#39;s Bob&#39;s turn. Bob cannot make a move on his turn since there are no &#39;B&#39;s whose neighbors are both &#39;B&#39;. Thus, Alice wins, so return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> colors = &quot;AA&quot; <strong>Output:</strong> false <strong>Explanation:</strong> Alice has her turn first. There are only two &#39;A&#39;s and both are on the edge of the line, so she cannot move on her turn. Thus, Bob wins, so return false. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> colors = &quot;ABBBBBBBAAA&quot; <strong>Output:</strong> false <strong>Explanation:</strong> ABBBBBBBA<u>A</u>A -&gt; ABBBBBBBAA Alice moves first. Her only option is to remove the second to last &#39;A&#39; from the right. ABBBB<u>B</u>BBAA -&gt; ABBBBBBAA Next is Bob&#39;s turn. He has many options for which &#39;B&#39; piece to remove. He can pick any. On Alice&#39;s second turn, she has no more pieces that she can remove. Thus, Bob wins, so return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;=&nbsp;colors.length &lt;= 10<sup>5</sup></code></li> <li><code>colors</code>&nbsp;consists of only the letters&nbsp;<code>&#39;A&#39;</code>&nbsp;and&nbsp;<code>&#39;B&#39;</code></li> </ul>
0
{ "code": "static const int speedup = []{ios::sync_with_stdio(0); cin.tie(0); return 0;}();\n\nclass Solution {\npublic:\n bool winnerOfGame(std::string& colors) {\n auto left1 = colors.begin();\n auto left2 = colors.begin();\n int alica = 0;\n int bob = 0;\n bool ans = false;\n while (left1 != colors.end() || left2 != colors.end()) {\n while (left1 != colors.end()) {\n if (*left1 == 'A')\n alica++;\n else\n alica = 0;\n left1++;\n if (alica == 3) {\n ans = true;\n alica--;\n break;\n }\n }\n\n while (left2 != colors.end()) {\n if (*left2 == 'B')\n bob++;\n else\n bob = 0;\n left2++;\n if (bob == 3) {\n ans = false;\n bob--;\n break;\n }\n }\n }\n return ans;\n }\n};", "memory": "12900" }
2,149
<p>There are <code>n</code> pieces arranged in a line, and each piece is colored either by <code>&#39;A&#39;</code> or by <code>&#39;B&#39;</code>. You are given a string <code>colors</code> of length <code>n</code> where <code>colors[i]</code> is the color of the <code>i<sup>th</sup></code> piece.</p> <p>Alice and Bob are playing a game where they take <strong>alternating turns</strong> removing pieces from the line. In this game, Alice moves<strong> first</strong>.</p> <ul> <li>Alice is only allowed to remove a piece colored <code>&#39;A&#39;</code> if <strong>both its neighbors</strong> are also colored <code>&#39;A&#39;</code>. She is <strong>not allowed</strong> to remove pieces that are colored <code>&#39;B&#39;</code>.</li> <li>Bob is only allowed to remove a piece colored <code>&#39;B&#39;</code> if <strong>both its neighbors</strong> are also colored <code>&#39;B&#39;</code>. He is <strong>not allowed</strong> to remove pieces that are colored <code>&#39;A&#39;</code>.</li> <li>Alice and Bob <strong>cannot</strong> remove pieces from the edge of the line.</li> <li>If a player cannot make a move on their turn, that player <strong>loses</strong> and the other player <strong>wins</strong>.</li> </ul> <p>Assuming Alice and Bob play optimally, return <code>true</code><em> if Alice wins, or return </em><code>false</code><em> if Bob wins</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> colors = &quot;AAABABB&quot; <strong>Output:</strong> true <strong>Explanation:</strong> A<u>A</u>ABABB -&gt; AABABB Alice moves first. She removes the second &#39;A&#39; from the left since that is the only &#39;A&#39; whose neighbors are both &#39;A&#39;. Now it&#39;s Bob&#39;s turn. Bob cannot make a move on his turn since there are no &#39;B&#39;s whose neighbors are both &#39;B&#39;. Thus, Alice wins, so return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> colors = &quot;AA&quot; <strong>Output:</strong> false <strong>Explanation:</strong> Alice has her turn first. There are only two &#39;A&#39;s and both are on the edge of the line, so she cannot move on her turn. Thus, Bob wins, so return false. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> colors = &quot;ABBBBBBBAAA&quot; <strong>Output:</strong> false <strong>Explanation:</strong> ABBBBBBBA<u>A</u>A -&gt; ABBBBBBBAA Alice moves first. Her only option is to remove the second to last &#39;A&#39; from the right. ABBBB<u>B</u>BBAA -&gt; ABBBBBBAA Next is Bob&#39;s turn. He has many options for which &#39;B&#39; piece to remove. He can pick any. On Alice&#39;s second turn, she has no more pieces that she can remove. Thus, Bob wins, so return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;=&nbsp;colors.length &lt;= 10<sup>5</sup></code></li> <li><code>colors</code>&nbsp;consists of only the letters&nbsp;<code>&#39;A&#39;</code>&nbsp;and&nbsp;<code>&#39;B&#39;</code></li> </ul>
0
{ "code": "const int _ = [](){\n ios::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n return 0;\n}();\nclass Solution {\npublic:\n bool winnerOfGame(string& colors) {\n int alice = 0, bob = 0, cnt = 1;\n\n for (int j = 1; j < colors.size(); j++) {\n if (colors[j] == colors[j - 1])\n cnt++;\n else {\n if (cnt >= 3) {\n if (colors[j - 1] == 'A')\n alice += cnt - 2;\n else\n bob += cnt - 2;\n }\n cnt = 1;\n }\n }\n if (cnt >= 3) {\n if (colors[colors.size()-1] == 'A')\n alice += cnt - 2;\n else\n bob += cnt - 2;\n }\n return alice>bob;\n }\n};", "memory": "13000" }
2,149
<p>There are <code>n</code> pieces arranged in a line, and each piece is colored either by <code>&#39;A&#39;</code> or by <code>&#39;B&#39;</code>. You are given a string <code>colors</code> of length <code>n</code> where <code>colors[i]</code> is the color of the <code>i<sup>th</sup></code> piece.</p> <p>Alice and Bob are playing a game where they take <strong>alternating turns</strong> removing pieces from the line. In this game, Alice moves<strong> first</strong>.</p> <ul> <li>Alice is only allowed to remove a piece colored <code>&#39;A&#39;</code> if <strong>both its neighbors</strong> are also colored <code>&#39;A&#39;</code>. She is <strong>not allowed</strong> to remove pieces that are colored <code>&#39;B&#39;</code>.</li> <li>Bob is only allowed to remove a piece colored <code>&#39;B&#39;</code> if <strong>both its neighbors</strong> are also colored <code>&#39;B&#39;</code>. He is <strong>not allowed</strong> to remove pieces that are colored <code>&#39;A&#39;</code>.</li> <li>Alice and Bob <strong>cannot</strong> remove pieces from the edge of the line.</li> <li>If a player cannot make a move on their turn, that player <strong>loses</strong> and the other player <strong>wins</strong>.</li> </ul> <p>Assuming Alice and Bob play optimally, return <code>true</code><em> if Alice wins, or return </em><code>false</code><em> if Bob wins</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> colors = &quot;AAABABB&quot; <strong>Output:</strong> true <strong>Explanation:</strong> A<u>A</u>ABABB -&gt; AABABB Alice moves first. She removes the second &#39;A&#39; from the left since that is the only &#39;A&#39; whose neighbors are both &#39;A&#39;. Now it&#39;s Bob&#39;s turn. Bob cannot make a move on his turn since there are no &#39;B&#39;s whose neighbors are both &#39;B&#39;. Thus, Alice wins, so return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> colors = &quot;AA&quot; <strong>Output:</strong> false <strong>Explanation:</strong> Alice has her turn first. There are only two &#39;A&#39;s and both are on the edge of the line, so she cannot move on her turn. Thus, Bob wins, so return false. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> colors = &quot;ABBBBBBBAAA&quot; <strong>Output:</strong> false <strong>Explanation:</strong> ABBBBBBBA<u>A</u>A -&gt; ABBBBBBBAA Alice moves first. Her only option is to remove the second to last &#39;A&#39; from the right. ABBBB<u>B</u>BBAA -&gt; ABBBBBBAA Next is Bob&#39;s turn. He has many options for which &#39;B&#39; piece to remove. He can pick any. On Alice&#39;s second turn, she has no more pieces that she can remove. Thus, Bob wins, so return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;=&nbsp;colors.length &lt;= 10<sup>5</sup></code></li> <li><code>colors</code>&nbsp;consists of only the letters&nbsp;<code>&#39;A&#39;</code>&nbsp;and&nbsp;<code>&#39;B&#39;</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool winnerOfGame(const string& s) {\n array<int, 2> count;\n const int n = (int)s.size();\n\n for (int i = 0; i < n-2; ++i) {\n if (s[i] == s[i+1] && s[i+1] == s[i+2])\n count[s[i]-'A']++;\n }\n return count[0] > count[1];\n }\n};", "memory": "13100" }
2,149
<p>There are <code>n</code> pieces arranged in a line, and each piece is colored either by <code>&#39;A&#39;</code> or by <code>&#39;B&#39;</code>. You are given a string <code>colors</code> of length <code>n</code> where <code>colors[i]</code> is the color of the <code>i<sup>th</sup></code> piece.</p> <p>Alice and Bob are playing a game where they take <strong>alternating turns</strong> removing pieces from the line. In this game, Alice moves<strong> first</strong>.</p> <ul> <li>Alice is only allowed to remove a piece colored <code>&#39;A&#39;</code> if <strong>both its neighbors</strong> are also colored <code>&#39;A&#39;</code>. She is <strong>not allowed</strong> to remove pieces that are colored <code>&#39;B&#39;</code>.</li> <li>Bob is only allowed to remove a piece colored <code>&#39;B&#39;</code> if <strong>both its neighbors</strong> are also colored <code>&#39;B&#39;</code>. He is <strong>not allowed</strong> to remove pieces that are colored <code>&#39;A&#39;</code>.</li> <li>Alice and Bob <strong>cannot</strong> remove pieces from the edge of the line.</li> <li>If a player cannot make a move on their turn, that player <strong>loses</strong> and the other player <strong>wins</strong>.</li> </ul> <p>Assuming Alice and Bob play optimally, return <code>true</code><em> if Alice wins, or return </em><code>false</code><em> if Bob wins</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> colors = &quot;AAABABB&quot; <strong>Output:</strong> true <strong>Explanation:</strong> A<u>A</u>ABABB -&gt; AABABB Alice moves first. She removes the second &#39;A&#39; from the left since that is the only &#39;A&#39; whose neighbors are both &#39;A&#39;. Now it&#39;s Bob&#39;s turn. Bob cannot make a move on his turn since there are no &#39;B&#39;s whose neighbors are both &#39;B&#39;. Thus, Alice wins, so return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> colors = &quot;AA&quot; <strong>Output:</strong> false <strong>Explanation:</strong> Alice has her turn first. There are only two &#39;A&#39;s and both are on the edge of the line, so she cannot move on her turn. Thus, Bob wins, so return false. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> colors = &quot;ABBBBBBBAAA&quot; <strong>Output:</strong> false <strong>Explanation:</strong> ABBBBBBBA<u>A</u>A -&gt; ABBBBBBBAA Alice moves first. Her only option is to remove the second to last &#39;A&#39; from the right. ABBBB<u>B</u>BBAA -&gt; ABBBBBBAA Next is Bob&#39;s turn. He has many options for which &#39;B&#39; piece to remove. He can pick any. On Alice&#39;s second turn, she has no more pieces that she can remove. Thus, Bob wins, so return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;=&nbsp;colors.length &lt;= 10<sup>5</sup></code></li> <li><code>colors</code>&nbsp;consists of only the letters&nbsp;<code>&#39;A&#39;</code>&nbsp;and&nbsp;<code>&#39;B&#39;</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool winnerOfGame(string &colors) {\n int n=colors.size();\n int na=0,nb=0;\n for(int i=1;i<n-1;i++){\n if(colors[i]=='A'){\n if(colors[i-1]=='A'&&colors[i+1]=='A')na++;\n }\n else{\n if(colors[i-1]=='B'&&colors[i+1]=='B')nb++;\n }\n }\n return na>nb;\n }\n};", "memory": "13200" }
2,149
<p>There are <code>n</code> pieces arranged in a line, and each piece is colored either by <code>&#39;A&#39;</code> or by <code>&#39;B&#39;</code>. You are given a string <code>colors</code> of length <code>n</code> where <code>colors[i]</code> is the color of the <code>i<sup>th</sup></code> piece.</p> <p>Alice and Bob are playing a game where they take <strong>alternating turns</strong> removing pieces from the line. In this game, Alice moves<strong> first</strong>.</p> <ul> <li>Alice is only allowed to remove a piece colored <code>&#39;A&#39;</code> if <strong>both its neighbors</strong> are also colored <code>&#39;A&#39;</code>. She is <strong>not allowed</strong> to remove pieces that are colored <code>&#39;B&#39;</code>.</li> <li>Bob is only allowed to remove a piece colored <code>&#39;B&#39;</code> if <strong>both its neighbors</strong> are also colored <code>&#39;B&#39;</code>. He is <strong>not allowed</strong> to remove pieces that are colored <code>&#39;A&#39;</code>.</li> <li>Alice and Bob <strong>cannot</strong> remove pieces from the edge of the line.</li> <li>If a player cannot make a move on their turn, that player <strong>loses</strong> and the other player <strong>wins</strong>.</li> </ul> <p>Assuming Alice and Bob play optimally, return <code>true</code><em> if Alice wins, or return </em><code>false</code><em> if Bob wins</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> colors = &quot;AAABABB&quot; <strong>Output:</strong> true <strong>Explanation:</strong> A<u>A</u>ABABB -&gt; AABABB Alice moves first. She removes the second &#39;A&#39; from the left since that is the only &#39;A&#39; whose neighbors are both &#39;A&#39;. Now it&#39;s Bob&#39;s turn. Bob cannot make a move on his turn since there are no &#39;B&#39;s whose neighbors are both &#39;B&#39;. Thus, Alice wins, so return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> colors = &quot;AA&quot; <strong>Output:</strong> false <strong>Explanation:</strong> Alice has her turn first. There are only two &#39;A&#39;s and both are on the edge of the line, so she cannot move on her turn. Thus, Bob wins, so return false. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> colors = &quot;ABBBBBBBAAA&quot; <strong>Output:</strong> false <strong>Explanation:</strong> ABBBBBBBA<u>A</u>A -&gt; ABBBBBBBAA Alice moves first. Her only option is to remove the second to last &#39;A&#39; from the right. ABBBB<u>B</u>BBAA -&gt; ABBBBBBAA Next is Bob&#39;s turn. He has many options for which &#39;B&#39; piece to remove. He can pick any. On Alice&#39;s second turn, she has no more pieces that she can remove. Thus, Bob wins, so return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;=&nbsp;colors.length &lt;= 10<sup>5</sup></code></li> <li><code>colors</code>&nbsp;consists of only the letters&nbsp;<code>&#39;A&#39;</code>&nbsp;and&nbsp;<code>&#39;B&#39;</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool winnerOfGame(string colors) {\n int n=colors.size();\n int alice=0;\n int bob=0;\n for(int i=1;i<n-1;i++)\n {\n if(colors[i]==colors[i-1]&& colors[i]==colors[i+1])\n {\n if(colors[i]=='A')\n {\n alice++;\n }\n else bob++;\n }\n }\n return alice>bob;\n }\n};", "memory": "14400" }
2,149
<p>There are <code>n</code> pieces arranged in a line, and each piece is colored either by <code>&#39;A&#39;</code> or by <code>&#39;B&#39;</code>. You are given a string <code>colors</code> of length <code>n</code> where <code>colors[i]</code> is the color of the <code>i<sup>th</sup></code> piece.</p> <p>Alice and Bob are playing a game where they take <strong>alternating turns</strong> removing pieces from the line. In this game, Alice moves<strong> first</strong>.</p> <ul> <li>Alice is only allowed to remove a piece colored <code>&#39;A&#39;</code> if <strong>both its neighbors</strong> are also colored <code>&#39;A&#39;</code>. She is <strong>not allowed</strong> to remove pieces that are colored <code>&#39;B&#39;</code>.</li> <li>Bob is only allowed to remove a piece colored <code>&#39;B&#39;</code> if <strong>both its neighbors</strong> are also colored <code>&#39;B&#39;</code>. He is <strong>not allowed</strong> to remove pieces that are colored <code>&#39;A&#39;</code>.</li> <li>Alice and Bob <strong>cannot</strong> remove pieces from the edge of the line.</li> <li>If a player cannot make a move on their turn, that player <strong>loses</strong> and the other player <strong>wins</strong>.</li> </ul> <p>Assuming Alice and Bob play optimally, return <code>true</code><em> if Alice wins, or return </em><code>false</code><em> if Bob wins</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> colors = &quot;AAABABB&quot; <strong>Output:</strong> true <strong>Explanation:</strong> A<u>A</u>ABABB -&gt; AABABB Alice moves first. She removes the second &#39;A&#39; from the left since that is the only &#39;A&#39; whose neighbors are both &#39;A&#39;. Now it&#39;s Bob&#39;s turn. Bob cannot make a move on his turn since there are no &#39;B&#39;s whose neighbors are both &#39;B&#39;. Thus, Alice wins, so return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> colors = &quot;AA&quot; <strong>Output:</strong> false <strong>Explanation:</strong> Alice has her turn first. There are only two &#39;A&#39;s and both are on the edge of the line, so she cannot move on her turn. Thus, Bob wins, so return false. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> colors = &quot;ABBBBBBBAAA&quot; <strong>Output:</strong> false <strong>Explanation:</strong> ABBBBBBBA<u>A</u>A -&gt; ABBBBBBBAA Alice moves first. Her only option is to remove the second to last &#39;A&#39; from the right. ABBBB<u>B</u>BBAA -&gt; ABBBBBBAA Next is Bob&#39;s turn. He has many options for which &#39;B&#39; piece to remove. He can pick any. On Alice&#39;s second turn, she has no more pieces that she can remove. Thus, Bob wins, so return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;=&nbsp;colors.length &lt;= 10<sup>5</sup></code></li> <li><code>colors</code>&nbsp;consists of only the letters&nbsp;<code>&#39;A&#39;</code>&nbsp;and&nbsp;<code>&#39;B&#39;</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool winnerOfGame(string colors) {\n int count = 0;\n int lenA = 0, lenB = 0;\n\n for(auto ch : colors) {\n if(ch == 'A') {\n lenA++;\n if(lenA > 2) \n count++; \n lenB = 0; \n } \n else {\n lenB++;\n if(lenB > 2) \n count--;\n lenA = 0;\n }\n }\n return count > 0;\n }\n};", "memory": "14500" }
2,149
<p>There are <code>n</code> pieces arranged in a line, and each piece is colored either by <code>&#39;A&#39;</code> or by <code>&#39;B&#39;</code>. You are given a string <code>colors</code> of length <code>n</code> where <code>colors[i]</code> is the color of the <code>i<sup>th</sup></code> piece.</p> <p>Alice and Bob are playing a game where they take <strong>alternating turns</strong> removing pieces from the line. In this game, Alice moves<strong> first</strong>.</p> <ul> <li>Alice is only allowed to remove a piece colored <code>&#39;A&#39;</code> if <strong>both its neighbors</strong> are also colored <code>&#39;A&#39;</code>. She is <strong>not allowed</strong> to remove pieces that are colored <code>&#39;B&#39;</code>.</li> <li>Bob is only allowed to remove a piece colored <code>&#39;B&#39;</code> if <strong>both its neighbors</strong> are also colored <code>&#39;B&#39;</code>. He is <strong>not allowed</strong> to remove pieces that are colored <code>&#39;A&#39;</code>.</li> <li>Alice and Bob <strong>cannot</strong> remove pieces from the edge of the line.</li> <li>If a player cannot make a move on their turn, that player <strong>loses</strong> and the other player <strong>wins</strong>.</li> </ul> <p>Assuming Alice and Bob play optimally, return <code>true</code><em> if Alice wins, or return </em><code>false</code><em> if Bob wins</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> colors = &quot;AAABABB&quot; <strong>Output:</strong> true <strong>Explanation:</strong> A<u>A</u>ABABB -&gt; AABABB Alice moves first. She removes the second &#39;A&#39; from the left since that is the only &#39;A&#39; whose neighbors are both &#39;A&#39;. Now it&#39;s Bob&#39;s turn. Bob cannot make a move on his turn since there are no &#39;B&#39;s whose neighbors are both &#39;B&#39;. Thus, Alice wins, so return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> colors = &quot;AA&quot; <strong>Output:</strong> false <strong>Explanation:</strong> Alice has her turn first. There are only two &#39;A&#39;s and both are on the edge of the line, so she cannot move on her turn. Thus, Bob wins, so return false. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> colors = &quot;ABBBBBBBAAA&quot; <strong>Output:</strong> false <strong>Explanation:</strong> ABBBBBBBA<u>A</u>A -&gt; ABBBBBBBAA Alice moves first. Her only option is to remove the second to last &#39;A&#39; from the right. ABBBB<u>B</u>BBAA -&gt; ABBBBBBAA Next is Bob&#39;s turn. He has many options for which &#39;B&#39; piece to remove. He can pick any. On Alice&#39;s second turn, she has no more pieces that she can remove. Thus, Bob wins, so return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;=&nbsp;colors.length &lt;= 10<sup>5</sup></code></li> <li><code>colors</code>&nbsp;consists of only the letters&nbsp;<code>&#39;A&#39;</code>&nbsp;and&nbsp;<code>&#39;B&#39;</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool winnerOfGame(string colors) {\n int a=0,b=0;\n for(int i=1;i<colors.size()-1;i++){\n if(colors[i]=='A' && colors[i]==colors[i-1] && colors[i]==colors[i+1])a++;\n else if(colors[i]=='B' && colors[i]==colors[i-1] && colors[i]==colors[i+1])b++;\n }\n return a>b;\n }\n};", "memory": "14500" }
2,149
<p>There are <code>n</code> pieces arranged in a line, and each piece is colored either by <code>&#39;A&#39;</code> or by <code>&#39;B&#39;</code>. You are given a string <code>colors</code> of length <code>n</code> where <code>colors[i]</code> is the color of the <code>i<sup>th</sup></code> piece.</p> <p>Alice and Bob are playing a game where they take <strong>alternating turns</strong> removing pieces from the line. In this game, Alice moves<strong> first</strong>.</p> <ul> <li>Alice is only allowed to remove a piece colored <code>&#39;A&#39;</code> if <strong>both its neighbors</strong> are also colored <code>&#39;A&#39;</code>. She is <strong>not allowed</strong> to remove pieces that are colored <code>&#39;B&#39;</code>.</li> <li>Bob is only allowed to remove a piece colored <code>&#39;B&#39;</code> if <strong>both its neighbors</strong> are also colored <code>&#39;B&#39;</code>. He is <strong>not allowed</strong> to remove pieces that are colored <code>&#39;A&#39;</code>.</li> <li>Alice and Bob <strong>cannot</strong> remove pieces from the edge of the line.</li> <li>If a player cannot make a move on their turn, that player <strong>loses</strong> and the other player <strong>wins</strong>.</li> </ul> <p>Assuming Alice and Bob play optimally, return <code>true</code><em> if Alice wins, or return </em><code>false</code><em> if Bob wins</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> colors = &quot;AAABABB&quot; <strong>Output:</strong> true <strong>Explanation:</strong> A<u>A</u>ABABB -&gt; AABABB Alice moves first. She removes the second &#39;A&#39; from the left since that is the only &#39;A&#39; whose neighbors are both &#39;A&#39;. Now it&#39;s Bob&#39;s turn. Bob cannot make a move on his turn since there are no &#39;B&#39;s whose neighbors are both &#39;B&#39;. Thus, Alice wins, so return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> colors = &quot;AA&quot; <strong>Output:</strong> false <strong>Explanation:</strong> Alice has her turn first. There are only two &#39;A&#39;s and both are on the edge of the line, so she cannot move on her turn. Thus, Bob wins, so return false. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> colors = &quot;ABBBBBBBAAA&quot; <strong>Output:</strong> false <strong>Explanation:</strong> ABBBBBBBA<u>A</u>A -&gt; ABBBBBBBAA Alice moves first. Her only option is to remove the second to last &#39;A&#39; from the right. ABBBB<u>B</u>BBAA -&gt; ABBBBBBAA Next is Bob&#39;s turn. He has many options for which &#39;B&#39; piece to remove. He can pick any. On Alice&#39;s second turn, she has no more pieces that she can remove. Thus, Bob wins, so return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;=&nbsp;colors.length &lt;= 10<sup>5</sup></code></li> <li><code>colors</code>&nbsp;consists of only the letters&nbsp;<code>&#39;A&#39;</code>&nbsp;and&nbsp;<code>&#39;B&#39;</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool winnerOfGame(string colors) {\n int n = colors.size();\n if (n < 3)return false;\n\n int alice = 0, bob = 0;\n\n for(int i = 1; i < n; ++i){\n int j = i-1;\n while(i < n && colors[i] == colors[i-1]){\n ++i;\n }\n int len = i-j;\n if(colors[j] == 'A'){\n alice += max(0, len-2);\n }\n else bob += max(0, len-2);\n } \n return alice > bob;\n }\n};", "memory": "14600" }
2,149
<p>There are <code>n</code> pieces arranged in a line, and each piece is colored either by <code>&#39;A&#39;</code> or by <code>&#39;B&#39;</code>. You are given a string <code>colors</code> of length <code>n</code> where <code>colors[i]</code> is the color of the <code>i<sup>th</sup></code> piece.</p> <p>Alice and Bob are playing a game where they take <strong>alternating turns</strong> removing pieces from the line. In this game, Alice moves<strong> first</strong>.</p> <ul> <li>Alice is only allowed to remove a piece colored <code>&#39;A&#39;</code> if <strong>both its neighbors</strong> are also colored <code>&#39;A&#39;</code>. She is <strong>not allowed</strong> to remove pieces that are colored <code>&#39;B&#39;</code>.</li> <li>Bob is only allowed to remove a piece colored <code>&#39;B&#39;</code> if <strong>both its neighbors</strong> are also colored <code>&#39;B&#39;</code>. He is <strong>not allowed</strong> to remove pieces that are colored <code>&#39;A&#39;</code>.</li> <li>Alice and Bob <strong>cannot</strong> remove pieces from the edge of the line.</li> <li>If a player cannot make a move on their turn, that player <strong>loses</strong> and the other player <strong>wins</strong>.</li> </ul> <p>Assuming Alice and Bob play optimally, return <code>true</code><em> if Alice wins, or return </em><code>false</code><em> if Bob wins</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> colors = &quot;AAABABB&quot; <strong>Output:</strong> true <strong>Explanation:</strong> A<u>A</u>ABABB -&gt; AABABB Alice moves first. She removes the second &#39;A&#39; from the left since that is the only &#39;A&#39; whose neighbors are both &#39;A&#39;. Now it&#39;s Bob&#39;s turn. Bob cannot make a move on his turn since there are no &#39;B&#39;s whose neighbors are both &#39;B&#39;. Thus, Alice wins, so return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> colors = &quot;AA&quot; <strong>Output:</strong> false <strong>Explanation:</strong> Alice has her turn first. There are only two &#39;A&#39;s and both are on the edge of the line, so she cannot move on her turn. Thus, Bob wins, so return false. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> colors = &quot;ABBBBBBBAAA&quot; <strong>Output:</strong> false <strong>Explanation:</strong> ABBBBBBBA<u>A</u>A -&gt; ABBBBBBBAA Alice moves first. Her only option is to remove the second to last &#39;A&#39; from the right. ABBBB<u>B</u>BBAA -&gt; ABBBBBBAA Next is Bob&#39;s turn. He has many options for which &#39;B&#39; piece to remove. He can pick any. On Alice&#39;s second turn, she has no more pieces that she can remove. Thus, Bob wins, so return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;=&nbsp;colors.length &lt;= 10<sup>5</sup></code></li> <li><code>colors</code>&nbsp;consists of only the letters&nbsp;<code>&#39;A&#39;</code>&nbsp;and&nbsp;<code>&#39;B&#39;</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool winnerOfGame(string colors) {\n int aIndex =0;\n int bIndex = 0;\n int aCounter = 0;\n int bCounter =0;\n bool result = false;\n \n while(true)\n {\n int i = 0;\n for( i=aIndex; i<colors.length(); ++i)\n {\n if(colors[i] == 'A') ++aCounter;\n else aCounter = 0;\n if(aCounter == 3) break;\n }\n //cout<< \"A : \" << aCounter <<endl;\n if(aCounter != 3) return false;\n aCounter = 1;\n aIndex = i;\n \n \n for( i=bIndex; i<colors.length(); ++i)\n {\n if(colors[i] == 'B') ++bCounter;\n else bCounter = 0;\n if(bCounter == 3) break;\n }\n //cout<< \"B : \" << bCounter <<endl;\n if(bCounter != 3) return true;\n bCounter = 1;\n bIndex = i;\n \n \n \n }\n \n \n return result; \n }\n};", "memory": "14600" }
2,149
<p>There are <code>n</code> pieces arranged in a line, and each piece is colored either by <code>&#39;A&#39;</code> or by <code>&#39;B&#39;</code>. You are given a string <code>colors</code> of length <code>n</code> where <code>colors[i]</code> is the color of the <code>i<sup>th</sup></code> piece.</p> <p>Alice and Bob are playing a game where they take <strong>alternating turns</strong> removing pieces from the line. In this game, Alice moves<strong> first</strong>.</p> <ul> <li>Alice is only allowed to remove a piece colored <code>&#39;A&#39;</code> if <strong>both its neighbors</strong> are also colored <code>&#39;A&#39;</code>. She is <strong>not allowed</strong> to remove pieces that are colored <code>&#39;B&#39;</code>.</li> <li>Bob is only allowed to remove a piece colored <code>&#39;B&#39;</code> if <strong>both its neighbors</strong> are also colored <code>&#39;B&#39;</code>. He is <strong>not allowed</strong> to remove pieces that are colored <code>&#39;A&#39;</code>.</li> <li>Alice and Bob <strong>cannot</strong> remove pieces from the edge of the line.</li> <li>If a player cannot make a move on their turn, that player <strong>loses</strong> and the other player <strong>wins</strong>.</li> </ul> <p>Assuming Alice and Bob play optimally, return <code>true</code><em> if Alice wins, or return </em><code>false</code><em> if Bob wins</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> colors = &quot;AAABABB&quot; <strong>Output:</strong> true <strong>Explanation:</strong> A<u>A</u>ABABB -&gt; AABABB Alice moves first. She removes the second &#39;A&#39; from the left since that is the only &#39;A&#39; whose neighbors are both &#39;A&#39;. Now it&#39;s Bob&#39;s turn. Bob cannot make a move on his turn since there are no &#39;B&#39;s whose neighbors are both &#39;B&#39;. Thus, Alice wins, so return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> colors = &quot;AA&quot; <strong>Output:</strong> false <strong>Explanation:</strong> Alice has her turn first. There are only two &#39;A&#39;s and both are on the edge of the line, so she cannot move on her turn. Thus, Bob wins, so return false. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> colors = &quot;ABBBBBBBAAA&quot; <strong>Output:</strong> false <strong>Explanation:</strong> ABBBBBBBA<u>A</u>A -&gt; ABBBBBBBAA Alice moves first. Her only option is to remove the second to last &#39;A&#39; from the right. ABBBB<u>B</u>BBAA -&gt; ABBBBBBAA Next is Bob&#39;s turn. He has many options for which &#39;B&#39; piece to remove. He can pick any. On Alice&#39;s second turn, she has no more pieces that she can remove. Thus, Bob wins, so return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;=&nbsp;colors.length &lt;= 10<sup>5</sup></code></li> <li><code>colors</code>&nbsp;consists of only the letters&nbsp;<code>&#39;A&#39;</code>&nbsp;and&nbsp;<code>&#39;B&#39;</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool winnerOfGame(string colors) {\n int aliceMoves = 0, bobMoves = 0;\n for (int i = 1; i < colors.size() - 1; i++) {\n if (colors[i] == 'A' && colors[i - 1] == 'A' && colors[i + 1] == 'A') {\n aliceMoves++;\n }\n if (colors[i] == 'B' && colors[i - 1] == 'B' && colors[i + 1] == 'B') {\n bobMoves++;\n }\n }\n\n return aliceMoves > bobMoves;\n }\n};", "memory": "14700" }
2,149
<p>There are <code>n</code> pieces arranged in a line, and each piece is colored either by <code>&#39;A&#39;</code> or by <code>&#39;B&#39;</code>. You are given a string <code>colors</code> of length <code>n</code> where <code>colors[i]</code> is the color of the <code>i<sup>th</sup></code> piece.</p> <p>Alice and Bob are playing a game where they take <strong>alternating turns</strong> removing pieces from the line. In this game, Alice moves<strong> first</strong>.</p> <ul> <li>Alice is only allowed to remove a piece colored <code>&#39;A&#39;</code> if <strong>both its neighbors</strong> are also colored <code>&#39;A&#39;</code>. She is <strong>not allowed</strong> to remove pieces that are colored <code>&#39;B&#39;</code>.</li> <li>Bob is only allowed to remove a piece colored <code>&#39;B&#39;</code> if <strong>both its neighbors</strong> are also colored <code>&#39;B&#39;</code>. He is <strong>not allowed</strong> to remove pieces that are colored <code>&#39;A&#39;</code>.</li> <li>Alice and Bob <strong>cannot</strong> remove pieces from the edge of the line.</li> <li>If a player cannot make a move on their turn, that player <strong>loses</strong> and the other player <strong>wins</strong>.</li> </ul> <p>Assuming Alice and Bob play optimally, return <code>true</code><em> if Alice wins, or return </em><code>false</code><em> if Bob wins</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> colors = &quot;AAABABB&quot; <strong>Output:</strong> true <strong>Explanation:</strong> A<u>A</u>ABABB -&gt; AABABB Alice moves first. She removes the second &#39;A&#39; from the left since that is the only &#39;A&#39; whose neighbors are both &#39;A&#39;. Now it&#39;s Bob&#39;s turn. Bob cannot make a move on his turn since there are no &#39;B&#39;s whose neighbors are both &#39;B&#39;. Thus, Alice wins, so return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> colors = &quot;AA&quot; <strong>Output:</strong> false <strong>Explanation:</strong> Alice has her turn first. There are only two &#39;A&#39;s and both are on the edge of the line, so she cannot move on her turn. Thus, Bob wins, so return false. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> colors = &quot;ABBBBBBBAAA&quot; <strong>Output:</strong> false <strong>Explanation:</strong> ABBBBBBBA<u>A</u>A -&gt; ABBBBBBBAA Alice moves first. Her only option is to remove the second to last &#39;A&#39; from the right. ABBBB<u>B</u>BBAA -&gt; ABBBBBBAA Next is Bob&#39;s turn. He has many options for which &#39;B&#39; piece to remove. He can pick any. On Alice&#39;s second turn, she has no more pieces that she can remove. Thus, Bob wins, so return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;=&nbsp;colors.length &lt;= 10<sup>5</sup></code></li> <li><code>colors</code>&nbsp;consists of only the letters&nbsp;<code>&#39;A&#39;</code>&nbsp;and&nbsp;<code>&#39;B&#39;</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool winnerOfGame(string colors) {\n int countA=0, countB=0;\n for(int i=1; i< colors.size()-1;i++){\n if(colors[i]=='A' && colors[i-1]=='A' && colors[i+1]=='A')\n countA++;\n else if(colors[i]=='B' && colors[i-1]=='B' && colors[i+1]=='B')\n countB++;\n }\n if(countA <= countB) return 0;\n return 1;\n }\n};", "memory": "14700" }
2,149
<p>There are <code>n</code> pieces arranged in a line, and each piece is colored either by <code>&#39;A&#39;</code> or by <code>&#39;B&#39;</code>. You are given a string <code>colors</code> of length <code>n</code> where <code>colors[i]</code> is the color of the <code>i<sup>th</sup></code> piece.</p> <p>Alice and Bob are playing a game where they take <strong>alternating turns</strong> removing pieces from the line. In this game, Alice moves<strong> first</strong>.</p> <ul> <li>Alice is only allowed to remove a piece colored <code>&#39;A&#39;</code> if <strong>both its neighbors</strong> are also colored <code>&#39;A&#39;</code>. She is <strong>not allowed</strong> to remove pieces that are colored <code>&#39;B&#39;</code>.</li> <li>Bob is only allowed to remove a piece colored <code>&#39;B&#39;</code> if <strong>both its neighbors</strong> are also colored <code>&#39;B&#39;</code>. He is <strong>not allowed</strong> to remove pieces that are colored <code>&#39;A&#39;</code>.</li> <li>Alice and Bob <strong>cannot</strong> remove pieces from the edge of the line.</li> <li>If a player cannot make a move on their turn, that player <strong>loses</strong> and the other player <strong>wins</strong>.</li> </ul> <p>Assuming Alice and Bob play optimally, return <code>true</code><em> if Alice wins, or return </em><code>false</code><em> if Bob wins</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> colors = &quot;AAABABB&quot; <strong>Output:</strong> true <strong>Explanation:</strong> A<u>A</u>ABABB -&gt; AABABB Alice moves first. She removes the second &#39;A&#39; from the left since that is the only &#39;A&#39; whose neighbors are both &#39;A&#39;. Now it&#39;s Bob&#39;s turn. Bob cannot make a move on his turn since there are no &#39;B&#39;s whose neighbors are both &#39;B&#39;. Thus, Alice wins, so return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> colors = &quot;AA&quot; <strong>Output:</strong> false <strong>Explanation:</strong> Alice has her turn first. There are only two &#39;A&#39;s and both are on the edge of the line, so she cannot move on her turn. Thus, Bob wins, so return false. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> colors = &quot;ABBBBBBBAAA&quot; <strong>Output:</strong> false <strong>Explanation:</strong> ABBBBBBBA<u>A</u>A -&gt; ABBBBBBBAA Alice moves first. Her only option is to remove the second to last &#39;A&#39; from the right. ABBBB<u>B</u>BBAA -&gt; ABBBBBBAA Next is Bob&#39;s turn. He has many options for which &#39;B&#39; piece to remove. He can pick any. On Alice&#39;s second turn, she has no more pieces that she can remove. Thus, Bob wins, so return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;=&nbsp;colors.length &lt;= 10<sup>5</sup></code></li> <li><code>colors</code>&nbsp;consists of only the letters&nbsp;<code>&#39;A&#39;</code>&nbsp;and&nbsp;<code>&#39;B&#39;</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n bool winnerOfGame(string colors) {\n int aliceMoves = 0; // Count of moves Alice can make\n int bobMoves = 0; // Count of moves Bob can make\n \n // Traverse through the string to count moves\n for (int i = 1; i < colors.size() - 1; i++) {\n if (colors[i-1] == 'A' && colors[i] == 'A' && colors[i+1] == 'A') {\n aliceMoves++; // Alice can make a move\n }\n if (colors[i-1] == 'B' && colors[i] == 'B' && colors[i+1] == 'B') {\n bobMoves++; // Bob can make a move\n }\n }\n \n // Alice wins if she has more moves than Bob, otherwise Bob wins\n return aliceMoves > bobMoves;\n }\n};\n", "memory": "14800" }
2,149
<p>There are <code>n</code> pieces arranged in a line, and each piece is colored either by <code>&#39;A&#39;</code> or by <code>&#39;B&#39;</code>. You are given a string <code>colors</code> of length <code>n</code> where <code>colors[i]</code> is the color of the <code>i<sup>th</sup></code> piece.</p> <p>Alice and Bob are playing a game where they take <strong>alternating turns</strong> removing pieces from the line. In this game, Alice moves<strong> first</strong>.</p> <ul> <li>Alice is only allowed to remove a piece colored <code>&#39;A&#39;</code> if <strong>both its neighbors</strong> are also colored <code>&#39;A&#39;</code>. She is <strong>not allowed</strong> to remove pieces that are colored <code>&#39;B&#39;</code>.</li> <li>Bob is only allowed to remove a piece colored <code>&#39;B&#39;</code> if <strong>both its neighbors</strong> are also colored <code>&#39;B&#39;</code>. He is <strong>not allowed</strong> to remove pieces that are colored <code>&#39;A&#39;</code>.</li> <li>Alice and Bob <strong>cannot</strong> remove pieces from the edge of the line.</li> <li>If a player cannot make a move on their turn, that player <strong>loses</strong> and the other player <strong>wins</strong>.</li> </ul> <p>Assuming Alice and Bob play optimally, return <code>true</code><em> if Alice wins, or return </em><code>false</code><em> if Bob wins</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> colors = &quot;AAABABB&quot; <strong>Output:</strong> true <strong>Explanation:</strong> A<u>A</u>ABABB -&gt; AABABB Alice moves first. She removes the second &#39;A&#39; from the left since that is the only &#39;A&#39; whose neighbors are both &#39;A&#39;. Now it&#39;s Bob&#39;s turn. Bob cannot make a move on his turn since there are no &#39;B&#39;s whose neighbors are both &#39;B&#39;. Thus, Alice wins, so return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> colors = &quot;AA&quot; <strong>Output:</strong> false <strong>Explanation:</strong> Alice has her turn first. There are only two &#39;A&#39;s and both are on the edge of the line, so she cannot move on her turn. Thus, Bob wins, so return false. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> colors = &quot;ABBBBBBBAAA&quot; <strong>Output:</strong> false <strong>Explanation:</strong> ABBBBBBBA<u>A</u>A -&gt; ABBBBBBBAA Alice moves first. Her only option is to remove the second to last &#39;A&#39; from the right. ABBBB<u>B</u>BBAA -&gt; ABBBBBBAA Next is Bob&#39;s turn. He has many options for which &#39;B&#39; piece to remove. He can pick any. On Alice&#39;s second turn, she has no more pieces that she can remove. Thus, Bob wins, so return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;=&nbsp;colors.length &lt;= 10<sup>5</sup></code></li> <li><code>colors</code>&nbsp;consists of only the letters&nbsp;<code>&#39;A&#39;</code>&nbsp;and&nbsp;<code>&#39;B&#39;</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n bool winnerOfGame(string colors) {\n int n = colors.size();\n int alice = 0;\n int bob = 0;\n\n for(int i = 1; i < n-1; i++){\n if(colors[i-1] == 'A' && colors[i] == 'A' && colors[i+1] == 'A'){\n alice++;\n }\n if(colors[i-1] == 'B' && colors[i] == 'B' && colors[i+1] == 'B'){\n bob++;\n }\n \n }\n return (alice > bob);\n }\n};", "memory": "14800" }
2,149
<p>There are <code>n</code> pieces arranged in a line, and each piece is colored either by <code>&#39;A&#39;</code> or by <code>&#39;B&#39;</code>. You are given a string <code>colors</code> of length <code>n</code> where <code>colors[i]</code> is the color of the <code>i<sup>th</sup></code> piece.</p> <p>Alice and Bob are playing a game where they take <strong>alternating turns</strong> removing pieces from the line. In this game, Alice moves<strong> first</strong>.</p> <ul> <li>Alice is only allowed to remove a piece colored <code>&#39;A&#39;</code> if <strong>both its neighbors</strong> are also colored <code>&#39;A&#39;</code>. She is <strong>not allowed</strong> to remove pieces that are colored <code>&#39;B&#39;</code>.</li> <li>Bob is only allowed to remove a piece colored <code>&#39;B&#39;</code> if <strong>both its neighbors</strong> are also colored <code>&#39;B&#39;</code>. He is <strong>not allowed</strong> to remove pieces that are colored <code>&#39;A&#39;</code>.</li> <li>Alice and Bob <strong>cannot</strong> remove pieces from the edge of the line.</li> <li>If a player cannot make a move on their turn, that player <strong>loses</strong> and the other player <strong>wins</strong>.</li> </ul> <p>Assuming Alice and Bob play optimally, return <code>true</code><em> if Alice wins, or return </em><code>false</code><em> if Bob wins</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> colors = &quot;AAABABB&quot; <strong>Output:</strong> true <strong>Explanation:</strong> A<u>A</u>ABABB -&gt; AABABB Alice moves first. She removes the second &#39;A&#39; from the left since that is the only &#39;A&#39; whose neighbors are both &#39;A&#39;. Now it&#39;s Bob&#39;s turn. Bob cannot make a move on his turn since there are no &#39;B&#39;s whose neighbors are both &#39;B&#39;. Thus, Alice wins, so return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> colors = &quot;AA&quot; <strong>Output:</strong> false <strong>Explanation:</strong> Alice has her turn first. There are only two &#39;A&#39;s and both are on the edge of the line, so she cannot move on her turn. Thus, Bob wins, so return false. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> colors = &quot;ABBBBBBBAAA&quot; <strong>Output:</strong> false <strong>Explanation:</strong> ABBBBBBBA<u>A</u>A -&gt; ABBBBBBBAA Alice moves first. Her only option is to remove the second to last &#39;A&#39; from the right. ABBBB<u>B</u>BBAA -&gt; ABBBBBBAA Next is Bob&#39;s turn. He has many options for which &#39;B&#39; piece to remove. He can pick any. On Alice&#39;s second turn, she has no more pieces that she can remove. Thus, Bob wins, so return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;=&nbsp;colors.length &lt;= 10<sup>5</sup></code></li> <li><code>colors</code>&nbsp;consists of only the letters&nbsp;<code>&#39;A&#39;</code>&nbsp;and&nbsp;<code>&#39;B&#39;</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool winnerOfGame(string colors) {\n int alice =0;\n int bob =0;\n\n int n=colors.length();\n for(int i=1;i<=n-2;i++){\n if(colors[i]== colors[i-1] && colors[i]==colors[i+1]){\n if(colors[i]=='A') alice++;\n else if(colors[i]=='B') bob++;\n }\n }\n\n\n return alice>bob;\n }\n};", "memory": "14900" }
2,149
<p>There are <code>n</code> pieces arranged in a line, and each piece is colored either by <code>&#39;A&#39;</code> or by <code>&#39;B&#39;</code>. You are given a string <code>colors</code> of length <code>n</code> where <code>colors[i]</code> is the color of the <code>i<sup>th</sup></code> piece.</p> <p>Alice and Bob are playing a game where they take <strong>alternating turns</strong> removing pieces from the line. In this game, Alice moves<strong> first</strong>.</p> <ul> <li>Alice is only allowed to remove a piece colored <code>&#39;A&#39;</code> if <strong>both its neighbors</strong> are also colored <code>&#39;A&#39;</code>. She is <strong>not allowed</strong> to remove pieces that are colored <code>&#39;B&#39;</code>.</li> <li>Bob is only allowed to remove a piece colored <code>&#39;B&#39;</code> if <strong>both its neighbors</strong> are also colored <code>&#39;B&#39;</code>. He is <strong>not allowed</strong> to remove pieces that are colored <code>&#39;A&#39;</code>.</li> <li>Alice and Bob <strong>cannot</strong> remove pieces from the edge of the line.</li> <li>If a player cannot make a move on their turn, that player <strong>loses</strong> and the other player <strong>wins</strong>.</li> </ul> <p>Assuming Alice and Bob play optimally, return <code>true</code><em> if Alice wins, or return </em><code>false</code><em> if Bob wins</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> colors = &quot;AAABABB&quot; <strong>Output:</strong> true <strong>Explanation:</strong> A<u>A</u>ABABB -&gt; AABABB Alice moves first. She removes the second &#39;A&#39; from the left since that is the only &#39;A&#39; whose neighbors are both &#39;A&#39;. Now it&#39;s Bob&#39;s turn. Bob cannot make a move on his turn since there are no &#39;B&#39;s whose neighbors are both &#39;B&#39;. Thus, Alice wins, so return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> colors = &quot;AA&quot; <strong>Output:</strong> false <strong>Explanation:</strong> Alice has her turn first. There are only two &#39;A&#39;s and both are on the edge of the line, so she cannot move on her turn. Thus, Bob wins, so return false. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> colors = &quot;ABBBBBBBAAA&quot; <strong>Output:</strong> false <strong>Explanation:</strong> ABBBBBBBA<u>A</u>A -&gt; ABBBBBBBAA Alice moves first. Her only option is to remove the second to last &#39;A&#39; from the right. ABBBB<u>B</u>BBAA -&gt; ABBBBBBAA Next is Bob&#39;s turn. He has many options for which &#39;B&#39; piece to remove. He can pick any. On Alice&#39;s second turn, she has no more pieces that she can remove. Thus, Bob wins, so return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;=&nbsp;colors.length &lt;= 10<sup>5</sup></code></li> <li><code>colors</code>&nbsp;consists of only the letters&nbsp;<code>&#39;A&#39;</code>&nbsp;and&nbsp;<code>&#39;B&#39;</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int n;\n bool winnerOfGame(string colors) {\n n = colors.length();\n vector<bool> as(n,false) , bs(n,false);\n for(int i = 1 ; i < n-1 ; i++){\n if(colors[i-1] == 'A' && colors[i+1] == 'A' && colors[i] == 'A')as[i] = true;\n if(colors[i-1] == 'B' && colors[i+1] == 'B' && colors[i] == 'B')bs[i] = true;\n }\n vector<bool> taken(n,false);\n int a = 0 , b = 0;\n for(int i = 0 ; i < n ; i++){\n if(as[i])a++;\n if(bs[i])b++;\n }\n if(a > b)return true;\n else return false;\n }\n};", "memory": "15600" }
2,149
<p>There are <code>n</code> pieces arranged in a line, and each piece is colored either by <code>&#39;A&#39;</code> or by <code>&#39;B&#39;</code>. You are given a string <code>colors</code> of length <code>n</code> where <code>colors[i]</code> is the color of the <code>i<sup>th</sup></code> piece.</p> <p>Alice and Bob are playing a game where they take <strong>alternating turns</strong> removing pieces from the line. In this game, Alice moves<strong> first</strong>.</p> <ul> <li>Alice is only allowed to remove a piece colored <code>&#39;A&#39;</code> if <strong>both its neighbors</strong> are also colored <code>&#39;A&#39;</code>. She is <strong>not allowed</strong> to remove pieces that are colored <code>&#39;B&#39;</code>.</li> <li>Bob is only allowed to remove a piece colored <code>&#39;B&#39;</code> if <strong>both its neighbors</strong> are also colored <code>&#39;B&#39;</code>. He is <strong>not allowed</strong> to remove pieces that are colored <code>&#39;A&#39;</code>.</li> <li>Alice and Bob <strong>cannot</strong> remove pieces from the edge of the line.</li> <li>If a player cannot make a move on their turn, that player <strong>loses</strong> and the other player <strong>wins</strong>.</li> </ul> <p>Assuming Alice and Bob play optimally, return <code>true</code><em> if Alice wins, or return </em><code>false</code><em> if Bob wins</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> colors = &quot;AAABABB&quot; <strong>Output:</strong> true <strong>Explanation:</strong> A<u>A</u>ABABB -&gt; AABABB Alice moves first. She removes the second &#39;A&#39; from the left since that is the only &#39;A&#39; whose neighbors are both &#39;A&#39;. Now it&#39;s Bob&#39;s turn. Bob cannot make a move on his turn since there are no &#39;B&#39;s whose neighbors are both &#39;B&#39;. Thus, Alice wins, so return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> colors = &quot;AA&quot; <strong>Output:</strong> false <strong>Explanation:</strong> Alice has her turn first. There are only two &#39;A&#39;s and both are on the edge of the line, so she cannot move on her turn. Thus, Bob wins, so return false. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> colors = &quot;ABBBBBBBAAA&quot; <strong>Output:</strong> false <strong>Explanation:</strong> ABBBBBBBA<u>A</u>A -&gt; ABBBBBBBAA Alice moves first. Her only option is to remove the second to last &#39;A&#39; from the right. ABBBB<u>B</u>BBAA -&gt; ABBBBBBAA Next is Bob&#39;s turn. He has many options for which &#39;B&#39; piece to remove. He can pick any. On Alice&#39;s second turn, she has no more pieces that she can remove. Thus, Bob wins, so return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;=&nbsp;colors.length &lt;= 10<sup>5</sup></code></li> <li><code>colors</code>&nbsp;consists of only the letters&nbsp;<code>&#39;A&#39;</code>&nbsp;and&nbsp;<code>&#39;B&#39;</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool winnerOfGame(string colors) {\n // bool check=false;\n // int cnt=0;\n // char cur_check='A';\n // while(check==false){\n // bool changed=false;\n // for(int i=1;i<colors.size()-1;i++){\n // if(colors[i-1]==cur_check && colors[i]==cur_check && colors[i+1]==cur_check){\n // colors.erase(i, 1);\n // cnt++;\n // changed=true;\n // break;\n // }\n // }\n // if(changed){\n // if(cnt%2!=0){\n // cur_check='B';\n // }else{\n // cur_check='A';\n // }\n // }else{\n // check=true;\n // }\n // }\n // return (cnt%2!=0);\n\n\n vector<int>cnt_a,cnt_b;\n int cur_a=0,cur_b=0;\n for(int i=0;i<colors.size();i++){\n if(colors[i]=='A'){\n cur_a++;\n if(cur_b>=3){\n cnt_b.push_back(cur_b);\n }\n cur_b=0;\n }else{\n cur_b++;\n if(cur_a>=3){\n cnt_a.push_back(cur_a);\n }\n cur_a=0;\n }\n }\n if(cur_a>=3)cnt_a.push_back(cur_a);\n if(cur_b>=3)cnt_b.push_back(cur_b);\n int check_a=0,check_b=0;\n for(auto it:cnt_a){\n check_a+=it-2;\n }\n for(auto it:cnt_b){\n check_b+=it-2;\n }\n\n if(check_a>check_b)return true;\n return false;\n\n }\n};", "memory": "15700" }
2,149
<p>There are <code>n</code> pieces arranged in a line, and each piece is colored either by <code>&#39;A&#39;</code> or by <code>&#39;B&#39;</code>. You are given a string <code>colors</code> of length <code>n</code> where <code>colors[i]</code> is the color of the <code>i<sup>th</sup></code> piece.</p> <p>Alice and Bob are playing a game where they take <strong>alternating turns</strong> removing pieces from the line. In this game, Alice moves<strong> first</strong>.</p> <ul> <li>Alice is only allowed to remove a piece colored <code>&#39;A&#39;</code> if <strong>both its neighbors</strong> are also colored <code>&#39;A&#39;</code>. She is <strong>not allowed</strong> to remove pieces that are colored <code>&#39;B&#39;</code>.</li> <li>Bob is only allowed to remove a piece colored <code>&#39;B&#39;</code> if <strong>both its neighbors</strong> are also colored <code>&#39;B&#39;</code>. He is <strong>not allowed</strong> to remove pieces that are colored <code>&#39;A&#39;</code>.</li> <li>Alice and Bob <strong>cannot</strong> remove pieces from the edge of the line.</li> <li>If a player cannot make a move on their turn, that player <strong>loses</strong> and the other player <strong>wins</strong>.</li> </ul> <p>Assuming Alice and Bob play optimally, return <code>true</code><em> if Alice wins, or return </em><code>false</code><em> if Bob wins</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> colors = &quot;AAABABB&quot; <strong>Output:</strong> true <strong>Explanation:</strong> A<u>A</u>ABABB -&gt; AABABB Alice moves first. She removes the second &#39;A&#39; from the left since that is the only &#39;A&#39; whose neighbors are both &#39;A&#39;. Now it&#39;s Bob&#39;s turn. Bob cannot make a move on his turn since there are no &#39;B&#39;s whose neighbors are both &#39;B&#39;. Thus, Alice wins, so return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> colors = &quot;AA&quot; <strong>Output:</strong> false <strong>Explanation:</strong> Alice has her turn first. There are only two &#39;A&#39;s and both are on the edge of the line, so she cannot move on her turn. Thus, Bob wins, so return false. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> colors = &quot;ABBBBBBBAAA&quot; <strong>Output:</strong> false <strong>Explanation:</strong> ABBBBBBBA<u>A</u>A -&gt; ABBBBBBBAA Alice moves first. Her only option is to remove the second to last &#39;A&#39; from the right. ABBBB<u>B</u>BBAA -&gt; ABBBBBBAA Next is Bob&#39;s turn. He has many options for which &#39;B&#39; piece to remove. He can pick any. On Alice&#39;s second turn, she has no more pieces that she can remove. Thus, Bob wins, so return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;=&nbsp;colors.length &lt;= 10<sup>5</sup></code></li> <li><code>colors</code>&nbsp;consists of only the letters&nbsp;<code>&#39;A&#39;</code>&nbsp;and&nbsp;<code>&#39;B&#39;</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool winnerOfGame(string colors) {\n int n = colors.size();\n int a = 0;\n int b = 0;\n for(int i = 0; i < n-2; i++){\n if(colors.substr(i, 3) == \"AAA\") a++;\n if(colors.substr(i, 3) == \"BBB\") b++;\n }\n return a > b;\n }\n};", "memory": "15800" }
2,149
<p>There are <code>n</code> pieces arranged in a line, and each piece is colored either by <code>&#39;A&#39;</code> or by <code>&#39;B&#39;</code>. You are given a string <code>colors</code> of length <code>n</code> where <code>colors[i]</code> is the color of the <code>i<sup>th</sup></code> piece.</p> <p>Alice and Bob are playing a game where they take <strong>alternating turns</strong> removing pieces from the line. In this game, Alice moves<strong> first</strong>.</p> <ul> <li>Alice is only allowed to remove a piece colored <code>&#39;A&#39;</code> if <strong>both its neighbors</strong> are also colored <code>&#39;A&#39;</code>. She is <strong>not allowed</strong> to remove pieces that are colored <code>&#39;B&#39;</code>.</li> <li>Bob is only allowed to remove a piece colored <code>&#39;B&#39;</code> if <strong>both its neighbors</strong> are also colored <code>&#39;B&#39;</code>. He is <strong>not allowed</strong> to remove pieces that are colored <code>&#39;A&#39;</code>.</li> <li>Alice and Bob <strong>cannot</strong> remove pieces from the edge of the line.</li> <li>If a player cannot make a move on their turn, that player <strong>loses</strong> and the other player <strong>wins</strong>.</li> </ul> <p>Assuming Alice and Bob play optimally, return <code>true</code><em> if Alice wins, or return </em><code>false</code><em> if Bob wins</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> colors = &quot;AAABABB&quot; <strong>Output:</strong> true <strong>Explanation:</strong> A<u>A</u>ABABB -&gt; AABABB Alice moves first. She removes the second &#39;A&#39; from the left since that is the only &#39;A&#39; whose neighbors are both &#39;A&#39;. Now it&#39;s Bob&#39;s turn. Bob cannot make a move on his turn since there are no &#39;B&#39;s whose neighbors are both &#39;B&#39;. Thus, Alice wins, so return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> colors = &quot;AA&quot; <strong>Output:</strong> false <strong>Explanation:</strong> Alice has her turn first. There are only two &#39;A&#39;s and both are on the edge of the line, so she cannot move on her turn. Thus, Bob wins, so return false. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> colors = &quot;ABBBBBBBAAA&quot; <strong>Output:</strong> false <strong>Explanation:</strong> ABBBBBBBA<u>A</u>A -&gt; ABBBBBBBAA Alice moves first. Her only option is to remove the second to last &#39;A&#39; from the right. ABBBB<u>B</u>BBAA -&gt; ABBBBBBAA Next is Bob&#39;s turn. He has many options for which &#39;B&#39; piece to remove. He can pick any. On Alice&#39;s second turn, she has no more pieces that she can remove. Thus, Bob wins, so return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;=&nbsp;colors.length &lt;= 10<sup>5</sup></code></li> <li><code>colors</code>&nbsp;consists of only the letters&nbsp;<code>&#39;A&#39;</code>&nbsp;and&nbsp;<code>&#39;B&#39;</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool winnerOfGame(string colors) {\n int count_AAA=0;\n int count_BBB=0;\n for(int i=0;i<colors.size();i++){\n if(colors.substr(i,3)==\"AAA\") count_AAA++;\n else if(colors.substr(i,3)==\"BBB\") count_BBB++;\n\n }\n return count_AAA>count_BBB;\n \n }\n};", "memory": "15900" }
2,149
<p>There are <code>n</code> pieces arranged in a line, and each piece is colored either by <code>&#39;A&#39;</code> or by <code>&#39;B&#39;</code>. You are given a string <code>colors</code> of length <code>n</code> where <code>colors[i]</code> is the color of the <code>i<sup>th</sup></code> piece.</p> <p>Alice and Bob are playing a game where they take <strong>alternating turns</strong> removing pieces from the line. In this game, Alice moves<strong> first</strong>.</p> <ul> <li>Alice is only allowed to remove a piece colored <code>&#39;A&#39;</code> if <strong>both its neighbors</strong> are also colored <code>&#39;A&#39;</code>. She is <strong>not allowed</strong> to remove pieces that are colored <code>&#39;B&#39;</code>.</li> <li>Bob is only allowed to remove a piece colored <code>&#39;B&#39;</code> if <strong>both its neighbors</strong> are also colored <code>&#39;B&#39;</code>. He is <strong>not allowed</strong> to remove pieces that are colored <code>&#39;A&#39;</code>.</li> <li>Alice and Bob <strong>cannot</strong> remove pieces from the edge of the line.</li> <li>If a player cannot make a move on their turn, that player <strong>loses</strong> and the other player <strong>wins</strong>.</li> </ul> <p>Assuming Alice and Bob play optimally, return <code>true</code><em> if Alice wins, or return </em><code>false</code><em> if Bob wins</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> colors = &quot;AAABABB&quot; <strong>Output:</strong> true <strong>Explanation:</strong> A<u>A</u>ABABB -&gt; AABABB Alice moves first. She removes the second &#39;A&#39; from the left since that is the only &#39;A&#39; whose neighbors are both &#39;A&#39;. Now it&#39;s Bob&#39;s turn. Bob cannot make a move on his turn since there are no &#39;B&#39;s whose neighbors are both &#39;B&#39;. Thus, Alice wins, so return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> colors = &quot;AA&quot; <strong>Output:</strong> false <strong>Explanation:</strong> Alice has her turn first. There are only two &#39;A&#39;s and both are on the edge of the line, so she cannot move on her turn. Thus, Bob wins, so return false. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> colors = &quot;ABBBBBBBAAA&quot; <strong>Output:</strong> false <strong>Explanation:</strong> ABBBBBBBA<u>A</u>A -&gt; ABBBBBBBAA Alice moves first. Her only option is to remove the second to last &#39;A&#39; from the right. ABBBB<u>B</u>BBAA -&gt; ABBBBBBAA Next is Bob&#39;s turn. He has many options for which &#39;B&#39; piece to remove. He can pick any. On Alice&#39;s second turn, she has no more pieces that she can remove. Thus, Bob wins, so return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;=&nbsp;colors.length &lt;= 10<sup>5</sup></code></li> <li><code>colors</code>&nbsp;consists of only the letters&nbsp;<code>&#39;A&#39;</code>&nbsp;and&nbsp;<code>&#39;B&#39;</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool winnerOfGame(string colors) {\n int cntA = 0,cntB = 0;\n for(int i=0;i<colors.size();i++){\n if(colors[i]=='A')\n if(colors.substr(i,3)==\"AAA\") cntA++;\n if(colors[i]=='B')\n if(colors.substr(i,3)==\"BBB\") cntB++;\n }\n return cntA>cntB;\n }\n};", "memory": "16000" }
2,149
<p>There are <code>n</code> pieces arranged in a line, and each piece is colored either by <code>&#39;A&#39;</code> or by <code>&#39;B&#39;</code>. You are given a string <code>colors</code> of length <code>n</code> where <code>colors[i]</code> is the color of the <code>i<sup>th</sup></code> piece.</p> <p>Alice and Bob are playing a game where they take <strong>alternating turns</strong> removing pieces from the line. In this game, Alice moves<strong> first</strong>.</p> <ul> <li>Alice is only allowed to remove a piece colored <code>&#39;A&#39;</code> if <strong>both its neighbors</strong> are also colored <code>&#39;A&#39;</code>. She is <strong>not allowed</strong> to remove pieces that are colored <code>&#39;B&#39;</code>.</li> <li>Bob is only allowed to remove a piece colored <code>&#39;B&#39;</code> if <strong>both its neighbors</strong> are also colored <code>&#39;B&#39;</code>. He is <strong>not allowed</strong> to remove pieces that are colored <code>&#39;A&#39;</code>.</li> <li>Alice and Bob <strong>cannot</strong> remove pieces from the edge of the line.</li> <li>If a player cannot make a move on their turn, that player <strong>loses</strong> and the other player <strong>wins</strong>.</li> </ul> <p>Assuming Alice and Bob play optimally, return <code>true</code><em> if Alice wins, or return </em><code>false</code><em> if Bob wins</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> colors = &quot;AAABABB&quot; <strong>Output:</strong> true <strong>Explanation:</strong> A<u>A</u>ABABB -&gt; AABABB Alice moves first. She removes the second &#39;A&#39; from the left since that is the only &#39;A&#39; whose neighbors are both &#39;A&#39;. Now it&#39;s Bob&#39;s turn. Bob cannot make a move on his turn since there are no &#39;B&#39;s whose neighbors are both &#39;B&#39;. Thus, Alice wins, so return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> colors = &quot;AA&quot; <strong>Output:</strong> false <strong>Explanation:</strong> Alice has her turn first. There are only two &#39;A&#39;s and both are on the edge of the line, so she cannot move on her turn. Thus, Bob wins, so return false. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> colors = &quot;ABBBBBBBAAA&quot; <strong>Output:</strong> false <strong>Explanation:</strong> ABBBBBBBA<u>A</u>A -&gt; ABBBBBBBAA Alice moves first. Her only option is to remove the second to last &#39;A&#39; from the right. ABBBB<u>B</u>BBAA -&gt; ABBBBBBAA Next is Bob&#39;s turn. He has many options for which &#39;B&#39; piece to remove. He can pick any. On Alice&#39;s second turn, she has no more pieces that she can remove. Thus, Bob wins, so return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;=&nbsp;colors.length &lt;= 10<sup>5</sup></code></li> <li><code>colors</code>&nbsp;consists of only the letters&nbsp;<code>&#39;A&#39;</code>&nbsp;and&nbsp;<code>&#39;B&#39;</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool winnerOfGame(string colors) {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n int alice=0;\n int bob=0;\n for(int i =1;i<colors.size()-1;i++){\n if(colors.substr(i-1,3)==\"AAA\") alice++;\n else if(colors.substr(i-1,3)==\"BBB\")bob++;\n }\n\n return alice>bob;\n }\n};", "memory": "16000" }
2,149
<p>There are <code>n</code> pieces arranged in a line, and each piece is colored either by <code>&#39;A&#39;</code> or by <code>&#39;B&#39;</code>. You are given a string <code>colors</code> of length <code>n</code> where <code>colors[i]</code> is the color of the <code>i<sup>th</sup></code> piece.</p> <p>Alice and Bob are playing a game where they take <strong>alternating turns</strong> removing pieces from the line. In this game, Alice moves<strong> first</strong>.</p> <ul> <li>Alice is only allowed to remove a piece colored <code>&#39;A&#39;</code> if <strong>both its neighbors</strong> are also colored <code>&#39;A&#39;</code>. She is <strong>not allowed</strong> to remove pieces that are colored <code>&#39;B&#39;</code>.</li> <li>Bob is only allowed to remove a piece colored <code>&#39;B&#39;</code> if <strong>both its neighbors</strong> are also colored <code>&#39;B&#39;</code>. He is <strong>not allowed</strong> to remove pieces that are colored <code>&#39;A&#39;</code>.</li> <li>Alice and Bob <strong>cannot</strong> remove pieces from the edge of the line.</li> <li>If a player cannot make a move on their turn, that player <strong>loses</strong> and the other player <strong>wins</strong>.</li> </ul> <p>Assuming Alice and Bob play optimally, return <code>true</code><em> if Alice wins, or return </em><code>false</code><em> if Bob wins</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> colors = &quot;AAABABB&quot; <strong>Output:</strong> true <strong>Explanation:</strong> A<u>A</u>ABABB -&gt; AABABB Alice moves first. She removes the second &#39;A&#39; from the left since that is the only &#39;A&#39; whose neighbors are both &#39;A&#39;. Now it&#39;s Bob&#39;s turn. Bob cannot make a move on his turn since there are no &#39;B&#39;s whose neighbors are both &#39;B&#39;. Thus, Alice wins, so return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> colors = &quot;AA&quot; <strong>Output:</strong> false <strong>Explanation:</strong> Alice has her turn first. There are only two &#39;A&#39;s and both are on the edge of the line, so she cannot move on her turn. Thus, Bob wins, so return false. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> colors = &quot;ABBBBBBBAAA&quot; <strong>Output:</strong> false <strong>Explanation:</strong> ABBBBBBBA<u>A</u>A -&gt; ABBBBBBBAA Alice moves first. Her only option is to remove the second to last &#39;A&#39; from the right. ABBBB<u>B</u>BBAA -&gt; ABBBBBBAA Next is Bob&#39;s turn. He has many options for which &#39;B&#39; piece to remove. He can pick any. On Alice&#39;s second turn, she has no more pieces that she can remove. Thus, Bob wins, so return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;=&nbsp;colors.length &lt;= 10<sup>5</sup></code></li> <li><code>colors</code>&nbsp;consists of only the letters&nbsp;<code>&#39;A&#39;</code>&nbsp;and&nbsp;<code>&#39;B&#39;</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool winnerOfGame(string colors) {\n int a_score = 0, b_score = 0, state;\n if(colors[0] == 'A') {\n state = 0;\n }\n else {\n state = 1;\n }\n\n string str = \"\";\n for(int i=0; i<colors.size(); i++) {\n if(colors[i] == 'A' && state == 1) {\n state = 0;\n int num = str.size();\n if(num >= 3) {\n b_score += num-2;\n }\n str = \"A\";\n }\n else if(colors[i] == 'B' && state == 0) {\n state = 1;\n int num = str.size();\n if(num >= 3) {\n a_score += num-2;\n }\n str = \"B\";\n }\n else {\n str += colors[i];\n }\n }\n if(!str.empty()) {\n if(state == 0) {\n int num = str.size();\n if(num >= 3) {\n a_score += num-2;\n }\n }\n else {\n int num = str.size();\n if(num >= 3) {\n b_score += num-2;\n }\n }\n }\n if(a_score > b_score) {\n return true;\n }\n else {\n return false;\n }\n }\n};", "memory": "16300" }
2,149
<p>There are <code>n</code> pieces arranged in a line, and each piece is colored either by <code>&#39;A&#39;</code> or by <code>&#39;B&#39;</code>. You are given a string <code>colors</code> of length <code>n</code> where <code>colors[i]</code> is the color of the <code>i<sup>th</sup></code> piece.</p> <p>Alice and Bob are playing a game where they take <strong>alternating turns</strong> removing pieces from the line. In this game, Alice moves<strong> first</strong>.</p> <ul> <li>Alice is only allowed to remove a piece colored <code>&#39;A&#39;</code> if <strong>both its neighbors</strong> are also colored <code>&#39;A&#39;</code>. She is <strong>not allowed</strong> to remove pieces that are colored <code>&#39;B&#39;</code>.</li> <li>Bob is only allowed to remove a piece colored <code>&#39;B&#39;</code> if <strong>both its neighbors</strong> are also colored <code>&#39;B&#39;</code>. He is <strong>not allowed</strong> to remove pieces that are colored <code>&#39;A&#39;</code>.</li> <li>Alice and Bob <strong>cannot</strong> remove pieces from the edge of the line.</li> <li>If a player cannot make a move on their turn, that player <strong>loses</strong> and the other player <strong>wins</strong>.</li> </ul> <p>Assuming Alice and Bob play optimally, return <code>true</code><em> if Alice wins, or return </em><code>false</code><em> if Bob wins</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> colors = &quot;AAABABB&quot; <strong>Output:</strong> true <strong>Explanation:</strong> A<u>A</u>ABABB -&gt; AABABB Alice moves first. She removes the second &#39;A&#39; from the left since that is the only &#39;A&#39; whose neighbors are both &#39;A&#39;. Now it&#39;s Bob&#39;s turn. Bob cannot make a move on his turn since there are no &#39;B&#39;s whose neighbors are both &#39;B&#39;. Thus, Alice wins, so return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> colors = &quot;AA&quot; <strong>Output:</strong> false <strong>Explanation:</strong> Alice has her turn first. There are only two &#39;A&#39;s and both are on the edge of the line, so she cannot move on her turn. Thus, Bob wins, so return false. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> colors = &quot;ABBBBBBBAAA&quot; <strong>Output:</strong> false <strong>Explanation:</strong> ABBBBBBBA<u>A</u>A -&gt; ABBBBBBBAA Alice moves first. Her only option is to remove the second to last &#39;A&#39; from the right. ABBBB<u>B</u>BBAA -&gt; ABBBBBBAA Next is Bob&#39;s turn. He has many options for which &#39;B&#39; piece to remove. He can pick any. On Alice&#39;s second turn, she has no more pieces that she can remove. Thus, Bob wins, so return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;=&nbsp;colors.length &lt;= 10<sup>5</sup></code></li> <li><code>colors</code>&nbsp;consists of only the letters&nbsp;<code>&#39;A&#39;</code>&nbsp;and&nbsp;<code>&#39;B&#39;</code></li> </ul>
3
{ "code": "class Solution {\n\npublic:\n bool winnerOfGame(string colors) {\n int player[2]={0};\n int n=colors.size();\n if (n<3) return 0;//Be careful for edge cases\n int prev2=colors[0], prev1=colors[1];\n\n #pragma unroll\n for(char& c: colors.substr(2)) {\n if (c==prev1 && c==prev2) player[c-'A']++;\n prev2=prev1, prev1=c;\n }\n// cout<<player[0]<<\" vs \"<<player[1]<<endl;\n return player[0]>player[1];\n }\n};", "memory": "16400" }
2,171
<p>A city is represented as a <strong>bi-directional connected</strong> graph with <code>n</code> vertices where each vertex is labeled from <code>1</code> to <code>n</code> (<strong>inclusive</strong>). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself. The time taken to traverse any edge is <code>time</code> minutes.</p> <p>Each vertex has a traffic signal which changes its color from <strong>green</strong> to <strong>red</strong> and vice versa every&nbsp;<code>change</code> minutes. All signals change <strong>at the same time</strong>. You can enter a vertex at <strong>any time</strong>, but can leave a vertex <strong>only when the signal is green</strong>. You <strong>cannot wait </strong>at a vertex if the signal is <strong>green</strong>.</p> <p>The <strong>second minimum value</strong> is defined as the smallest value<strong> strictly larger </strong>than the minimum value.</p> <ul> <li>For example the second minimum value of <code>[2, 3, 4]</code> is <code>3</code>, and the second minimum value of <code>[2, 2, 4]</code> is <code>4</code>.</li> </ul> <p>Given <code>n</code>, <code>edges</code>, <code>time</code>, and <code>change</code>, return <em>the <strong>second minimum time</strong> it will take to go from vertex </em><code>1</code><em> to vertex </em><code>n</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>You can go through any vertex <strong>any</strong> number of times, <strong>including</strong> <code>1</code> and <code>n</code>.</li> <li>You can assume that when the journey <strong>starts</strong>, all signals have just turned <strong>green</strong>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no duplicate edges.</li> <li>Each vertex can be reached directly or indirectly from every other vertex.</li> <li><code>1 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
0
{ "code": "#include <bits/stdc++.h>\nusing namespace std;\n\nint s = 1, cnt = 0, head[10005], minDis[10005], secDis[10005];\n\nstruct edge {\n int to;\n int next;\n} e[40005];\n\nstruct node {\n int id, dis;\n node(int _id, int _dis) : id(_id), dis(_dis) {}\n friend bool operator > (const node &a, const node &b) {\n return a.dis > b.dis;\n }\n};\n\nvoid AddEdge(int u, int v) {\n e[++cnt].to = v;\n e[cnt].next = head[u];\n head[u] = cnt;\n}\n\nclass Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n memset(head, 0, sizeof(head));\n cnt = 0; // 重置计数器\n for (auto& edge : edges) {\n AddEdge(edge[0], edge[1]);\n AddEdge(edge[1], edge[0]); // 添加反向边,因为是无向图\n }\n\n memset(minDis, 0x3f, sizeof(minDis));\n memset(secDis, 0x3f, sizeof(secDis));\n int timeTaken = 0;\n int visit[10005] = {0};\n minDis[s] = 0;\n priority_queue<node, vector<node>, greater<node>> Q;\n Q.push(node(s, minDis[s]));\n\n while (!Q.empty()) {\n node tmp = Q.top();\n Q.pop();\n int u = tmp.id;\n timeTaken = tmp.dis;\n \n // if (visit[u] == 2) continue;\n visit[u]++;\n\n if(visit[n] == 2 && u == n) return timeTaken;\n\n // 计算到达下一个节点的时间\n if ((timeTaken / change) % 2) {\n timeTaken = change * (timeTaken / change + 1) + time;\n } else {\n timeTaken = timeTaken + time;\n }\n\n for (int i = head[u]; i; i = e[i].next) {\n int v = e[i].to;\n\n if(visit[v] == 2) continue;\n\n if (minDis[v] > timeTaken) {\n secDis[v] = minDis[v];\n minDis[v] = timeTaken;\n Q.push(node(v, timeTaken));\n } else if(secDis[v] > timeTaken && minDis[v] != timeTaken){\n secDis[v] = timeTaken;\n Q.push(node(v, timeTaken));\n }\n \n }\n\n }\n\n return 0;\n }\n};\n", "memory": "149573" }
2,171
<p>A city is represented as a <strong>bi-directional connected</strong> graph with <code>n</code> vertices where each vertex is labeled from <code>1</code> to <code>n</code> (<strong>inclusive</strong>). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself. The time taken to traverse any edge is <code>time</code> minutes.</p> <p>Each vertex has a traffic signal which changes its color from <strong>green</strong> to <strong>red</strong> and vice versa every&nbsp;<code>change</code> minutes. All signals change <strong>at the same time</strong>. You can enter a vertex at <strong>any time</strong>, but can leave a vertex <strong>only when the signal is green</strong>. You <strong>cannot wait </strong>at a vertex if the signal is <strong>green</strong>.</p> <p>The <strong>second minimum value</strong> is defined as the smallest value<strong> strictly larger </strong>than the minimum value.</p> <ul> <li>For example the second minimum value of <code>[2, 3, 4]</code> is <code>3</code>, and the second minimum value of <code>[2, 2, 4]</code> is <code>4</code>.</li> </ul> <p>Given <code>n</code>, <code>edges</code>, <code>time</code>, and <code>change</code>, return <em>the <strong>second minimum time</strong> it will take to go from vertex </em><code>1</code><em> to vertex </em><code>n</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>You can go through any vertex <strong>any</strong> number of times, <strong>including</strong> <code>1</code> and <code>n</code>.</li> <li>You can assume that when the journey <strong>starts</strong>, all signals have just turned <strong>green</strong>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no duplicate edges.</li> <li>Each vertex can be reached directly or indirectly from every other vertex.</li> <li><code>1 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
0
{ "code": "constexpr static int N = 1e4 + 10, M = 4 * N, INF = 0x3f3f3f3f;\nint dist1[N], dist2[N], he[N], ne[M], e[M], idx;\nvoid add(int a, int b) {\n e[idx] = b;\n ne[idx] = he[a];\n he[a] = idx;\n idx++;\n}\n\n\nclass Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n memset(dist1, INF, sizeof(dist1)), memset(dist2, INF, sizeof(dist2));\n memset(he, -1, sizeof(he));\n idx = 0;\n for (auto& e : edges) {\n add(e[0], e[1]);\n add(e[1], e[0]);\n }\n\n queue<pair<int, int>> bfs;\n bfs.push({1, 0});\n dist1[1] = 0;\n while (!bfs.empty()) {\n auto [node, dist] = bfs.front(); bfs.pop();\n for (int i = he[node]; i != -1; i = ne[i]) {\n int j = e[i];\n if (dist1[j] > dist + 1) {\n dist2[j] = dist1[j];\n dist1[j] = dist + 1;\n bfs.push({j, dist1[j]});\n }\n else if (dist1[j] != dist + 1 && dist + 1 < dist2[j]) {\n dist2[j] = dist + 1;\n bfs.push({j, dist2[j]});\n }\n }\n }\n int res = 0;\n for (int i = 0; i < dist2[n]; ++i) {\n int wait = (res / change) % 2 == 0 ? 0 : change - res % change;\n res += wait + time;\n }\n return res;\n }\n};", "memory": "150756" }
2,171
<p>A city is represented as a <strong>bi-directional connected</strong> graph with <code>n</code> vertices where each vertex is labeled from <code>1</code> to <code>n</code> (<strong>inclusive</strong>). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself. The time taken to traverse any edge is <code>time</code> minutes.</p> <p>Each vertex has a traffic signal which changes its color from <strong>green</strong> to <strong>red</strong> and vice versa every&nbsp;<code>change</code> minutes. All signals change <strong>at the same time</strong>. You can enter a vertex at <strong>any time</strong>, but can leave a vertex <strong>only when the signal is green</strong>. You <strong>cannot wait </strong>at a vertex if the signal is <strong>green</strong>.</p> <p>The <strong>second minimum value</strong> is defined as the smallest value<strong> strictly larger </strong>than the minimum value.</p> <ul> <li>For example the second minimum value of <code>[2, 3, 4]</code> is <code>3</code>, and the second minimum value of <code>[2, 2, 4]</code> is <code>4</code>.</li> </ul> <p>Given <code>n</code>, <code>edges</code>, <code>time</code>, and <code>change</code>, return <em>the <strong>second minimum time</strong> it will take to go from vertex </em><code>1</code><em> to vertex </em><code>n</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>You can go through any vertex <strong>any</strong> number of times, <strong>including</strong> <code>1</code> and <code>n</code>.</li> <li>You can assume that when the journey <strong>starts</strong>, all signals have just turned <strong>green</strong>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no duplicate edges.</li> <li>Each vertex can be reached directly or indirectly from every other vertex.</li> <li><code>1 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
0
{ "code": "#define ll long long\n#define ull unsigned long long\n#define lll __int128\n#define rep(i,n,N) for(int i=(n);i<=(N);++i)\n#define For(i,n,N) for(int i=(n);i< (N);++i)\n#define rap(i,n,N) for(int i=(n);i>=(N);--i)\n#define rip(i,n,N,V) for(int i=(n);i<=(N);i+=V)\n#define mp make_pair\n#define pb push_back\n#define pob pop_back\n#define pf push_front\n#define pof pop_front\n//#define le left\n//#define ri right\n#define fi first\n#define se second\n//#define ff fi.fi\n//#define fs fi.se\n//#define sf se.fi\n//#define ss se.se\n#define lc (id<<1)\n#define rc ((id<<1)|1)\n#define ou3(x,y,z) cout << \">> \" << (x) << \" \" << (y) << \" \" << (z) << endl\n#define mems(x,y) memset(&x,y,sizeof x)\n#define memsv(x,y) memset(&x[0], y, sizeof(x[0]) * x.size())\n#define popcount __builtin_popcountll\n#define clz(x) (1<<(31-__builtin_clz(x)))\n#define clzll(x) (1LL<<(63-__builtin_clzll(x)))\n#define all(x) x.begin(),x.end()\n#define rsort(x) sort(x), reverse(x)\n#define revsort(a,b) sort(a,b), reverse(a,b)\n#define PQ(T) priority_queue<T>\n#define PQr(T) priority_queue<T, vector<T>, greater<T>>\n#define permute(x) while(next_permutation(all(x)))\n#define distinct(x) x.erase(unique(all(x)),x.end())\n#define vi vector<int>\n#define vvi vector<vi>\n#define vii vector<pii>\n#define vll vector<ll>\n#define vlll vector<pll>\n#define vvll vector<vll>\n#define ari(x) array<int,x>\n#define arll(x) array<ll,x>\n#define ard(x) array<double,x>\n#define pii pair<int,int> \n#define pll pair<ll,ll>\n#define plll pair<ll,pll>\n#define pllll pair<pll,pll>\n#define piii pair<int,pii>\n#define piiii pair<pii,pii>\n#define psi pair<string,int>\n#define endl '\\n'\nconst int MAX = 1e4+5;\nconst ll MOD = 1000000007;\nconst ll MOD2 = 998244353;\nconst ll INF = 2e18;\nconst int dr[]={1,-1,0,0,1,1,-1,-1};\nconst int dc[]={0,0,-1,1,1,-1,1,-1};\nconst double pi = acos(-1);\nconst double EPS = 1e-9;\nconst int block = 320;\ninline ll pw(ll x,ll y,ll md=MOD){ll ret=1;x%=md;while(y){if(y&1)ret=ret*x%md;x=x*x%md,y>>=1;}return ret;}\nmt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());\n\nint n,vis[2][MAX],nw,ans,rem;\nvi v[MAX];\nqueue<int> q;\n\nvoid bfs(int id,int st){\n rep(i,1,n)vis[id][i] = -1;\n vis[id][st] = 0;\n q.push(st);\n while(q.size()){\n nw = q.front();\n q.pop();\n for(int i:v[nw])if(vis[id][i]==-1){\n vis[id][i] = vis[id][nw]+1;\n q.push(i);\n }\n }\n return;\n}\n\nclass Solution {\npublic:\n int secondMinimum(int _n, vector<vector<int>>& edges, int t, int c) {\n n = _n;\n rep(i,1,n)v[i].clear();\n for(auto &i:edges){\n v[i[0]].pb(i[1]);\n v[i[1]].pb(i[0]);\n }\n bfs(0, 1);\n bfs(1, n);\n ans = vis[0][n];\n for(auto &i:edges){\n if(vis[0][i[0]]+vis[1][i[1]]==ans || vis[1][i[0]]+vis[0][i[1]]==ans){\n --ans;\n break;\n }\n }\n n = ans+2;\n ans = n*t;\n rem = c;\n For(i,1,n){\n rem-= t;\n rem%= c*2;\n if(rem<=-c)rem+= 2*c;\n if(rem<=0){\n ans+= c+rem;\n rem = c;\n }\n }\n return ans;\n }\n};", "memory": "150756" }
2,171
<p>A city is represented as a <strong>bi-directional connected</strong> graph with <code>n</code> vertices where each vertex is labeled from <code>1</code> to <code>n</code> (<strong>inclusive</strong>). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself. The time taken to traverse any edge is <code>time</code> minutes.</p> <p>Each vertex has a traffic signal which changes its color from <strong>green</strong> to <strong>red</strong> and vice versa every&nbsp;<code>change</code> minutes. All signals change <strong>at the same time</strong>. You can enter a vertex at <strong>any time</strong>, but can leave a vertex <strong>only when the signal is green</strong>. You <strong>cannot wait </strong>at a vertex if the signal is <strong>green</strong>.</p> <p>The <strong>second minimum value</strong> is defined as the smallest value<strong> strictly larger </strong>than the minimum value.</p> <ul> <li>For example the second minimum value of <code>[2, 3, 4]</code> is <code>3</code>, and the second minimum value of <code>[2, 2, 4]</code> is <code>4</code>.</li> </ul> <p>Given <code>n</code>, <code>edges</code>, <code>time</code>, and <code>change</code>, return <em>the <strong>second minimum time</strong> it will take to go from vertex </em><code>1</code><em> to vertex </em><code>n</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>You can go through any vertex <strong>any</strong> number of times, <strong>including</strong> <code>1</code> and <code>n</code>.</li> <li>You can assume that when the journey <strong>starts</strong>, all signals have just turned <strong>green</strong>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no duplicate edges.</li> <li>Each vertex can be reached directly or indirectly from every other vertex.</li> <li><code>1 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n const int inf = 0x3f3f3f3f;\n struct edge\n {\n int to,ne,w;\n }e[40005];\n void add(int u,int v)\n {\n e[++id] = {v,h[u]};\n h[u] = id;\n }\n int h[10005],id = 0;\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n memset(h,-1,sizeof h);\n for(auto &v:edges) add(v[0],v[1]),add(v[1],v[0]);\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<>> pq;\n pq.push({0,1});\n vector<int> visit(n + 1);\n vector<vector<int>> d(2,vector<int>(n + 1,inf));\n while(d[1][n] == inf)\n {\n auto [t,u] = pq.top();\n pq.pop();\n t += time + ((t/change)&1 ? change - (t%change) : 0);\n for(int i = h[u];~i;i = e[i].ne)\n {\n int v = e[i].to;\n if(d[0][v] > t) \n {\n d[0][v] = t;\n pq.push({t,v});\n }\n else if(d[0][v] < t && t < d[1][v])\n {\n d[1][v] = t;\n pq.push({t,v});\n }\n }\n }\n return d[1][n];\n }\n};", "memory": "151938" }
2,171
<p>A city is represented as a <strong>bi-directional connected</strong> graph with <code>n</code> vertices where each vertex is labeled from <code>1</code> to <code>n</code> (<strong>inclusive</strong>). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself. The time taken to traverse any edge is <code>time</code> minutes.</p> <p>Each vertex has a traffic signal which changes its color from <strong>green</strong> to <strong>red</strong> and vice versa every&nbsp;<code>change</code> minutes. All signals change <strong>at the same time</strong>. You can enter a vertex at <strong>any time</strong>, but can leave a vertex <strong>only when the signal is green</strong>. You <strong>cannot wait </strong>at a vertex if the signal is <strong>green</strong>.</p> <p>The <strong>second minimum value</strong> is defined as the smallest value<strong> strictly larger </strong>than the minimum value.</p> <ul> <li>For example the second minimum value of <code>[2, 3, 4]</code> is <code>3</code>, and the second minimum value of <code>[2, 2, 4]</code> is <code>4</code>.</li> </ul> <p>Given <code>n</code>, <code>edges</code>, <code>time</code>, and <code>change</code>, return <em>the <strong>second minimum time</strong> it will take to go from vertex </em><code>1</code><em> to vertex </em><code>n</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>You can go through any vertex <strong>any</strong> number of times, <strong>including</strong> <code>1</code> and <code>n</code>.</li> <li>You can assume that when the journey <strong>starts</strong>, all signals have just turned <strong>green</strong>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no duplicate edges.</li> <li>Each vertex can be reached directly or indirectly from every other vertex.</li> <li><code>1 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n const int inf = 0x3f3f3f3f;\n struct edge\n {\n int to,ne,w;\n }e[40005];\n void add(int u,int v)\n {\n e[++id] = {v,h[u]};\n h[u] = id;\n }\n int h[10005],id = 0;\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n memset(h,-1,sizeof h);\n for(auto &v:edges) add(v[0],v[1]),add(v[1],v[0]);\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<>> pq;\n pq.push({0,1});\n vector<int> visit(n + 1);\n vector<vector<int>> d(2,vector<int>(n + 1,inf));\n while(d[1][n] == inf)\n {\n auto [t,u] = pq.top();\n pq.pop();\n t += time + ((t/change)&1 ? change - (t%change) : 0);\n for(int i = h[u];~i;i = e[i].ne)\n {\n int v = e[i].to;\n if(d[0][v] > t) \n {\n d[1][v] = d[0][v];\n d[0][v] = t;\n pq.push({t,v});\n }\n else if(d[0][v] < t && t < d[1][v])\n {\n d[1][v] = t;\n pq.push({t,v});\n }\n }\n }\n return d[1][n];\n }\n};", "memory": "151938" }
2,171
<p>A city is represented as a <strong>bi-directional connected</strong> graph with <code>n</code> vertices where each vertex is labeled from <code>1</code> to <code>n</code> (<strong>inclusive</strong>). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself. The time taken to traverse any edge is <code>time</code> minutes.</p> <p>Each vertex has a traffic signal which changes its color from <strong>green</strong> to <strong>red</strong> and vice versa every&nbsp;<code>change</code> minutes. All signals change <strong>at the same time</strong>. You can enter a vertex at <strong>any time</strong>, but can leave a vertex <strong>only when the signal is green</strong>. You <strong>cannot wait </strong>at a vertex if the signal is <strong>green</strong>.</p> <p>The <strong>second minimum value</strong> is defined as the smallest value<strong> strictly larger </strong>than the minimum value.</p> <ul> <li>For example the second minimum value of <code>[2, 3, 4]</code> is <code>3</code>, and the second minimum value of <code>[2, 2, 4]</code> is <code>4</code>.</li> </ul> <p>Given <code>n</code>, <code>edges</code>, <code>time</code>, and <code>change</code>, return <em>the <strong>second minimum time</strong> it will take to go from vertex </em><code>1</code><em> to vertex </em><code>n</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>You can go through any vertex <strong>any</strong> number of times, <strong>including</strong> <code>1</code> and <code>n</code>.</li> <li>You can assume that when the journey <strong>starts</strong>, all signals have just turned <strong>green</strong>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no duplicate edges.</li> <li>Each vertex can be reached directly or indirectly from every other vertex.</li> <li><code>1 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
0
{ "code": "const int maxN = 1e4 + 5;\nusing pii = pair<int, int>;\nvector<int> adj[maxN];\nint dist[maxN][2];\n\nclass Solution {\npublic:\n inline int calculate_time_taken(int steps_taken, int time, int change) {\n int ans = 0;\n for (int i = 0; i < steps_taken; ++i) {\n int green_light = ans / change;\n if (green_light & 1)\n ans = (green_light + 1) * change;\n ans += time;\n }\n return ans;\n }\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n const int SRC = 1, DEST = n;\n const int initial_distance = 0;\n\n for (int i = 0; i <= n; ++i) {\n adj[i].clear();\n dist[i][0] = dist[i][1] = 1e9;\n }\n for (const auto& edge : edges) {\n adj[edge[0]].push_back(edge[1]);\n adj[edge[1]].push_back(edge[0]);\n }\n\n queue<pii> q;\n q.push({SRC, initial_distance});\n dist[SRC][0] = initial_distance;\n\n while (!q.empty()) {\n auto [node, distance] = q.front();\n q.pop();\n\n for (const int& child : adj[node]) {\n int newDistance = distance + 1;\n\n if (newDistance >= dist[child][1] || newDistance == dist[child][0]) \n continue;\n\n if (newDistance < dist[child][0]) {\n dist[child][1] = dist[child][0];\n dist[child][0] = newDistance;\n } else \n dist[child][1] = newDistance;\n q.push({child, newDistance});\n }\n }\n\n int steps_taken = dist[DEST][1];\n return steps_taken == 1e9\n ? -1\n : calculate_time_taken(steps_taken, time, change);\n }\n};\n\n// auto init = []() {\n// ios_base::sync_with_stdio(false);\n// cin.tie(nullptr);\n// cout.tie(nullptr);\n// return 0;\n// }();", "memory": "153121" }
2,171
<p>A city is represented as a <strong>bi-directional connected</strong> graph with <code>n</code> vertices where each vertex is labeled from <code>1</code> to <code>n</code> (<strong>inclusive</strong>). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself. The time taken to traverse any edge is <code>time</code> minutes.</p> <p>Each vertex has a traffic signal which changes its color from <strong>green</strong> to <strong>red</strong> and vice versa every&nbsp;<code>change</code> minutes. All signals change <strong>at the same time</strong>. You can enter a vertex at <strong>any time</strong>, but can leave a vertex <strong>only when the signal is green</strong>. You <strong>cannot wait </strong>at a vertex if the signal is <strong>green</strong>.</p> <p>The <strong>second minimum value</strong> is defined as the smallest value<strong> strictly larger </strong>than the minimum value.</p> <ul> <li>For example the second minimum value of <code>[2, 3, 4]</code> is <code>3</code>, and the second minimum value of <code>[2, 2, 4]</code> is <code>4</code>.</li> </ul> <p>Given <code>n</code>, <code>edges</code>, <code>time</code>, and <code>change</code>, return <em>the <strong>second minimum time</strong> it will take to go from vertex </em><code>1</code><em> to vertex </em><code>n</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>You can go through any vertex <strong>any</strong> number of times, <strong>including</strong> <code>1</code> and <code>n</code>.</li> <li>You can assume that when the journey <strong>starts</strong>, all signals have just turned <strong>green</strong>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no duplicate edges.</li> <li>Each vertex can be reached directly or indirectly from every other vertex.</li> <li><code>1 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n static const int N = 10010, M = 4 * N, INF = 0x3f3f3f3f;\n int idx = 0;\n int h[N], e[M], ne[M];\n int dis1[N], dis2[N];\n\n void add(int a, int b){\n e[idx] = b;\n ne[idx] = h[a];\n h[a] = idx++;\n }\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n memset(dis1, INF, sizeof(dis1));\n memset(dis2, INF, sizeof(dis2));\n memset(h, -1, sizeof(h));\n\n for(auto &e : edges){\n add(e[0], e[1]);\n add(e[1], e[0]);\n }\n\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;\n\n q.push({0, 1});\n dis1[1] = 0;\n while(!q.empty()){\n auto t = q.top();\n q.pop();\n int u = t.second, step = t.first;\n for(int i = h[u]; i != -1; i = ne[i]){\n int j = e[i];\n int a = step / change, b = step % change;\n int wait = a % 2 == 0 ? 0 : change -b;\n int dist = step + time + wait;\n if(dis1[j] > dist){\n dis2[j] = dis1[j];\n dis1[j] = dist;\n q.push({dis1[j], j});\n q.push({dis2[j], j});\n }else if(dis1[j] < dist && dist < dis2[j]){\n dis2[j] = dist;\n q.push({dis2[j], j});\n }\n }\n }\n return dis2[n];\n }\n};", "memory": "154303" }
2,171
<p>A city is represented as a <strong>bi-directional connected</strong> graph with <code>n</code> vertices where each vertex is labeled from <code>1</code> to <code>n</code> (<strong>inclusive</strong>). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself. The time taken to traverse any edge is <code>time</code> minutes.</p> <p>Each vertex has a traffic signal which changes its color from <strong>green</strong> to <strong>red</strong> and vice versa every&nbsp;<code>change</code> minutes. All signals change <strong>at the same time</strong>. You can enter a vertex at <strong>any time</strong>, but can leave a vertex <strong>only when the signal is green</strong>. You <strong>cannot wait </strong>at a vertex if the signal is <strong>green</strong>.</p> <p>The <strong>second minimum value</strong> is defined as the smallest value<strong> strictly larger </strong>than the minimum value.</p> <ul> <li>For example the second minimum value of <code>[2, 3, 4]</code> is <code>3</code>, and the second minimum value of <code>[2, 2, 4]</code> is <code>4</code>.</li> </ul> <p>Given <code>n</code>, <code>edges</code>, <code>time</code>, and <code>change</code>, return <em>the <strong>second minimum time</strong> it will take to go from vertex </em><code>1</code><em> to vertex </em><code>n</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>You can go through any vertex <strong>any</strong> number of times, <strong>including</strong> <code>1</code> and <code>n</code>.</li> <li>You can assume that when the journey <strong>starts</strong>, all signals have just turned <strong>green</strong>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no duplicate edges.</li> <li>Each vertex can be reached directly or indirectly from every other vertex.</li> <li><code>1 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
0
{ "code": "#pragma GCC optimize(\"Ofast\", \"unroll-loops\", \"omit-frame-pointer\", \"inline\")\n#pragma GCC target(\"avx,avx2,fma\")\n\nconst int maxN = 1e4 + 5;\nusing pii = pair<int, int>;\nvector<int> adj[maxN];\nint dist[maxN][2];\n\nclass Solution {\npublic:\n inline int calculate_time_taken(int steps_taken, int time, int change) {\n int ans = 0;\n for (int i = 0; i < steps_taken; ++i) {\n int green_light = ans / change;\n if (green_light & 1)\n ans = (green_light + 1) * change;\n ans += time;\n }\n return ans;\n }\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n const int SRC = 1, DEST = n;\n const int initial_distance = 0;\n\n for (int i = 0; i <= n; ++i) {\n adj[i].clear();\n dist[i][0] = dist[i][1] = 1e9;\n }\n for (const auto& edge : edges) {\n adj[edge[0]].push_back(edge[1]);\n adj[edge[1]].push_back(edge[0]);\n }\n\n queue<pii> q;\n q.push({SRC, initial_distance});\n dist[SRC][0] = initial_distance;\n\n while (!q.empty()) {\n auto [node, distance] = q.front();\n q.pop();\n\n for (const int& child : adj[node]) {\n int newDistance = distance + 1;\n\n if (newDistance >= dist[child][1] || newDistance == dist[child][0]) \n continue;\n\n if (newDistance < dist[child][0]) {\n dist[child][1] = dist[child][0];\n dist[child][0] = newDistance;\n } else \n dist[child][1] = newDistance;\n q.push({child, newDistance});\n }\n }\n\n int steps_taken = dist[DEST][1];\n return steps_taken == 1e9\n ? -1\n : calculate_time_taken(steps_taken, time, change);\n }\n};\n\nauto init = []() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 0;\n}();", "memory": "154303" }
2,171
<p>A city is represented as a <strong>bi-directional connected</strong> graph with <code>n</code> vertices where each vertex is labeled from <code>1</code> to <code>n</code> (<strong>inclusive</strong>). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself. The time taken to traverse any edge is <code>time</code> minutes.</p> <p>Each vertex has a traffic signal which changes its color from <strong>green</strong> to <strong>red</strong> and vice versa every&nbsp;<code>change</code> minutes. All signals change <strong>at the same time</strong>. You can enter a vertex at <strong>any time</strong>, but can leave a vertex <strong>only when the signal is green</strong>. You <strong>cannot wait </strong>at a vertex if the signal is <strong>green</strong>.</p> <p>The <strong>second minimum value</strong> is defined as the smallest value<strong> strictly larger </strong>than the minimum value.</p> <ul> <li>For example the second minimum value of <code>[2, 3, 4]</code> is <code>3</code>, and the second minimum value of <code>[2, 2, 4]</code> is <code>4</code>.</li> </ul> <p>Given <code>n</code>, <code>edges</code>, <code>time</code>, and <code>change</code>, return <em>the <strong>second minimum time</strong> it will take to go from vertex </em><code>1</code><em> to vertex </em><code>n</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>You can go through any vertex <strong>any</strong> number of times, <strong>including</strong> <code>1</code> and <code>n</code>.</li> <li>You can assume that when the journey <strong>starts</strong>, all signals have just turned <strong>green</strong>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no duplicate edges.</li> <li>Each vertex can be reached directly or indirectly from every other vertex.</li> <li><code>1 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
0
{ "code": "const int MAXN=1e4+5;\nint c,t;\nvector<vector<int>> g;\n\nint bfs(int n)\n{\n queue<pair<int,int>> q;\n vector<int> dist1(n+1,-1);\n vector<int> dist2(n+1,-1);\n\n q.push({1,1});\n dist1[1]=0;\n\n while(!q.empty())\n {\n auto u=q.front();\n q.pop();\n\n int tt=(u.second==1)? dist1[u.first]:dist2[u.first];\n if((tt/c)%2 != 0)\n {\n tt=t+c*(tt/c+1);\n }\n else\n {\n tt=tt+t;\n }\n\n for(auto v:g[u.first])\n {\n if(dist1[v]==-1)\n {\n dist1[v]=tt;\n q.push({v,1});\n }\n else if(dist2[v]==-1 && dist1[v]!=tt)\n {\n dist2[v]=tt;\n if(v==n) break;\n q.push({v,2});\n }\n }\n }\n return dist2[n];\n}\nclass Solution \n{\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) \n {\n t=time;\n c=change;\n\n g.resize(n+1);\n for(int i=0;i<=n;i++) g[i].clear();\n for(int i=0;i<edges.size();i++)\n {\n g[edges[i][0]].push_back(edges[i][1]);\n g[edges[i][1]].push_back(edges[i][0]);\n }\n\n return bfs(n);\n }\n};", "memory": "155486" }
2,171
<p>A city is represented as a <strong>bi-directional connected</strong> graph with <code>n</code> vertices where each vertex is labeled from <code>1</code> to <code>n</code> (<strong>inclusive</strong>). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself. The time taken to traverse any edge is <code>time</code> minutes.</p> <p>Each vertex has a traffic signal which changes its color from <strong>green</strong> to <strong>red</strong> and vice versa every&nbsp;<code>change</code> minutes. All signals change <strong>at the same time</strong>. You can enter a vertex at <strong>any time</strong>, but can leave a vertex <strong>only when the signal is green</strong>. You <strong>cannot wait </strong>at a vertex if the signal is <strong>green</strong>.</p> <p>The <strong>second minimum value</strong> is defined as the smallest value<strong> strictly larger </strong>than the minimum value.</p> <ul> <li>For example the second minimum value of <code>[2, 3, 4]</code> is <code>3</code>, and the second minimum value of <code>[2, 2, 4]</code> is <code>4</code>.</li> </ul> <p>Given <code>n</code>, <code>edges</code>, <code>time</code>, and <code>change</code>, return <em>the <strong>second minimum time</strong> it will take to go from vertex </em><code>1</code><em> to vertex </em><code>n</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>You can go through any vertex <strong>any</strong> number of times, <strong>including</strong> <code>1</code> and <code>n</code>.</li> <li>You can assume that when the journey <strong>starts</strong>, all signals have just turned <strong>green</strong>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no duplicate edges.</li> <li>Each vertex can be reached directly or indirectly from every other vertex.</li> <li><code>1 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
0
{ "code": "class Solution {\n bool update(vector<vector<int>>& dist, int u, int v) {\n // 1 -> u -> v\n int f = dist[v][0];\n int s = dist[v][1];\n\n\n int n1 = dist[u][0] == INT_MAX ? INT_MAX : dist[u][0] + 1;\n int n2 = dist[u][1] == INT_MAX ? INT_MAX : dist[u][1] + 1;\n\n // cout << \"update \" << u << \" - \" << v << endl;\n // cout << \"f, s: \" << f << \", \" << s << endl;\n // cout << \"n1, n2: \" << n1 << \", \" << n2 << endl;\n\n if (f <= n1) {\n if (f == n1 && s <= n2)\n return false;\n \n if (f != n1 && s <= n1)\n return false;\n }\n \n\n // cout << \"change!\" << endl;\n\n int newFirst = f;\n newFirst = min(newFirst, n1);\n newFirst = min(newFirst, n2);\n\n if (newFirst != f) {\n dist[v][0] = newFirst;\n }\n\n int newSecond = (f == newFirst) ? INT_MAX : f;\n newSecond = (newSecond == newFirst) ? newSecond : min(newSecond, s);\n newSecond = (n1 == newFirst) ? newSecond : min(newSecond, n1);\n newSecond = (n2 == newFirst) ? newSecond : min(newSecond, n2);\n dist[v][1] = newSecond;\n\n // cout << newFirst << \", \" << newSecond << endl;\n\n return true;\n }\n\n int calculateTime(int distance, int green, int red) {\n int time = 0;\n while (distance > 0) {\n if ((time / red) % 2 == 1) {\n time = ((time / red) + 1) * red + green;\n } else {\n time += green;\n }\n\n distance--;\n }\n\n return time;\n }\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<vector<int>> dist(n + 1, vector<int>(2, INT_MAX));\n dist[1][0] = 0;\n\n bool updated = true;\n while (updated) {\n updated = false;\n\n for (int i = 0; i < edges.size(); i++) {\n int u = edges[i][0];\n int v = edges[i][1];\n\n // 1 -> u -> v\n updated = updated | update(dist, u, v);\n updated = updated | update(dist, v, u);\n }\n }\n\n // cout << dist[n][0] << endl;\n // cout << dist[n][1] << endl;\n\n return calculateTime(dist[n][1], time, change);\n }\n};", "memory": "156668" }
2,171
<p>A city is represented as a <strong>bi-directional connected</strong> graph with <code>n</code> vertices where each vertex is labeled from <code>1</code> to <code>n</code> (<strong>inclusive</strong>). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself. The time taken to traverse any edge is <code>time</code> minutes.</p> <p>Each vertex has a traffic signal which changes its color from <strong>green</strong> to <strong>red</strong> and vice versa every&nbsp;<code>change</code> minutes. All signals change <strong>at the same time</strong>. You can enter a vertex at <strong>any time</strong>, but can leave a vertex <strong>only when the signal is green</strong>. You <strong>cannot wait </strong>at a vertex if the signal is <strong>green</strong>.</p> <p>The <strong>second minimum value</strong> is defined as the smallest value<strong> strictly larger </strong>than the minimum value.</p> <ul> <li>For example the second minimum value of <code>[2, 3, 4]</code> is <code>3</code>, and the second minimum value of <code>[2, 2, 4]</code> is <code>4</code>.</li> </ul> <p>Given <code>n</code>, <code>edges</code>, <code>time</code>, and <code>change</code>, return <em>the <strong>second minimum time</strong> it will take to go from vertex </em><code>1</code><em> to vertex </em><code>n</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>You can go through any vertex <strong>any</strong> number of times, <strong>including</strong> <code>1</code> and <code>n</code>.</li> <li>You can assume that when the journey <strong>starts</strong>, all signals have just turned <strong>green</strong>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no duplicate edges.</li> <li>Each vertex can be reached directly or indirectly from every other vertex.</li> <li><code>1 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
0
{ "code": "static auto _ = [](){ ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); return 0; }();\n\nclass Solution {\n vector<int> start;\n vector<pair<int,int>> next; // index, v\n vector<int> first;\n vector<int> second;\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n start.assign(n, -1);\n next.reserve(edges.size() * 2);\n for (auto&& e : edges) {\n int u = e[0] - 1, v = e[1] - 1;\n for (int i = 0; i < 2; ++i) {\n next.emplace_back(start[u], v);\n start[u] = next.size() - 1;\n swap(u, v);\n }\n }\n first.assign(n, n + 1);\n second.assign(n, n + 1);\n queue<pair<int,int>> Q; // u, depth\n Q.emplace(0, 0);\n first[0] = 0;\n second[0] = 1;\n while (!Q.empty()) {\n auto [u, d] = Q.front();\n Q.pop();\n for (int i = start[u]; i != -1; i = next[i].first) {\n int v = next[i].second, d1 = d + 1;\n if (d1 < first[v]) {\n second[v] = min(first[v], d1 + 2);\n first[v] = d1;\n Q.emplace(v, d1);\n } else if (d1 > first[v] && d1 < second[v]) {\n second[v] = min(second[v], d1);\n Q.emplace(v, d1);\n }\n }\n }\n int m = second[n - 1];\n int t = 0;\n for (int i = 0; i < m; ++i) {\n if (t % (2 * change) < change) {\n t += time;\n } else {\n t = (t / (2 * change) + 1) * 2 * change + time; \n }\n //cout << t << endl;\n }\n return t;\n }\n};", "memory": "157851" }
2,171
<p>A city is represented as a <strong>bi-directional connected</strong> graph with <code>n</code> vertices where each vertex is labeled from <code>1</code> to <code>n</code> (<strong>inclusive</strong>). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself. The time taken to traverse any edge is <code>time</code> minutes.</p> <p>Each vertex has a traffic signal which changes its color from <strong>green</strong> to <strong>red</strong> and vice versa every&nbsp;<code>change</code> minutes. All signals change <strong>at the same time</strong>. You can enter a vertex at <strong>any time</strong>, but can leave a vertex <strong>only when the signal is green</strong>. You <strong>cannot wait </strong>at a vertex if the signal is <strong>green</strong>.</p> <p>The <strong>second minimum value</strong> is defined as the smallest value<strong> strictly larger </strong>than the minimum value.</p> <ul> <li>For example the second minimum value of <code>[2, 3, 4]</code> is <code>3</code>, and the second minimum value of <code>[2, 2, 4]</code> is <code>4</code>.</li> </ul> <p>Given <code>n</code>, <code>edges</code>, <code>time</code>, and <code>change</code>, return <em>the <strong>second minimum time</strong> it will take to go from vertex </em><code>1</code><em> to vertex </em><code>n</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>You can go through any vertex <strong>any</strong> number of times, <strong>including</strong> <code>1</code> and <code>n</code>.</li> <li>You can assume that when the journey <strong>starts</strong>, all signals have just turned <strong>green</strong>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no duplicate edges.</li> <li>Each vertex can be reached directly or indirectly from every other vertex.</li> <li><code>1 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n int edgesSize = edges.size();\n \n // Create the adjacency list\n uint16_t **graph = (uint16_t **)malloc((n + 1) * sizeof(uint16_t *));\n int *connectionCount = (int *)calloc(n + 1, sizeof(int));\n\n // First pass to count connections\n for (int i = 0; i < edgesSize; i++) {\n connectionCount[edges[i][0]]++;\n connectionCount[edges[i][1]]++;\n }\n\n // Allocate space for each node's connections\n for (uint16_t i = 0; i <= n; i++) {\n graph[i] = (uint16_t *)malloc((connectionCount[i] + 1) * sizeof(uint16_t));\n graph[i][0] = 0; // Initialize the number of connections to 0\n }\n\n // Second pass to fill connections\n for (int i = 0; i < edgesSize; i++) {\n uint16_t u = edges[i][0];\n uint16_t v = edges[i][1];\n graph[u][++graph[u][0]] = v;\n graph[v][++graph[v][0]] = u;\n }\n\n free(connectionCount);\n\n // Distance array, initialized to INF\n int (*dist)[2] = (int (*)[2])malloc((n + 1) * sizeof(int[2]));\n for (uint16_t i = 0; i <= n; i++) {\n dist[i][0] = INT_MAX;\n dist[i][1] = INT_MAX;\n }\n\n // Priority queue implemented with a simple array\n uint16_t *queueNode = (uint16_t *)malloc(2 * n * sizeof(uint16_t));\n int *queueTime = (int *)malloc(2 * n * sizeof(int));\n int front = 0, rear = 0;\n\n queueNode[rear] = 1;\n queueTime[rear++] = 0;\n\n dist[1][0] = 0;\n\n while (front < rear) {\n uint16_t u = queueNode[front];\n int t = queueTime[front++];\n\n int timeTaken = dist[u][t];\n\n // Calculate the new time considering traffic signals\n int signal = timeTaken / change;\n if (signal % 2 == 1) {\n timeTaken = change * (signal + 1);\n }\n timeTaken += time;\n\n for (uint16_t i = 1; i <= graph[u][0]; i++) {\n uint16_t v = graph[u][i];\n\n if (dist[v][0] > timeTaken) {\n dist[v][1] = dist[v][0];\n dist[v][0] = timeTaken;\n queueNode[rear] = v;\n queueTime[rear++] = 0;\n } else if (dist[v][1] > timeTaken && dist[v][0] != timeTaken) {\n dist[v][1] = timeTaken;\n queueNode[rear] = v;\n queueTime[rear++] = 1;\n }\n }\n }\n\n int result = (dist[n][1] == INT_MAX) ? -1 : dist[n][1];\n\n // Free allocated memory\n for (uint16_t i = 0; i <= n; i++) {\n free(graph[i]);\n }\n free(graph);\n free(dist);\n free(queueNode);\n free(queueTime);\n\n return result;\n }\n};", "memory": "159033" }
2,171
<p>A city is represented as a <strong>bi-directional connected</strong> graph with <code>n</code> vertices where each vertex is labeled from <code>1</code> to <code>n</code> (<strong>inclusive</strong>). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself. The time taken to traverse any edge is <code>time</code> minutes.</p> <p>Each vertex has a traffic signal which changes its color from <strong>green</strong> to <strong>red</strong> and vice versa every&nbsp;<code>change</code> minutes. All signals change <strong>at the same time</strong>. You can enter a vertex at <strong>any time</strong>, but can leave a vertex <strong>only when the signal is green</strong>. You <strong>cannot wait </strong>at a vertex if the signal is <strong>green</strong>.</p> <p>The <strong>second minimum value</strong> is defined as the smallest value<strong> strictly larger </strong>than the minimum value.</p> <ul> <li>For example the second minimum value of <code>[2, 3, 4]</code> is <code>3</code>, and the second minimum value of <code>[2, 2, 4]</code> is <code>4</code>.</li> </ul> <p>Given <code>n</code>, <code>edges</code>, <code>time</code>, and <code>change</code>, return <em>the <strong>second minimum time</strong> it will take to go from vertex </em><code>1</code><em> to vertex </em><code>n</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>You can go through any vertex <strong>any</strong> number of times, <strong>including</strong> <code>1</code> and <code>n</code>.</li> <li>You can assume that when the journey <strong>starts</strong>, all signals have just turned <strong>green</strong>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no duplicate edges.</li> <li>Each vertex can be reached directly or indirectly from every other vertex.</li> <li><code>1 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n int edgesSize = edges.size();\n \n // Create the adjacency list\n uint16_t **graph = (uint16_t **)malloc((n + 1) * sizeof(uint16_t *));\n int *connectionCount = (int *)calloc(n + 1, sizeof(int));\n\n // First pass to count connections\n for (int i = 0; i < edgesSize; i++) {\n connectionCount[edges[i][0]]++;\n connectionCount[edges[i][1]]++;\n }\n\n // Allocate space for each node's connections\n for (uint16_t i = 0; i <= n; i++) {\n graph[i] = (uint16_t *)malloc((connectionCount[i] + 1) * sizeof(uint16_t));\n graph[i][0] = 0; // Initialize the number of connections to 0\n }\n\n // Second pass to fill connections\n for (int i = 0; i < edgesSize; i++) {\n uint16_t u = edges[i][0];\n uint16_t v = edges[i][1];\n graph[u][++graph[u][0]] = v;\n graph[v][++graph[v][0]] = u;\n }\n\n free(connectionCount);\n\n // Distance array, initialized to INF\n int (*dist)[2] = (int (*)[2])malloc((n + 1) * sizeof(int[2]));\n for (uint16_t i = 0; i <= n; i++) {\n dist[i][0] = INT_MAX;\n dist[i][1] = INT_MAX;\n }\n\n // Priority queue implemented with a simple array\n uint16_t *queueNode = (uint16_t *)malloc(2 * n * sizeof(uint16_t));\n int *queueTime = (int *)malloc(2 * n * sizeof(int));\n int front = 0, rear = 0;\n\n queueNode[rear] = 1;\n queueTime[rear++] = 0;\n\n dist[1][0] = 0;\n\n while (front < rear) {\n uint16_t u = queueNode[front];\n int t = queueTime[front++];\n\n int timeTaken = dist[u][t];\n\n // Calculate the new time considering traffic signals\n int signal = timeTaken / change;\n if (signal % 2 == 1) {\n timeTaken = change * (signal + 1);\n }\n timeTaken += time;\n\n for (uint16_t i = 1; i <= graph[u][0]; i++) {\n uint16_t v = graph[u][i];\n\n if (dist[v][0] > timeTaken) {\n dist[v][1] = dist[v][0];\n dist[v][0] = timeTaken;\n queueNode[rear] = v;\n queueTime[rear++] = 0;\n } else if (dist[v][1] > timeTaken && dist[v][0] != timeTaken) {\n dist[v][1] = timeTaken;\n queueNode[rear] = v;\n queueTime[rear++] = 1;\n }\n }\n }\n\n int result = (dist[n][1] == INT_MAX) ? -1 : dist[n][1];\n\n // Free allocated memory\n for (uint16_t i = 0; i <= n; i++) {\n free(graph[i]);\n }\n free(graph);\n free(dist);\n free(queueNode);\n free(queueTime);\n\n return result;\n }\n};", "memory": "160216" }
2,171
<p>A city is represented as a <strong>bi-directional connected</strong> graph with <code>n</code> vertices where each vertex is labeled from <code>1</code> to <code>n</code> (<strong>inclusive</strong>). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself. The time taken to traverse any edge is <code>time</code> minutes.</p> <p>Each vertex has a traffic signal which changes its color from <strong>green</strong> to <strong>red</strong> and vice versa every&nbsp;<code>change</code> minutes. All signals change <strong>at the same time</strong>. You can enter a vertex at <strong>any time</strong>, but can leave a vertex <strong>only when the signal is green</strong>. You <strong>cannot wait </strong>at a vertex if the signal is <strong>green</strong>.</p> <p>The <strong>second minimum value</strong> is defined as the smallest value<strong> strictly larger </strong>than the minimum value.</p> <ul> <li>For example the second minimum value of <code>[2, 3, 4]</code> is <code>3</code>, and the second minimum value of <code>[2, 2, 4]</code> is <code>4</code>.</li> </ul> <p>Given <code>n</code>, <code>edges</code>, <code>time</code>, and <code>change</code>, return <em>the <strong>second minimum time</strong> it will take to go from vertex </em><code>1</code><em> to vertex </em><code>n</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>You can go through any vertex <strong>any</strong> number of times, <strong>including</strong> <code>1</code> and <code>n</code>.</li> <li>You can assume that when the journey <strong>starts</strong>, all signals have just turned <strong>green</strong>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no duplicate edges.</li> <li>Each vertex can be reached directly or indirectly from every other vertex.</li> <li><code>1 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n int edgesSize = edges.size();\n \n // Create the adjacency list\n uint16_t **graph = (uint16_t **)malloc((n + 1) * sizeof(uint16_t *));\n int *connectionCount = (int *)calloc(n + 1, sizeof(int));\n\n // First pass to count connections\n for (int i = 0; i < edgesSize; i++) {\n connectionCount[edges[i][0]]++;\n connectionCount[edges[i][1]]++;\n }\n\n // Allocate space for each node's connections\n for (uint16_t i = 0; i <= n; i++) {\n graph[i] = (uint16_t *)malloc((connectionCount[i] + 1) * sizeof(uint16_t));\n graph[i][0] = 0; // Initialize the number of connections to 0\n }\n\n // Second pass to fill connections\n for (int i = 0; i < edgesSize; i++) {\n uint16_t u = edges[i][0], v = edges[i][1];\n graph[u][++graph[u][0]] = v, graph[v][++graph[v][0]] = u;\n }\n\n free(connectionCount);\n\n // Distance array, initialized to INF\n int (*dist)[2] = (int (*)[2])malloc((n + 1) * sizeof(int[2]));\n for (uint16_t i = 0; i <= n; i++) {\n dist[i][0] = INT_MAX;\n dist[i][1] = INT_MAX;\n }\n\n // Priority queue implemented with a simple array\n uint16_t *queueNode = (uint16_t *)malloc(2 * n * sizeof(uint16_t));\n int *queueTime = (int *)malloc(2 * n * sizeof(int));\n int front = 0, rear = 0;\n\n queueNode[rear] = 1;\n queueTime[rear++] = 0;\n\n dist[1][0] = 0;\n\n while (front < rear) {\n uint16_t u = queueNode[front];\n int t = queueTime[front++], timeTaken = dist[u][t], signal = timeTaken / change;\n if (signal % 2 == 1) {\n timeTaken = change * (signal + 1);\n }\n timeTaken += time;\n\n for (uint16_t i = 1; i <= graph[u][0]; i++) {\n uint16_t v = graph[u][i];\n\n if (dist[v][0] > timeTaken) {\n dist[v][1] = dist[v][0], dist[v][0] = timeTaken, queueNode[rear] = v, queueTime[rear++] = 0;\n } else if (dist[v][1] > timeTaken && dist[v][0] != timeTaken) {\n dist[v][1] = timeTaken, queueNode[rear] = v, queueTime[rear++] = 1;\n }\n }\n }\n\n int result = (dist[n][1] == INT_MAX) ? -1 : dist[n][1];\n\n // Free allocated memory\n for (uint16_t i = 0; i <= n; i++) {\n free(graph[i]);\n }\n free(graph);\n free(dist);\n free(queueNode);\n free(queueTime);\n\n return result;\n }\n};", "memory": "160216" }
2,171
<p>A city is represented as a <strong>bi-directional connected</strong> graph with <code>n</code> vertices where each vertex is labeled from <code>1</code> to <code>n</code> (<strong>inclusive</strong>). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself. The time taken to traverse any edge is <code>time</code> minutes.</p> <p>Each vertex has a traffic signal which changes its color from <strong>green</strong> to <strong>red</strong> and vice versa every&nbsp;<code>change</code> minutes. All signals change <strong>at the same time</strong>. You can enter a vertex at <strong>any time</strong>, but can leave a vertex <strong>only when the signal is green</strong>. You <strong>cannot wait </strong>at a vertex if the signal is <strong>green</strong>.</p> <p>The <strong>second minimum value</strong> is defined as the smallest value<strong> strictly larger </strong>than the minimum value.</p> <ul> <li>For example the second minimum value of <code>[2, 3, 4]</code> is <code>3</code>, and the second minimum value of <code>[2, 2, 4]</code> is <code>4</code>.</li> </ul> <p>Given <code>n</code>, <code>edges</code>, <code>time</code>, and <code>change</code>, return <em>the <strong>second minimum time</strong> it will take to go from vertex </em><code>1</code><em> to vertex </em><code>n</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>You can go through any vertex <strong>any</strong> number of times, <strong>including</strong> <code>1</code> and <code>n</code>.</li> <li>You can assume that when the journey <strong>starts</strong>, all signals have just turned <strong>green</strong>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no duplicate edges.</li> <li>Each vertex can be reached directly or indirectly from every other vertex.</li> <li><code>1 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
0
{ "code": "class Solution {\nprivate:\n static uint waittime(const uint s, const uint16_t t, const uint16_t c) __attribute__((always_inline)) {\n uint w = 0;\n for (uint i = 0; i < s; i++) {\n const uint g = w / c;\n if (g & 1u) w = (g + 1u) * c;\n w += t;\n }\n return w;\n }\n\npublic:\n static int secondMinimum(const uint16_t n, vector<vector<int>>& edges, const uint16_t time, const uint16_t change) {\n vector<uint16_t> adj[n];\n fill(adj, adj + n, vector<uint16_t>());\n for (auto &e : edges) {\n const uint16_t v = e[0] - 1u, w = e[1] - 1u;\n adj[v].push_back(w);\n adj[w].push_back(v);\n }\n uint64_t dmax[n];\n fill(dmax, dmax + n, -1ull);\n const uint16_t qcap = max<uint16_t>(n, edges.size()) + 1u;\n uint64_t q[qcap];\n uint16_t qstart = 0, qend = 1;\n *q = 0;\n *dmax = uint64_t(UINT_MAX) << 32;\n while (qstart != qend) {\n const uint64_t qelem = q[qstart++];\n if (qstart >= qcap) qstart = 0;\n for (const uint16_t j : adj[qelem >> 32]) {\n const uint64_t dq = dmax[j];\n const uint dnxt = (qelem & UINT_MAX) + 1u, dprv = dq, dpr2 = dq >> 32;\n const uint64_t qnext = (uint64_t(j) << 32) + dnxt;\n q[qend] = qnext;\n const bool ins1 = dnxt < dprv, ins2 = dnxt > dprv && dnxt < dpr2;\n const uint64_t dqnext = (uint64_t(dprv) << (ins1 << 5)) + (uint64_t(dnxt) << (ins2 << 5));\n dmax[j] = (ins1 | ins2) ? dqnext : dq;\n qend += ins1 | ins2;\n if (qend >= qcap) qend = 0;\n }\n }\n const uint total = dmax[n-1u] >> 32;\n return total == UINT_MAX ? total : waittime(total, time, change);\n }\n};\n\nauto init = []() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 'c';\n}();", "memory": "161398" }
2,171
<p>A city is represented as a <strong>bi-directional connected</strong> graph with <code>n</code> vertices where each vertex is labeled from <code>1</code> to <code>n</code> (<strong>inclusive</strong>). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself. The time taken to traverse any edge is <code>time</code> minutes.</p> <p>Each vertex has a traffic signal which changes its color from <strong>green</strong> to <strong>red</strong> and vice versa every&nbsp;<code>change</code> minutes. All signals change <strong>at the same time</strong>. You can enter a vertex at <strong>any time</strong>, but can leave a vertex <strong>only when the signal is green</strong>. You <strong>cannot wait </strong>at a vertex if the signal is <strong>green</strong>.</p> <p>The <strong>second minimum value</strong> is defined as the smallest value<strong> strictly larger </strong>than the minimum value.</p> <ul> <li>For example the second minimum value of <code>[2, 3, 4]</code> is <code>3</code>, and the second minimum value of <code>[2, 2, 4]</code> is <code>4</code>.</li> </ul> <p>Given <code>n</code>, <code>edges</code>, <code>time</code>, and <code>change</code>, return <em>the <strong>second minimum time</strong> it will take to go from vertex </em><code>1</code><em> to vertex </em><code>n</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>You can go through any vertex <strong>any</strong> number of times, <strong>including</strong> <code>1</code> and <code>n</code>.</li> <li>You can assume that when the journey <strong>starts</strong>, all signals have just turned <strong>green</strong>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no duplicate edges.</li> <li>Each vertex can be reached directly or indirectly from every other vertex.</li> <li><code>1 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
0
{ "code": "class Solution {\nprivate:\n static uint waittime(const uint s, const uint16_t t, const uint16_t c) __attribute__((always_inline)) {\n uint w = 0;\n for (uint i = 0; i < s; i++) {\n const uint g = w / c;\n if (g & 1u) w = (g + 1u) * c;\n w += t;\n }\n return w;\n }\n\npublic:\n static int secondMinimum(const uint16_t n, vector<vector<int>>& edges, const uint16_t time, const uint16_t change) {\n vector<uint16_t> adj[n];\n fill(adj, adj + n, vector<uint16_t>());\n for (auto &e : edges) {\n const uint16_t v = e[0] - 1u, w = e[1] - 1u;\n adj[v].push_back(w);\n adj[w].push_back(v);\n }\n uint64_t dmax[n];\n fill(dmax, dmax + n, -1ull);\n const uint16_t qcap = max<uint16_t>(n, edges.size()) + 1u;\n uint64_t q[qcap];\n uint16_t qstart = 0, qend = 1;\n *q = 0;\n *dmax = uint64_t(UINT_MAX) << 32;\n while (qstart != qend) {\n const uint64_t qelem = q[qstart++];\n if (qstart >= qcap) qstart = 0;\n for (const uint16_t j : adj[qelem >> 32]) {\n const uint64_t dq = dmax[j];\n const uint dnxt = (qelem & UINT_MAX) + 1u, dprv = dq, dpr2 = dq >> 32;\n const uint64_t qnext = (uint64_t(j) << 32) + dnxt;\n q[qend] = qnext;\n const bool ins1 = dnxt < dprv, ins2 = dnxt > dprv && dnxt < dpr2;\n const uint64_t dqnext = (uint64_t(dprv) << (ins1 << 5)) + (uint64_t(dnxt) << (ins2 << 5));\n dmax[j] = (ins1 | ins2) ? dqnext : dq;\n qend += ins1 | ins2;\n if (qend >= qcap) qend = 0;\n }\n }\n const uint total = dmax[n-1u] >> 32;\n return total == UINT_MAX ? total : waittime(total, time, change);\n }\n};\n\nauto init = []() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 'c';\n}();", "memory": "161398" }
2,171
<p>A city is represented as a <strong>bi-directional connected</strong> graph with <code>n</code> vertices where each vertex is labeled from <code>1</code> to <code>n</code> (<strong>inclusive</strong>). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself. The time taken to traverse any edge is <code>time</code> minutes.</p> <p>Each vertex has a traffic signal which changes its color from <strong>green</strong> to <strong>red</strong> and vice versa every&nbsp;<code>change</code> minutes. All signals change <strong>at the same time</strong>. You can enter a vertex at <strong>any time</strong>, but can leave a vertex <strong>only when the signal is green</strong>. You <strong>cannot wait </strong>at a vertex if the signal is <strong>green</strong>.</p> <p>The <strong>second minimum value</strong> is defined as the smallest value<strong> strictly larger </strong>than the minimum value.</p> <ul> <li>For example the second minimum value of <code>[2, 3, 4]</code> is <code>3</code>, and the second minimum value of <code>[2, 2, 4]</code> is <code>4</code>.</li> </ul> <p>Given <code>n</code>, <code>edges</code>, <code>time</code>, and <code>change</code>, return <em>the <strong>second minimum time</strong> it will take to go from vertex </em><code>1</code><em> to vertex </em><code>n</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>You can go through any vertex <strong>any</strong> number of times, <strong>including</strong> <code>1</code> and <code>n</code>.</li> <li>You can assume that when the journey <strong>starts</strong>, all signals have just turned <strong>green</strong>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no duplicate edges.</li> <li>Each vertex can be reached directly or indirectly from every other vertex.</li> <li><code>1 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
0
{ "code": "typedef pair<int, int> Data;\nconst int INF = 1000000000;\n\nint d1[10001];\nint d2[10001];\nint f[10001];\nvector<int> adj[10001];\n\nclass Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n for (int i = 1; i <= n; i++) adj[i].clear();\n for (auto e : edges) {\n adj[e[0]].push_back(e[1]);\n adj[e[1]].push_back(e[0]);\n }\n\n priority_queue<Data, vector<Data>, greater<Data>> q;\n d1[1] = 0;\n d2[1] = INF;\n for (int i = 2; i <= n; i++) d1[i] = d2[i] = INF;\n memset(f, 0, sizeof(f));\n\n q.push({d1[1], 1});\n while (!q.empty()) {\n int in = q.top().first;\n int v = q.top().second;\n q.pop();\n f[v]++;\n if (v == n && f[v] == 2) return d2[v];\n if (f[v] > 2) continue;\n\n int out = (in / change);\n if (out & 1) out = (out+1) * change; else out = in;\n\n for (auto w : adj[v]) {\n int t = out+time;\n if (t < d1[w]) {\n d1[w] = t;\n q.push({t, w});\n } else if (t > d1[w] && t < d2[w]) {\n d2[w] = t;\n q.push({t, w});\n }\n }\n }\n return 0;\n }\n};", "memory": "162581" }
2,171
<p>A city is represented as a <strong>bi-directional connected</strong> graph with <code>n</code> vertices where each vertex is labeled from <code>1</code> to <code>n</code> (<strong>inclusive</strong>). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself. The time taken to traverse any edge is <code>time</code> minutes.</p> <p>Each vertex has a traffic signal which changes its color from <strong>green</strong> to <strong>red</strong> and vice versa every&nbsp;<code>change</code> minutes. All signals change <strong>at the same time</strong>. You can enter a vertex at <strong>any time</strong>, but can leave a vertex <strong>only when the signal is green</strong>. You <strong>cannot wait </strong>at a vertex if the signal is <strong>green</strong>.</p> <p>The <strong>second minimum value</strong> is defined as the smallest value<strong> strictly larger </strong>than the minimum value.</p> <ul> <li>For example the second minimum value of <code>[2, 3, 4]</code> is <code>3</code>, and the second minimum value of <code>[2, 2, 4]</code> is <code>4</code>.</li> </ul> <p>Given <code>n</code>, <code>edges</code>, <code>time</code>, and <code>change</code>, return <em>the <strong>second minimum time</strong> it will take to go from vertex </em><code>1</code><em> to vertex </em><code>n</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>You can go through any vertex <strong>any</strong> number of times, <strong>including</strong> <code>1</code> and <code>n</code>.</li> <li>You can assume that when the journey <strong>starts</strong>, all signals have just turned <strong>green</strong>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no duplicate edges.</li> <li>Each vertex can be reached directly or indirectly from every other vertex.</li> <li><code>1 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
0
{ "code": "class Solution {\n public:\n int secondMinimum(const int n, const vector<vector<int>> &edges, const int time, const int change) {\n constexpr int source = 0;\n constexpr int second_time = 2;\n constexpr int range = 2;\n const int target = n - 1;\n vector<int> graph[n];\n for (const vector<int> &edge : edges) {\n const int u = edge.front() - 1;\n const int v = edge.back() - 1;\n graph[u].emplace_back(v);\n graph[v].emplace_back(u);\n }\n \n bool nodes[range][n];\n memset(nodes, 0, sizeof(nodes));\n int previous = 0;\n int current = 1;\n nodes[previous][0] = true;\n int step = 0;\n for (int times = 0; true; ++step) {\n if (nodes[previous][target] && ++times == second_time) {\n break;\n }\n\n for (int node = 0; node < n; ++node) {\n if (!nodes[previous][node]) {\n continue;\n }\n\n for (const int next : graph[node]) {\n nodes[current][next] = true;\n }\n }\n \n previous ^= 1;\n current ^= 1;\n memset(nodes[current], 0, sizeof(nodes[current]));\n }\n \n int ret = 0;\n for (int s = 0; s < step; ++s) {\n ret += time;\n if (s < step - 1 && ((ret / change) & 1) == 1) {\n ret += change - (ret % change);\n }\n }\n return ret;\n }\n};", "memory": "163763" }
2,171
<p>A city is represented as a <strong>bi-directional connected</strong> graph with <code>n</code> vertices where each vertex is labeled from <code>1</code> to <code>n</code> (<strong>inclusive</strong>). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself. The time taken to traverse any edge is <code>time</code> minutes.</p> <p>Each vertex has a traffic signal which changes its color from <strong>green</strong> to <strong>red</strong> and vice versa every&nbsp;<code>change</code> minutes. All signals change <strong>at the same time</strong>. You can enter a vertex at <strong>any time</strong>, but can leave a vertex <strong>only when the signal is green</strong>. You <strong>cannot wait </strong>at a vertex if the signal is <strong>green</strong>.</p> <p>The <strong>second minimum value</strong> is defined as the smallest value<strong> strictly larger </strong>than the minimum value.</p> <ul> <li>For example the second minimum value of <code>[2, 3, 4]</code> is <code>3</code>, and the second minimum value of <code>[2, 2, 4]</code> is <code>4</code>.</li> </ul> <p>Given <code>n</code>, <code>edges</code>, <code>time</code>, and <code>change</code>, return <em>the <strong>second minimum time</strong> it will take to go from vertex </em><code>1</code><em> to vertex </em><code>n</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>You can go through any vertex <strong>any</strong> number of times, <strong>including</strong> <code>1</code> and <code>n</code>.</li> <li>You can assume that when the journey <strong>starts</strong>, all signals have just turned <strong>green</strong>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no duplicate edges.</li> <li>Each vertex can be reached directly or indirectly from every other vertex.</li> <li><code>1 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
0
{ "code": "class Solution {\n static int const N = 10001;\n static int const M = 4 * N;\n int dir1[N], dir2[N];\n int he[N], e[M], ne[M];\n int idx = 0;\n int INF = 0x3F3F3F3F;\n using PP = pair<int, int>;\npublic:\n void add(int u, int v) {\n e[idx] = v;\n ne[idx] = he[u];\n he[u] = idx++;\n }\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n memset(he, -1, sizeof(he));\n memset(dir1, INF, sizeof(dir1));\n memset(dir2, INF, sizeof(dir2));\n for (auto edge : edges) {\n add(edge[0], edge[1]);\n add(edge[1], edge[0]);\n }\n priority_queue<PP, vector<PP>, greater<PP>> q;\n q.push(make_pair(0, 1));\n dir1[1] = 0;\n while (!q.empty()) {\n PP top = q.top(); q.pop();\n int step = top.first, u = top.second;\n for (int i = he[u]; ~i; i = ne[i]) {\n int j = e[i];\n int a = step / change, b = step % change;\n int wait = a % 2 ? change - b : 0;\n int dist = step + time + wait;\n if (dist < dir1[j]) {\n dir2[j] = dir1[j];\n dir1[j] = dist;\n q.push(make_pair(dir1[j], j));\n q.push(make_pair(dir2[j], j));\n } else if (dir1[j] < dist && dist < dir2[j]) {\n dir2[j] = dist;\n q.push(make_pair(dir2[j], j));\n }\n }\n }\n return dir2[n];\n }\n};", "memory": "164946" }
2,171
<p>A city is represented as a <strong>bi-directional connected</strong> graph with <code>n</code> vertices where each vertex is labeled from <code>1</code> to <code>n</code> (<strong>inclusive</strong>). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself. The time taken to traverse any edge is <code>time</code> minutes.</p> <p>Each vertex has a traffic signal which changes its color from <strong>green</strong> to <strong>red</strong> and vice versa every&nbsp;<code>change</code> minutes. All signals change <strong>at the same time</strong>. You can enter a vertex at <strong>any time</strong>, but can leave a vertex <strong>only when the signal is green</strong>. You <strong>cannot wait </strong>at a vertex if the signal is <strong>green</strong>.</p> <p>The <strong>second minimum value</strong> is defined as the smallest value<strong> strictly larger </strong>than the minimum value.</p> <ul> <li>For example the second minimum value of <code>[2, 3, 4]</code> is <code>3</code>, and the second minimum value of <code>[2, 2, 4]</code> is <code>4</code>.</li> </ul> <p>Given <code>n</code>, <code>edges</code>, <code>time</code>, and <code>change</code>, return <em>the <strong>second minimum time</strong> it will take to go from vertex </em><code>1</code><em> to vertex </em><code>n</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>You can go through any vertex <strong>any</strong> number of times, <strong>including</strong> <code>1</code> and <code>n</code>.</li> <li>You can assume that when the journey <strong>starts</strong>, all signals have just turned <strong>green</strong>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no duplicate edges.</li> <li>Each vertex can be reached directly or indirectly from every other vertex.</li> <li><code>1 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
0
{ "code": "#pragma GCC optimize(\"Ofast\",\"inline\",\"-ffast-math\")\n#pragma GCC target(\"avx,mmx,sse2,sse3,sse4\")\ntypedef pair<int, short> pr;\nclass Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n ios::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n \n vector<short> adj[n + 1];\n \n for(vector<int> &v : edges) {\n adj[v[0]].push_back(v[1]);\n adj[v[1]].push_back(v[0]);\n }\n \n int d0[n + 1], d1[n + 1];\n for(short i = 0; i != n + 1; ++i) {\n d0[i] = d1[i] = INT_MAX;\n }\n \n priority_queue<pr, vector<pr>, greater<pr>> pq; //time, node\n \n pq.push({0, 1});\n d0[1] = 0;\n \n while(!pq.empty()) {\n auto [t, u] = pq.top();\n pq.pop();\n \n if(t > d1[u]) continue;\n \n if(t % (change << 1) >= change) { //have to wait till green\n t = (change << 1) * (t / (change << 1) + 1);\n }\n t += time;\n \n for(short &v : adj[u]) {\n if(t >= d1[v] || t == d0[v]) continue;\n \n if(t > d0[v]) {\n d1[v] = t;\n }\n else {\n d1[v] = d0[v];\n d0[v] = t;\n }\n \n pq.push({t, v});\n }\n }\n \n return d1[n];\n }\n};", "memory": "166128" }
2,171
<p>A city is represented as a <strong>bi-directional connected</strong> graph with <code>n</code> vertices where each vertex is labeled from <code>1</code> to <code>n</code> (<strong>inclusive</strong>). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself. The time taken to traverse any edge is <code>time</code> minutes.</p> <p>Each vertex has a traffic signal which changes its color from <strong>green</strong> to <strong>red</strong> and vice versa every&nbsp;<code>change</code> minutes. All signals change <strong>at the same time</strong>. You can enter a vertex at <strong>any time</strong>, but can leave a vertex <strong>only when the signal is green</strong>. You <strong>cannot wait </strong>at a vertex if the signal is <strong>green</strong>.</p> <p>The <strong>second minimum value</strong> is defined as the smallest value<strong> strictly larger </strong>than the minimum value.</p> <ul> <li>For example the second minimum value of <code>[2, 3, 4]</code> is <code>3</code>, and the second minimum value of <code>[2, 2, 4]</code> is <code>4</code>.</li> </ul> <p>Given <code>n</code>, <code>edges</code>, <code>time</code>, and <code>change</code>, return <em>the <strong>second minimum time</strong> it will take to go from vertex </em><code>1</code><em> to vertex </em><code>n</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>You can go through any vertex <strong>any</strong> number of times, <strong>including</strong> <code>1</code> and <code>n</code>.</li> <li>You can assume that when the journey <strong>starts</strong>, all signals have just turned <strong>green</strong>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no duplicate edges.</li> <li>Each vertex can be reached directly or indirectly from every other vertex.</li> <li><code>1 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
0
{ "code": "#pragma GCC optimize(\"Ofast\",\"inline\",\"-ffast-math\")\n#pragma GCC target(\"avx,mmx,sse2,sse3,sse4\")\ntypedef pair<int, short> pr;\nclass Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n ios::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n \n vector<short> adj[n + 1];\n \n for(vector<int> &v : edges) {\n adj[v[0]].push_back(v[1]);\n adj[v[1]].push_back(v[0]);\n }\n \n int d0[n + 1], d1[n + 1];\n for(short i = 0; i != n + 1; ++i) {\n d0[i] = d1[i] = INT_MAX;\n }\n \n priority_queue<pr, vector<pr>, greater<pr>> pq; //time, node\n \n pq.push({0, 1});\n d0[1] = 0;\n \n while(!pq.empty()) {\n auto [t, u] = pq.top();\n pq.pop();\n \n if(t > d1[u]) continue;\n \n if(t % (change << 1) >= change) { //have to wait till green\n t = (change << 1) * (t / (change << 1) + 1);\n }\n t += time;\n \n for(short &v : adj[u]) {\n if(t >= d1[v] || t == d0[v]) continue;\n \n if(t > d0[v]) {\n d1[v] = t;\n }\n else {\n d1[v] = d0[v];\n d0[v] = t;\n }\n \n pq.push({t, v});\n }\n }\n \n return d1[n];\n }\n};", "memory": "167311" }
2,171
<p>A city is represented as a <strong>bi-directional connected</strong> graph with <code>n</code> vertices where each vertex is labeled from <code>1</code> to <code>n</code> (<strong>inclusive</strong>). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself. The time taken to traverse any edge is <code>time</code> minutes.</p> <p>Each vertex has a traffic signal which changes its color from <strong>green</strong> to <strong>red</strong> and vice versa every&nbsp;<code>change</code> minutes. All signals change <strong>at the same time</strong>. You can enter a vertex at <strong>any time</strong>, but can leave a vertex <strong>only when the signal is green</strong>. You <strong>cannot wait </strong>at a vertex if the signal is <strong>green</strong>.</p> <p>The <strong>second minimum value</strong> is defined as the smallest value<strong> strictly larger </strong>than the minimum value.</p> <ul> <li>For example the second minimum value of <code>[2, 3, 4]</code> is <code>3</code>, and the second minimum value of <code>[2, 2, 4]</code> is <code>4</code>.</li> </ul> <p>Given <code>n</code>, <code>edges</code>, <code>time</code>, and <code>change</code>, return <em>the <strong>second minimum time</strong> it will take to go from vertex </em><code>1</code><em> to vertex </em><code>n</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>You can go through any vertex <strong>any</strong> number of times, <strong>including</strong> <code>1</code> and <code>n</code>.</li> <li>You can assume that when the journey <strong>starts</strong>, all signals have just turned <strong>green</strong>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no duplicate edges.</li> <li>Each vertex can be reached directly or indirectly from every other vertex.</li> <li><code>1 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
0
{ "code": "#pragma GCC optimize(\"Ofast\",\"inline\",\"-ffast-math\")\n#pragma GCC target(\"avx,mmx,sse2,sse3,sse4\")\ntypedef pair<int, short> pr;\nclass Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n ios::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n \n vector<short> adj[n + 1];\n \n for(vector<int> &v : edges) {\n adj[v[0]].push_back(v[1]);\n adj[v[1]].push_back(v[0]);\n }\n \n int d0[n + 1], d1[n + 1];\n for(short i = 0; i != n + 1; ++i) {\n d0[i] = d1[i] = INT_MAX;\n }\n \n priority_queue<pr, vector<pr>, greater<pr>> pq; //time, node\n \n pq.push({0, 1});\n d0[1] = 0;\n \n while(!pq.empty()) {\n auto [t, u] = pq.top();\n pq.pop();\n \n if(t > d1[u]) continue;\n \n if(t % (change << 1) >= change) { //have to wait till green\n t = (change << 1) * (t / (change << 1) + 1);\n }\n t += time;\n \n for(short &v : adj[u]) {\n if(t >= d1[v] || t == d0[v]) continue;\n \n if(t > d0[v]) {\n d1[v] = t;\n }\n else {\n d1[v] = d0[v];\n d0[v] = t;\n }\n \n pq.push({t, v});\n }\n }\n \n return d1[n];\n }\n};", "memory": "167311" }
2,171
<p>A city is represented as a <strong>bi-directional connected</strong> graph with <code>n</code> vertices where each vertex is labeled from <code>1</code> to <code>n</code> (<strong>inclusive</strong>). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself. The time taken to traverse any edge is <code>time</code> minutes.</p> <p>Each vertex has a traffic signal which changes its color from <strong>green</strong> to <strong>red</strong> and vice versa every&nbsp;<code>change</code> minutes. All signals change <strong>at the same time</strong>. You can enter a vertex at <strong>any time</strong>, but can leave a vertex <strong>only when the signal is green</strong>. You <strong>cannot wait </strong>at a vertex if the signal is <strong>green</strong>.</p> <p>The <strong>second minimum value</strong> is defined as the smallest value<strong> strictly larger </strong>than the minimum value.</p> <ul> <li>For example the second minimum value of <code>[2, 3, 4]</code> is <code>3</code>, and the second minimum value of <code>[2, 2, 4]</code> is <code>4</code>.</li> </ul> <p>Given <code>n</code>, <code>edges</code>, <code>time</code>, and <code>change</code>, return <em>the <strong>second minimum time</strong> it will take to go from vertex </em><code>1</code><em> to vertex </em><code>n</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>You can go through any vertex <strong>any</strong> number of times, <strong>including</strong> <code>1</code> and <code>n</code>.</li> <li>You can assume that when the journey <strong>starts</strong>, all signals have just turned <strong>green</strong>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no duplicate edges.</li> <li>Each vertex can be reached directly or indirectly from every other vertex.</li> <li><code>1 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& _edges, int time, int change) {\n vector<int> edges[n];\n\n for(const auto &vp : _edges) {\n int u = vp[0] - 1, v = vp[1] - 1;\n edges[u].push_back(v);\n edges[v].push_back(u);\n }\n\n int dists[n];\n memset(dists, 0x3f, sizeof(dists));\n dists[n - 1] = 0;\n queue<int> q;\n\n q.push(n - 1);\n\n while(!q.empty()) {\n int u = q.front(); q.pop();\n for(const auto v : edges[u]) {\n if(dists[v] == 0x3f3f3f3f) {\n dists[v] = dists[u] + 1;\n q.push(v);\n }\n }\n }\n\n int len = dists[0] + 2;\n\n q.push(0);\n bool done = false;\n\n while(!q.empty()) {\n int u = q.front(); q.pop();\n for(const auto v : edges[u]) {\n if(dists[u] == dists[v]) {\n --len;\n done = true;\n break;\n } else if(dists[v] == dists[u] - 1)\n q.push(v);\n }\n\n if(done)\n break;\n }\n\n int res = 0;\n\n for(int i = 0; i < len; ++i) {\n if((res / change) % 2 == 1)\n res = ((res / change) + 1) * change;\n res += time;\n }\n\n return res;\n }\n};", "memory": "168493" }
2,171
<p>A city is represented as a <strong>bi-directional connected</strong> graph with <code>n</code> vertices where each vertex is labeled from <code>1</code> to <code>n</code> (<strong>inclusive</strong>). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself. The time taken to traverse any edge is <code>time</code> minutes.</p> <p>Each vertex has a traffic signal which changes its color from <strong>green</strong> to <strong>red</strong> and vice versa every&nbsp;<code>change</code> minutes. All signals change <strong>at the same time</strong>. You can enter a vertex at <strong>any time</strong>, but can leave a vertex <strong>only when the signal is green</strong>. You <strong>cannot wait </strong>at a vertex if the signal is <strong>green</strong>.</p> <p>The <strong>second minimum value</strong> is defined as the smallest value<strong> strictly larger </strong>than the minimum value.</p> <ul> <li>For example the second minimum value of <code>[2, 3, 4]</code> is <code>3</code>, and the second minimum value of <code>[2, 2, 4]</code> is <code>4</code>.</li> </ul> <p>Given <code>n</code>, <code>edges</code>, <code>time</code>, and <code>change</code>, return <em>the <strong>second minimum time</strong> it will take to go from vertex </em><code>1</code><em> to vertex </em><code>n</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>You can go through any vertex <strong>any</strong> number of times, <strong>including</strong> <code>1</code> and <code>n</code>.</li> <li>You can assume that when the journey <strong>starts</strong>, all signals have just turned <strong>green</strong>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no duplicate edges.</li> <li>Each vertex can be reached directly or indirectly from every other vertex.</li> <li><code>1 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
0
{ "code": "const int mxN = 1e4+1;\n\nclass Solution {\npublic:\n vector<int> adj[mxN];\n int vis[mxN];\n int par[mxN];\n int dist[mxN];\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n memset(dist, -1, sizeof(dist));\n memset(par, -100, sizeof(par));\n for (auto &x: edges){\n adj[x[0]].emplace_back(x[1]);\n adj[x[1]].emplace_back(x[0]);\n }\n queue<int> q;\n q.push(1);\n dist[1] = 0;\n par[1] = -1;\n while (!q.empty()){\n int currNode = q.front();\n for (auto &x: adj[currNode]){\n if (dist[x]==-1){\n q.push(x);\n dist[x] = dist[currNode]+1;\n par[x] = currNode;\n }\n }\n q.pop();\n }\n int N = n;\n bool ok = 0;\n memset(vis, 0, sizeof(vis));\n q.push(N);\n vis[N] = 1;\n while (!q.empty()){\n int top = q.front();\n if (par[top]!=-1){\n vis[top] = 1;\n for (auto &x: adj[top]){\n if (!vis[x]){\n if (dist[x]==dist[top]){ok = 1; break;}\n if (dist[x]==dist[top]-1){\n q.push(x);\n } \n }\n }\n }\n if (ok) break;\n q.pop();\n }\n // while (par[N]!=-1){\n // for (auto &x: adj[N]){\n // if (dist[x]==dist[N]){ok = 1; break;}\n // }\n // N = par[N];\n // if (ok) break;\n // }\n int nEdg = dist[n]+1;\n cout << nEdg << endl;\n if (!ok) nEdg = dist[n]+2;\n cout << nEdg << endl;\n int ans = 0;\n while (nEdg--){\n ans += time;\n if (nEdg>0){\n if ((ans/change)&1) ans += (change-(ans%change));\n }\n }\n return ans;\n }\n};", "memory": "169676" }
2,171
<p>A city is represented as a <strong>bi-directional connected</strong> graph with <code>n</code> vertices where each vertex is labeled from <code>1</code> to <code>n</code> (<strong>inclusive</strong>). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself. The time taken to traverse any edge is <code>time</code> minutes.</p> <p>Each vertex has a traffic signal which changes its color from <strong>green</strong> to <strong>red</strong> and vice versa every&nbsp;<code>change</code> minutes. All signals change <strong>at the same time</strong>. You can enter a vertex at <strong>any time</strong>, but can leave a vertex <strong>only when the signal is green</strong>. You <strong>cannot wait </strong>at a vertex if the signal is <strong>green</strong>.</p> <p>The <strong>second minimum value</strong> is defined as the smallest value<strong> strictly larger </strong>than the minimum value.</p> <ul> <li>For example the second minimum value of <code>[2, 3, 4]</code> is <code>3</code>, and the second minimum value of <code>[2, 2, 4]</code> is <code>4</code>.</li> </ul> <p>Given <code>n</code>, <code>edges</code>, <code>time</code>, and <code>change</code>, return <em>the <strong>second minimum time</strong> it will take to go from vertex </em><code>1</code><em> to vertex </em><code>n</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>You can go through any vertex <strong>any</strong> number of times, <strong>including</strong> <code>1</code> and <code>n</code>.</li> <li>You can assume that when the journey <strong>starts</strong>, all signals have just turned <strong>green</strong>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no duplicate edges.</li> <li>Each vertex can be reached directly or indirectly from every other vertex.</li> <li><code>1 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n \n // https://leetcode.com/problems/second-minimum-time-to-reach-destination/discuss/1525165/C%2B%2B-Two-BFS-with-detailed-explanation\n \n int calc_time(int s, int t, int c)\n {\n int res = 0;\n \n while(s--)\n {\n res += t;\n \n // add extra wait time when light turns red in between\n if(s && (res/c)%2)\n res += c - (res%c);\n }\n \n return res;\n }\n \n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<int> graph[n];\n \n for(auto& e : edges)\n {\n graph[e[0]-1].push_back(e[1]-1);\n graph[e[1]-1].push_back(e[0]-1);\n }\n \n int min_steps[n], visited[n];\n fill_n(min_steps, n, 1e4+1);\n fill_n(visited, n, 0);\n \n min_steps[0] = 0;\n \n queue<int> q;\n q.push(0);\n \n visited[0] = 1;\n \n int steps = 0;\n \n // perform bfs; compute min. steps to reach all nodes starting from 0\n while(q.size())\n {\n int l = q.size();\n \n while(l--)\n {\n int u = q.front();\n q.pop();\n \n min_steps[u] = steps;\n \n for(int i: graph[u])\n if(!visited[i])\n q.push(i), \n visited[i] = 1;\n }\n \n steps++;\n }\n \n /*\n Second min. dist is going to have min_steps[n-1] + 2 at max.\n \n Because we can simply go to one to the node and come back thus adding 2 more steps\n \n Ex : for minimum the path 1->2->3->4\n We can go to 3 and come back to 4 to get a POSSIBLE SECOND min. path\n 1->2->3->4->3->4\n \n Its also possible to have min_steps[n-1] + 1 as second min. path\n \n (check test case 1)\n \n And for checking whether we can have a path of length min_steps[n-1] + 1, second\n BFS is done; if possible then return the time to cover min_steps[n-1] + 1 steps\n otherwise, time to min_steps[n-1] + 2 steps\n \n \n The first BFS is done by starting from 0 which computes the min. steps required to\n to reach each node starting from 0\n \n \n The second BFS is done by starting from n-1\n Because we only want to checkout the possible extensions for the path from 0 to \n n-1\n Given that we are traversing from n-1 to 0 and we are currently in node u\n \n If an UNVISITED neighbour of u, i, is having\n min_steps[u] == min_steps[i] + 1\n \n then it means that i is a part of SHORTEST path form 0 to n\n \n \n For now DIY why it works\n */\n \n queue<int> q_empty;\n swap(q, q_empty);\n fill_n(visited, n, 0);\n \n visited[n-1] = 1;\n \n q.push(n-1);\n \n while(q.size())\n {\n int l = q.size();\n \n while(l--)\n {\n int u = q.front();\n q.pop();\n\n for(int i: graph[u])\n {\n if(min_steps[i] == min_steps[u])\n return calc_time(min_steps[n-1] + 1, time, change);\n\n if(!visited[i] && min_steps[u] == min_steps[i] + 1)\n {\n q.push(i), \n visited[i] = 1;\n }\n } \n }\n }\n \n return calc_time(min_steps[n-1] + 2, time, change);\n }\n};", "memory": "170858" }
2,171
<p>A city is represented as a <strong>bi-directional connected</strong> graph with <code>n</code> vertices where each vertex is labeled from <code>1</code> to <code>n</code> (<strong>inclusive</strong>). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself. The time taken to traverse any edge is <code>time</code> minutes.</p> <p>Each vertex has a traffic signal which changes its color from <strong>green</strong> to <strong>red</strong> and vice versa every&nbsp;<code>change</code> minutes. All signals change <strong>at the same time</strong>. You can enter a vertex at <strong>any time</strong>, but can leave a vertex <strong>only when the signal is green</strong>. You <strong>cannot wait </strong>at a vertex if the signal is <strong>green</strong>.</p> <p>The <strong>second minimum value</strong> is defined as the smallest value<strong> strictly larger </strong>than the minimum value.</p> <ul> <li>For example the second minimum value of <code>[2, 3, 4]</code> is <code>3</code>, and the second minimum value of <code>[2, 2, 4]</code> is <code>4</code>.</li> </ul> <p>Given <code>n</code>, <code>edges</code>, <code>time</code>, and <code>change</code>, return <em>the <strong>second minimum time</strong> it will take to go from vertex </em><code>1</code><em> to vertex </em><code>n</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>You can go through any vertex <strong>any</strong> number of times, <strong>including</strong> <code>1</code> and <code>n</code>.</li> <li>You can assume that when the journey <strong>starts</strong>, all signals have just turned <strong>green</strong>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no duplicate edges.</li> <li>Each vertex can be reached directly or indirectly from every other vertex.</li> <li><code>1 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
0
{ "code": "class Solution \n{\n public:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) \n {\n int m=edges.size();\n vector<int> total[n+1]; // the graph of the edges\n for(int i=0;i<m;i++)\n {\n total[edges[i][0]].push_back(edges[i][1]);\n total[edges[i][1]].push_back(edges[i][0]);\n }\n int c=0,duration=0,step=-1; // c is the step count, duration is the time required to the node\n int counter[n+1]; // the number of steps required to get to the node\n bool check[n+1]; // to prevent revisiting a node if the step count is 2 steps greater or more\n deque<int> q={1};\n memset(counter,-1,sizeof(counter));\n memset(check,false,sizeof(check));\n while(true)\n {\n int s=q.size();\n c++;\n if((duration/change)%2) duration=duration+change-duration%change; // time required to get here\n duration+=time;\n \n if(step!=-1&&c==step+2) return duration;\n for(int i=0;i<s;i++)\n {\n int p=q.front();\n q.pop_front();\n for(int j=0;j<total[p].size();j++)\n {\n if(counter[total[p][j]]==-1||(counter[total[p][j]]==c-1&&!check[total[p][j]]))\n {\n q.push_back(total[p][j]);\n if(counter[total[p][j]]==c-1)\n {\n check[total[p][j]]=true;\n if(total[p][j]==n&&step!=-1) return duration;\n }\n counter[total[p][j]]=c;\n if(total[p][j]==n) step=c;\n }\n }\n }\n }\n return duration;\n }\n};", "memory": "172041" }
2,171
<p>A city is represented as a <strong>bi-directional connected</strong> graph with <code>n</code> vertices where each vertex is labeled from <code>1</code> to <code>n</code> (<strong>inclusive</strong>). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself. The time taken to traverse any edge is <code>time</code> minutes.</p> <p>Each vertex has a traffic signal which changes its color from <strong>green</strong> to <strong>red</strong> and vice versa every&nbsp;<code>change</code> minutes. All signals change <strong>at the same time</strong>. You can enter a vertex at <strong>any time</strong>, but can leave a vertex <strong>only when the signal is green</strong>. You <strong>cannot wait </strong>at a vertex if the signal is <strong>green</strong>.</p> <p>The <strong>second minimum value</strong> is defined as the smallest value<strong> strictly larger </strong>than the minimum value.</p> <ul> <li>For example the second minimum value of <code>[2, 3, 4]</code> is <code>3</code>, and the second minimum value of <code>[2, 2, 4]</code> is <code>4</code>.</li> </ul> <p>Given <code>n</code>, <code>edges</code>, <code>time</code>, and <code>change</code>, return <em>the <strong>second minimum time</strong> it will take to go from vertex </em><code>1</code><em> to vertex </em><code>n</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>You can go through any vertex <strong>any</strong> number of times, <strong>including</strong> <code>1</code> and <code>n</code>.</li> <li>You can assume that when the journey <strong>starts</strong>, all signals have just turned <strong>green</strong>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no duplicate edges.</li> <li>Each vertex can be reached directly or indirectly from every other vertex.</li> <li><code>1 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
0
{ "code": "const int INF = 0x3f3f3f3f;\nconst int N = 10003;\nvector<int> g[N];\nint dis[N][2];\nint vst[N];\n\nclass Solution {\n void bfs(int u, int t, int c) {\n vst[u] = 1;\n queue<int> q;\n q.push(u);\n while(!q.empty()) {\n int v = q.front();\n q.pop();\n int curt = dis[v][0];\n if(curt / c & 1) { curt = curt + c - curt % c; }\n curt = curt + t;\n int sec = dis[v][1];\n if(sec == INF) { sec = -1; }\n else {\n if(sec / c & 1) { sec = sec + c - sec % c; }\n sec = sec + t;\n }\n for(int i = 0; i < g[v].size(); ++i) {\n int w = g[v][i];\n if(-1 == sec) {\n if(dis[w][0] == INF && dis[w][1] == INF) {\n dis[w][0] = curt;\n vst[w] = 0;\n }\n else if(dis[w][0] != INF && dis[w][1] == INF) {\n if(curt != dis[w][0]) {vst[w] = 0; dis[w][1] = curt; }\n }\n else {\n if(curt != dis[w][0] && curt != dis[w][1]) {\n if(curt < dis[w][1]) {vst[w] = 0; dis[w][1] = curt; }\n if(dis[w][1] < dis[w][0]) { swap(dis[w][0], dis[w][1]); }\n }\n }\n }\n else {\n if(dis[w][0] == INF && dis[w][1] == INF) {\n dis[w][0] = curt;\n dis[w][1] = sec;\n vst[w] = 0;\n }\n else if(dis[w][0] != INF && dis[w][1] == INF) {\n if(curt != dis[w][0]) {vst[w] = 0; dis[w][1] = curt; }\n else {\n vst[w] = 0;\n dis[w][1] = sec;\n }\n }\n else {\n if(curt != dis[w][0] && curt != dis[w][1]) {\n if(curt < dis[w][1]) {vst[w] = 0; dis[w][1] = curt; }\n if(dis[w][1] < dis[w][0]) { swap(dis[w][0], dis[w][1]); }\n }\n else if(sec != dis[w][0] && sec != dis[w][1]) {\n if(sec < dis[w][1]) {vst[w] = 0; dis[w][1] = sec; }\n if(dis[w][1] < dis[w][0]) { swap(dis[w][0], dis[w][1]); }\n }\n }\n }\n \n // printf(\"dealing %d, %d, %d\\n\", w, dis[w][0], dis[w][1]);\n if(0 == vst[w]) {\n vst[w] = 1;\n q.push(w);\n }\n }\n }\n return;\n }\n int cal(int n, int t, int c) {\n int curt = dis[n][0];\n if(curt / c & 1) { curt = curt + c - curt % c; }\n curt = curt + t;\n if(curt / c & 1) { curt = curt + c - curt % c; }\n return curt + t;\n }\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n for(int i = 0; i < n; ++i) { g[i] = vector<int>(); }\n memset(dis, 0x3f, sizeof(dis));\n memset(vst, 0, sizeof(vst));\n dis[1][0] = 0;\n dis[1][1] = INF;\n \n for(int i = 0; i < edges.size(); ++i) {\n int u = edges[i][0];\n int v = edges[i][1];\n g[u].push_back(v);\n g[v].push_back(u);\n }\n bfs(1, time, change);\n int ans = dis[n][1];\n int second = cal(n, time, change);\n // printf(\"%d, %d, %d, %d\\n\", dis[n][0], dis[n][1], second, min(dis[n][1], second));\n return min(dis[n][1], second);\n }\n};", "memory": "173223" }
2,171
<p>A city is represented as a <strong>bi-directional connected</strong> graph with <code>n</code> vertices where each vertex is labeled from <code>1</code> to <code>n</code> (<strong>inclusive</strong>). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself. The time taken to traverse any edge is <code>time</code> minutes.</p> <p>Each vertex has a traffic signal which changes its color from <strong>green</strong> to <strong>red</strong> and vice versa every&nbsp;<code>change</code> minutes. All signals change <strong>at the same time</strong>. You can enter a vertex at <strong>any time</strong>, but can leave a vertex <strong>only when the signal is green</strong>. You <strong>cannot wait </strong>at a vertex if the signal is <strong>green</strong>.</p> <p>The <strong>second minimum value</strong> is defined as the smallest value<strong> strictly larger </strong>than the minimum value.</p> <ul> <li>For example the second minimum value of <code>[2, 3, 4]</code> is <code>3</code>, and the second minimum value of <code>[2, 2, 4]</code> is <code>4</code>.</li> </ul> <p>Given <code>n</code>, <code>edges</code>, <code>time</code>, and <code>change</code>, return <em>the <strong>second minimum time</strong> it will take to go from vertex </em><code>1</code><em> to vertex </em><code>n</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>You can go through any vertex <strong>any</strong> number of times, <strong>including</strong> <code>1</code> and <code>n</code>.</li> <li>You can assume that when the journey <strong>starts</strong>, all signals have just turned <strong>green</strong>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no duplicate edges.</li> <li>Each vertex can be reached directly or indirectly from every other vertex.</li> <li><code>1 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n int cnt=0;\n queue<int>q;\n vector<int> adj[n+1];\n for(auto &edge: edges)\n {\n adj[edge[0]].push_back(edge[1]);\n adj[edge[1]].push_back(edge[0]);\n }\n q.push(n);\n vector<int> d(n+1, 1e9);\n d[n]=0;\n while(q.size())\n {\n auto pr=q.front();\n q.pop();\n int node=pr;\n for(auto child: adj[node])\n {\n if(d[child]==1e9)\n {\n q.push(child);\n d[child]=d[node]+1;\n }\n }\n }\n int len=d[1]+2;\n q.push(1);\n while(q.size())\n {\n bool done=false;\n int node=q.front();\n q.pop();\n for(auto child: adj[node])\n {\n if(d[child]==d[node])\n {\n len--;\n done=true;\n break;\n }\n else if(d[child]==d[node]-1)\n {\n q.push(child);\n }\n }\n if(done)\n break;\n }\n int res=0;\n cout<<len<<endl;\n for(int i=0; i<len; i++)\n {\n if((res/change)%2==1)\n res=res+change-res%change;\n res=res+time;\n }\n return res;\n }\n};", "memory": "174406" }
2,171
<p>A city is represented as a <strong>bi-directional connected</strong> graph with <code>n</code> vertices where each vertex is labeled from <code>1</code> to <code>n</code> (<strong>inclusive</strong>). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself. The time taken to traverse any edge is <code>time</code> minutes.</p> <p>Each vertex has a traffic signal which changes its color from <strong>green</strong> to <strong>red</strong> and vice versa every&nbsp;<code>change</code> minutes. All signals change <strong>at the same time</strong>. You can enter a vertex at <strong>any time</strong>, but can leave a vertex <strong>only when the signal is green</strong>. You <strong>cannot wait </strong>at a vertex if the signal is <strong>green</strong>.</p> <p>The <strong>second minimum value</strong> is defined as the smallest value<strong> strictly larger </strong>than the minimum value.</p> <ul> <li>For example the second minimum value of <code>[2, 3, 4]</code> is <code>3</code>, and the second minimum value of <code>[2, 2, 4]</code> is <code>4</code>.</li> </ul> <p>Given <code>n</code>, <code>edges</code>, <code>time</code>, and <code>change</code>, return <em>the <strong>second minimum time</strong> it will take to go from vertex </em><code>1</code><em> to vertex </em><code>n</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>You can go through any vertex <strong>any</strong> number of times, <strong>including</strong> <code>1</code> and <code>n</code>.</li> <li>You can assume that when the journey <strong>starts</strong>, all signals have just turned <strong>green</strong>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no duplicate edges.</li> <li>Each vertex can be reached directly or indirectly from every other vertex.</li> <li><code>1 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
0
{ "code": "#pragma GCC optimize(\"O3,unroll-loops\")\n#pragma GCC target(\"avx2,bmi,bmi2,lzcnt,popcnt,arch=pantherlake\")\n\nusing u16 = uint16_t;\n\nclass Solution {\npublic:\n static bitset<10001> expandCircle(const vector<vector<u16>>& adjacencies, const bitset<10001>& circle) __attribute__ ((__target__ (\"avx2,bmi,bmi2,lzcnt,popcnt,arch=pantherlake\"))) {\n bitset<10001> new_circle;\n int n = adjacencies.size();\n for (int i = 1; i < n; ++i) {\n if (circle.test(i)) {\n for (auto adj : adjacencies[i]) {\n new_circle.set(adj);\n }\n }\n }\n return new_circle;\n }\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) __attribute__ ((__target__ (\"avx2,bmi,bmi2,lzcnt,popcnt,arch=pantherlake\"))) {\n vector<vector<u16>> adjacencies(n + 1);\n for (const auto& e : edges) {\n u16 x = e.front(), y=e[1];\n adjacencies[x].push_back(y);\n adjacencies[y].push_back(x);\n }\n for (auto& a : adjacencies) {\n sort(a.begin(), a.end());\n }\n int n_expansions = 0;\n int o_expansions = 0;\n bitset<10001> ncircle, ocircle;\n ncircle.set(n);\n ocircle.set(1);\n int found = 0;\n while (found < 2) {\n if (ncircle.count() < ocircle.count()) {\n ++n_expansions;\n ncircle = expandCircle(adjacencies, ncircle);\n } else {\n ++o_expansions;\n ocircle = expandCircle(adjacencies, ocircle);\n }\n if ((ncircle & ocircle).count()) ++found;\n }\n int hops = n_expansions + o_expansions;\n int ans = 0;\n int cycle_offset = 0;\n while (hops) {\n if ((cycle_offset / change) & 1) {\n // wait for green\n ans += change - (cycle_offset % change);\n cycle_offset = 0;\n }\n int hops_before_red = min(hops, change / time);\n hops -= hops_before_red;\n ans += hops_before_red * time;\n cycle_offset += hops_before_red * time;\n cycle_offset %= (change << 1);\n if (hops && (cycle_offset < change)) {\n // 1 more hop into the light change.\n --hops;\n ans += time;\n cycle_offset += time;\n }\n }\n return ans;\n }\n};\nauto fast = (ios::sync_with_stdio(0), cout.tie(0), cin.tie(0), true);\n/****\n// first move: [0;946]\n// light is red at 183, green at 366, green at 732, red at 915, green at 1098\n// 2nd move: [1098;2044]\n// light is green at 1830, red at 2013, green at 2196\n// 3rd move: [2196:3142]\n\n****/", "memory": "174406" }
2,171
<p>A city is represented as a <strong>bi-directional connected</strong> graph with <code>n</code> vertices where each vertex is labeled from <code>1</code> to <code>n</code> (<strong>inclusive</strong>). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself. The time taken to traverse any edge is <code>time</code> minutes.</p> <p>Each vertex has a traffic signal which changes its color from <strong>green</strong> to <strong>red</strong> and vice versa every&nbsp;<code>change</code> minutes. All signals change <strong>at the same time</strong>. You can enter a vertex at <strong>any time</strong>, but can leave a vertex <strong>only when the signal is green</strong>. You <strong>cannot wait </strong>at a vertex if the signal is <strong>green</strong>.</p> <p>The <strong>second minimum value</strong> is defined as the smallest value<strong> strictly larger </strong>than the minimum value.</p> <ul> <li>For example the second minimum value of <code>[2, 3, 4]</code> is <code>3</code>, and the second minimum value of <code>[2, 2, 4]</code> is <code>4</code>.</li> </ul> <p>Given <code>n</code>, <code>edges</code>, <code>time</code>, and <code>change</code>, return <em>the <strong>second minimum time</strong> it will take to go from vertex </em><code>1</code><em> to vertex </em><code>n</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>You can go through any vertex <strong>any</strong> number of times, <strong>including</strong> <code>1</code> and <code>n</code>.</li> <li>You can assume that when the journey <strong>starts</strong>, all signals have just turned <strong>green</strong>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no duplicate edges.</li> <li>Each vertex can be reached directly or indirectly from every other vertex.</li> <li><code>1 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n unsigned int u, dist[10000];\n vector<int> adj[10000];\n queue<int> q;\n memset(dist, -1, n << 2);\n dist[0] = 0;\n for (auto &e: edges) {\n adj[e[0] - 1].push_back(e[1] - 1);\n adj[e[1] - 1].push_back(e[0] - 1);\n }\n for (q.push(0); !q.empty(); q.pop()) {\n u = q.front();\n for (int v: adj[u]) {\n if (dist[v] <= dist[u] + 1) continue;\n dist[v] = dist[u] + 1;\n q.push(v);\n }\n }\n n = dist[n - 1] + (2 >> DFS(adj, dist, n - 1));\n for (u = 0; n--;) {\n if (u / change & 1) u += change - u % change;\n u += time;\n }\n return u;\n }\n\n int DFS(vector<int> adj[], unsigned int dist[], int u) {\n for (int v: adj[u]) {\n if (dist[v] == dist[u]) return 1;\n if (dist[v] == dist[u] - 1 && DFS(adj, dist, v)) return 1;\n }\n return 0;\n }\n};", "memory": "175588" }
2,171
<p>A city is represented as a <strong>bi-directional connected</strong> graph with <code>n</code> vertices where each vertex is labeled from <code>1</code> to <code>n</code> (<strong>inclusive</strong>). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself. The time taken to traverse any edge is <code>time</code> minutes.</p> <p>Each vertex has a traffic signal which changes its color from <strong>green</strong> to <strong>red</strong> and vice versa every&nbsp;<code>change</code> minutes. All signals change <strong>at the same time</strong>. You can enter a vertex at <strong>any time</strong>, but can leave a vertex <strong>only when the signal is green</strong>. You <strong>cannot wait </strong>at a vertex if the signal is <strong>green</strong>.</p> <p>The <strong>second minimum value</strong> is defined as the smallest value<strong> strictly larger </strong>than the minimum value.</p> <ul> <li>For example the second minimum value of <code>[2, 3, 4]</code> is <code>3</code>, and the second minimum value of <code>[2, 2, 4]</code> is <code>4</code>.</li> </ul> <p>Given <code>n</code>, <code>edges</code>, <code>time</code>, and <code>change</code>, return <em>the <strong>second minimum time</strong> it will take to go from vertex </em><code>1</code><em> to vertex </em><code>n</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>You can go through any vertex <strong>any</strong> number of times, <strong>including</strong> <code>1</code> and <code>n</code>.</li> <li>You can assume that when the journey <strong>starts</strong>, all signals have just turned <strong>green</strong>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no duplicate edges.</li> <li>Each vertex can be reached directly or indirectly from every other vertex.</li> <li><code>1 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<int> adjList[n];\n for (vector<int> &edge : edges) {\n int u = edge[0] - 1, v = edge[1] - 1;\n adjList[u].emplace_back(v);\n adjList[v].emplace_back(u);\n }\n\n // label the depth from node 0\n int cnt = 0;\n vector<int> dist(n, INT_MAX);\n queue<int> q;\n dist[0] = cnt;\n q.emplace(0);\n while (!q.empty()) {\n ++cnt;\n int size = q.size();\n while (size--) {\n int curr = q.front();\n q.pop();\n for (int neighbor : adjList[curr]) {\n if (dist[neighbor] == INT_MAX) {\n dist[neighbor] = cnt;\n q.emplace(neighbor);\n }\n }\n }\n }\n\n // trace back from node n - 1\n int len = dist[n - 1];\n bool found = false;\n q.emplace(n - 1);\n while (!found && !q.empty()) {\n int size = q.size();\n while (!found && size--) {\n int curr = q.front();\n q.pop();\n for (int neighbor : adjList[curr]) {\n if (dist[neighbor] < dist[curr]) {\n q.emplace(neighbor);\n }\n else if (dist[neighbor] == dist[curr]) {\n len += 1;\n found = true;\n break;\n }\n }\n }\n }\n if (!found)\n len += 2;\n\n int ans = 0;\n for (int i = 0; i < len; ++i) {\n bool isRed = (ans / change) % 2;\n if (isRed)\n ans += change - ans % change;\n ans += time;\n }\n return ans;\n }\n};", "memory": "175588" }
2,171
<p>A city is represented as a <strong>bi-directional connected</strong> graph with <code>n</code> vertices where each vertex is labeled from <code>1</code> to <code>n</code> (<strong>inclusive</strong>). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself. The time taken to traverse any edge is <code>time</code> minutes.</p> <p>Each vertex has a traffic signal which changes its color from <strong>green</strong> to <strong>red</strong> and vice versa every&nbsp;<code>change</code> minutes. All signals change <strong>at the same time</strong>. You can enter a vertex at <strong>any time</strong>, but can leave a vertex <strong>only when the signal is green</strong>. You <strong>cannot wait </strong>at a vertex if the signal is <strong>green</strong>.</p> <p>The <strong>second minimum value</strong> is defined as the smallest value<strong> strictly larger </strong>than the minimum value.</p> <ul> <li>For example the second minimum value of <code>[2, 3, 4]</code> is <code>3</code>, and the second minimum value of <code>[2, 2, 4]</code> is <code>4</code>.</li> </ul> <p>Given <code>n</code>, <code>edges</code>, <code>time</code>, and <code>change</code>, return <em>the <strong>second minimum time</strong> it will take to go from vertex </em><code>1</code><em> to vertex </em><code>n</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>You can go through any vertex <strong>any</strong> number of times, <strong>including</strong> <code>1</code> and <code>n</code>.</li> <li>You can assume that when the journey <strong>starts</strong>, all signals have just turned <strong>green</strong>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no duplicate edges.</li> <li>Each vertex can be reached directly or indirectly from every other vertex.</li> <li><code>1 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
0
{ "code": "class Solution {\n\nint levelOfVertex(int n, vector<int> adj[]){\n vector<int> level(n+1, 1e9);\n level[1] = 0;\n\n queue<int> q;\n q.push(1);\n\n while(!q.empty()){\n int topNode = q.front();\n q.pop();\n for(auto it: adj[topNode]){\n if(level[topNode] + 1 < level[it]){\n level[it] = level[topNode] + 1;\n q.push(it);\n }\n }\n }\n\n q.push(n);\n while(!q.empty()){\n int topNode = q.front();\n q.pop();\n for(auto it: adj[topNode]){\n if(level[it] == level[topNode]) return level[n];\n else if(level[topNode] - 1 == level[it]) q.push(it);\n }\n }\n \n return ++level[n];\n}\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int et, int st) {\n \n vector<int> adj[n+1];\n for(int i = 0; i < edges.size(); i++){\n adj[edges[i][0]].push_back(edges[i][1]);\n adj[edges[i][1]].push_back(edges[i][0]);\n }\n\n int level = levelOfVertex(n, adj);\n int total = 0;\n\n for(int i = 0; i <= level; i++){\n total += et;\n\n int sc = total/st;\n if(sc % 2 != 0 && i != level){\n total = (sc + 1) * st;\n }\n }\n return total;\n }\n};", "memory": "176771" }
2,171
<p>A city is represented as a <strong>bi-directional connected</strong> graph with <code>n</code> vertices where each vertex is labeled from <code>1</code> to <code>n</code> (<strong>inclusive</strong>). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself. The time taken to traverse any edge is <code>time</code> minutes.</p> <p>Each vertex has a traffic signal which changes its color from <strong>green</strong> to <strong>red</strong> and vice versa every&nbsp;<code>change</code> minutes. All signals change <strong>at the same time</strong>. You can enter a vertex at <strong>any time</strong>, but can leave a vertex <strong>only when the signal is green</strong>. You <strong>cannot wait </strong>at a vertex if the signal is <strong>green</strong>.</p> <p>The <strong>second minimum value</strong> is defined as the smallest value<strong> strictly larger </strong>than the minimum value.</p> <ul> <li>For example the second minimum value of <code>[2, 3, 4]</code> is <code>3</code>, and the second minimum value of <code>[2, 2, 4]</code> is <code>4</code>.</li> </ul> <p>Given <code>n</code>, <code>edges</code>, <code>time</code>, and <code>change</code>, return <em>the <strong>second minimum time</strong> it will take to go from vertex </em><code>1</code><em> to vertex </em><code>n</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>You can go through any vertex <strong>any</strong> number of times, <strong>including</strong> <code>1</code> and <code>n</code>.</li> <li>You can assume that when the journey <strong>starts</strong>, all signals have just turned <strong>green</strong>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no duplicate edges.</li> <li>Each vertex can be reached directly or indirectly from every other vertex.</li> <li><code>1 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n\n vector<int>adj[n+1];\n\n for(auto&v : edges){\n for(int i = 0 ; i < 2 ; ++i){\n adj[v[i]].push_back(v[i^1]);\n }\n }\n\n queue<int>q;\n\n vector<int>len(n + 1 , -1);\n\n q.push(1);\n\n len[1] = 1;\n\n while(!q.empty()){\n\n int src = q.front();\n q.pop();\n\n for(int&nxt : adj[src]){\n if(len[nxt] == -1){\n len[nxt] = len[src] + 1;\n q.push(nxt);\n }\n }\n }\n\n int pathLen = len[n] + 2;\n bool cycle = false;\n\n q.push(n);\n\n while(!q.empty()){\n if(cycle) break;\n int src = q.front();\n q.pop();\n\n for(int&nxt : adj[src]){\n if(len[nxt] == len[src]){\n --pathLen;\n cycle = true;\n break;\n }\n else if(len[nxt] == len[src] - 1){\n q.push(nxt);\n }\n }\n }\n \n int ans = 0;\n\n for(int i = 0 ; i < pathLen - 1 ; ++i){\n\n ans += time;\n\n if(i == pathLen - 2){\n break;\n }\n\n if((ans/change) & 1){\n ans += change - ans%change;\n }\n }\n\n return ans;\n\n }\n};", "memory": "176771" }
2,171
<p>A city is represented as a <strong>bi-directional connected</strong> graph with <code>n</code> vertices where each vertex is labeled from <code>1</code> to <code>n</code> (<strong>inclusive</strong>). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself. The time taken to traverse any edge is <code>time</code> minutes.</p> <p>Each vertex has a traffic signal which changes its color from <strong>green</strong> to <strong>red</strong> and vice versa every&nbsp;<code>change</code> minutes. All signals change <strong>at the same time</strong>. You can enter a vertex at <strong>any time</strong>, but can leave a vertex <strong>only when the signal is green</strong>. You <strong>cannot wait </strong>at a vertex if the signal is <strong>green</strong>.</p> <p>The <strong>second minimum value</strong> is defined as the smallest value<strong> strictly larger </strong>than the minimum value.</p> <ul> <li>For example the second minimum value of <code>[2, 3, 4]</code> is <code>3</code>, and the second minimum value of <code>[2, 2, 4]</code> is <code>4</code>.</li> </ul> <p>Given <code>n</code>, <code>edges</code>, <code>time</code>, and <code>change</code>, return <em>the <strong>second minimum time</strong> it will take to go from vertex </em><code>1</code><em> to vertex </em><code>n</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>You can go through any vertex <strong>any</strong> number of times, <strong>including</strong> <code>1</code> and <code>n</code>.</li> <li>You can assume that when the journey <strong>starts</strong>, all signals have just turned <strong>green</strong>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no duplicate edges.</li> <li>Each vertex can be reached directly or indirectly from every other vertex.</li> <li><code>1 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
0
{ "code": "inline const auto optimize = []() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return 0;\n}();\n\nclass Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<int> adj[n];\n\n for(auto&i : edges) {\n adj[i[0] - 1].push_back(i[1] - 1);\n adj[i[1] - 1].push_back(i[0] - 1);\n }\n\n /*\n to find the second minimum time we have to consider \n the smallest path (there can be a bunch of them)\n\n at best the second smallest path can be smallest + 2\n so we really just need to determine if there exists a path i.e. is\n smallest + 1\n */\n\n int distFromSrc[n], distFromDest[n];\n for(int i = 0; i < n; i++) distFromSrc[i] = distFromDest[i] = n;\n distFromSrc[0] = distFromDest[n - 1] = 0;\n\n // do a doubly sourced bfs\n queue<int> q;\n q.push(0);\n\n while (!q.empty()) {\n int curr = q.front(); q.pop();\n\n for(auto&i : adj[curr]) {\n if (distFromSrc[curr] + 1 < distFromSrc[i]) {\n distFromSrc[i] = distFromSrc[curr] + 1;\n q.push(i);\n }\n }\n }\n\n q.push(n - 1);\n while (!q.empty()) {\n int curr = q.front(); q.pop();\n\n for(auto&i : adj[curr]) {\n if (distFromDest[curr] + 1 < distFromDest[i]) {\n distFromDest[i] = distFromDest[curr] + 1;\n q.push(i);\n }\n }\n }\n\n int smallestDistance = distFromDest[0];\n bool plusOneFound = false;\n for(auto&edge : edges) {\n if ((distFromSrc[edge[0] - 1] + distFromDest[edge[1] - 1] == smallestDistance) || \n (distFromSrc[edge[1] - 1] + distFromDest[edge[0] - 1] == smallestDistance)) {\n plusOneFound = true; break;\n }\n }\n\n int secondSmallestDistance = smallestDistance + ((plusOneFound) ? 1 : 2);\n int totalTime = 0;\n\n for(int i = 0; i < secondSmallestDistance - 1; i++) {\n totalTime += time;\n\n if ((totalTime / change) % 2 != 0) {\n totalTime += change - (totalTime % change);\n }\n }\n\n return totalTime + time;\n }\n};", "memory": "177953" }
2,171
<p>A city is represented as a <strong>bi-directional connected</strong> graph with <code>n</code> vertices where each vertex is labeled from <code>1</code> to <code>n</code> (<strong>inclusive</strong>). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself. The time taken to traverse any edge is <code>time</code> minutes.</p> <p>Each vertex has a traffic signal which changes its color from <strong>green</strong> to <strong>red</strong> and vice versa every&nbsp;<code>change</code> minutes. All signals change <strong>at the same time</strong>. You can enter a vertex at <strong>any time</strong>, but can leave a vertex <strong>only when the signal is green</strong>. You <strong>cannot wait </strong>at a vertex if the signal is <strong>green</strong>.</p> <p>The <strong>second minimum value</strong> is defined as the smallest value<strong> strictly larger </strong>than the minimum value.</p> <ul> <li>For example the second minimum value of <code>[2, 3, 4]</code> is <code>3</code>, and the second minimum value of <code>[2, 2, 4]</code> is <code>4</code>.</li> </ul> <p>Given <code>n</code>, <code>edges</code>, <code>time</code>, and <code>change</code>, return <em>the <strong>second minimum time</strong> it will take to go from vertex </em><code>1</code><em> to vertex </em><code>n</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>You can go through any vertex <strong>any</strong> number of times, <strong>including</strong> <code>1</code> and <code>n</code>.</li> <li>You can assume that when the journey <strong>starts</strong>, all signals have just turned <strong>green</strong>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no duplicate edges.</li> <li>Each vertex can be reached directly or indirectly from every other vertex.</li> <li><code>1 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n bool flag[10000]{};\n unsigned int i, u, x, y, dist[10000];\n vector<int> adj[10000];\n queue<int> q;\n int meme=50;\n memset(dist, -1, n << 2);\n dist[0] = 0;\n for (auto &e: edges) {\n adj[e[0] - 1].push_back(e[1] - 1);\n adj[e[1] - 1].push_back(e[0] - 1);\n meme++;\n }\n for (q.push(0); !q.empty(); q.pop()) {\n u = q.front();\n for (int v: adj[u]) {\n if (dist[v] <= dist[u] + 1) continue;\n dist[v] = dist[u] + 1;\n q.push(v);\n }\n }\n flag[n - 1] = true;\n DFS(adj, dist, flag, n - 1);\n for (i = u = 0; !u && i < n; i++) {\n if (!flag[i]) continue;\n for (int v: adj[i]) {\n if (dist[v] == dist[i] + 1) continue;\n u |= dist[v] == dist[i];\n }\n }\n x = dist[n - 1] + (2 >> u);\n for (y = 0; x--;) {\n if (y / change & 1) y += change - y % change;\n y += time;\n }\n return y;\n }\nvector<int > ro;\nstack<int>shiv_ji;\n void DFS(vector<int> adj[], unsigned int dist[], bool flag[], int u) {\n for (int v: adj[u]) {\n if (dist[v] != dist[u] - 1) continue;\n DFS(adj, dist, flag, v);\n flag[v] = true;\n }\n }\n};", "memory": "177953" }
2,171
<p>A city is represented as a <strong>bi-directional connected</strong> graph with <code>n</code> vertices where each vertex is labeled from <code>1</code> to <code>n</code> (<strong>inclusive</strong>). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself. The time taken to traverse any edge is <code>time</code> minutes.</p> <p>Each vertex has a traffic signal which changes its color from <strong>green</strong> to <strong>red</strong> and vice versa every&nbsp;<code>change</code> minutes. All signals change <strong>at the same time</strong>. You can enter a vertex at <strong>any time</strong>, but can leave a vertex <strong>only when the signal is green</strong>. You <strong>cannot wait </strong>at a vertex if the signal is <strong>green</strong>.</p> <p>The <strong>second minimum value</strong> is defined as the smallest value<strong> strictly larger </strong>than the minimum value.</p> <ul> <li>For example the second minimum value of <code>[2, 3, 4]</code> is <code>3</code>, and the second minimum value of <code>[2, 2, 4]</code> is <code>4</code>.</li> </ul> <p>Given <code>n</code>, <code>edges</code>, <code>time</code>, and <code>change</code>, return <em>the <strong>second minimum time</strong> it will take to go from vertex </em><code>1</code><em> to vertex </em><code>n</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>You can go through any vertex <strong>any</strong> number of times, <strong>including</strong> <code>1</code> and <code>n</code>.</li> <li>You can assume that when the journey <strong>starts</strong>, all signals have just turned <strong>green</strong>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no duplicate edges.</li> <li>Each vertex can be reached directly or indirectly from every other vertex.</li> <li><code>1 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<int> adj[n+1];\n for(int i = 0;i<edges.size();i++){\n adj[edges[i][0]].push_back(edges[i][1]);\n adj[edges[i][1]].push_back(edges[i][0]);\n }\n queue<pair<int,int>> q;\n int dist[n+1],dist2[n+1];\n memset(dist,-1,sizeof(dist));\n memset(dist2,-1,sizeof(dist2));\n q.push({1,1});\n dist[1] = 0;\n while(!q.empty()){\n auto [node,count] = q.front();\n q.pop();\n int timeTaken = count == 1 ? dist[node] : dist2[node];\n if((timeTaken/change)&1){\n timeTaken = time + change*(timeTaken/change + 1);\n }else\n timeTaken += time;\n for(auto child : adj[node]){\n if(dist[child]==-1){\n dist[child] = timeTaken;\n q.push({child,1});\n }else if(dist2[child]==-1 and dist[child]<timeTaken){\n if (child == n) return timeTaken;\n dist2[child] = timeTaken;\n q.push({child,2});\n }\n }\n }\n return 0;\n }\n};", "memory": "179136" }
2,171
<p>A city is represented as a <strong>bi-directional connected</strong> graph with <code>n</code> vertices where each vertex is labeled from <code>1</code> to <code>n</code> (<strong>inclusive</strong>). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself. The time taken to traverse any edge is <code>time</code> minutes.</p> <p>Each vertex has a traffic signal which changes its color from <strong>green</strong> to <strong>red</strong> and vice versa every&nbsp;<code>change</code> minutes. All signals change <strong>at the same time</strong>. You can enter a vertex at <strong>any time</strong>, but can leave a vertex <strong>only when the signal is green</strong>. You <strong>cannot wait </strong>at a vertex if the signal is <strong>green</strong>.</p> <p>The <strong>second minimum value</strong> is defined as the smallest value<strong> strictly larger </strong>than the minimum value.</p> <ul> <li>For example the second minimum value of <code>[2, 3, 4]</code> is <code>3</code>, and the second minimum value of <code>[2, 2, 4]</code> is <code>4</code>.</li> </ul> <p>Given <code>n</code>, <code>edges</code>, <code>time</code>, and <code>change</code>, return <em>the <strong>second minimum time</strong> it will take to go from vertex </em><code>1</code><em> to vertex </em><code>n</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>You can go through any vertex <strong>any</strong> number of times, <strong>including</strong> <code>1</code> and <code>n</code>.</li> <li>You can assume that when the journey <strong>starts</strong>, all signals have just turned <strong>green</strong>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no duplicate edges.</li> <li>Each vertex can be reached directly or indirectly from every other vertex.</li> <li><code>1 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n\n vector<int>adj[10001];\n for(int i=0;i<edges.size();i++)\n {\n adj[edges[i][0]].push_back(edges[i][1]);\n adj[edges[i][1]].push_back(edges[i][0]);\n }\n\n int vis1[10001] = {0}, vis2[10001]={0}, level1[10001], level2[10001];\n queue<pair<int,int>>q;\n level1[1]=1;\n vis1[1]=1;\n q.push({1,1});\n\n while(!q.empty())\n {\n int src = q.front().first, level = q.front().second;\n q.pop();\n if(vis1[n] && vis2[n])\n break;\n\n for(auto child:adj[src])\n {\n if(vis1[child] && vis2[child])\n continue;\n if(!vis1[child])\n {\n vis1[child] = 1;\n level1[child] = level+1;\n q.push({child,level1[child]});\n }\n else if(!vis2[child] && level+1 > level1[child])\n {\n vis2[child] = 1;\n level2[child] = level+1;\n q.push({child,level2[child]});\n }\n }\n }\n\n int ans = 0;\n for(int i=1;i<level2[n];i++)\n {\n if((ans/change) % 2)\n ans=change*((ans+change)/change);\n ans+=time;\n }\n return ans;\n }\n};", "memory": "179136" }
2,171
<p>A city is represented as a <strong>bi-directional connected</strong> graph with <code>n</code> vertices where each vertex is labeled from <code>1</code> to <code>n</code> (<strong>inclusive</strong>). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself. The time taken to traverse any edge is <code>time</code> minutes.</p> <p>Each vertex has a traffic signal which changes its color from <strong>green</strong> to <strong>red</strong> and vice versa every&nbsp;<code>change</code> minutes. All signals change <strong>at the same time</strong>. You can enter a vertex at <strong>any time</strong>, but can leave a vertex <strong>only when the signal is green</strong>. You <strong>cannot wait </strong>at a vertex if the signal is <strong>green</strong>.</p> <p>The <strong>second minimum value</strong> is defined as the smallest value<strong> strictly larger </strong>than the minimum value.</p> <ul> <li>For example the second minimum value of <code>[2, 3, 4]</code> is <code>3</code>, and the second minimum value of <code>[2, 2, 4]</code> is <code>4</code>.</li> </ul> <p>Given <code>n</code>, <code>edges</code>, <code>time</code>, and <code>change</code>, return <em>the <strong>second minimum time</strong> it will take to go from vertex </em><code>1</code><em> to vertex </em><code>n</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>You can go through any vertex <strong>any</strong> number of times, <strong>including</strong> <code>1</code> and <code>n</code>.</li> <li>You can assume that when the journey <strong>starts</strong>, all signals have just turned <strong>green</strong>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no duplicate edges.</li> <li>Each vertex can be reached directly or indirectly from every other vertex.</li> <li><code>1 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<vector<int16_t>> gr(n);\n for (const auto& e : edges) {\n gr[e[0] - 1].push_back(e[1] - 1);\n gr[e[1] - 1].push_back(e[0] - 1);\n }\n\n queue<pair<int16_t, bool>> q;\n vector<int> d1(n, -1), d2(n, -1);\n\n q.emplace(0, false);\n d1[0] = 0;\n\n while (!q.empty()) {\n auto [node, isFirst] = q.front();\n q.pop();\n int tt = !isFirst ? d1[node] : d2[node];\n if ((tt / change) % 2) {\n tt = change * (tt / change + 1) + time;\n } else {\n tt += time;\n }\n\n for (const auto& neigh : gr[node]) {\n if (auto& d1v = d1[neigh]; d1v == -1) {\n d1v = tt;\n q.emplace(neigh, false);\n } else if (auto& d2v = d2[neigh]; d2v == -1 && d1v != tt) {\n if (neigh == n - 1) {\n return tt;\n }\n d2v = tt;\n q.emplace(neigh, true);\n }\n }\n }\n\n return 0;\n\n }\n};", "memory": "180318" }
2,171
<p>A city is represented as a <strong>bi-directional connected</strong> graph with <code>n</code> vertices where each vertex is labeled from <code>1</code> to <code>n</code> (<strong>inclusive</strong>). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself. The time taken to traverse any edge is <code>time</code> minutes.</p> <p>Each vertex has a traffic signal which changes its color from <strong>green</strong> to <strong>red</strong> and vice versa every&nbsp;<code>change</code> minutes. All signals change <strong>at the same time</strong>. You can enter a vertex at <strong>any time</strong>, but can leave a vertex <strong>only when the signal is green</strong>. You <strong>cannot wait </strong>at a vertex if the signal is <strong>green</strong>.</p> <p>The <strong>second minimum value</strong> is defined as the smallest value<strong> strictly larger </strong>than the minimum value.</p> <ul> <li>For example the second minimum value of <code>[2, 3, 4]</code> is <code>3</code>, and the second minimum value of <code>[2, 2, 4]</code> is <code>4</code>.</li> </ul> <p>Given <code>n</code>, <code>edges</code>, <code>time</code>, and <code>change</code>, return <em>the <strong>second minimum time</strong> it will take to go from vertex </em><code>1</code><em> to vertex </em><code>n</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>You can go through any vertex <strong>any</strong> number of times, <strong>including</strong> <code>1</code> and <code>n</code>.</li> <li>You can assume that when the journey <strong>starts</strong>, all signals have just turned <strong>green</strong>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no duplicate edges.</li> <li>Each vertex can be reached directly or indirectly from every other vertex.</li> <li><code>1 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n \n vector<int> adj[n];\n for(int i=0;i<edges.size();i++)\n {\n int u = edges[i][0]-1;\n int v = edges[i][1]-1;\n\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n\n vector<pair<int,int>> dist(n+1,{1e9,1e9});\n\n priority_queue<pair<int,int>> pq;\n\n dist[0] = {0,1e9};\n pq.push({0,0});\n\n while(!pq.empty())\n {\n auto top = pq.top();\n pq.pop();\n\n int curtime = top.first;\n int node = top.second;\n curtime*=-1;\n if(curtime>dist[node].second) continue;\n\n int newTime = curtime+time;\n int temp = curtime/change;\n\n if(temp%2==1)\n newTime = (temp+1)*change + time;\n \n for(auto adjNode: adj[node])\n {\n if(dist[adjNode].first>newTime){\n dist[adjNode].second = dist[adjNode].first;\n dist[adjNode].first = newTime;\n\n pq.push({-1*dist[adjNode].first, adjNode});\n }\n else if(dist[adjNode].first==newTime)\n continue;\n else if(dist[adjNode].second > newTime)\n {\n dist[adjNode].second = newTime;\n pq.push({-1*dist[adjNode].second,adjNode});\n }\n }\n }\n return dist[n-1].second;\n }\n};", "memory": "180318" }
2,171
<p>A city is represented as a <strong>bi-directional connected</strong> graph with <code>n</code> vertices where each vertex is labeled from <code>1</code> to <code>n</code> (<strong>inclusive</strong>). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself. The time taken to traverse any edge is <code>time</code> minutes.</p> <p>Each vertex has a traffic signal which changes its color from <strong>green</strong> to <strong>red</strong> and vice versa every&nbsp;<code>change</code> minutes. All signals change <strong>at the same time</strong>. You can enter a vertex at <strong>any time</strong>, but can leave a vertex <strong>only when the signal is green</strong>. You <strong>cannot wait </strong>at a vertex if the signal is <strong>green</strong>.</p> <p>The <strong>second minimum value</strong> is defined as the smallest value<strong> strictly larger </strong>than the minimum value.</p> <ul> <li>For example the second minimum value of <code>[2, 3, 4]</code> is <code>3</code>, and the second minimum value of <code>[2, 2, 4]</code> is <code>4</code>.</li> </ul> <p>Given <code>n</code>, <code>edges</code>, <code>time</code>, and <code>change</code>, return <em>the <strong>second minimum time</strong> it will take to go from vertex </em><code>1</code><em> to vertex </em><code>n</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>You can go through any vertex <strong>any</strong> number of times, <strong>including</strong> <code>1</code> and <code>n</code>.</li> <li>You can assume that when the journey <strong>starts</strong>, all signals have just turned <strong>green</strong>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no duplicate edges.</li> <li>Each vertex can be reached directly or indirectly from every other vertex.</li> <li><code>1 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n void print(vector<pair<int,int>>& v){\n cout<<\"here\\n\";\n for(int i=1; i<v.size(); ++i){\n cout<<i<<\"#\"<<v[i].first<<\",\"<<v[i].second<<\"$\";\n }\n cout<<\"here\\n\";\n }\n\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<int> adj[n+1];\n for(auto& pr : edges){\n adj[pr[0]].push_back(pr[1]);\n adj[pr[1]].push_back(pr[0]);\n }\n vector<pair<int,int>> dist(n+1, {1e9, 1e9});\n dist[1] = {0, 1e9};\n\n // print(dist);\n\n queue<pair<int,int>> q;\n q.push({1, 0});\n while(!q.empty()){\n auto pr = q.front(); q.pop();\n int node = pr.first, wt = pr.second;\n // signal\n int mulFactor = wt%change;\n mulFactor = (wt - mulFactor)/change;\n if(mulFactor & 1) wt = change*(mulFactor+1);\n\n for(auto& child : adj[node]){\n if(dist[child].first > wt+time){\n swap(dist[child].first, dist[child].second);\n dist[child].first = wt+time;\n q.push({child, wt+time});\n // print(dist);\n }\n else if(dist[child].second > wt+time && dist[child].first != wt+time){\n dist[child].second = wt+time;\n q.push({child, wt+time});\n // print(dist);\n }\n }\n }\n return dist[n].second;\n }\n};", "memory": "181501" }
2,171
<p>A city is represented as a <strong>bi-directional connected</strong> graph with <code>n</code> vertices where each vertex is labeled from <code>1</code> to <code>n</code> (<strong>inclusive</strong>). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself. The time taken to traverse any edge is <code>time</code> minutes.</p> <p>Each vertex has a traffic signal which changes its color from <strong>green</strong> to <strong>red</strong> and vice versa every&nbsp;<code>change</code> minutes. All signals change <strong>at the same time</strong>. You can enter a vertex at <strong>any time</strong>, but can leave a vertex <strong>only when the signal is green</strong>. You <strong>cannot wait </strong>at a vertex if the signal is <strong>green</strong>.</p> <p>The <strong>second minimum value</strong> is defined as the smallest value<strong> strictly larger </strong>than the minimum value.</p> <ul> <li>For example the second minimum value of <code>[2, 3, 4]</code> is <code>3</code>, and the second minimum value of <code>[2, 2, 4]</code> is <code>4</code>.</li> </ul> <p>Given <code>n</code>, <code>edges</code>, <code>time</code>, and <code>change</code>, return <em>the <strong>second minimum time</strong> it will take to go from vertex </em><code>1</code><em> to vertex </em><code>n</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>You can go through any vertex <strong>any</strong> number of times, <strong>including</strong> <code>1</code> and <code>n</code>.</li> <li>You can assume that when the journey <strong>starts</strong>, all signals have just turned <strong>green</strong>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no duplicate edges.</li> <li>Each vertex can be reached directly or indirectly from every other vertex.</li> <li><code>1 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
0
{ "code": "\nint solve(int n,vector<vector<int>>&adj){\n int dist[n+3];\n for(int i=0;i<=n;i++) dist[i]=1e7;\n dist[1]=0;\n queue<int> q;\n q.push(1);\n while(!q.empty())\n {\n int u=q.front();\n q.pop();\n for(int i=0;i<adj[u].size();i++)\n {\n int v=adj[u][i];\n if(dist[v]>dist[u]+1) \n {\n dist[v]=dist[u]+1;\n q.push(v);\n }\n }\n }\n\n int mini=dist[n];\n\n while(!q.empty()) q.pop();\n q.push(n);\n\n while(!q.empty())\n {\n int u=q.front();\n q.pop();\n for(int i=0;i<adj[u].size();i++)\n {\n int v=adj[u][i];\n if(dist[u]==dist[v]) return mini+1;\n if(dist[u]-1==dist[v]) q.push(v);\n }\n }\n return mini+2;\n\n}\n\nint ceel(int a,int b){\n if(a%b!=0) return 1+a/b;\n return a/b;\n}\nclass Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int ti, int ch) {\n vector<vector<int>> adj(n+2);\n for(int i=0;i<edges.size();i++)\n {\n int u=edges[i][0];\n int v=edges[i][1];\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n int d=solve(n,adj);\n int t=0;\n cout<<d<<\"\\n\";\n for(int i=1;i<=d;i++)\n {\n t+=ti;\n int t1=ceel(t+1,ch);\n if(t1%2==0 && i!=d)\n {\n t+=(ch-t%ch);\n }\n else\n {\n // if(t%ch==0 && i!=d) t+=ch; \n // green good to go \n }\n cout<<i<<\" \"<<t<<\"\\n\";\n } \n return t;\n // cout<<d<<\"\\n\";\n // if(ch<=ti)\n // {\n // int t1=ceel(ti,ch);\n // if(t1%2==0)\n // {\n\n // }\n // int t2=ceel(d,t1);\n // cout<<t1<<\" \"<<(t2-1)*2*ch<<\"\\n\";\n // return (t2-1)*2*ch+(d%t1)*ti;\n // }\n // int t1=ceel(ch,ti);\n // int t2=ceel(d,t1);\n // cout<<t1<<\" \"<<(t2-1)*2*ch<<\"\\n\";\n // return (t2-1)*2*ch+(d%t1)*ti;\n }\n};", "memory": "181501" }
2,171
<p>A city is represented as a <strong>bi-directional connected</strong> graph with <code>n</code> vertices where each vertex is labeled from <code>1</code> to <code>n</code> (<strong>inclusive</strong>). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself. The time taken to traverse any edge is <code>time</code> minutes.</p> <p>Each vertex has a traffic signal which changes its color from <strong>green</strong> to <strong>red</strong> and vice versa every&nbsp;<code>change</code> minutes. All signals change <strong>at the same time</strong>. You can enter a vertex at <strong>any time</strong>, but can leave a vertex <strong>only when the signal is green</strong>. You <strong>cannot wait </strong>at a vertex if the signal is <strong>green</strong>.</p> <p>The <strong>second minimum value</strong> is defined as the smallest value<strong> strictly larger </strong>than the minimum value.</p> <ul> <li>For example the second minimum value of <code>[2, 3, 4]</code> is <code>3</code>, and the second minimum value of <code>[2, 2, 4]</code> is <code>4</code>.</li> </ul> <p>Given <code>n</code>, <code>edges</code>, <code>time</code>, and <code>change</code>, return <em>the <strong>second minimum time</strong> it will take to go from vertex </em><code>1</code><em> to vertex </em><code>n</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>You can go through any vertex <strong>any</strong> number of times, <strong>including</strong> <code>1</code> and <code>n</code>.</li> <li>You can assume that when the journey <strong>starts</strong>, all signals have just turned <strong>green</strong>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no duplicate edges.</li> <li>Each vertex can be reached directly or indirectly from every other vertex.</li> <li><code>1 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n // if the shortest path from 1 to n is of length L\n // find whether there is a path of length L+1\n // there is always a path of length L+2\n \n vector<vector<int>> adj(n);\n for (auto& e : edges) {\n int u = e[0]-1, v = e[1]-1;\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n \n // bfs from goal\n vector<int> d(n, 1e9);\n d[n-1] = 0;\n queue<int> q;\n q.push(n-1);\n while (!q.empty()) {\n int cur = q.front(); q.pop();\n for (auto nei : adj[cur]) {\n if (d[nei] == 1e9) {\n d[nei] = d[cur] + 1;\n q.push(nei);\n }\n }\n }\n \n // check the existence of a path with length = d[0]+1\n int len = d[0] + 2;\n q.push(0);\n bool done = false;\n while (!q.empty()) {\n int cur = q.front(); q.pop();\n for (auto nei : adj[cur]) {\n if (d[nei] == d[cur]) {\n len--;\n done = true;\n break;\n } else if (d[nei] == d[cur] - 1) {\n q.push(nei);\n }\n }\n if (done) break;\n }\n \n // calculate the time needed\n // light : green in [0, c), [2c, 3c), ... \n // red in [c, 2c), [3c, 4c), ...\n int currTime = 0;\n //cout << len << '\\n';\n for (int i = 0; i < len; i++) {\n\t\t\tif ((currTime / change) % 2 == 1) // have to wait until the signal turns into green\n currTime = ((currTime / change) + 1) * change; \n currTime += time;\n }\n return currTime;\n }\n};", "memory": "182683" }
2,171
<p>A city is represented as a <strong>bi-directional connected</strong> graph with <code>n</code> vertices where each vertex is labeled from <code>1</code> to <code>n</code> (<strong>inclusive</strong>). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself. The time taken to traverse any edge is <code>time</code> minutes.</p> <p>Each vertex has a traffic signal which changes its color from <strong>green</strong> to <strong>red</strong> and vice versa every&nbsp;<code>change</code> minutes. All signals change <strong>at the same time</strong>. You can enter a vertex at <strong>any time</strong>, but can leave a vertex <strong>only when the signal is green</strong>. You <strong>cannot wait </strong>at a vertex if the signal is <strong>green</strong>.</p> <p>The <strong>second minimum value</strong> is defined as the smallest value<strong> strictly larger </strong>than the minimum value.</p> <ul> <li>For example the second minimum value of <code>[2, 3, 4]</code> is <code>3</code>, and the second minimum value of <code>[2, 2, 4]</code> is <code>4</code>.</li> </ul> <p>Given <code>n</code>, <code>edges</code>, <code>time</code>, and <code>change</code>, return <em>the <strong>second minimum time</strong> it will take to go from vertex </em><code>1</code><em> to vertex </em><code>n</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>You can go through any vertex <strong>any</strong> number of times, <strong>including</strong> <code>1</code> and <code>n</code>.</li> <li>You can assume that when the journey <strong>starts</strong>, all signals have just turned <strong>green</strong>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no duplicate edges.</li> <li>Each vertex can be reached directly or indirectly from every other vertex.</li> <li><code>1 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<int> adj[n + 1];\n for (auto& it : edges) {\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n vector<int> minimum_time(n + 1, INT_MAX);\n vector<int> second_minimum_time(n + 1, INT_MAX);\n priority_queue<pair<int, int>, vector<pair<int, int>>,greater<pair<int, int>>>q;\n q.push({1, 0});\n minimum_time[1] = 0;\n\n while (!q.empty()) {\n int node = q.top().first;\n int t = q.top().second;\n q.pop();\n\n int new_change = t / change;\n int wait_time =\n (new_change % 2 == 1) ? (new_change + 1) * change : t;\n int new_time = wait_time + time;\n\n for (auto it : adj[node]) {\n if (new_time < minimum_time[it]) {\n second_minimum_time[it] = minimum_time[it];\n minimum_time[it] = new_time;\n q.push({it, new_time});\n } else if (new_time > minimum_time[it] &&\n new_time < second_minimum_time[it]) {\n second_minimum_time[it] = new_time;\n q.push({it, new_time});\n }\n }\n }\n\n return second_minimum_time[n];\n }\n};", "memory": "182683" }
2,171
<p>A city is represented as a <strong>bi-directional connected</strong> graph with <code>n</code> vertices where each vertex is labeled from <code>1</code> to <code>n</code> (<strong>inclusive</strong>). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself. The time taken to traverse any edge is <code>time</code> minutes.</p> <p>Each vertex has a traffic signal which changes its color from <strong>green</strong> to <strong>red</strong> and vice versa every&nbsp;<code>change</code> minutes. All signals change <strong>at the same time</strong>. You can enter a vertex at <strong>any time</strong>, but can leave a vertex <strong>only when the signal is green</strong>. You <strong>cannot wait </strong>at a vertex if the signal is <strong>green</strong>.</p> <p>The <strong>second minimum value</strong> is defined as the smallest value<strong> strictly larger </strong>than the minimum value.</p> <ul> <li>For example the second minimum value of <code>[2, 3, 4]</code> is <code>3</code>, and the second minimum value of <code>[2, 2, 4]</code> is <code>4</code>.</li> </ul> <p>Given <code>n</code>, <code>edges</code>, <code>time</code>, and <code>change</code>, return <em>the <strong>second minimum time</strong> it will take to go from vertex </em><code>1</code><em> to vertex </em><code>n</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>You can go through any vertex <strong>any</strong> number of times, <strong>including</strong> <code>1</code> and <code>n</code>.</li> <li>You can assume that when the journey <strong>starts</strong>, all signals have just turned <strong>green</strong>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no duplicate edges.</li> <li>Each vertex can be reached directly or indirectly from every other vertex.</li> <li><code>1 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n queue<pair<int, int>> Q;\n vector<bool> Status(n+1);\n vector<vector<int>> Adj(n+1);\n vector<int> Dist(n+1);\n for (auto & edge : edges)\n {\n Adj[edge[0]].push_back(edge[1]);\n Adj[edge[1]].push_back(edge[0]);\n }\n Q.push({1, 0});\n Status[1] = true;\n Dist[1] = 0;\n\n vector<bool> HasCrossEdgeOnShortestPath(n+1);\n int shortestDist = 0;\n while (!Q.empty())\n {\n auto [node, dist] = Q.front();\n Q.pop();\n if (node == n)\n shortestDist = dist;\n\n for (int adj : Adj[node])\n {\n if (Status[adj] == false)\n {\n Status[adj] = true;\n Q.push({adj, dist + 1});\n Dist[adj] = dist + 1;\n if (HasCrossEdgeOnShortestPath[node])\n HasCrossEdgeOnShortestPath[adj] = true;\n }\n else\n if (Dist[adj] == dist)\n {\n //on same bfs layer, so we have path with +1 edge\n HasCrossEdgeOnShortestPath[adj] = true;\n HasCrossEdgeOnShortestPath[node] = true;\n }\n else\n if (Dist[adj] == dist + 1 && HasCrossEdgeOnShortestPath[node])\n {\n HasCrossEdgeOnShortestPath[adj] = true;\n }\n else\n if (Dist[adj] == dist - 1 && HasCrossEdgeOnShortestPath[adj])\n {\n HasCrossEdgeOnShortestPath[node] = true;\n }\n }\n }\n int secondDist = HasCrossEdgeOnShortestPath[n] ? shortestDist + 1 : shortestDist + 2;\n \n int totalTime = 0;\n for (int i = 0; i < secondDist; ++i)\n {\n int changes = totalTime / change;\n if (changes % 2 == 1) //wait till green\n totalTime = (changes + 1) * change;\n totalTime += time;\n }\n return totalTime;\n }\n};", "memory": "183866" }
2,171
<p>A city is represented as a <strong>bi-directional connected</strong> graph with <code>n</code> vertices where each vertex is labeled from <code>1</code> to <code>n</code> (<strong>inclusive</strong>). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself. The time taken to traverse any edge is <code>time</code> minutes.</p> <p>Each vertex has a traffic signal which changes its color from <strong>green</strong> to <strong>red</strong> and vice versa every&nbsp;<code>change</code> minutes. All signals change <strong>at the same time</strong>. You can enter a vertex at <strong>any time</strong>, but can leave a vertex <strong>only when the signal is green</strong>. You <strong>cannot wait </strong>at a vertex if the signal is <strong>green</strong>.</p> <p>The <strong>second minimum value</strong> is defined as the smallest value<strong> strictly larger </strong>than the minimum value.</p> <ul> <li>For example the second minimum value of <code>[2, 3, 4]</code> is <code>3</code>, and the second minimum value of <code>[2, 2, 4]</code> is <code>4</code>.</li> </ul> <p>Given <code>n</code>, <code>edges</code>, <code>time</code>, and <code>change</code>, return <em>the <strong>second minimum time</strong> it will take to go from vertex </em><code>1</code><em> to vertex </em><code>n</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>You can go through any vertex <strong>any</strong> number of times, <strong>including</strong> <code>1</code> and <code>n</code>.</li> <li>You can assume that when the journey <strong>starts</strong>, all signals have just turned <strong>green</strong>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no duplicate edges.</li> <li>Each vertex can be reached directly or indirectly from every other vertex.</li> <li><code>1 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change)\n {\n vector<vector<int>> adj(n);\n for(auto& edge: edges)\n {\n adj[edge[0] - 1].push_back(edge[1] - 1);\n adj[edge[1] - 1].push_back(edge[0] - 1);\n }\n int end = n - 1;\n queue<int> q;\n vector<int> firstTime(n, -1);\n vector<int> secondTime(n, -1);\n firstTime[0] = 0;\n q.push(0);\n int t = 0;\n while(q.size())\n {\n if(t / change % 2)\n {\n t += change - t % change;\n }\n t += time;\n int size = q.size();\n while(size--)\n {\n int cur = q.front();\n q.pop();\n for(auto& next: adj[cur])\n {\n if(firstTime[next] == -1)\n {\n firstTime[next] = t;\n q.push(next);\n }\n else if(secondTime[next] == -1 && firstTime[next] != t)\n {\n if(next == end)\n {\n return t;\n }\n secondTime[next] = t;\n q.push(next);\n }\n }\n }\n }\n return 0;\n }\n};", "memory": "183866" }
2,171
<p>A city is represented as a <strong>bi-directional connected</strong> graph with <code>n</code> vertices where each vertex is labeled from <code>1</code> to <code>n</code> (<strong>inclusive</strong>). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself. The time taken to traverse any edge is <code>time</code> minutes.</p> <p>Each vertex has a traffic signal which changes its color from <strong>green</strong> to <strong>red</strong> and vice versa every&nbsp;<code>change</code> minutes. All signals change <strong>at the same time</strong>. You can enter a vertex at <strong>any time</strong>, but can leave a vertex <strong>only when the signal is green</strong>. You <strong>cannot wait </strong>at a vertex if the signal is <strong>green</strong>.</p> <p>The <strong>second minimum value</strong> is defined as the smallest value<strong> strictly larger </strong>than the minimum value.</p> <ul> <li>For example the second minimum value of <code>[2, 3, 4]</code> is <code>3</code>, and the second minimum value of <code>[2, 2, 4]</code> is <code>4</code>.</li> </ul> <p>Given <code>n</code>, <code>edges</code>, <code>time</code>, and <code>change</code>, return <em>the <strong>second minimum time</strong> it will take to go from vertex </em><code>1</code><em> to vertex </em><code>n</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>You can go through any vertex <strong>any</strong> number of times, <strong>including</strong> <code>1</code> and <code>n</code>.</li> <li>You can assume that when the journey <strong>starts</strong>, all signals have just turned <strong>green</strong>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no duplicate edges.</li> <li>Each vertex can be reached directly or indirectly from every other vertex.</li> <li><code>1 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n vector<int> get_min_distances(vector<vector<int>>& G) {\n int n = G.size();\n vector<int> dist(n, 1000000);\n vector<int> visited(n);\n dist[0] = 0;\n\n std::priority_queue<std::pair<int, int>> q;\n q.emplace(0, 0);\n\n while (!q.empty()) {\n auto [d, v] = q.top();\n q.pop();\n if (visited[v]) continue;\n\n visited[v] = true;\n\n for (auto w : G[v]) {\n if (dist[v] + 1 < dist[w]) {\n dist[w] = dist[v] + 1;\n q.emplace(-dist[w], w);\n }\n }\n }\n return dist;\n }\n\n bool get_is_on_shortest_path(vector<vector<int>>& G, vector<int>& dist) {\n vector<int> q;\n vector<int> visited(G.size());\n\n q.push_back(G.size() - 1);\n\n while (!q.empty()) {\n int v = q.back();\n q.pop_back();\n\n if (visited[v]) continue;\n visited[v] = true;\n\n for (auto w : G[v]) {\n if (dist[w] == dist[v] - 1) {\n q.push_back(w);\n }\n\n if (dist[w] == dist[v]) {\n return true;\n }\n }\n\n }\n return false;\n }\n\n int convert_to_ans(int n_steps, int time, int change) {\n int cur_time = 0;\n for (int i = 0; i < n_steps; ++i) {\n int d = cur_time % (2 * change);\n if (d >= change) {\n cur_time += 2 * change - d;\n }\n // std::cout << \"cur_time: \" << cur_time << std::endl;\n cur_time += time;\n }\n return cur_time;\n }\n\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<vector<int>> G(n, vector<int>());\n\n for (auto& e : edges) {\n int v = e[0] - 1;\n int w = e[1] - 1;\n\n G[v].push_back(w);\n G[w].push_back(v);\n }\n\n auto min_distance = get_min_distances(G);\n bool has_one = get_is_on_shortest_path(G, min_distance);\n\n // for (int i = 0; i < n; ++i) {\n // std::cout << i << \" \" << min_distance[i] << std::endl;\n // }\n\n int second_min_dist = min_distance.back();\n if (has_one) {\n second_min_dist += 1;\n } else {\n second_min_dist += 2;\n }\n\n // std::cout << \"min distance: \" << min_distance.back() << std::endl;\n // std::cout << \"second_min_dist: \" << second_min_dist << std::endl;\n\n return convert_to_ans(second_min_dist, time, change);\n \n }\n};", "memory": "185048" }
2,171
<p>A city is represented as a <strong>bi-directional connected</strong> graph with <code>n</code> vertices where each vertex is labeled from <code>1</code> to <code>n</code> (<strong>inclusive</strong>). The edges in the graph are represented as a 2D integer array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes a bi-directional edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by <strong>at most one</strong> edge, and no vertex has an edge to itself. The time taken to traverse any edge is <code>time</code> minutes.</p> <p>Each vertex has a traffic signal which changes its color from <strong>green</strong> to <strong>red</strong> and vice versa every&nbsp;<code>change</code> minutes. All signals change <strong>at the same time</strong>. You can enter a vertex at <strong>any time</strong>, but can leave a vertex <strong>only when the signal is green</strong>. You <strong>cannot wait </strong>at a vertex if the signal is <strong>green</strong>.</p> <p>The <strong>second minimum value</strong> is defined as the smallest value<strong> strictly larger </strong>than the minimum value.</p> <ul> <li>For example the second minimum value of <code>[2, 3, 4]</code> is <code>3</code>, and the second minimum value of <code>[2, 2, 4]</code> is <code>4</code>.</li> </ul> <p>Given <code>n</code>, <code>edges</code>, <code>time</code>, and <code>change</code>, return <em>the <strong>second minimum time</strong> it will take to go from vertex </em><code>1</code><em> to vertex </em><code>n</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>You can go through any vertex <strong>any</strong> number of times, <strong>including</strong> <code>1</code> and <code>n</code>.</li> <li>You can assume that when the journey <strong>starts</strong>, all signals have just turned <strong>green</strong>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e1.png" style="width: 200px; height: 250px;" /> &emsp; &emsp; &emsp; &emsp;<img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/e2.png" style="width: 200px; height: 250px;" /> <pre> <strong>Input:</strong> n = 5, edges = [[1,2],[1,3],[1,4],[3,4],[4,5]], time = 3, change = 5 <strong>Output:</strong> 13 <strong>Explanation:</strong> The figure on the left shows the given graph. The blue path in the figure on the right is the minimum time path. The time taken is: - Start at 1, time elapsed=0 - 1 -&gt; 4: 3 minutes, time elapsed=3 - 4 -&gt; 5: 3 minutes, time elapsed=6 Hence the minimum time needed is 6 minutes. The red path shows the path to get the second minimum time. - Start at 1, time elapsed=0 - 1 -&gt; 3: 3 minutes, time elapsed=3 - 3 -&gt; 4: 3 minutes, time elapsed=6 - Wait at 4 for 4 minutes, time elapsed=10 - 4 -&gt; 5: 3 minutes, time elapsed=13 Hence the second minimum time is 13 minutes. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/29/eg2.png" style="width: 225px; height: 50px;" /> <pre> <strong>Input:</strong> n = 2, edges = [[1,2]], time = 3, change = 2 <strong>Output:</strong> 11 <strong>Explanation:</strong> The minimum time path is 1 -&gt; 2 with time = 3 minutes. The second minimum time path is 1 -&gt; 2 -&gt; 1 -&gt; 2 with time = 11 minutes.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>n - 1 &lt;= edges.length &lt;= min(2 * 10<sup>4</sup>, n * (n - 1) / 2)</code></li> <li><code>edges[i].length == 2</code></li> <li><code>1 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt;= n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no duplicate edges.</li> <li>Each vertex can be reached directly or indirectly from every other vertex.</li> <li><code>1 &lt;= time, change &lt;= 10<sup>3</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int calcTime(int t, int change, int time) {\n int i = t / change;\n if(i % 2 == 0)\n return t + time;\n return t + time + change-(t%change);\n }\n int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {\n vector<vector<int>> adj(n+1);\n vector<int> dist(n+1, INT_MAX);\n vector<bool> visited(n+1);\n\n for(vector<int> &edge : edges) {\n adj[edge[0]].push_back(edge[1]);\n adj[edge[1]].push_back(edge[0]);\n }\n\n queue<pair<int, int>> q;\n priority_queue<int> pq;\n\n q.push({1, 0});\n visited[1] = 1;\n while(!q.empty()) {\n pair<int, int> curr = q.front(); q.pop();\n int node = curr.first;\n int t = curr.second;\n\n if(visited[n])\n break;\n\n int time_neighbour = calcTime(t, change, time);\n for(int nb : adj[node]) {\n if(dist[nb] == INT_MAX) {\n dist[nb] = time_neighbour;\n q.push({nb, time_neighbour});\n }\n else if(!visited[nb] and time_neighbour > dist[nb]) {\n dist[nb] = time_neighbour;\n visited[nb] = 1;\n q.push({nb, time_neighbour});\n }\n\n if(nb == n)\n cout<<dist[n]<<endl;\n }\n }\n cout<<visited[n]<<endl;\n return dist[n];\n }\n};", "memory": "185048" }