id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A <strong>subarray</strong> is a contiguous sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,2,3,3], k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The subarrays that contain the element 3 at least 2 times are: [1,3,2,3], [1,3,2,3,3], [3,2,3], [3,2,3,3], [2,3,3] and [3,3]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,2,1], k = 3 <strong>Output:</strong> 0 <strong>Explanation:</strong> No subarray contains the element 4 at least 3 times. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\nlong long countSubarrays(vector<int>& a, int k) {\n int n=a.size();\n vector<long long> maxIndex;\n int maxIdx=0;\n for (int i=0; i<n; i++) {\n if (a[maxIdx]<a[i]) maxIdx=i;\n }\n for (int i=0; i<n; i++) {\n if (a[i]==a[maxIdx]) maxIndex.push_back(i);\n }\n long long res=0;\n maxIndex.push_back(n);\n for (int i=k-1; i<maxIndex.size()-1; i++) {\n res+=(maxIndex[i+1]-maxIndex[i])*(maxIndex[i-k+1]+1);\n }\n return res;\n}\n};", "memory": "127900" }
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A <strong>subarray</strong> is a contiguous sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,2,3,3], k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The subarrays that contain the element 3 at least 2 times are: [1,3,2,3], [1,3,2,3,3], [3,2,3], [3,2,3,3], [2,3,3] and [3,3]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,2,1], k = 3 <strong>Output:</strong> 0 <strong>Explanation:</strong> No subarray contains the element 4 at least 3 times. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n int n = nums.size();\n int maxx = *max_element(nums.begin(), nums.end());\n vector<int> prefixes, suffixes;\n int count = 0, total = 0;\n for (int i = 0; i < n; i++) {\n count++;\n if (nums[i] == maxx) {\n prefixes.push_back(count);\n count = 0;\n total++;\n }\n }\n\n if (total < k) return 0;\n for (int i = n-1; i >= 0; i--) {\n if (nums[i] == maxx) suffixes.push_back(n-i);\n }\n\n int64_t summ = 0;\n for (int i = 0; i < total-k+1; i++){\n summ += (int64_t) prefixes[i] * (int64_t) suffixes[total-k-i];\n }\n return summ;\n }\n};", "memory": "128199" }
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A <strong>subarray</strong> is a contiguous sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,2,3,3], k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The subarrays that contain the element 3 at least 2 times are: [1,3,2,3], [1,3,2,3,3], [3,2,3], [3,2,3,3], [2,3,3] and [3,3]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,2,1], k = 3 <strong>Output:</strong> 0 <strong>Explanation:</strong> No subarray contains the element 4 at least 3 times. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n int maxVal = *std::max_element(nums.begin(), nums.end());\n int len = nums.size();\n vector<long long> prefix(len);\n prefix[0] = nums[0] == maxVal;\n for (int i = 1; i < len; i++) {\n prefix[i] = prefix[i-1] + (nums[i] == maxVal);\n }\n\n long long totalCount = 0;\n int left = -1, right = 0;\n while (right < len && left < right) {\n int leftCount = left == -1 ? 0 : prefix[left];\n int rightCount = prefix[right];\n if (rightCount-leftCount >= k) {\n totalCount += len-right;\n left ++;\n if (left == right && right < len) {\n right ++;\n }\n } else {\n right ++;\n }\n }\n\n return totalCount;\n }\n};", "memory": "130100" }
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A <strong>subarray</strong> is a contiguous sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,2,3,3], k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The subarrays that contain the element 3 at least 2 times are: [1,3,2,3], [1,3,2,3,3], [3,2,3], [3,2,3,3], [2,3,3] and [3,3]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,2,1], k = 3 <strong>Output:</strong> 0 <strong>Explanation:</strong> No subarray contains the element 4 at least 3 times. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n \n int n=nums.size(), i=0, mx=0, f=0;\n long long ans=0;\n vector<long long> dp(n, 0);\n\n for(int j=0; j<nums.size(); j++)\n {\n mx=max(mx, nums[j]);\n }\n \n for(int j=0; j<nums.size(); j++)\n {\n if(nums[j]==mx)\n {\n f++;\n }\n\n long long cnt=0;\n\n while(f>=k)\n {\n cnt++;\n\n if(nums[i]==mx)\n {\n f--;\n }\n\n i++;\n }\n\n dp[j]=cnt;\n\n if(j-1>=0)\n {\n dp[j]+=dp[j-1];\n }\n\n ans+=dp[j];\n }\n\n return ans;\n \n // int i=0, mx=0, f=0;\n // long long cnt=0;\n\n // for(int j=0; j<nums.size(); j++)\n // {\n // mx=max(mx, nums[j]);\n // }\n \n // for(int j=0; j<nums.size(); j++)\n // {\n // if(nums[j]==mx)\n // {\n // f++;\n // }\n\n // while(f>=k)\n // {\n // cnt += nums.size()-j; // nums.size()-j ae i start thata valid subarr. ape\n\n // if(nums[i]==mx)\n // {\n // f--;\n // }\n\n // i++;\n // }\n // }\n\n // return cnt;\n }\n};", "memory": "130300" }
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A <strong>subarray</strong> is a contiguous sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,2,3,3], k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The subarrays that contain the element 3 at least 2 times are: [1,3,2,3], [1,3,2,3,3], [3,2,3], [3,2,3,3], [2,3,3] and [3,3]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,2,1], k = 3 <strong>Output:</strong> 0 <strong>Explanation:</strong> No subarray contains the element 4 at least 3 times. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n \n int n=nums.size(), i=0, mx=0, f=0;\n long long ans=0;\n vector<long long> dp(n, 0);\n\n for(int j=0; j<nums.size(); j++)\n {\n mx=max(mx, nums[j]);\n }\n \n for(int j=0; j<nums.size(); j++)\n {\n if(nums[j]==mx)\n {\n f++;\n }\n\n long long cnt=0;\n\n while(f>=k)\n {\n cnt++;\n\n if(nums[i]==mx)\n {\n f--;\n }\n\n i++;\n }\n\n dp[j]=cnt;\n\n if(j-1>=0)\n {\n dp[j]+=dp[j-1];\n }\n\n ans+=dp[j];\n }\n\n return ans;\n \n /*\n int i=0, mx=0, f=0;\n long long cnt=0;\n\n for(int j=0; j<nums.size(); j++)\n {\n mx=max(mx, nums[j]);\n }\n \n for(int j=0; j<nums.size(); j++)\n {\n if(nums[j]==mx)\n {\n f++;\n }\n\n while(f>=k)\n {\n cnt += nums.size()-j; // nums.size()-j ae i start thata valid subarr. ape\n\n if(nums[i]==mx)\n {\n f--;\n }\n\n i++;\n }\n }\n\n return cnt;\n */\n }\n};", "memory": "130400" }
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A <strong>subarray</strong> is a contiguous sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,2,3,3], k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The subarrays that contain the element 3 at least 2 times are: [1,3,2,3], [1,3,2,3,3], [3,2,3], [3,2,3,3], [2,3,3] and [3,3]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,2,1], k = 3 <strong>Output:</strong> 0 <strong>Explanation:</strong> No subarray contains the element 4 at least 3 times. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n \n int n = nums.size();\n\n vector<long long> sumArr(n, 0);\n\n int maxNum = nums[0];\n\n for(int i = 1 ; i < n ; i++){\n\n maxNum = max(maxNum, nums[i]);\n \n }\n\n\n for(int i = 0 ; i < n ; i++){\n\n if(nums[i] == maxNum) sumArr[i]++;\n if(i > 0){\n sumArr[i] += sumArr[i - 1];\n }\n \n }\n\n\n long long ans = 0;\n\n for(int i = 0 ; i < n ; i++){\n\n if(i > 0){\n auto it = lower_bound(sumArr.begin(), sumArr.end(), k + sumArr[i - 1]);\n\n if(it != sumArr.end()){\n int index = it - sumArr.begin();\n ans += n - index;\n }\n }\n else{\n auto it = lower_bound(sumArr.begin(), sumArr.end(), k);\n\n if(it != sumArr.end()){\n int index = it - sumArr.begin();\n ans += n - index;\n }\n } \n \n \n }\n\n return ans;\n }\n};", "memory": "130500" }
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A <strong>subarray</strong> is a contiguous sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,2,3,3], k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The subarrays that contain the element 3 at least 2 times are: [1,3,2,3], [1,3,2,3,3], [3,2,3], [3,2,3,3], [2,3,3] and [3,3]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,2,1], k = 3 <strong>Output:</strong> 0 <strong>Explanation:</strong> No subarray contains the element 4 at least 3 times. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n \n int n = nums.size();\n\n vector<long long> sumArr(n, 0);\n\n int maxNum = nums[0];\n\n for(int i = 1 ; i < n ; i++){\n\n maxNum = max(maxNum, nums[i]);\n \n }\n\n\n for(int i = 0 ; i < n ; i++){\n\n if(nums[i] == maxNum) sumArr[i]++;\n if(i > 0){\n sumArr[i] += sumArr[i - 1];\n }\n \n }\n\n\n long long ans = 0;\n\n for(int i = 0 ; i < n ; i++){\n\n if(i > 0){\n auto it = lower_bound(sumArr.begin(), sumArr.end(), k + sumArr[i - 1]);\n\n if(it != sumArr.end()){\n int index = it - sumArr.begin();\n ans += n - index;\n }\n }\n else{\n auto it = lower_bound(sumArr.begin(), sumArr.end(), k);\n\n if(it != sumArr.end()){\n int index = it - sumArr.begin();\n ans += n - index;\n }\n } \n \n \n }\n\n return ans;\n }\n};", "memory": "130500" }
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A <strong>subarray</strong> is a contiguous sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,2,3,3], k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The subarrays that contain the element 3 at least 2 times are: [1,3,2,3], [1,3,2,3,3], [3,2,3], [3,2,3,3], [2,3,3] and [3,3]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,2,1], k = 3 <strong>Output:</strong> 0 <strong>Explanation:</strong> No subarray contains the element 4 at least 3 times. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n int target=*max_element(nums.begin(),nums.end()),n=nums.size();\n vector<int> p(n,0);\n // if(n==1)return 1ll;\n if(nums[0]==target)p[0]=1;\n long long ans=0;\n vector<int> mp(n+1,0);\n mp[0]=-1;\n for(int i=1;i<n;i++){p[i]=p[i-1]+(nums[i]==target);}\n // mp[0]=0;\n for(int i=0;i<n;i++){\n if(p[i]==0){\n mp[0]=i;\n }else{\n break;\n }\n }\n // cout<<mp[0]<<endl;\n for(int i=0;i<n;i++){\n // cout<<p[i]<<\" \";\n int value=p[i]-k;\n // if(value==0){\n // ans++;\n // }\n if(value>=0){\n int r=i-mp[value];\n int v1=i+1;\n int v2=r-1;\n // cout<<value<<\" \"<<v1<<\" \"<<v2<<endl;\n ans=ans+v1-v2;\n }\n // cout<<ans<<endl;\n mp[p[i]]=i;\n }\n return ans;\n\n \n }\n};", "memory": "130600" }
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A <strong>subarray</strong> is a contiguous sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,2,3,3], k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The subarrays that contain the element 3 at least 2 times are: [1,3,2,3], [1,3,2,3,3], [3,2,3], [3,2,3,3], [2,3,3] and [3,3]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,2,1], k = 3 <strong>Output:</strong> 0 <strong>Explanation:</strong> No subarray contains the element 4 at least 3 times. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n vector<long long> v;\n v.push_back(0);\n long long n = nums.size();\n int mx = nums[0];\n for(int i=0;i<n;i++) {\n mx = max(mx, nums[i]);\n }\n\n for(int i=1;i<=n;i++) {\n if(nums[i-1] == mx) {\n v.push_back(1);\n } else {\n v.push_back(0);\n }\n \n v[i] += v[i-1];\n \n }\n\n // for(auto i: v) {\n // cout<<i<<\" \";\n // }\n\n // cout<<endl;\n\n long long ans = 0;\n\n for(int i=1;i<=n;i++) {\n int l = i, r = n + 1;\n while(l < r) {\n long long m = (l + r)/2;\n long long totalOne = v[m]-v[i-1];\n if(totalOne < k) {\n l = m + 1;\n } else {\n r = m;\n }\n }\n \n if(r < n + 1) {\n ans += n - r + 1; \n }\n }\n\n return ans;\n \n }\n};", "memory": "143700" }
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A <strong>subarray</strong> is a contiguous sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,2,3,3], k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The subarrays that contain the element 3 at least 2 times are: [1,3,2,3], [1,3,2,3,3], [3,2,3], [3,2,3,3], [2,3,3] and [3,3]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,2,1], k = 3 <strong>Output:</strong> 0 <strong>Explanation:</strong> No subarray contains the element 4 at least 3 times. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n \n vector<long long> v;\n v.push_back(0);\n long long n = nums.size();\n int mx = nums[0];\n for(int i=0;i<n;i++) {\n mx = max(mx, nums[i]);\n }\n\n for(int i=1;i<=n;i++) {\n if(nums[i-1] == mx) {\n v.push_back(1);\n } else {\n v.push_back(0);\n }\n \n v[i] += v[i-1];\n \n }\n\n\n long long ans = 0;\n\n for(int i=1;i<=n;i++) {\n int l = i, r = n + 1;\n while(l < r) {\n long long m = (l + r)/2;\n long long totalOne = v[m]-v[i-1];\n if(totalOne < k) {\n l = m + 1;\n } else {\n r = m;\n }\n }\n \n if(r < n + 1) {\n ans += n - r + 1; \n }\n }\n\n return ans;\n \n }\n};", "memory": "143700" }
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A <strong>subarray</strong> is a contiguous sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,2,3,3], k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The subarrays that contain the element 3 at least 2 times are: [1,3,2,3], [1,3,2,3,3], [3,2,3], [3,2,3,3], [2,3,3] and [3,3]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,2,1], k = 3 <strong>Output:</strong> 0 <strong>Explanation:</strong> No subarray contains the element 4 at least 3 times. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "//////////// Sliding Window solution ///////////\n\n// class Solution {\n// public:\n// long long countSubarrays(vector<int>& nums, int k) {\n// int maxElement = *max_element(nums.begin(), nums.end());\n// int i = 0, j = 0, n = nums.size(), maxCount = 0;\n// long long ans = 0;\n\n// while(j < n){\n// if(nums[j] == maxElement) maxCount++;\n// while(maxCount == k){\n// ans += n-j;\n// if(nums[i] == maxElement) maxCount--;\n// i++;\n// }\n// j++;\n// }\n\n// return ans;\n// }\n// };\n\n\n///////// Usng Hash table to store index ///////\n\nclass Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n int maxElement = *max_element(nums.begin(), nums.end());\n int n = nums.size(), maxCount = 0;\n long long ans = 0;\n unordered_map<int, int> mp;\n\n for(int i = 0; i<n; i++){\n if(nums[i] == maxElement){\n maxCount++;\n mp[maxCount] = i;\n }\n if(maxCount >= k) ans += mp[maxCount - k + 1] + 1;\n }\n\n return ans;\n }\n};", "memory": "144200" }
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A <strong>subarray</strong> is a contiguous sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,2,3,3], k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The subarrays that contain the element 3 at least 2 times are: [1,3,2,3], [1,3,2,3,3], [3,2,3], [3,2,3,3], [2,3,3] and [3,3]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,2,1], k = 3 <strong>Output:</strong> 0 <strong>Explanation:</strong> No subarray contains the element 4 at least 3 times. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n int n = nums.size();\n int maxi = *max_element(nums.begin(),nums.end());\n unordered_map<int,int> mp;\n long long ans =0;\n int i=0,j=0;\n while(j<n){\n mp[nums[j]]++;\n while(mp[maxi] >= k){\n ans += n-j;\n mp[nums[i]]--;\n if(mp[nums[i]] == 0) mp.erase(nums[i]);\n i++;\n }\n j++;\n }\n return ans;\n }\n};", "memory": "146100" }
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A <strong>subarray</strong> is a contiguous sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,2,3,3], k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The subarrays that contain the element 3 at least 2 times are: [1,3,2,3], [1,3,2,3,3], [3,2,3], [3,2,3,3], [2,3,3] and [3,3]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,2,1], k = 3 <strong>Output:</strong> 0 <strong>Explanation:</strong> No subarray contains the element 4 at least 3 times. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "#include <iostream>\n#include <vector>\n#include <unordered_map>\n\nusing namespace std;\n\nclass Solution {\npublic:\n long countSubarrays(vector<int>& nums, int k) {\n int max = 0;\n for(int i: nums)\n max = std::max(i, max);\n vector<int> cnt(nums.size());\n int m = 0;\n int j = 0;\n unordered_map<int, int> map;\n for(int i: nums){\n if(i == max){\n m++;\n map[m] = j;\n }\n cnt[j++] = m;\n }\n long ans = 0;\n j = 0;\n for(int i : cnt){\n if(i >= k){\n int last = map.count(i-k+1) ? map[i-k+1] : i;\n ans += last+1;\n }\n j++;\n }\n return ans;\n }\n};", "memory": "149400" }
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A <strong>subarray</strong> is a contiguous sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,2,3,3], k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The subarrays that contain the element 3 at least 2 times are: [1,3,2,3], [1,3,2,3,3], [3,2,3], [3,2,3,3], [2,3,3] and [3,3]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,2,1], k = 3 <strong>Output:</strong> 0 <strong>Explanation:</strong> No subarray contains the element 4 at least 3 times. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n\n int maxi=0;\n for(int i=0;i<nums.size();i++) maxi=max(maxi,nums[i]);\n vector<int>p(nums.size(),0);\n for(int i=0;i<nums.size();i++){\n if(nums[i]==maxi) p[i]++;\n if(i!=0) p[i]=p[i]+p[i-1];\n }\n unordered_map<int,int>upb;\n long long count=0;\n for(int i=0;i<nums.size();i++){\n \n if(p[i]>=k){\n int y=p[i]-k;\n int u=-1;\n if(upb.find(y)!=upb.end()) u=upb[y];\n count+=(u+2);\n \n cout<<(u+2);\n }\n upb[p[i]]=i;\n }\n return count;\n }\n};", "memory": "149500" }
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A <strong>subarray</strong> is a contiguous sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,2,3,3], k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The subarrays that contain the element 3 at least 2 times are: [1,3,2,3], [1,3,2,3,3], [3,2,3], [3,2,3,3], [2,3,3] and [3,3]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,2,1], k = 3 <strong>Output:</strong> 0 <strong>Explanation:</strong> No subarray contains the element 4 at least 3 times. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n\n int maxi=0;\n for(int i=0;i<nums.size();i++) maxi=max(maxi,nums[i]);\n vector<int>p(nums.size(),0);\n for(int i=0;i<nums.size();i++){\n if(nums[i]==maxi) p[i]++;\n if(i!=0) p[i]=p[i]+p[i-1];\n }\n unordered_map<int,int>upb;\n long long count=0;\n for(int i=0;i<nums.size();i++){\n \n if(p[i]>=k){\n int y=p[i]-k;\n int u=-1;\n if(upb.find(y)!=upb.end()) u=upb[y];\n count+=(u+2);\n \n cout<<(u+2);\n }\n upb[p[i]]=i;\n }\n return count;\n }\n};", "memory": "149500" }
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A <strong>subarray</strong> is a contiguous sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,2,3,3], k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The subarrays that contain the element 3 at least 2 times are: [1,3,2,3], [1,3,2,3,3], [3,2,3], [3,2,3,3], [2,3,3] and [3,3]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,2,1], k = 3 <strong>Output:</strong> 0 <strong>Explanation:</strong> No subarray contains the element 4 at least 3 times. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n long long sub_num = 0;\n int n = nums.size();\n int h = 0; int max_val = 0;\n unordered_map<int, int> counts;\n for (int i = 0; i < n; i++) {\n max_val = std::max(max_val, nums[i]);\n counts[nums[i]]++;\n }\n\n int max_occurences = counts[max_val];\n\n int l = 0; int r = l;\n while (r < n) {\n if (nums[r] == max_val) h++;\n while (h == k) {\n sub_num += 1 + (n-r-1);\n if (nums[l] == max_val) {\n h--;\n }\n l++;\n }\n r++;\n }\n return sub_num;\n }\n};", "memory": "150100" }
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A <strong>subarray</strong> is a contiguous sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,2,3,3], k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The subarrays that contain the element 3 at least 2 times are: [1,3,2,3], [1,3,2,3,3], [3,2,3], [3,2,3,3], [2,3,3] and [3,3]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,2,1], k = 3 <strong>Output:</strong> 0 <strong>Explanation:</strong> No subarray contains the element 4 at least 3 times. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n int n=nums.size();\n int maxElement=0;\n\n for(auto it : nums){\n maxElement=max(it,maxElement);\n }\n int first=0,last=0;\n long long ans=0;\n unordered_map<int , int> mpp;\n while(first<n){\n mpp[nums[first]]++;\n\n while(mpp[maxElement]>=k){\n mpp[nums[last]]--;\n ans+=(n-1)-first+1;\n last++;\n }\n\n first++;\n }\n\n return ans;\n\n }\n};\n\n", "memory": "150200" }
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A <strong>subarray</strong> is a contiguous sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,2,3,3], k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The subarrays that contain the element 3 at least 2 times are: [1,3,2,3], [1,3,2,3,3], [3,2,3], [3,2,3,3], [2,3,3] and [3,3]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,2,1], k = 3 <strong>Output:</strong> 0 <strong>Explanation:</strong> No subarray contains the element 4 at least 3 times. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n int findMax(vector<int>& nums)\n {\n int maxEle = INT_MIN;\n for (int i=0; i<nums.size(); i++) {\n maxEle = max(maxEle, nums[i]);\n }\n return maxEle;\n }\n\n long long countSubarrays(vector<int>& nums, int k) {\n long long result = 0;\n int count = 0;\n int left = 0;\n int maxEle = findMax(nums);\n unordered_map<int,int> freq;\n\n for (int i=0; i<nums.size(); i++) {\n if (freq.find(nums[i]) != freq.end()) {\n freq[nums[i]]++;\n } else {\n freq[nums[i]] = 1;\n }\n\n\n while (nums[i] == maxEle && freq[nums[i]] == k) {\n if (nums[left] == maxEle) {\n freq[nums[left]]--;\n if (freq[nums[left]] == 0) {\n freq.erase(nums[left]);\n }\n }\n left++;\n }\n result+= left;\n }\n \n return result;\n }\n};", "memory": "150300" }
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A <strong>subarray</strong> is a contiguous sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,2,3,3], k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The subarrays that contain the element 3 at least 2 times are: [1,3,2,3], [1,3,2,3,3], [3,2,3], [3,2,3,3], [2,3,3] and [3,3]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,2,1], k = 3 <strong>Output:</strong> 0 <strong>Explanation:</strong> No subarray contains the element 4 at least 3 times. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long int countSubs(vector<int>& nums, int k, int mx) {\n long long int count = 0;\n int left = 0;\n unordered_map<int, int> freq;\n\n for (int right = 0; right < nums.size(); ++right) {\n \n freq[nums[right]]++;\n \n while (freq[mx] >= k) {\n freq[nums[left]]--;\n left++; \n }\n \n count += right - left + 1;\n \n }\n return count;\n }\n long long countSubarrays(vector<int>& nums, int k) {\n int mx = -1;\n for(int m: nums)\n {\n mx = max(mx, m);\n }\n long long int Tsubs = nums.size()*(nums.size()+1)/2;\n \n return (long long) Tsubs - countSubs(nums, k, mx);\n \n \n }\n};", "memory": "150300" }
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A <strong>subarray</strong> is a contiguous sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,2,3,3], k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The subarrays that contain the element 3 at least 2 times are: [1,3,2,3], [1,3,2,3,3], [3,2,3], [3,2,3,3], [2,3,3] and [3,3]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,2,1], k = 3 <strong>Output:</strong> 0 <strong>Explanation:</strong> No subarray contains the element 4 at least 3 times. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n long long i=0,j=0,ans=0;\n unordered_map<int,int> umap;\n int max_element;\n for(auto x:nums){\n max_element=max(max_element,x);\n }\n while(j<nums.size()){\n umap[nums[j]]++;\n while(umap[max_element]>=k){\n ans+=nums.size()-j;\n umap[nums[i]]--;\n ++i;\n }\n ++j;\n }\n return ans;\n }\n};", "memory": "150400" }
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A <strong>subarray</strong> is a contiguous sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,2,3,3], k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The subarrays that contain the element 3 at least 2 times are: [1,3,2,3], [1,3,2,3,3], [3,2,3], [3,2,3,3], [2,3,3] and [3,3]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,2,1], k = 3 <strong>Output:</strong> 0 <strong>Explanation:</strong> No subarray contains the element 4 at least 3 times. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& arr, int k)\n {\n int mx = INT_MIN;\n for(int i=0; i<arr.size(); i++)\n mx = max(mx, arr[i]);\n\n long long start=0, end=0, ans=0;\n unordered_map<int, int> m;\n\n while(end<arr.size())\n {\n m[arr[end]]++;\n\n while(m[mx]>=k && start<=end)\n {\n ans += arr.size()-end;\n m[arr[start++]]--;\n }\n\n end++;\n }\n\n return ans;\n }\n};", "memory": "150500" }
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A <strong>subarray</strong> is a contiguous sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,2,3,3], k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The subarrays that contain the element 3 at least 2 times are: [1,3,2,3], [1,3,2,3,3], [3,2,3], [3,2,3,3], [2,3,3] and [3,3]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,2,1], k = 3 <strong>Output:</strong> 0 <strong>Explanation:</strong> No subarray contains the element 4 at least 3 times. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& A, int k) {\n long long ans=0;\n int l=0;\n int mx = *max_element(A.begin(), A.end());\n unordered_map<int,int> m;\n int n=A.size();\n for(int i=0;i<n;i++){\n m[A[i]]++;\n while(m[mx]>=k){\n m[A[l]]--;\n l++;\n }\n ans += i-l+1;\n }\n return 1ll*(n)*(n+1)/2 - ans;\n }\n};", "memory": "150500" }
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A <strong>subarray</strong> is a contiguous sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,2,3,3], k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The subarrays that contain the element 3 at least 2 times are: [1,3,2,3], [1,3,2,3,3], [3,2,3], [3,2,3,3], [2,3,3] and [3,3]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,2,1], k = 3 <strong>Output:</strong> 0 <strong>Explanation:</strong> No subarray contains the element 4 at least 3 times. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n int maxiEle=*max_element(nums.begin(),nums.end());\n long long ans=0;\n int i=0,j=0,cnt=0;\n int n=nums.size();\n unordered_map<int,int>mp;\n while(j<n){\n mp[nums[j]]++;\n while(mp[maxiEle]>=k){\n ans+=n-j;\n mp[nums[i++]]--;\n\n }\n j++;\n }\n return ans;\n }\n};", "memory": "150600" }
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A <strong>subarray</strong> is a contiguous sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,2,3,3], k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The subarrays that contain the element 3 at least 2 times are: [1,3,2,3], [1,3,2,3,3], [3,2,3], [3,2,3,3], [2,3,3] and [3,3]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,2,1], k = 3 <strong>Output:</strong> 0 <strong>Explanation:</strong> No subarray contains the element 4 at least 3 times. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "static const bool Booster = [](){\n #pragma GCC optimize(\"OFast\")\n std::ios_base::sync_with_stdio(0);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return true;\n}();\n\nclass Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n int n = nums.size();\n int maxElement = 0;\n\n for(auto it : nums) maxElement=max(it, maxElement);\n\n int first=0, last=0;\n long long ans=0;\n unordered_map<int, int> mpp;\n while (last < n) {\n mpp[nums[last]]++;\n\n while(mpp[maxElement] >= k){\n mpp[nums[first]]--;\n ans += (n-1)-last+1;\n first++;\n }\n last++;\n }\n return ans;\n }\n};", "memory": "150700" }
3,213
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>Return <em>the number of subarrays where the <strong>maximum</strong> element of </em><code>nums</code><em> appears <strong>at least</strong> </em><code>k</code><em> times in that subarray.</em></p> <p>A <strong>subarray</strong> is a contiguous sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,2,3,3], k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> The subarrays that contain the element 3 at least 2 times are: [1,3,2,3], [1,3,2,3,3], [3,2,3], [3,2,3,3], [2,3,3] and [3,3]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,2,1], k = 3 <strong>Output:</strong> 0 <strong>Explanation:</strong> No subarray contains the element 4 at least 3 times. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n Solution(){\n ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);\n }\n long long countSubarrays(vector<int>& nums, int k) {\n int n=nums.size(),maxElement=0;\n for(auto it:nums){\n maxElement=max(it,maxElement);\n }\n int first=0,last=0;\n long long ans=0;\n unordered_map<int,int> mp;\n while(first<n){\n mp[nums[first]]++;\n while(mp[maxElement]>=k){\n mp[nums[last]]--;\n ans+=(n-1)-first+1;\n last++;\n }\n first++;\n }\n return ans;\n }\n};", "memory": "150800" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n = roads.size() + 1;\n vector<int> deg(n, 0), gragh(n, 0);\n for (auto& road : roads) {\n int node = road[0], vec = road[1];\n gragh[node] ^= vec;\n gragh[vec] ^= node;\n deg[node]++;\n deg[vec]++;\n }\n vector<int> cnt(n, 1), queue(n, 0);\n int front = 0, size = 0;\n for (int i = 1; i < n; ++i)\n if (deg[i] == 1)\n queue[size++] = i;\n long long ans = 0;\n\n for (; front < size; front++) {\n int node = queue[front];\n int vec = gragh[node];\n ans += cnt[node] / seats + (cnt[node] % seats > 0);\n cnt[vec] += cnt[node];\n gragh[vec] ^= node;\n deg[vec]--;\n\n if ((deg[vec] == 1) && (vec > 0))\n queue[size++] = vec;\n }\n return ans;\n }\n};", "memory": "129139" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n = roads.size() + 1;\n vector<int> deg(n, 0), gragh(n, 0);\n for (auto& road : roads) {\n int node = road[0], vec = road[1];\n gragh[node] ^= vec;\n gragh[vec] ^= node;\n deg[node]++;\n deg[vec]++;\n }\n vector<int> cnt(n, 1), queue(n, 0);\n int front = 0, size = 0;\n for (int i = 1; i < n; ++i)\n if (deg[i] == 1)\n queue[size++] = i;\n long long ans = 0;\n\n for (; front < size; front++) {\n int node = queue[front];\n int vec = gragh[node];\n ans += (cnt[node] + seats - 1) / seats;\n cnt[vec] += cnt[node];\n gragh[vec] ^= node;\n deg[vec]--;\n\n if ((deg[vec] == 1) && (vec != 0))\n queue[size++] = vec;\n }\n return ans;\n }\n};", "memory": "129139" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n=roads.size()+1;\n vector<int>graph[n];\n vector<int>indegree(n);\n vector<int> people(n+1,1);\n people[0]=0;\n for(auto&x:roads)\n {\n graph[x[0]].push_back(x[1]);\n graph[x[1]].push_back(x[0]);\n indegree[x[0]]++;\n indegree[x[1]]++;\n }\n\n queue<int>q;\n for(int i=0;i<n;i++)\n {\n if(indegree[i]==1) \n {\n cout<<i<<endl;\n q.push(i);\n }\n }\n long long ans=0;\n while(!q.empty())\n {\n int sz=q.size();\n while(sz--)\n {\n int node=q.front();\n // cout<<node<<\" \"<<currSeat<<endl;\n q.pop();\n if(node==0) break;\n ans+=people[node]/seats;\n\n if(people[node]%seats){\n ans++;\n }\n \n indegree[node]=0;\n for(auto&x:graph[node])\n {\n people[x]+=people[node];\n indegree[x]--;\n \n if(indegree[x]==1)\n {\n q.push(x);\n }\n }\n }\n }\n // cout<<level<<endl;\n // for(auto&x:mp)\n // {\n // ans+=level-x.second;\n // cout<<\" \"<<x.second<<endl;\n // }\n return ans;\n\n }\n};", "memory": "131019" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n=roads.size()+1;\n vector<int>indegree(n);\n vector<int>adj[n];\n for(int i=0;i<roads.size();i++)\n {\n adj[roads[i][0]].push_back(roads[i][1]);\n adj[roads[i][1]].push_back(roads[i][0]);\n indegree[roads[i][1]]++;\n indegree[roads[i][0]]++;\n }\n queue<int>q;\n for(int i=1;i<n;i++)\n {\n if(indegree[i]==1)\n q.push(i);\n }\n long long ans=0;\n vector<int>p(n,1);\n while(q.empty()==false)\n {\n int node = q.front();\n q.pop();\n ans = ans + ceil((double)p[node]/seats);\n for(auto neighbor : adj[node])\n {\n indegree[neighbor]--;\n p[neighbor] += p[node];\n if(indegree[neighbor]==1 && neighbor!=0)\n {\n q.push(neighbor);\n }\n }\n }\n return ans;\n }\n};", "memory": "132899" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\n long long ans = 0;\n int s;\n\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n=roads.size()+1;\n vector<int> graph[n];\n vector<int> deg(n, 0);\n for (vector<int>& r : roads) {\n graph[r[0]].push_back(r[1]);\n graph[r[1]].push_back(r[0]);\n deg[r[0]]++;\n deg[r[1]]++;\n }\n vector<int> people(n, 1);\n queue<int> q;\n long long ans=0;\n for (int i=0; i<n; i++){\n if (deg[i]==1) q.push(i);\n }\n int sz, person;\n while(!q.empty()) {\n sz = q.size();\n for(int i = 0; i < sz; i++) {\n person = q.front(); q.pop();\n if(!person) break;\n ans += ceil(people[person]*1.0/seats);\n for(int v: graph[person]) {\n deg[v]--;\n people[v]+=people[person];\n if (deg[v]==1) q.push(v);\n }\n }\n }\n return ans;\n }\n};", "memory": "134779" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> adj[100005];\n bool visit[100005];\n long long s, ans = 0;\n int dfs(int cur){\n if(visit[cur]){\n return 0;\n }\n visit[cur]=true;\n int p = 0;\n for(int x : adj[cur]){\n p+=dfs(x);\n }\n p++;\n if(cur)\n ans += ceil(p*1.0/s);\n return p;\n }\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n s = seats;\n for(int i = 0; i<roads.size(); i++){\n adj[roads[i][0]].push_back(roads[i][1]);\n adj[roads[i][1]].push_back(roads[i][0]);\n\n }\n dfs(0);\n return ans;\n }\n};", "memory": "136659" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int k;\n long long ans=0;\n long long dfs(vector<int>adj[],int node,int par){\n long long count=1; //Count==>Total nodes in subtree with node as parent\n for(int child:adj[node]){\n if(child!=par){\n count+=dfs(adj,child,node);\n }\n }\n ans+=(count%k==0)?count/k:((count/k)+1); // Grouping people in same cars before returning to parent node\n return count;\n \n }\n\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n=roads.size()+1;\n vector<int> adj[n];\n for(auto &i: roads){\n adj[i[0]].push_back(i[1]);\n adj[i[1]].push_back(i[0]);\n }\n k=seats;\n long long ans1=0;\n for(auto i : adj[0]){\n long long a=dfs(adj,i,0);\n ans1+=ans;\n ans=0;\n }\n return ans1;\n }\n};", "memory": "138539" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long ans = 0;\n int seat;\n int dfs(int node , int prev , vector<int>adj[]){\n int people = 1;\n for(auto it : adj[node]){\n if(prev == it){\n continue;\n }\n people+= dfs(it, node , adj);\n }\n if(node!=0){\n ans+= (int)ceil((double)people/seat);\n }\n return people;\n }\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n seat = seats;\n vector<int>adj[roads.size()+1];\n for(int i = 0 ; i<roads.size() ; i++){\n adj[roads[i][1]].push_back(roads[i][0]);\n adj[roads[i][0]].push_back(roads[i][1]);\n }\n \n dfs(0 , -1 , adj);\n return ans;\n \n }\n};", "memory": "140419" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long ans=0;\n long long solve(int i,int j,int s,vector<int>a[])\n { \n long long x=0;\n for(int c:a[i])\n {\n if(c==j)continue;\n x+=solve(c,i,s,a);\n }\n x+=1;\n if(i!=0){\n ans+=x/s;\n if(x%s!=0)ans++;}\n return x;\n }\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n=roads.size()+1;\n vector<int>a[n];\n for(int i=0;i<roads.size();i++)\n {\n a[roads[i][0]].push_back(roads[i][1]);\n a[roads[i][1]].push_back(roads[i][0]);\n }\n solve(0,-1,seats,a);\n return ans;\n }\n};", "memory": "142299" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n\nlong long ans = 0;\n\n int dfs(int ver,int parent, vector<int> adj[], int seats){\n int people = 1;\n for(auto it: adj[ver]) if(it != parent) people += dfs(it,ver, adj,seats);\n\n if(ver > 0) ans += ceil( (double) people / (double) seats );\n return people;\n }\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n = roads.size() + 1;\n if(n == 1) return 0;\n vector<int> adj[n];\n\n for(auto & i : roads) adj[i[0]].push_back(i[1]), adj[i[1]].push_back(i[0]);\n dfs(0, -1, adj, seats);\n return ans; \n } \n};", "memory": "144179" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int dfs(int u, int par, vector<int> adj[], long long &ans, int seats) {\n int passengers = 0;\n for(auto &child : adj[u]) {\n if (child!=par) {\n int p = dfs(child, u, adj, ans, seats);\n passengers += p;\n ans += (p + seats - 1)/seats;\n }\n }\n return passengers+1;\n }\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n = roads.size();\n vector<int> adj[n+1];\n for(auto &e : roads) {\n adj[e[0]].push_back(e[1]);\n adj[e[1]].push_back(e[0]);\n }\n long long ans = 0;\n dfs(0, -1, adj, ans, seats);\n return ans;\n }\n};", "memory": "146059" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n vector<int> adjList[roads.size()+1];\n for (const auto& r : roads) {\n adjList[r[0]].push_back(r[1]);\n adjList[r[1]].push_back(r[0]);\n }\n auto [_, FuelCost] = dfs(-1, 0, adjList, seats);\n return FuelCost;\n }\n\n pair<long long, long long> dfs(int prev, int cur, vector<int> adjList[], int seats) {\n long long rep = 0;\n long long cost = 0;\n for (const auto& next : adjList[cur]) {\n if (prev == next)\n continue;\n auto [r, c] = dfs(cur, next, adjList, seats);\n rep += r;\n cost += c + (r + seats - 1) / seats;\n }\n ++rep;\n // cout << cur << \" \" << \" \" << rep << \" \" << cost << endl;\n return {rep, cost};\n }\n};", "memory": "147939" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int dfs(int u, int prev, vector<int> g[], int seats, long long &ans) {\n long long count = 1;\n for(auto v:g[u]) {\n if(v!=prev) {\n count+=dfs(v,u,g,seats, ans);\n }\n \n }\n if(u!=0) {\n ans+= ((count/seats) + (count%seats!=0));\n }\n \n return count;\n }\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n = roads.size()+1;\n long long ans = 0;\n vector<int> g[n];\n for(int i=0;i<roads.size();i++) {\n int u = roads[i][0];\n int v = roads[i][1];\n g[u].push_back(v);\n g[v].push_back(u);\n }\n dfs(0,-1,g,seats,ans);\n return ans;\n }\n};", "memory": "149819" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n void dfs(int s, vector<int> adj[], bool vis[], vector<int> &st) {\n vis[s] = 1;\n for(auto &a : adj[s]) {\n if(!vis[a])\n dfs(a, adj, vis, st);\n }\n st.push_back(s);\n }\n int calculateFule(int s, vector<int> adj[], bool vis[], long long &ans, int seats) {\n int n1 = 0;\n vis[s] = 1;\n for(auto &a : adj[s]) {\n int n2 = 0;\n if(!vis[a])\n n2 = calculateFule(a, adj, vis, ans, seats);\n ans += (n2/seats);\n if(n2%seats!=0)\n ans += 1;\n n1 += n2;\n }\n return n1+1;\n }\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n long long ans = 0;\n int n = roads.size()+1;\n vector<int> adj[n];\n for(int i=0;i<n-1;i++) {\n adj[roads[i][0]].push_back(roads[i][1]);\n adj[roads[i][1]].push_back(roads[i][0]);\n }\n bool vis[n];\n memset(vis, 0, sizeof(vis));\n vis[0] = 1;\n int n2 = 0;\n for(auto &a : adj[0]) {\n vector<int> st;\n //dfs(a, adj, vis, st);\n //reverse(st.begin(), st.end());\n //memset(vis, 0, sizeof(vis));\n //vis[0] = 1;\n int n1 = calculateFule(a, adj, vis, ans, seats);\n ans += (n1/seats);\n if(n1%seats!=0)\n ans += 1;\n }\n return ans;\n }\n};", "memory": "151699" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\nusing ll = long long; \nprivate: ll totalFuel= 0;\n int dfs(int curr, int parent, vector<ll> *adj, int seats){\n int count = 1;\n for(auto &u: adj[curr]){\n if(u!=parent){\n count += dfs(u, curr, adj, seats);\n }\n }\n \n if(curr !=0){\n totalFuel += 1ll* ceil((double) count/ seats);\n }\n \n return count;\n }\n \npublic:\n \n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n = roads.size();\n if(n==0) return 0;\n n++;\n vector<ll> adj[n];\n for(auto &x: roads){\n auto u = x[0], v = x[1];\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n \n dfs(0,-1, adj, seats);\n \n return totalFuel;\n \n }\n};", "memory": "153579" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\nprivate:\n int dfs(vector<int> adjList[], int src, int par, long long& total, int seats) {\n int people = 0;\n for (int nei : adjList[src]) {\n if (nei == par) continue;\n int peopleFromNeiCity = dfs(adjList, nei, src, total, seats);\n people += peopleFromNeiCity;\n total += (long long) ceil(peopleFromNeiCity*1.0/seats);\n }\n return people + 1;\n }\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n vector<int> adjList[roads.size() + 1];\n for (int i = 0; i < roads.size(); i++) {\n int f = roads[i][0];\n int t = roads[i][1];\n adjList[f].push_back(t);\n adjList[t].push_back(f);\n }\n long long total = 0;\n dfs(adjList, 0, -1, total, seats);\n return total;\n }\n};", "memory": "155459" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long rec(int src ,vector<int> adj[],vector<int> &visited,int seats,long long &ans){\n int passenger_count=0;\n visited[src]=1; \n\n for(auto x: adj[src]){\n if(!visited[x]){\n int pas=rec(x,adj,visited,seats,ans);\n passenger_count+=pas;\n ans+= ((pas + seats -1 )/ seats);\n }\n }\n return ++passenger_count;\n }\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n=roads.size();\n vector<int> adj[n+1];\n vector<int> visited(n+1,0);\n long long ans=0;\n for(int i=0;i<n;i++){\n adj[roads[i][0]].push_back(roads[i][1]);\n adj[roads[i][1]].push_back(roads[i][0]);\n }\n rec(0,adj,visited,seats,ans);\n return ans;\n\n }\n};", "memory": "157339" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long fuel = 0;\n long long dfs(int i,vector<int>&visited,vector<int>adj[],int size,int seats){\n visited[i] = 1;\n size++;\n for(auto it:adj[i]){\n if(!visited[it]){\n size += dfs(it,visited,adj,0,seats);\n }\n }\n fuel+=ceil(double(size)/seats);\n return size;\n }\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n = roads.size() + 1;\n vector<int>adj[n];\n for(int i=0;i<n-1;i++){\n adj[roads[i][0]].push_back(roads[i][1]);\n adj[roads[i][1]].push_back(roads[i][0]);\n }\n vector<int>visited(n,0);\n visited[0]=1;\n for(auto it:adj[0]){\n int x = dfs(it,visited,adj,0,seats);\n // fuel+=ceil(float(x)/seats);\n }\n return fuel;\n }\n};", "memory": "157339" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\nlong long ans=0;\nint dfs(int node, vector<int> adj[], int seat, vector<int> &vis){\n vis[node]=1;\n int child=1;\n for(auto it:adj[node]){\n if(vis[it]==0){\n int her= dfs(it,adj,seat,vis); \n int car=her/seat;\n if(her%seat!=0) car++;\n ans+=car;\n child+=her;\n }\n \n }\n return child;\n}\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int m = roads.size();\n ans=0;\n vector<int> adj[m+1];\n for(int i=0;i<m;i++){\n adj[roads[i][0]].push_back(roads[i][1]);\n adj[roads[i][1]].push_back(roads[i][0]);\n }\n vector<int> vis(m+1, 0);\n int node=dfs(0, adj, seats, vis);\n return ans;\n }\n};", "memory": "159219" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long ans=0;\n long long dfs(vector<int>adj[],int node,int seats,vector<int>&vis){\n vis[node]=1;\n long long cnt=1;\n for(auto it:adj[node]){\n if(vis[it]==0)\n cnt+=dfs(adj,it,seats,vis);\n }\n long long x=cnt/seats;\n if(cnt%seats) x++;\n if(node!=0) ans+=x;\n return cnt;\n }\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n=roads.size();\n if(n==0) return 0;\n ans=0;\n vector<int>adj[n+1];\n vector<int>vis(n+1,0);\n for(int i=0;i<roads.size();i++){\n adj[roads[i][0]].push_back(roads[i][1]);\n adj[roads[i][1]].push_back(roads[i][0]);\n }\n\n dfs(adj,0,seats,vis);\n return ans;\n }\n};", "memory": "161099" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long ans = 0; int s;\n vector<int> graph[300005];\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n s = seats;\n for(vector<int>& r : roads){\n graph[r[0]].push_back(r[1]);\n graph[r[1]].push_back(r[0]);\n }\n dfs(0, 0, 1);\n return ans;\n }\n int dfs(int i, int prev, int people){ //people到現在有幾個人\n for(int&x : graph[i]){\n if (x==prev) continue;\n people += dfs(x, i, 1);\n }\n if (i != 0) ans += (people + s - 1) / s;\n return people;\n }\n};", "memory": "162979" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long ans = 0; int s;\n vector<int> graph[300005]; \n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n s = seats;\n for (vector<int>& r: roads) {\n graph[r[0]].push_back(r[1]);\n graph[r[1]].push_back(r[0]);\n }\n dfs(0, 0, 1);\n return ans;\n }\n int dfs(int i, int prev, int people) {\n // people 到現在有幾個人\n for (int& x : graph[i]) {\n if (x == prev) continue;\n people += dfs(x, i, 1);\n }\n if (i != 0) ans += (people + s - 1) / s; // 無條件捨去\n return people;\n }\n};", "memory": "162979" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long ans = 0; int s;\n vector<int> graph[300005];\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n s = seats;\n for(vector<int>& r : roads){\n graph[r[0]].push_back(r[1]);\n graph[r[1]].push_back(r[0]);\n }\n dfs(0, 0, 1);\n return ans;\n }\n int dfs(int i, int prev, int people){\n for(int&x : graph[i]){\n if (x==prev) continue;\n people += dfs(x, i, 1);\n }\n if (i != 0) ans += (people + s - 1) / s;\n return people;\n }\n};", "memory": "164859" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int dfs(int node,int vis[],vector<int> adj[],int ct[],int seats)\n {\n vis[node]=1;\n int ans = 0;\n for(auto child: adj[node])\n {\n if(!vis[child])\n {\n int p = dfs(child,vis,adj,ct,seats);\n ans+=p;\n }\n }\n ans++;\n int x = (ans/seats) + (ans%seats!=0);\n // cout<<node<<\" \"<<x<<endl;\n ct[node]=x;\n return ans;\n }\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n long long ans =0 ;\n int n = roads.size()+1;\n vector<int> adj[n+1];\n int ct[n+1];\n int vis[n+1];\n for(int i = 0;i<n+1;i++)\n {\n ct[i]=0;vis[i]=0;\n }\n for(int i =0;i<n-1;i++)\n {\n adj[roads[i][0]].push_back(roads[i][1]);\n adj[roads[i][1]].push_back(roads[i][0]);\n }\n dfs(0,vis,adj,ct,seats);\n for(int i =1 ;i<n;i++)\n {\n ans+=ct[i];\n }\n return ans;\n }\n};", "memory": "166739" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long bfs(int n, vector<vector<int>>& adj, vector<int>& degree, int& seats) \n {\n queue<int> q;\n for (int i = 1; i < n; i++) \n {\n if (degree[i] == 1) \n {\n q.push(i);\n }\n }\n\n vector<int> representatives(n, 1);\n long long fuel = 0;\n\n while (!q.empty()) \n {\n int node = q.front();\n q.pop();\n\n fuel += ceil((double)representatives[node] / seats);\n for (auto& neighbor : adj[node]) \n {\n degree[neighbor]--;\n representatives[neighbor] += representatives[node];\n if (degree[neighbor] == 1 && neighbor != 0) \n {\n q.push(neighbor);\n }\n }\n }\n return fuel;\n }\n\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) \n {\n int n = roads.size() + 1;\n vector<vector<int>> adj(n);\n vector<int> degree(n);\n\n for (auto& road : roads) {\n adj[road[0]].push_back(road[1]);\n adj[road[1]].push_back(road[0]);\n degree[road[0]]++;\n degree[road[1]]++;\n }\n\n return bfs(n, adj, degree, seats);\n }\n};", "memory": "168619" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n = roads.size() + 1;\n vector<vector<int>> graph(n);\n vector<int> degree(n, 0);\n vector<int> people(n, 1); // Mỗi thành phố ban đầu có 1 người\n vector<bool> visited(n, false); // Đánh dấu các thành phố đã được duyệt\n\n // Xây dựng đồ thị\n for (const auto& road : roads) {\n int u = road[0], v = road[1];\n graph[u].push_back(v);\n graph[v].push_back(u);\n degree[u]++;\n degree[v]++;\n }\n\n queue<int> q;\n // Tìm tất cả các node lá (degree == 1)\n for (int i = 1; i < n; ++i) {\n if (degree[i] == 1) {\n q.push(i);\n visited[i] = true; // Đánh dấu node lá là đã duyệt\n }\n }\n\n long long fuelCost = 0;\n\n // BFS từ các node lá\n while (!q.empty()) {\n int node = q.front();\n q.pop();\n\n for (int neighbor : graph[node]) {\n if (!visited[neighbor]) { // Kiểm tra node cha chưa được duyệt\n int trips = (people[node] + seats - 1) / seats;\n fuelCost += trips;\n\n // Di chuyển người lên thành phố cha\n people[neighbor] += people[node];\n degree[neighbor]--;\n\n // Nếu node cha trở thành node lá, thêm nó vào queue\n if (degree[neighbor] == 1 && neighbor != 0) {\n q.push(neighbor);\n visited[neighbor] = true; // Đánh dấu node cha là đã duyệt\n }\n }\n }\n }\n\n return fuelCost;\n }\n};\n", "memory": "168619" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n = roads.size() + 1;\n vector<int> people(n, 1), degree(n, 0);\n vector<vector<int>> graph(n);\n people[0] = 0;\n\n for (auto& road : roads) {\n graph[road[0]].push_back(road[1]);\n graph[road[1]].push_back(road[0]);\n degree[road[0]]++;\n degree[road[1]]++;\n }\n\n queue<int> q;\n for (int i = 1; i < n; i++) {\n if (degree[i] == 1) q.push(i);\n }\n\n long long ans = 0;\n while (!q.empty()) {\n int city = q.front(); q.pop();\n if (city == 0) continue;\n\n ans += (people[city] + seats - 1) / seats; // ceil(people[city] / seats)\n\n for (int neighbor : graph[city]) {\n if (--degree[neighbor] == 1) q.push(neighbor);\n people[neighbor] += people[city];\n }\n }\n\n return ans;\n }\n};\n", "memory": "170499" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n = roads.size();\n vector<vector<int>> adj(n + 1); // adjacency list\n vector<int> degree(n + 1, 0); // out-degree count\n vector<bool> vis(n + 1, false); // visited array\n\n // Construct the adjacency list and out-degree count\n for (int i = 0; i < n; i++) {\n adj[roads[i][0]].push_back(roads[i][1]);\n adj[roads[i][1]].push_back(roads[i][0]);\n degree[roads[i][0]]++;\n degree[roads[i][1]]++;\n }\n\n queue<int> q;\n vector<int> people(n + 1, 1); // people count for each node, initialized to 1 (each node initially has 1 person)\n long long ans = 0;\n\n // Initialize the queue with leaf nodes\n for (int i = 1; i <= n; i++) { // Start from node 1 to n (excluding the capital 0)\n if (degree[i] == 1) {\n q.push(i);\n vis[i] = true;\n }\n }\n\n // BFS traversal from leaf nodes towards the capital (node 0)\n while (!q.empty()) {\n int node = q.front();\n q.pop();\n\n if (node == 0) continue; // Skip processing for the capital\n\n for (auto neighbor : adj[node]) {\n if (!vis[neighbor]) {\n // Calculate trips required\n ans += (people[node] + seats - 1) / seats; // Ceiling of people[node] / seats\n\n // Add people to the parent node\n people[neighbor] += people[node];\n\n // Decrease the degree of the parent node\n if (--degree[neighbor] == 1 && neighbor != 0) {\n q.push(neighbor);\n vis[neighbor] = true;\n }\n }\n }\n }\n\n return ans;\n }\n};\n", "memory": "170499" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n long long minCost=0,n=roads.size()+1;\n vector<int>parent(n,-1);\n vector<vector<int>>adj(n);\n for(int i=0;i<n-1;i++){\n adj[roads[i][0]].push_back(roads[i][1]);\n adj[roads[i][1]].push_back(roads[i][0]);\n }\n vector<int>q;\n q.push_back(0);\n int i=0;\n while(i<q.size()){\n for(auto node:adj[q[i]]){\n if(parent[q[i]]!=node){\n parent[node]=q[i];\n q.push_back(node);\n }\n }\n i++;\n }\n vector<int>peopleInCar(n,1);\n cout<<q.size();\n long long ans=0;\n for(int i=n-1;i>0;i--){\n int par=parent[q[i]];\n if(par>0)peopleInCar[par]+=peopleInCar[q[i]];\n ans+=ceil((double)peopleInCar[q[i]]/seats);\n }\n return ans;\n }\n};", "memory": "172379" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long ans = 0;\n \n void dfs(int v,int par,vector<int> g[],vector<long long>& child,int seats){\n for(auto c:g[v]){\n if(c == par) continue;\n dfs(c,v,g,child,seats);\n //this is the number of children from a branch that is\n //the people who want to traver from a road \n int bchild = (1 + child[c]);\n //so petrol needed will be this\n ans += ((bchild + seats - 1)/seats);\n //now store the total child from all branches of that\n //node also\n child[v] += bchild;\n }\n }\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n = roads.size();\n vector<int> g[n+1];\n \n for(int i=0;i<n;i++){\n g[roads[i][0]].push_back(roads[i][1]);\n g[roads[i][1]].push_back(roads[i][0]);\n }\n \n vector<long long> child(n+1 , 0);\n dfs(0,-1,g,child,seats);\n \n // for(int i=0;i<n+1;i++) cout<<child[i]<<\" \";\n // cout<<endl;\n return ans;\n }\n};", "memory": "172379" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n pair<long long,int> dfs(vector<vector<int>>& tree,int seats,int parent,int node) {\n int subNodes = 1;\n long long fuel = 0;\n for (int i : tree[node]) {\n if (i != parent) {\n pair<long long,int> temp = dfs(tree,seats,node,i);\n fuel = fuel + temp.first;\n subNodes = subNodes + temp.second;\n fuel = fuel +(long long) (temp.second/seats + (int)(temp.second%seats != 0));\n }\n }\n return {fuel,subNodes};\n }\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n int m = roads.size();\n vector<vector<int>> tree(m+1);\n for (int i = 0;i < m;i++) {\n tree[roads[i][0]].push_back(roads[i][1]);\n tree[roads[i][1]].push_back(roads[i][0]);\n }\n pair<long long,int> ans = dfs(tree,seats,0,0);\n \n return ans.first;\n }\n};", "memory": "174259" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\nvector<vector<int>>g;\n\nlong long dfs(int node, int parent, int seats, long long &ans){\n\n long long contri = 1;\n for(auto &child : g[node]){\n if(child != parent){\n long long dis = dfs(child, node, seats, ans); \n contri += dis;\n ans += (dis + seats-1)/seats;\n }\n }\n\n return contri;\n}\n\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n // make whatever we need to make\n int n = roads.size();\n g = vector<vector<int>> (n+1);\n\n for(auto &road : roads){\n int u = road[0], v = road[1];\n g[u].push_back(v);\n g[v].push_back(u);\n }\n long long ans = 0;\n\n dfs(0, -1, seats, ans);\n \n return ans;\n }\n};", "memory": "174259" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n#if 1\n //有點像車行發車的感覺,由乘客數量決定每個站點的發車數量,最後再由每站的發車數量來得知總體油量消耗,\n //return值表示這一站要搭車的乘客數量\n long long answer = 0;\n int DFS(int index, vector<vector<int>>& adj, vector<unsigned char>& visited, int seats) {\n visited[index] = 1;\n //if(visited[index] == true)\n // return 0;\n \n int passengers = 1;\n //visited[index] = true;\n for(int city : adj[index]) {\n if(visited[city] == 0)\n passengers += DFS(city, adj, visited, seats);\n }\n //cout << \"index: \" << index << \" passengers: \" << passengers << endl;\n\n //回到capital就到終點了,可以不用再派車了!\n if(index != 0) {\n answer += (passengers - 1) / seats + 1;\n //if(passengers % seats == 0)\n // answer += passengers / seats;\n //else\n // answer += passengers / seats + 1;\n }\n return passengers;\n }\n\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n //ios_base::sync_with_stdio(false);\n //cin.tie(NULL);\n //cout.tie(NULL);\n\n //1.先建立相鄰矩陣(P.S.因為是雙向矩陣為了避免cycle,需要額外用visited array去阻止回溯)\n int limit = roads.size() ; //題目條件有說邊數為點數-1,如果沒說的話就得用hash_map去存相鄰陣列\n vector<unsigned char> visited(limit + 1, 0); //千萬不要用vector<bool>效率和記憶體反而更差...\n vector<vector<int>> adj(limit + 1);\n for(int i = 0; i < limit; ++i) {\n int u = roads[i][0], v = roads[i][1];\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n\n //2.再透過DFS去累加答案\n DFS(0, adj, visited, seats);\n return answer;\n }\n#else\n long long fuel = 0;\n int dfs(int node, vector<vector<int>>& graph, vector<int> &vis, int seats) {\n vis[node] = 1;\n int people = 1;\n\n for (int neighbor : graph[node]) {\n if (!vis[neighbor]) {\n people += dfs(neighbor, graph, vis, seats);\n }\n }\n\n if (node != 0) {\n fuel += (people + seats - 1) / seats; //same as ceil(peopel / seats) \n }\n\n return people;\n }\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n = roads.size();\n vector<vector<int>> graph(n + 1);\n for (int i = 0; i < n; i++) {\n int u = roads[i][0];\n int v = roads[i][1];\n\n graph[u].push_back(v);\n graph[v].push_back(u);\n }\n\n vector<int> vis(n + 1, 0);\n dfs(0, graph, vis, seats);\n\n return fuel;\n }\n#endif\n};", "memory": "176139" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n#if 1\n //有點像車行發車的感覺,由乘客數量決定每個站點的發車數量,最後再由每站的發車數量來得知總體油量消耗,\n //return值表示這一站要搭車的乘客數量\n long long answer = 0;\n int DFS(int index, vector<vector<int>>& adj, vector<unsigned char>& visited, int seats) {\n visited[index] = 1;\n //if(visited[index] == true)\n // return 0;\n \n int passengers = 1;\n //visited[index] = true;\n for(int city : adj[index]) {\n if(visited[city] == 0)\n passengers += DFS(city, adj, visited, seats);\n }\n //cout << \"index: \" << index << \" passengers: \" << passengers << endl;\n\n //回到capital就到終點了,可以不用再派車了!\n if(index != 0) {\n answer += (passengers - 1) / seats + 1;\n //if(passengers % seats == 0)\n // answer += passengers / seats;\n //else\n // answer += passengers / seats + 1;\n }\n return passengers;\n }\n\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n //ios_base::sync_with_stdio(false);\n //cin.tie(NULL);\n //cout.tie(NULL);\n\n //1.先建立相鄰矩陣(P.S.因為是雙向矩陣為了避免cycle,需要額外用visited array去阻止回溯)\n int limit = roads.size() ; //題目條件有說邊數為點數-1,如果沒說的話就得用hash_map去存相鄰陣列\n vector<unsigned char> visited(limit + 1, 0); //千萬不要用vector<bool>效率和記憶體反而更差...\n vector<vector<int>> adj(limit + 1);\n for(int i = 0; i < limit; ++i) {\n int u = roads[i][0], v = roads[i][1];\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n\n //2.再透過DFS去累加答案\n DFS(0, adj, visited, seats);\n return answer;\n }\n#else\n long long fuel = 0;\n int dfs(int node, vector<vector<int>>& graph, vector<int> &vis, int seats) {\n vis[node] = 1;\n int people = 1;\n\n for (int neighbor : graph[node]) {\n if (!vis[neighbor]) {\n people += dfs(neighbor, graph, vis, seats);\n }\n }\n\n if (node != 0) {\n fuel += (people + seats - 1) / seats; //same as ceil(peopel / seats) \n }\n\n return people;\n }\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n = roads.size();\n vector<vector<int>> graph(n + 1);\n for (int i = 0; i < n; i++) {\n int u = roads[i][0];\n int v = roads[i][1];\n\n graph[u].push_back(v);\n graph[v].push_back(u);\n }\n\n vector<int> vis(n + 1, 0);\n dfs(0, graph, vis, seats);\n\n return fuel;\n }\n#endif\n};", "memory": "176139" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int dfs(int node, vector<vector<int>>& roads, vector<vector<int>>& adj,\n vector<int>& vis, long long& ans, int seats) {\n vis[node] = 1;\n int cnt = 1;\n\n for (int adjNode : adj[node]) {\n if (!vis[adjNode])\n cnt += dfs(adjNode, roads, adj, vis, ans, seats);\n }\n if (node != 0) {\n ans += (ceil(1.0 * cnt / seats));\n }\n return cnt;\n }\n\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n long long ans = 0;\n\n vector<vector<int>> adj(roads.size() + 1);\n vector<int> vis(roads.size() + 1, 0);\n for (int i = 0; i < roads.size(); i++) {\n int u = roads[i][0];\n int v = roads[i][1];\n\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n dfs(0, roads, adj, vis, ans, seats);\n return ans;\n }\n};", "memory": "178019" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int n;\n vector<vector<int>> g;\n vector<int> vis;\n vector<int> subsize;\n\n long long dfs(int node, int seats, long long &fuel) {\n vis[node] = 1;\n int representatives = 1; // Each city has one representative\n\n // Traverse all neighbors\n for (auto neighbor : g[node]) {\n if (!vis[neighbor]) {\n // Count the representatives from the subtrees\n representatives += dfs(neighbor, seats, fuel);\n }\n }\n\n subsize[node] = representatives;\n\n // Calculate the fuel needed for the representatives from this node\n if (node != 0) { // Capital city doesn't need to move anywhere\n fuel += (representatives + seats - 1) / seats;\n }\n\n return representatives;\n }\n\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n n = roads.size() + 1;\n g.resize(n);\n vis.assign(n, 0);\n subsize.assign(n, 0);\n\n for (auto &road : roads) {\n g[road[0]].push_back(road[1]);\n g[road[1]].push_back(road[0]);\n }\n\n long long fuel = 0;\n dfs(0, seats, fuel); // Start DFS from the capital city (node 0)\n \n long long fuels = 0;\n for(int i=1; i<n; i++){\n fuels += (subsize[i] + seats -1)/seats;\n }\n return fuels;\n }\n};\n", "memory": "178019" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n\n long long int dfs(int index, vector<vector<int>>& graph, int seats, long long int& ans, vector<int>& visited){\n long long int count=1;\n visited[index]=1;\n for(auto i:graph[index]){\n if(visited[i]==0){\n long long int a=dfs(i,graph,seats,ans,visited);\n if(a%seats==0){\n ans=ans+(a/seats);\n }\n else{\n ans=ans+(a/seats)+1;\n }\n count=count+a;\n }\n }\n return count;\n }\n\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n=roads.size()+1;\n vector<vector<int>> graph(n);\n for(int i=0;i<n-1;i++){\n graph[roads[i][0]].push_back(roads[i][1]);\n graph[roads[i][1]].push_back(roads[i][0]);\n }\n long long int ans=0;\n vector<int> visited(n,0);\n long long int count=dfs(0,graph,seats,ans,visited);\n return ans;\n }\n};", "memory": "179899" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\nprivate:\n std::vector<std::vector<int> > RebuildMap(std::vector<std::vector<int> > &roads) {\n std::vector<std::vector<int> > country(roads.size() + 1);\n for (auto &road : roads) {\n country[road[0]].push_back(road[1]);\n country[road[1]].push_back(road[0]);\n }\n return country;\n }\n\n void MoveRepresentative(int city, int prev, std::vector<std::vector<int> > &country, std::vector<int> &reprs, int seats, int64_t &fuel) {\n if (country[city].size() == 1 && country[city][0] == prev) {\n --reprs[city];\n ++reprs[country[city][0]];\n ++fuel;\n return;\n }\n for (int next : country[city]) {\n if (next != prev) {\n MoveRepresentative(next, city, country, reprs, seats, fuel);\n fuel += reprs[next] / seats + (reprs[next] % seats == 0 ? 0 : 1);\n reprs[city] += reprs[next];\n reprs[next] = 0;\n }\n }\n }\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n auto country = RebuildMap(roads);\n std::vector<int> reprs(roads.size() + 1, 1);\n int64_t total_fuel = 0;\n MoveRepresentative(0, -1, country, reprs, seats, total_fuel);\n return total_fuel;\n }\n};", "memory": "179899" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
1
{ "code": "#include <iostream>\n#include <vector>\n#include <cmath>\n\nusing namespace std;\n\nclass Solution {\npublic:\n // Function to calculate the minimum fuel cost\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n = roads.size() + 1; // Number of nodes (cities)\n \n // Create adjacency list for the tree graph\n vector<vector<int>> adj(n);\n for (const auto& road : roads) {\n adj[road[0]].push_back(road[1]);\n adj[road[1]].push_back(road[0]);\n }\n\n // Vectors to store depth and subtree size\n vector<int> dp(n, 0), sm(n, 0);\n long ans = 0; // Variable to store the final answer\n\n // Call DFS to calculate the number of cars required\n dfs(0, -1, adj, dp, sm, ans, seats);\n\n return ans; // Return the total minimum fuel cost\n }\n\nprivate:\n // DFS function to calculate the minimum number of cars (fuel cost)\n int dfs(int src, int par, vector<vector<int>>& adj, vector<int>& dp, vector<int>& sm, long& ans, int seats) {\n sm[src]++; // Increment the number of people at this node\n for (auto &neighbor : adj[src]) {\n if (neighbor != par) {\n dp[neighbor] = 1 + dp[src]; // Set the depth of the neighbor\n sm[src] += dfs(neighbor, src, adj, dp, sm, ans, seats); // Recursive DFS call for neighbors\n }\n }\n\n // If it's not the root node, calculate the fuel (car requirement)\n if (src != 0) {\n if (sm[src] <= seats) {\n ans++; // If the number of people is less than or equal to seats, use 1 car\n } else {\n // Calculate the number of cars based on the number of people\n ans += (sm[src] / seats); // Full cars\n if (sm[src] % seats != 0) {\n ans++; // Add one more car for remaining people\n }\n }\n }\n\n return sm[src]; // Return the number of people in the subtree rooted at `src`\n }\n};", "memory": "181779" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n\n void dfs(int node , vector<vector<int>>&adj , vector<int>&vis , vector<long long int> &people)\n {\n vis[node]=1;\n\n \n\n \n people[node]=1;\n long long int child =0;\n\n for(auto it: adj[node])\n {\n if(!vis[it]) \n {\n dfs(it,adj,vis,people);\n people[node] += people[it];\n }\n }\n\n \n return ;\n\n }\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n\n int n = roads.size()+1;\n\n vector<vector<int>>adj(n);\n\n for(int i=0; i<roads.size() ;i++)\n {\n adj[roads[i][0]].push_back(roads[i][1]);\n adj[roads[i][1]].push_back(roads[i][0]);\n }\n\n long long int ans =0;\n\n vector<long long int>people(n);\n vector<int>vis(n);\n\n dfs(0,adj,vis,people);\n\n for(int i=0; i<n ;i++) cout<<people[i]<<\" \";\n for(int i=1; i<n ;i++)\n {\n ans += 1ll*( (people[i] + seats - 1)/seats );\n // if(people[i]%seats) ans++;\n }\n\n return ans;\n \n }\n};", "memory": "181779" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n\n void dfs(int v, int par, vector<vector<int>> &adj, vector<int> &size, int seats, vector<long long> &ans){\n for(int &u: adj[v]){\n if(u==par){\n continue;\n }\n dfs(u,v,adj,size,seats,ans);\n ans[v]+=(size[u]+seats-1)/seats;\n ans[v]+=ans[u];\n size[v]+=size[u];\n }\n }\n\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n=roads.size()+1;\n vector<vector<int>> adj(n);\n for(auto &road: roads){\n adj[road[0]].push_back(road[1]);\n adj[road[1]].push_back(road[0]);\n }\n vector<int> size(n,1);\n vector<long long> ans(n,0ll);\n dfs(0,-1,adj,size,seats,ans);\n return ans[0];\n }\n};", "memory": "183659" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n \n int dfs(int cur,int parent,vector<int> adjaList[],int& seats,long long& result){\n int representatives = 1;\n\n for(auto children:adjaList[cur]){\n if(children == parent)continue;\n\n int path = dfs(children,cur,adjaList,seats,result);\n result = result + (path + seats - 1)/ seats;\n \n representatives += path;\n\n } \n\n return representatives;\n\n }\n \n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n \n long long result = 0;\n \n int n = roads.size() + 1;\n vector<int> adjaList[n];\n\n for(auto road:roads){\n adjaList[road[0]].push_back(road[1]);\n adjaList[road[1]].push_back(road[0]);\n }\n\n dfs(0,-1,adjaList,seats,result);\n\n return result;\n\n }\n};", "memory": "183659" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n \n int n = roads.size() + 1;\n long long res=0;\n vector<int> graph[n];\n for(auto edge:roads){\n int u=edge[0],v=edge[1];\n graph[u].push_back(v);\n graph[v].push_back(u);\n }\n\n dfs(0,-1,0,graph,seats,res);\n return res;\n }\n int dfs(int node,int prev,int dist,vector<int> graph[],const int& seats,long long& res){\n\n int people=1; // every node has one person\n for(int &x:graph[node]){\n if(x == prev)\n continue;\n people += dfs(x,node,dist+1,graph,seats,res);\n }\n if(dist == 0)\n return 0; // tricky, dry-run\n int del = (people / seats);\n people %= seats;\n res += del*dist;\n if(people !=0) \n res++;\n return people;\n }\n};", "memory": "185539" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n \n int n = roads.size() + 1;\n long long res=0;\n vector<int> graph[n];\n for(auto edge:roads){\n int u=edge[0],v=edge[1];\n graph[u].push_back(v);\n graph[v].push_back(u);\n }\n\n dfs(0,-1,0,graph,seats,res);\n return res;\n }\n int dfs(int node,int prev,int dist,vector<int> graph[],const int& seats,long long& res){\n\n int people=1; // every node has one person\n for(int &x:graph[node]){\n if(x == prev)\n continue;\n people += dfs(x,node,dist+1,graph,seats,res);\n }\n if(dist == 0)\n return 0; // tricky, dry-run\n int del = (people / seats);\n people %= seats;\n res += del*dist;\n if(people !=0) \n res++;\n return people;\n }\n};", "memory": "185539" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n=roads.size()+1;\n long long ans=0;\n vector<vector<int>> V(n,vector<int>());\n vector<int> deg(n,0);\n vector<int> pep(n,1);\n for(vector<int> v:roads){\n V[v[0]].push_back(v[1]);\n V[v[1]].push_back(v[0]);\n deg[v[0]]++;\n deg[v[1]]++;\n }\n queue<int> q;\n for(int i=0;i<n;i++){\n if(deg[i]==1&&i!=0){\n q.push(i);\n deg[i]=0;\n }\n }\n \n while(!q.empty()){\n int curr=q.front();\n q.pop();\n \n \n ans+=pep[curr]/seats;\n if(pep[curr]%seats>0){\n ans++;\n }\n \n for(int i:V[curr]){\n\n if(deg[i]>0){\n deg[i]--;\n pep[i]+=pep[curr];\n if(deg[i]==1&&i!=0){\n q.push(i);\n deg[i]=0;\n }\n }\n }\n \n }\n return ans;\n }\n};", "memory": "187419" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n\tint dis[100100], sz[100100]; vector<int> adj[100100];\n long long ans = 0;\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n \tfor (auto &e : roads) {\n \t\tadj[e[0]].push_back(e[1]);\n \t\tadj[e[1]].push_back(e[0]);\n \t} \n function< void(int, int) > dfs_sub = [&] (int nn, int pp) {\n if (pp != -1) dis[nn] = 1 + dis[pp], sz[nn] = 1;\n \tfor (auto &ch : adj[nn]) {\n \t\tif (ch != pp) {\n \t\t\tdfs_sub(ch, nn);\n \t\t\tsz[nn] += sz[ch];\n \t\t}\n \t}\n\n if (nn && sz[nn] >= seats) {\n ans += 1LL * (seats + dis[nn]) * (sz[nn] / seats);\n sz[nn] %= seats;\n if (sz[nn] == 0) ans--;\n }\n \treturn ;\n };\n dfs_sub(0, -1);\n \treturn ans + sz[0]; \n }\n};", "memory": "187419" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n pair<long long,long long> dfs(long long x,long long p,vector<vector<long long>> &adj,long long& ans,long long h,long long s){\n if((adj[x].size()==1 && adj[x][0]==p)){\n ans += h;\n return {1,s};\n }\n long long cap = 0;\n long long oc = 0;\n for(auto u:adj[x]){\n if(u!=p){\n pair<long long,long long> y = dfs(u,x,adj,ans,h+1,s);\n cap+=y.second;\n oc+= y.first;\n }\n }\n //cout<<x<<\"--\"<<oc<<\" \"<<cap<<endl;\n if(oc+1>cap){\n ans += h;\n return {1,s};\n }\n else{\n ans -= ((cap-oc-1)/s)*(h);\n return{oc+1,cap-(((cap-oc-1)/s)*s)};\n }\n }\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n long long n = roads.size()+1;\n vector<vector<long long>> adj(n);\n for(auto x: roads){\n adj[x[0]].push_back(x[1]);\n adj[x[1]].push_back(x[0]);\n }\n long long ans = 0;\n pair<long long,long long> res = dfs(0,-1,adj,ans,0,seats);\n return ans;\n }\n};", "memory": "196819" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n void dfs(vector<vector<int>> &adj,vector<long long> &child,int seats,int par,int curr,long long &ans){\n for(auto it : adj[curr]){\n if(it == par) continue;\n dfs(adj,child,seats,curr,it,ans);\n int bchild = 1 + child[it];\n\n ans += ((bchild + seats - 1)/seats);\n\n child[curr] += bchild;\n }\n }\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n = roads.size();\n vector<vector<int>> adj(n+1);\n for(int i = 0;i<n;i++){\n vector<int> temp = roads[i];\n adj[temp[0]].push_back(temp[1]);\n adj[temp[1]].push_back(temp[0]);\n }\n vector<long long> child(n+1,0);\n int par = -1;\n int curr = 0;\n long long ans = 0;\n dfs(adj,child,seats,par,curr,ans);\n return ans;\n }\n};", "memory": "196819" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n\n long long dfs(int node, long long &ans, vector<bool> &visited, int seats, vector<vector<int>> &adj) {\n\n visited[node] = true;\n long long cnt = 1;\n for (auto child : adj[node]) {\n if (visited[child] == false)\n cnt += dfs(child, ans, visited, seats, adj);\n }\n\n if (node != 0) {\n ans += 0LL + (cnt/seats) + (cnt%seats != 0);\n }\n return cnt;\n }\n\n long long minimumFuelCost(vector<vector<int>>& r, int seats) {\n\n int n = r.size()+1;\n vector<vector<int>> roads(n);\n for (int i = 0; i < n-1; i++) {\n int u = r[i][0], v = r[i][1];\n roads[u].push_back(v);\n roads[v].push_back(u);\n }\n \n vector<bool> visited(n, false);\n long long ans = 0;\n dfs(0, ans, visited, seats, roads);\n return ans;\n }\n};\n", "memory": "198699" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n \n long long ans = 0;\n \n int dfs(vector<vector<int>>& adj, int u, int seats, vector<bool>& vis)\n {\n // mark the current node as visited \n vis[u] = true; \n // count will keep track of no. of nodes (descendent + including) \n int count = 1;\n \n // call all the adjacent nodes \n for(auto v : adj[u])\n {\n if(vis[v] == false)\n {\n count += dfs(adj, v, seats, vis);\n }\n }\n \n // increment the ans \n if(u != 0)\n {\n ans += ceil((double) count / seats);\n }\n \n return count;\n }\n \n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n \n int n = roads.size(); \n // declare an adjacency list \n vector<vector<int>> adj(n + 1); \n // fill the adjacency list\n \n for(int i = 0; i < n; i++)\n {\n int u = roads[i][0]; \n int v = roads[i][1]; \n adj[u].push_back(v); \n adj[v].push_back(u);\n }\n \n // declare a visited array \n vector<bool> vis(n + 1, false);\n \n // call dfs function \n dfs(adj, 0, seats, vis);\n \n return ans;\n }\n};", "memory": "198699" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n //有點像車行發車的感覺,由乘客數量決定每個站點的發車數量,最後再由每站的發車數量來得知總體油量消耗,\n //return值表示乘客數量\n long long answer = 0;\n vector<vector<int>> adj;\n long long DFS(vector<bool>& visited, int index, int seats) {\n visited[index] = true;\n //if(visited[index] == true)\n // return 0;\n \n long long passengers = 1;\n visited[index] = true;\n for(int city : adj[index]) {\n if(visited[city] == false)\n passengers += DFS(visited, city, seats);\n }\n //cout << \"index: \" << index << \" passengers: \" << passengers << endl;\n\n //回到capital就到終點了,可以不用再派車了!\n if(index != 0) {\n if(passengers % seats == 0)\n answer += passengers / seats;\n else\n answer += passengers / seats + 1;\n }\n return passengers;\n }\n\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n\n //1.先建立相鄰矩陣(P.S.因為是雙向矩陣為了避免cycle,需要額外用visited array去阻止回溯)\n int nodes = roads.size() + 1; //題目條件有說邊數為點數-1,如果沒說的話就得用hash_map去存相鄰陣列\n vector<bool> visited(nodes); \n adj.resize(nodes);\n for(vector<int>& road : roads) {\n adj[road[0]].push_back(road[1]);\n adj[road[1]].push_back(road[0]);\n }\n\n //2.再透過DFS去累加答案\n DFS(visited, 0, seats);\n return answer;\n }\n};", "memory": "200579" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n //有點像車行發車的感覺,由乘客數量決定每個站點的發車數量,最後再由每站的發車數量來得知總體油量消耗,\n //return值表示乘客數量\n long long answer = 0;\n vector<vector<int>> adj;\n vector<bool> visited;\n long long DFS(int index, int seats) {\n visited[index] = true;\n //if(visited[index] == true)\n // return 0;\n \n long long passengers = 1;\n visited[index] = true;\n for(int city : adj[index]) {\n if(visited[city] == false)\n passengers += DFS(city, seats);\n }\n //cout << \"index: \" << index << \" passengers: \" << passengers << endl;\n\n //回到capital就到終點了,可以不用再派車了!\n if(index != 0) {\n if(passengers % seats == 0)\n answer += passengers / seats;\n else\n answer += passengers / seats + 1;\n }\n return passengers;\n }\n\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n\n //1.先建立相鄰矩陣(P.S.因為是雙向矩陣為了避免cycle,需要額外用visited array去阻止回溯)\n int nodes = roads.size() + 1; //題目條件有說邊數為點數-1,如果沒說的話就得用hash_map去存相鄰陣列\n visited.resize(nodes); \n adj.resize(nodes);\n\n for(vector<int>& road : roads) {\n adj[road[0]].push_back(road[1]);\n adj[road[1]].push_back(road[0]);\n }\n\n //2.再透過DFS去累加答案\n DFS(0, seats);\n return answer;\n }\n};", "memory": "200579" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "#define ll long long \nclass Solution {\npublic:\n ll res = 0;\n vector<ll> cars;\n vector<ll> ssiz;\n int st;\n vector<vector<int>> adj;\n void dfs(int nod,int par){\n ssiz[nod] = 1;\n cars[nod] = 1;\n for(auto i : adj[nod]){\n if(i!=par){\n dfs(i,nod);\n ssiz[nod]+=ssiz[i];\n res+=(ssiz[i]/st+((ssiz[i]%st)!=0));\n cars[nod]+=(ssiz[i]/st+((ssiz[i]%st)!=0));\n }\n }\n }\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n = roads.size()+1;\n st = seats;\n cars.resize(n);\n ssiz.resize(n);\n adj.resize(n);\n for(auto i : roads){\n adj[i[0]].push_back(i[1]);\n adj[i[1]].push_back(i[0]);\n }\n dfs(0,-1);\n return res;\n\n }\n};", "memory": "202459" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "void dfs(int curr,vector<int>& child,vector<vector<int>>& g,vector<bool>& vis){\n vis[curr] = 1;\n for(auto & it : g[curr]){\n if(vis[it]) continue;\n dfs(it,child,g,vis);\n child[curr] += child[it];\n }\n}\nclass Solution {\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n = roads.size()+1;\n vector<vector<int>> g(n);\n for(auto & it : roads){\n int x = it[0], y = it[1];\n g[x].push_back(y);\n g[y].push_back(x);\n }\n vector<int> child(n,1);\n vector<bool> vis(n,0);\n dfs(0,child,g,vis);\n for(int i = 0; i < n; i++){\n vis[i] = 0;\n }\n queue<int> q;\n \n q.push(0);\n vis[0] = 1;\n long long cnt = 0, ans = 0;\n while(!q.empty()){\n long long node = q.front();\n q.pop();\n for(auto & it : g[node]){\n if(vis[it]) continue;\n cnt += child[it]/seats;\n if(child[it] % seats != 0) cnt++;\n q.push(it);\n vis[it] = 1;\n \n \n }\n }\n return cnt;\n }\n};", "memory": "202459" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> endNodes;\n\n int dfs(int node, vector<int> g[], vector<int>& vis, int& seats,\n long long& p) {\n vis[node] = 1;\n int flag = 0, ans = 0;\n for (auto child : g[node]) {\n if (vis[child])\n continue;\n\n flag = 1;\n int temp = dfs(child, g, vis, seats, p);\n ans+=temp;\n p += ceil((1.0 * temp) / seats);\n }\n // cout << node << \" \" << p << endl;\n return 1 + ans;\n }\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n = 0;\n for (auto it : roads) {\n n = max({n, it[0], it[1]});\n }\n // cout << n << endl;\n vector<int> g[n + 1];\n vector<int> vis(n + 1);\n for (auto road : roads) {\n g[road[0]].push_back(road[1]);\n g[road[1]].push_back(road[0]);\n }\n\n long long p = 0;\n dfs(0, g, vis, seats, p);\n return p;\n }\n};", "memory": "204339" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n auto [cost, num] = dfs(0, getAdj2(roads), seats);\n return cost;\n }\n\n vector<vector<int>> getAdj(const vector<vector<int>>& roads) {\n unordered_map<int, vector<int>> edges;\n int max = 0;\n for (const auto& r : roads) {\n edges[r[0]].emplace_back(r[1]);\n edges[r[1]].emplace_back(r[0]);\n if (max < r[0]) {\n max = r[0];\n }\n if (max < r[1]) {\n max = r[1];\n }\n }\n unordered_set<int> visited;\n queue<int> q;\n q.push(0);\n\n vector<vector<int>> adj(max + 1, std::vector<int>(0));\n while (!q.empty()) {\n int city = q.front();\n q.pop();\n visited.insert(city);\n for (int ac : edges[city]) {\n if (!visited.contains(ac)) {\n q.push(ac);\n adj[city].push_back(ac);\n }\n }\n }\n\n return adj;\n }\n\n vector<vector<int>> getAdj2(const vector<vector<int>>& roads) {\n int numRows = 0;\n for (const auto& r : roads) {\n int max2 = max(r[0], r[1]);\n if (numRows < max2) {\n numRows = max2;\n }\n }\n vector<vector<int>> adj(numRows + 1, vector<int>(0));\n\n for (const auto& r : roads) {\n adj[r[0]].emplace_back(r[1]);\n adj[r[1]].emplace_back(r[0]);\n }\n\n unordered_set<int> visited;\n queue<int> q;\n q.push(0);\n while (!q.empty()) {\n int city = q.front();\n q.pop();\n visited.insert(city);\n for (auto it = adj[city].begin(); it != adj[city].end(); ) {\n if (visited.contains(*it)) {\n it = adj[city].erase(it);\n } else {\n q.push(*it);\n ++it;\n }\n }\n }\n\n return adj;\n}\n\npair<int64_t, int32_t>\ndfs(int node, const vector<vector<int>>& adj, int s) {\n int64_t totalCost = 0;\n int32_t totalNum = 1;\n for (int i = 0; i < adj[node].size(); ++i) {\n const auto [cost, num] = dfs(adj[node][i], adj, s);\n totalCost += cost + (num + s - 1) / s;\n totalNum += num;\n }\n\n return {totalCost, totalNum};\n}\n}\n;", "memory": "204339" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n void countChild(int i, vector<int> adj[], vector<int> &child){\n child[i] = 1;\n for(auto it:adj[i]){\n if(!child[it]){\n countChild(it, adj, child);\n child[i] += child[it];\n }\n }\n }\n\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n if(roads.empty()) return 0;\n int k = seats;\n int n = roads.size();\n int v{-1};\n for(auto i:roads){\n v = max({v, i[0], i[1]});\n }\n vector<int> adj[v+1];\n for(auto i:roads){\n adj[i[0]].push_back(i[1]);\n adj[i[1]].push_back(i[0]);\n }\n queue<int> q;\n vector<int> vis(v+1), child(v+1);\n countChild(0, adj, child);\n q.push(0);\n vis[0] = 1;\n long long res{};\n while(!q.empty()){\n int node = q.front(); q.pop();\n for(auto i:adj[node]){\n if(!vis[i]){\n int cnt = child[i];\n res += cnt / k + (cnt%k != 0);\n q.push(i);\n vis[i] = 1;\n }\n }\n }\n return res;\n }\n};", "memory": "206219" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\n typedef long long ll;\n struct triple {\n ll first, second, third;\n };\n triple dfs(int p, int u, int seats, vector<int> graph[]) {\n ll dist = 0, cars = u != 0, rem_seats = u == 0 ? 0 : seats - 1;\n for(int v: graph[u]) {\n if (v == p) continue;\n triple temp = dfs(u, v, seats, graph);\n dist += temp.first, cars += temp.second, rem_seats += temp.third;\n }\n if (u != 0) {\n cars = (cars * seats - rem_seats + seats - 1) / seats;\n dist += cars;\n }\n // cout << u << \": \" << dist << \" \" << cars << \" \" << rem_seats % seats << endl;\n return {dist, cars, rem_seats % seats};\n }\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n = roads.size() + 1;\n vector<int> graph[n];\n for (vector<int> road: roads) {\n graph[road[0]].push_back(road[1]);\n graph[road[1]].push_back(road[0]);\n }\n return dfs(-1, 0, seats, graph).first;\n }\n};", "memory": "206219" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int dfs(vector<vector<int>>& adj, int n, int node, vector<bool>& visited, int& seats, long long& cost)\n {\n visited[node] = true;\n int subtreeSize = 0;\n for(int i=0; i<adj[node].size(); i++)\n {\n if(!visited[adj[node][i]])\n {\n visited[adj[node][i]] = true;\n int temp = dfs(adj, n, adj[node][i], visited, seats, cost);\n subtreeSize += temp;\n }\n }\n subtreeSize++;\n \n if(node != 0)\n {\n int carsReq = subtreeSize/seats + (subtreeSize%seats > 0);\n cost += carsReq;\n }\n return subtreeSize;\n }\n\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n long long cost = 0;\n int n = 0;\n for(int i=0; i<roads.size(); i++)\n {\n n = max(roads[i][0], roads[i][1]);\n }\n vector<vector<int>> adj(n+1, vector<int>());\n vector<bool> visited(n+1, false);\n\n for(int i=0; i<roads.size(); i++)\n {\n adj[roads[i][0]].push_back(roads[i][1]);\n adj[roads[i][1]].push_back(roads[i][0]);\n }\n\n int val = dfs(adj, n, 0, visited, seats, cost);\n return cost;\n }\n};", "memory": "208099" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int dfs(vector<vector<int>>& adj, int n, int node, vector<bool>& visited, int& seats, long long& cost)\n{\n visited[node] = true;\n int subtreeSize = 0;\n for(int i=0; i<adj[node].size(); i++)\n {\n if(!visited[adj[node][i]])\n {\n visited[adj[node][i]] = true;\n int temp = dfs(adj, n, adj[node][i], visited, seats, cost);\n subtreeSize += temp;\n }\n }\n subtreeSize++;\n \n if(node != 0)\n {\n int carsReq = subtreeSize/seats + (subtreeSize%seats > 0);\n cost += carsReq;\n }\n return subtreeSize;\n}\n\nlong long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n long long cost = 0;\n int n = 0;\n for(int i=0; i<roads.size(); i++)\n {\n n = max(n, max(roads[i][0], roads[i][1]));\n }\n vector<vector<int>> adj(n+1, vector<int>());\n vector<bool> visited(n+1, false);\n\n for(int i=0; i<roads.size(); i++)\n {\n adj[roads[i][0]].push_back(roads[i][1]);\n adj[roads[i][1]].push_back(roads[i][0]);\n }\n\n int val = dfs(adj, n, 0, visited, seats, cost);\n return cost;\n}\n};", "memory": "208099" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n // number of seats available, number of cars taken, ltrs of fuel\n pair<long long, pair<long long, long long>> dfs(long long node, long long par, vector<vector<long long>>& adj, long long seats, vector<long long>& sub) {\n pair<long long, pair<long long, long long>> ans;\n sub[node]++;\n for(auto& e : adj[node]) {\n if(e != par) {\n pair<long long, pair<long long, long long>> curr = dfs(e, node, adj, seats, sub);\n sub[node] += sub[e];\n ans.first += curr.first;\n ans.second.first += curr.second.first;\n ans.second.second += curr.second.second;\n }\n }\n\n if(par == -1) return ans;\n\n if(ans.first == 0) {\n ans.second.second += ans.second.first;\n ans.first += seats - 1;\n ans.second.first += 1;\n ans.second.second += 1;\n return ans;\n } else {\n long long gaadi = ans.second.first;\n long long totalSeats = gaadi * seats;\n long long members = sub[node];\n long long space = totalSeats - members;\n long long carRemove = space / seats;\n long long remSpace = space % seats;\n long long cars = gaadi - carRemove;\n return {remSpace, {cars, ans.second.second + cars}};\n }\n }\n\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n long long n = roads.size() + 1;\n\n vector<vector<long long>> adj(n, vector<long long>());\n for(auto& e : roads) {\n adj[e[0]].push_back(e[1]);\n adj[e[1]].push_back(e[0]);\n }\n\n vector<long long> sub(n);\n\n return dfs(0, -1, adj, seats, sub).second.second;\n }\n};", "memory": "209979" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n = roads.size() + 1;\n vector<vector<int>> g(n);\n for(vector<int> x : roads) {\n g[x[0]].push_back(x[1]);\n g[x[1]].push_back(x[0]);\n }\n function<pair<long long, long long>(int, int)> fun = [&](int x, int p) {\n long long c1 = 0, c2 = 0;\n for(int y : g[x]) {\n if(y != p) {\n pair<long long, long long> p = fun(y, x);\n c1 += p.first;\n c2 += p.second;\n }\n }\n if(x != 0) {\n c2 += 1;\n long long t = (c2 + seats - 1) / seats;\n c1 += t;\n }\n pair<long long, long long> p1;\n p1.first = c1;\n p1.second = c2;\n return p1;\n };\n\n pair<long long, long long> ans = fun(0, -1);\n return ans.first; \n }\n};", "memory": "209979" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n long long myceil(long long x,int seats){\n if(x%seats==0) return x/seats;\n return (long long)ceil(x/seats)+1;\n }\n vector<long long> func(int node,vector<int> adj[],vector<int>& vis,int seats){\n vis[node]=1;\n long long people=1;long long fuel=0;\n for(auto adjnode:adj[node]){\n if(!vis[adjnode]){\n vector<long long> temp = func(adjnode,adj,vis,seats);\n people+=temp[0];\n fuel+=temp[1];\n }\n }\n if(node!=0)\n fuel+=myceil(people,seats);\n return {people,fuel};\n }\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int N = roads.size();\n vector<int> adj[N+1];\n for(int i=0;i<N;i++){\n adj[roads[i][0]].push_back(roads[i][1]);\n adj[roads[i][1]].push_back(roads[i][0]);\n }\n vector<int> vis(N+1);\n vector<long long> ans = func(0,adj,vis,seats);\n return ans[1];\n }\n};", "memory": "211859" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n long long myceil(long long x,int seats){\n if(x%seats==0) return x/seats;\n return (long long)(x/seats)+1;\n }\n vector<long long> func(int node,vector<int> adj[],vector<int>& vis,int seats){\n vis[node]=1;\n long long people=1;long long fuel=0;\n for(auto adjnode:adj[node]){\n if(!vis[adjnode]){\n vector<long long> temp = func(adjnode,adj,vis,seats);\n people+=temp[0];\n fuel+=temp[1];\n }\n }\n if(node!=0)\n fuel+=myceil(people,seats);\n return {people,fuel};\n }\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int N = roads.size();\n vector<int> adj[N+1];\n for(int i=0;i<N;i++){\n adj[roads[i][0]].push_back(roads[i][1]);\n adj[roads[i][1]].push_back(roads[i][0]);\n }\n vector<int> vis(N+1);\n vector<long long> ans = func(0,adj,vis,seats);\n return ans[1];\n }\n};", "memory": "211859" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n struct City\n {\n City()\n : cost(0ll), car(0), seats(0), id(-1)\n {\n }\n City(long long cost, int car, int seats, int id)\n : cost(cost), car(car), seats(seats), id(id)\n {\n\n }\n long long cost;\n int car;\n int seats;\n int id;\n };\n /*\n\n if seats = 2\n\n (f, c, s)\n 5 (0, 1, 1)\n |\n 4 6\n \\ / \n 2 3 (car, seats)\n \\ /\n 1\n |\n 0\n\n (curr-cost, curr-car, remain-seats)\n 5 (0, 0, 0) -> 4 (1, 1, 1) -> 2 (2, 1, 0) -> 1 (4, 2, 1)\n 6 (0, 0, 0) -> 3 (1, 1, 1) -> 1 (2, 1, 0) \n 1 (6, 3, 1) -> 0 (9, 3, 0)\n\n if seats = 3\n\n 5 (0, 0, 0) -> 4 (1, 1, 2) -> 2 (2, 1, 1) -> 1 (3, 1, 0)\n 6 (0, 0, 0) -> 3 (1, 1, 2) -> 1 (2, 1, 1)\n 1 (5, 2, 1) -> 0 (7, 2, 0)\n\n if seats = 5\n 5 (0, 0, 0) -> 4 (1, 1, 4) -> 2 (2, 1, 3) -> 1 (3, 1, 2)\n 6 (0, 0, 0) -> 3 (1, 1, 4) -> 1 (2, 1, 3)\n 1 (5, 2, 5) -> 1 (5, 1, 0) -> 1 (5, 2, 5) -> 0 (7, 2, 4)\n\n if seats = 6\n 5 (0, 0, 0) -> 4 (1, 1, 5) -> 2 (2, 1, 4) -> 1 (3, 1, 3)\n 6 (0, 0, 0) -> 3 (1, 1, 5) -> 1 (2, 1, 4)\n 1 (5, 2, 7) -> 1 (5, 1, 2) -> 0 (6, 1, 1)\n\n seats = 11\n 1 (8, 8, 80)\n 1 (8, 1, 3) -> 0 (9, 1, 2)\n */\n void FindParentAndLeaf(const vector<vector<int> > &adj_list, vector<int> &indegrees, \n vector<City> &cities, queue<int> &bfs, vector<int> &parent,\n const int parent_id, const int node_id)\n {\n parent[node_id] = parent_id;\n int count = 0;\n for (int next_id : adj_list[node_id])\n {\n if (next_id != parent_id)\n {\n count++;\n FindParentAndLeaf(adj_list, indegrees, cities, bfs, parent, node_id, next_id);\n }\n }\n if (count == 0)\n {\n bfs.push(node_id);\n cities[node_id] = City(0, 0, 0, node_id);\n }\n indegrees[node_id] = count;\n }\n\n long long minimumFuelCost(vector<vector<int>>& roads, int seat_limit) \n {\n int n = roads.size() + 1;\n vector<City> cities(n);\n for (int i = 0; i < n; ++i)\n cities[i].id = i;\n std::queue<int> bfs;\n vector<int> indegree(n, 0), parent(n, 0);\n vector<vector<int> > adj_list(n);\n for (auto &road : roads)\n {\n adj_list[road.front()].push_back(road.back());\n adj_list[road.back()].push_back(road.front());\n }\n FindParentAndLeaf(adj_list, indegree, cities, bfs, parent, 0, 0);\n while (bfs.front() != 0)\n {\n int curr = bfs.front();\n bfs.pop();\n if (indegree[curr] == 0)\n {\n // can go to next\n int next = parent[curr];\n // \n if (cities[curr].seats > seat_limit)\n {\n cities[curr].car -= (cities[curr].seats / seat_limit);\n cities[curr].seats %= seat_limit;\n }\n if (cities[curr].seats == 0)\n {\n cities[curr].car++;\n cities[curr].seats += seat_limit;\n }\n //cout << curr << \": { \" << cities[curr].cost << \", \" << cities[curr].car << \", \" << cities[curr].seats << \"}\" << endl;\n indegree[next]--;\n cities[next].cost += (cities[curr].cost + cities[curr].car);\n cities[next].car += cities[curr].car;\n cities[next].seats += (cities[curr].seats - 1);\n if (indegree[next] == 0)\n {\n bfs.push(next);\n }\n }\n }\n return cities[0].cost;\n }\n};", "memory": "213739" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "typedef long long ll;\nclass Solution {\npublic:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n ll n=roads.size() + 1;\n vector<ll> subtreeSz(n,0);\n vector<vector<int>> adj(n);\n for(auto it:roads){\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n ll ans=0;\n function<void(int,int)> dfs=[&](int node,int parent){\n for(auto it:adj[node]){\n if(it==parent) continue;\n dfs(it,node);\n ans+=(subtreeSz[it]+seats-1)/seats;\n subtreeSz[node]+=subtreeSz[it];\n }\n subtreeSz[node]++;\n };\n dfs(0,-1);\n return ans;\n\n }\n};", "memory": "213739" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long dfs(vector<vector<int>>& graph,int source,long long& fuelConsumed,vector<bool>& visited,int seats) {\n visited[source] = true;\n\n long long countPeople = 1;\n \n for(auto i: graph[source]) {\n if(visited[i]) continue;\n countPeople += dfs(graph,i,fuelConsumed,visited,seats);\n }\n\n if(source != 0) {\n fuelConsumed += countPeople/seats + (countPeople%seats > 0);\n }\n return countPeople; \n }\n\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n if(roads.empty()) return 0;\n long long fuelConsumed = 0;\n vector<vector<int>> graph(roads.size()+1);\n for(auto i: roads) {\n graph[i[0]].push_back(i[1]);\n graph[i[1]].push_back(i[0]);\n }\n \n vector<bool> visited(roads.size()+1,false);\n dfs(graph,0,fuelConsumed,visited,seats);\n return fuelConsumed;\n }\n};", "memory": "215619" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long dfs(vector<vector<int>>& graph,int source,long long& fuelConsumed,vector<bool>& visited,int seats) {\n visited[source] = true;\n\n long long countPeople = 1;\n \n for(auto i: graph[source]) {\n if(visited[i]) continue;\n countPeople += dfs(graph,i,fuelConsumed,visited,seats);\n }\n\n if(source != 0) {\n fuelConsumed += countPeople/seats + (countPeople%seats > 0);\n }\n return countPeople; \n }\n\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n if(roads.empty()) return 0;\n long long fuelConsumed = 0;\n vector<vector<int>> graph(roads.size()+1);\n for(auto i: roads) {\n graph[i[0]].push_back(i[1]);\n graph[i[1]].push_back(i[0]);\n }\n \n vector<bool> visited(roads.size()+1,false);\n dfs(graph,0,fuelConsumed,visited,seats);\n return fuelConsumed;\n }\n};", "memory": "215619" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n long long answer = 0;\n\n void dfs(vector<vector<int>>& g, vector<pair<int, int> >& ans, int u, int p, int seats) {\n vector<pair<int, int> > pairs;\n for (const auto& ne : g[u]) {\n if (ne == p) continue;\n dfs(g, ans, ne, u, seats);\n pairs.push_back(ans[ne]);\n }\n if (pairs.empty()) {\n ans[u] = {1, 1};\n return;\n }\n ans[u] = {0, 1};\n for (const auto& p : pairs) {\n answer += p.first;\n ans[u].first += p.first;\n ans[u].second += p.second;\n }\n ans[u].first = (ans[u].second + seats - 1) / seats;\n // max(ans[u].first, (ans[u].second + seats - 1) / seats);\n }\n\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n = roads.size() + 1;\n vector<vector<int> > g(n);\n for (const auto& r : roads) {\n int u = r[0], v = r[1];\n g[u].push_back(v);\n g[v].push_back(u);\n } \n vector<pair<int, int> > ans(n);\n dfs(g, ans, 0, 0, seats);\n return answer;\n }\n};", "memory": "217499" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\nvector<list<int>>graph;\nvector<pair<long long,int> >price;//{fuel and no of person}\nvector<bool>visited;\nvoid dfs(int idx,int seats){\n for(auto &i:graph[idx]){\n if(!visited[i]){\n visited[i] = true;\n dfs(i,seats);\n price[idx].first+= price[i].first + (price[i].second/seats);\n price[idx].second+=price[i].second;\n if(price[i].second%seats != 0){\n price[idx].first+=1;\n }\n }\n }\n price[idx].second++;\n return;\n}\nlong long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n int n = roads.size();\n graph.resize(n+1,list<int>());\n price.resize(n+1,{0,0});\n visited.resize(n+1,false);\n for(auto &i:roads){\n graph[i[0]].push_back(i[1]);//no of car and no of person and fuel \n graph[i[1]].push_back(i[0]);\n }\n visited[0] = true;\n dfs(0,seats);\n return price[0].first;\n}\n};", "memory": "219379" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long dfs(int node, vector<bool> &vis, int seats, vector<int> &p, vector<vector<int>> &adj)\n{\n vis[node] = true;\n long long fuel = 0;\n for (auto k : adj[node])\n {\n if (!vis[k]){\n fuel += dfs(k, vis, seats, p, adj);\n p[node] += p[k];\n }\n }\n if(node != 0)\n fuel += (long long)ceil((double)p[node] / seats);\n return fuel;\n}\nlong long minimumFuelCost(vector<vector<int>> &roads, int seats)\n{\n int n = roads.size() + 1;\n vector<vector<int>> adj(n);\n for (auto it : roads)\n {\n adj[it[0]].push_back(it[1]);\n adj[it[1]].push_back(it[0]);\n }\n vector<bool> vis(n, false);\n vector<int> p(n, 1);\n return dfs(0, vis, seats, p, adj);\n}\n};", "memory": "219379" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "#define ll long long\nclass Solution {\n private:\n ll netFuel = 0;\n int dfs(int node, int seats, int parent, unordered_map<int, vector<int>>& adjMat) {\n int netPass = 0;\n for (int i : adjMat[node]) {\n if (parent != i)\n netPass += dfs(i, seats, node, adjMat);\n }\n\n netPass += 1;\n\n if (node != 0)\n netFuel += (ll)((netPass / seats) + (netPass % seats != 0));\n\n return netPass;\n }\n\n public:\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n unordered_map<int, vector<int>> adjMat;\n int len = roads.size();\n for (int i = 0; i < len; i++) {\n adjMat[roads[i][0]].push_back(roads[i][1]),\n adjMat[roads[i][1]].push_back(roads[i][0]);\n }\n\n dfs(0, seats, -1, adjMat);\n\n return netFuel;\n }\n};", "memory": "221259" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n unordered_map<int,vector<int>> hash;\n int seats;\n long long res=0;\n int dfs(int node,int prev){\n int p=0;\n\n for(int num:hash[node]){\n if(num!=prev){\n int pass=dfs(num,node);\n p+=pass;\n res+=(pass/seats)+(pass%seats!=0);\n }\n }\n return p+1;\n }\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n long long ans=0;\n this->seats=seats;\n\n for(int i=0;i<roads.size();i++){\n hash[roads[i][0]].push_back(roads[i][1]);\n hash[roads[i][1]].push_back(roads[i][0]);\n }\n\n dfs(0,-1);\n return res;\n }\n};", "memory": "221259" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n void dfs(unordered_map<int,vector<int>> &adj, int node, int par, vector<int> &size){\n size[node] = 1;\n for(auto it:adj[node]){\n if(it!= par){\n dfs(adj,it,node,size);\n }\n }\n if(par!=-1){\n size[par] += size[node];\n }\n }\n long long minimumFuelCost(vector<vector<int>>& r, int s) {\n unordered_map<int,vector<int>> adj;\n int n = r.size()+1;\n for(int i=0;i<n-1;i++){\n adj[r[i][0]].push_back(r[i][1]);\n adj[r[i][1]].push_back(r[i][0]);\n }\n long long ans = 0;\n vector<int> size(n,0);\n dfs(adj,0,-1,size);\n for( int i = 1 ; i<n ; ++i ){\n int cnt = size[i]; \n ans += 1ll*( (cnt + s - 1)/s );\n }\n return ans;\n }\n};", "memory": "223139" }
2,568
<p>There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> roads. The capital city is city <code>0</code>. You are given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidirectional road</strong> connecting cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>There is a meeting for the representatives of each city. The meeting is in the capital city.</p> <p>There is a car in each city. You are given an integer <code>seats</code> that indicates the number of seats in each car.</p> <p>A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.</p> <p>Return <em>the minimum number of liters of fuel to reach the capital city</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/22/a4c380025e3ff0c379525e96a7d63a3.png" style="width: 303px; height: 332px;" /> <pre> <strong>Input:</strong> roads = [[0,1],[0,2],[0,3]], seats = 5 <strong>Output:</strong> 3 <strong>Explanation:</strong> - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>2</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>3</sub> goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/16/2.png" style="width: 274px; height: 340px;" /> <pre> <strong>Input:</strong> roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> - Representative<sub>2</sub> goes directly to city 3 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to city 1 with 1 liter of fuel. - Representative<sub>2</sub> and representative<sub>3</sub> go together to the capital with 1 liter of fuel. - Representative<sub>1</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>5</sub> goes directly to the capital with 1 liter of fuel. - Representative<sub>6</sub> goes directly to city 4 with 1 liter of fuel. - Representative<sub>4</sub> and representative<sub>6</sub> go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/09/27/efcf7f7be6830b8763639cfd01b690a.png" style="width: 108px; height: 86px;" /> <pre> <strong>Input:</strong> roads = [], seats = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> No representatives need to travel to the capital city. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>roads.length == n - 1</code></li> <li><code>roads[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>roads</code> represents a valid tree.</li> <li><code>1 &lt;= seats &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long totalCost = 0;\n \n int dfs(int x, unordered_map<int, vector<int>>&adj, int seats, vector<int>&vis, int n){\n if(x>=n || x<0) return 0;\n\n vis[x] = 1;\n\n int totalPeople = 1, sz = adj[x].size();\n\n\n for(int i=0;i<sz;i++){\n if(vis[adj[x][i]]==0)\n totalPeople += dfs(adj[x][i], adj, seats, vis, n);\n }\n\n // cout<<x<<\" \"<<totalPeople<<endl;\n if(x!=0)\n totalCost += (totalPeople/seats) + (totalPeople%seats>0?1:0);\n\n return totalPeople;\n }\n\n long long minimumFuelCost(vector<vector<int>>& roads, int seats) {\n unordered_map<int, vector<int>>adj;\n\n int nroads = roads.size(), n = nroads+1, i;\n\n if(nroads==0) return totalCost;\n\n for(i=0;i<nroads;i++){\n adj[roads[i][0]].push_back(roads[i][1]);\n adj[roads[i][1]].push_back(roads[i][0]);\n }\n\n vector<int>vis(n,0);\n\n dfs(0, adj, seats, vis, n);\n\n return totalCost;\n }\n};", "memory": "223139" }