id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
2,346
<p>You are given a string <code>num</code> representing a large integer. An integer is <strong>good</strong> if it meets the following conditions:</p> <ul> <li>It is a <strong>substring</strong> of <code>num</code> with length <code>3</code>.</li> <li>It consists of only one unique digit.</li> </ul> <p>Return <em>the <strong>maximum good </strong>integer as a <strong>string</strong> or an empty string </em><code>&quot;&quot;</code><em> if no such integer exists</em>.</p> <p>Note:</p> <ul> <li>A <strong>substring</strong> is a contiguous sequence of characters within a string.</li> <li>There may be <strong>leading zeroes</strong> in <code>num</code> or a good integer.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> num = &quot;6<strong><u>777</u></strong>133339&quot; <strong>Output:</strong> &quot;777&quot; <strong>Explanation:</strong> There are two distinct good integers: &quot;777&quot; and &quot;333&quot;. &quot;777&quot; is the largest, so we return &quot;777&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> num = &quot;23<strong><u>000</u></strong>19&quot; <strong>Output:</strong> &quot;000&quot; <strong>Explanation:</strong> &quot;000&quot; is the only good integer. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> num = &quot;42352338&quot; <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> No substring of length 3 consists of only one unique digit. Therefore, there are no good integers. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= num.length &lt;= 1000</code></li> <li><code>num</code> only consists of digits.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string largestGoodInteger(string num) {\n string s=\"\";\n int n=num.size();\n \n for(int i=0;i<n-2;i++){\n if(num[i]==num[i+1] && num[i+1]==num[i+2]){\n string t=\"\";\n for(int j=0;j<3;j++) t+=num[i];\n if(s<t) s=t;\n }\n }\n return s;\n }\n};", "memory": "8900" }
2,346
<p>You are given a string <code>num</code> representing a large integer. An integer is <strong>good</strong> if it meets the following conditions:</p> <ul> <li>It is a <strong>substring</strong> of <code>num</code> with length <code>3</code>.</li> <li>It consists of only one unique digit.</li> </ul> <p>Return <em>the <strong>maximum good </strong>integer as a <strong>string</strong> or an empty string </em><code>&quot;&quot;</code><em> if no such integer exists</em>.</p> <p>Note:</p> <ul> <li>A <strong>substring</strong> is a contiguous sequence of characters within a string.</li> <li>There may be <strong>leading zeroes</strong> in <code>num</code> or a good integer.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> num = &quot;6<strong><u>777</u></strong>133339&quot; <strong>Output:</strong> &quot;777&quot; <strong>Explanation:</strong> There are two distinct good integers: &quot;777&quot; and &quot;333&quot;. &quot;777&quot; is the largest, so we return &quot;777&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> num = &quot;23<strong><u>000</u></strong>19&quot; <strong>Output:</strong> &quot;000&quot; <strong>Explanation:</strong> &quot;000&quot; is the only good integer. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> num = &quot;42352338&quot; <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> No substring of length 3 consists of only one unique digit. Therefore, there are no good integers. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= num.length &lt;= 1000</code></li> <li><code>num</code> only consists of digits.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string largestGoodInteger(string num) {\n string maxGoodInteger = \"\"; // Initialize an empty string to track the largest good integer.\n\n // Iterate through the string, checking each substring of length 3\n for (int i = 0; i <= num.size() - 3; i++) {\n // Check if the current substring has three identical digits\n if (num[i] == num[i+1] && num[i+1] == num[i+2]) {\n string currentGoodInteger = num.substr(i, 3); // Extract the substring of length 3.\n // Update the maximum good integer if the current one is larger lexicographically\n if (currentGoodInteger > maxGoodInteger) {\n maxGoodInteger = currentGoodInteger;\n }\n }\n }\n\n return maxGoodInteger;\n\n }\n};", "memory": "9000" }
2,346
<p>You are given a string <code>num</code> representing a large integer. An integer is <strong>good</strong> if it meets the following conditions:</p> <ul> <li>It is a <strong>substring</strong> of <code>num</code> with length <code>3</code>.</li> <li>It consists of only one unique digit.</li> </ul> <p>Return <em>the <strong>maximum good </strong>integer as a <strong>string</strong> or an empty string </em><code>&quot;&quot;</code><em> if no such integer exists</em>.</p> <p>Note:</p> <ul> <li>A <strong>substring</strong> is a contiguous sequence of characters within a string.</li> <li>There may be <strong>leading zeroes</strong> in <code>num</code> or a good integer.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> num = &quot;6<strong><u>777</u></strong>133339&quot; <strong>Output:</strong> &quot;777&quot; <strong>Explanation:</strong> There are two distinct good integers: &quot;777&quot; and &quot;333&quot;. &quot;777&quot; is the largest, so we return &quot;777&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> num = &quot;23<strong><u>000</u></strong>19&quot; <strong>Output:</strong> &quot;000&quot; <strong>Explanation:</strong> &quot;000&quot; is the only good integer. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> num = &quot;42352338&quot; <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> No substring of length 3 consists of only one unique digit. Therefore, there are no good integers. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= num.length &lt;= 1000</code></li> <li><code>num</code> only consists of digits.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string largestGoodInteger(string num) {\n int n = num.length();\n std::string maksi; // Store the largest good integer as a string\n for (int i = 0; i < n - 2; ++i) {\n // Check if three consecutive characters are the same\n if (num[i] == num[i + 1] && num[i] == num[i + 2]) {\n std::string mate = num.substr(i, 3);\n // Update the result to the maximum lexicographically\n maksi = std::max(maksi, mate);\n }\n }\n return maksi;\n }\n};", "memory": "9100" }
2,346
<p>You are given a string <code>num</code> representing a large integer. An integer is <strong>good</strong> if it meets the following conditions:</p> <ul> <li>It is a <strong>substring</strong> of <code>num</code> with length <code>3</code>.</li> <li>It consists of only one unique digit.</li> </ul> <p>Return <em>the <strong>maximum good </strong>integer as a <strong>string</strong> or an empty string </em><code>&quot;&quot;</code><em> if no such integer exists</em>.</p> <p>Note:</p> <ul> <li>A <strong>substring</strong> is a contiguous sequence of characters within a string.</li> <li>There may be <strong>leading zeroes</strong> in <code>num</code> or a good integer.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> num = &quot;6<strong><u>777</u></strong>133339&quot; <strong>Output:</strong> &quot;777&quot; <strong>Explanation:</strong> There are two distinct good integers: &quot;777&quot; and &quot;333&quot;. &quot;777&quot; is the largest, so we return &quot;777&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> num = &quot;23<strong><u>000</u></strong>19&quot; <strong>Output:</strong> &quot;000&quot; <strong>Explanation:</strong> &quot;000&quot; is the only good integer. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> num = &quot;42352338&quot; <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> No substring of length 3 consists of only one unique digit. Therefore, there are no good integers. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= num.length &lt;= 1000</code></li> <li><code>num</code> only consists of digits.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string largestGoodInteger(string num) {\n string res = \"\";\n string maxi = \"0\";\n int n = num.size();\n for(int i = 0; i < n - 2; i++){\n if(num[i] == num[i + 1] && num[i] == num[i + 2])\n maxi = max(maxi, num.substr(i,3));\n }\n\n if(maxi == \"0\")\n return res;\n \n return maxi;\n \n }\n};", "memory": "9200" }
2,346
<p>You are given a string <code>num</code> representing a large integer. An integer is <strong>good</strong> if it meets the following conditions:</p> <ul> <li>It is a <strong>substring</strong> of <code>num</code> with length <code>3</code>.</li> <li>It consists of only one unique digit.</li> </ul> <p>Return <em>the <strong>maximum good </strong>integer as a <strong>string</strong> or an empty string </em><code>&quot;&quot;</code><em> if no such integer exists</em>.</p> <p>Note:</p> <ul> <li>A <strong>substring</strong> is a contiguous sequence of characters within a string.</li> <li>There may be <strong>leading zeroes</strong> in <code>num</code> or a good integer.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> num = &quot;6<strong><u>777</u></strong>133339&quot; <strong>Output:</strong> &quot;777&quot; <strong>Explanation:</strong> There are two distinct good integers: &quot;777&quot; and &quot;333&quot;. &quot;777&quot; is the largest, so we return &quot;777&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> num = &quot;23<strong><u>000</u></strong>19&quot; <strong>Output:</strong> &quot;000&quot; <strong>Explanation:</strong> &quot;000&quot; is the only good integer. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> num = &quot;42352338&quot; <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> No substring of length 3 consists of only one unique digit. Therefore, there are no good integers. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= num.length &lt;= 1000</code></li> <li><code>num</code> only consists of digits.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string largestGoodInteger(string num) {\n string maxi = \"0\";\n int n = num.size();\n for(int i = 0; i < n - 2; i++){\n if(num[i] == num[i + 1] && num[i] == num[i + 2])\n maxi = max(maxi, num.substr(i,3));\n }\n\n if(maxi == \"0\")\n return \"\";\n \n return maxi;\n \n }\n};", "memory": "9200" }
2,346
<p>You are given a string <code>num</code> representing a large integer. An integer is <strong>good</strong> if it meets the following conditions:</p> <ul> <li>It is a <strong>substring</strong> of <code>num</code> with length <code>3</code>.</li> <li>It consists of only one unique digit.</li> </ul> <p>Return <em>the <strong>maximum good </strong>integer as a <strong>string</strong> or an empty string </em><code>&quot;&quot;</code><em> if no such integer exists</em>.</p> <p>Note:</p> <ul> <li>A <strong>substring</strong> is a contiguous sequence of characters within a string.</li> <li>There may be <strong>leading zeroes</strong> in <code>num</code> or a good integer.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> num = &quot;6<strong><u>777</u></strong>133339&quot; <strong>Output:</strong> &quot;777&quot; <strong>Explanation:</strong> There are two distinct good integers: &quot;777&quot; and &quot;333&quot;. &quot;777&quot; is the largest, so we return &quot;777&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> num = &quot;23<strong><u>000</u></strong>19&quot; <strong>Output:</strong> &quot;000&quot; <strong>Explanation:</strong> &quot;000&quot; is the only good integer. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> num = &quot;42352338&quot; <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> No substring of length 3 consists of only one unique digit. Therefore, there are no good integers. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= num.length &lt;= 1000</code></li> <li><code>num</code> only consists of digits.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string largestGoodInteger(string num) {\n int n = num.size();\n string ans = \"\";\n for (int i = 0; i < n - 1; i++)\n {\n int a = 0;\n string cnt = \"\";\n int j = i;\n while (j < i + 3)\n {\n if (num[j] == num[j + 1])\n cnt += num[j];\n else\n break;\n j++;\n }\n cnt += num[j];\n if (cnt.size() == 3 && cnt > ans)\n ans = cnt;\n }\n return ans;\n }\n};", "memory": "9300" }
2,346
<p>You are given a string <code>num</code> representing a large integer. An integer is <strong>good</strong> if it meets the following conditions:</p> <ul> <li>It is a <strong>substring</strong> of <code>num</code> with length <code>3</code>.</li> <li>It consists of only one unique digit.</li> </ul> <p>Return <em>the <strong>maximum good </strong>integer as a <strong>string</strong> or an empty string </em><code>&quot;&quot;</code><em> if no such integer exists</em>.</p> <p>Note:</p> <ul> <li>A <strong>substring</strong> is a contiguous sequence of characters within a string.</li> <li>There may be <strong>leading zeroes</strong> in <code>num</code> or a good integer.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> num = &quot;6<strong><u>777</u></strong>133339&quot; <strong>Output:</strong> &quot;777&quot; <strong>Explanation:</strong> There are two distinct good integers: &quot;777&quot; and &quot;333&quot;. &quot;777&quot; is the largest, so we return &quot;777&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> num = &quot;23<strong><u>000</u></strong>19&quot; <strong>Output:</strong> &quot;000&quot; <strong>Explanation:</strong> &quot;000&quot; is the only good integer. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> num = &quot;42352338&quot; <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> No substring of length 3 consists of only one unique digit. Therefore, there are no good integers. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= num.length &lt;= 1000</code></li> <li><code>num</code> only consists of digits.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string largestGoodInteger(string s) {\n int i=0,j=0;\n int n=s.size();\n int res=0;\n bool say=false;\n while(j<n && i<n && i<=j){\n if(s[i]==s[j]){\n j++;\n if(j-i+1==3 && s[i]==s[j]){\n string temp= s.substr(i,j-i+1);\n res=max(res,stoi(temp));\n i=j;\n say=true;\n }\n }\n else{\n i++;\n }\n }\n\n if(res==0 && say){\n return \"000\";\n }\n if(!say){\n return \"\";\n }\n return to_string(res);\n \n }\n};", "memory": "9400" }
2,346
<p>You are given a string <code>num</code> representing a large integer. An integer is <strong>good</strong> if it meets the following conditions:</p> <ul> <li>It is a <strong>substring</strong> of <code>num</code> with length <code>3</code>.</li> <li>It consists of only one unique digit.</li> </ul> <p>Return <em>the <strong>maximum good </strong>integer as a <strong>string</strong> or an empty string </em><code>&quot;&quot;</code><em> if no such integer exists</em>.</p> <p>Note:</p> <ul> <li>A <strong>substring</strong> is a contiguous sequence of characters within a string.</li> <li>There may be <strong>leading zeroes</strong> in <code>num</code> or a good integer.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> num = &quot;6<strong><u>777</u></strong>133339&quot; <strong>Output:</strong> &quot;777&quot; <strong>Explanation:</strong> There are two distinct good integers: &quot;777&quot; and &quot;333&quot;. &quot;777&quot; is the largest, so we return &quot;777&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> num = &quot;23<strong><u>000</u></strong>19&quot; <strong>Output:</strong> &quot;000&quot; <strong>Explanation:</strong> &quot;000&quot; is the only good integer. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> num = &quot;42352338&quot; <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> No substring of length 3 consists of only one unique digit. Therefore, there are no good integers. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= num.length &lt;= 1000</code></li> <li><code>num</code> only consists of digits.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string largestGoodInteger(string num) {\n set<string> strSet {\"999\",\"888\", \"777\", \"666\", \"555\", \"444\", \"333\", \"222\", \"111\", \"000\"};\n string result = \"\";\n for (int i = 0; i < num.size(); ++i) {\n const string curr = num.substr(i, 3);\n if (strSet.contains(curr) && curr > result) {\n result = curr;\n }\n }\n return result;\n }\n};", "memory": "9500" }
2,346
<p>You are given a string <code>num</code> representing a large integer. An integer is <strong>good</strong> if it meets the following conditions:</p> <ul> <li>It is a <strong>substring</strong> of <code>num</code> with length <code>3</code>.</li> <li>It consists of only one unique digit.</li> </ul> <p>Return <em>the <strong>maximum good </strong>integer as a <strong>string</strong> or an empty string </em><code>&quot;&quot;</code><em> if no such integer exists</em>.</p> <p>Note:</p> <ul> <li>A <strong>substring</strong> is a contiguous sequence of characters within a string.</li> <li>There may be <strong>leading zeroes</strong> in <code>num</code> or a good integer.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> num = &quot;6<strong><u>777</u></strong>133339&quot; <strong>Output:</strong> &quot;777&quot; <strong>Explanation:</strong> There are two distinct good integers: &quot;777&quot; and &quot;333&quot;. &quot;777&quot; is the largest, so we return &quot;777&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> num = &quot;23<strong><u>000</u></strong>19&quot; <strong>Output:</strong> &quot;000&quot; <strong>Explanation:</strong> &quot;000&quot; is the only good integer. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> num = &quot;42352338&quot; <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> No substring of length 3 consists of only one unique digit. Therefore, there are no good integers. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= num.length &lt;= 1000</code></li> <li><code>num</code> only consists of digits.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string largestGoodInteger(string num) {\n deque<int> window{};\n window.push_back(num[0] - '0');\n window.push_back(num[1] - '0');\n array<bool, 10> candidates{};\n for (size_t i{2}; i < num.size(); ++i) {\n window.push_back(num[i] - '0');\n if (window[0] == window[1] && window[1] == window[2]) {\n candidates[window[0]] = true;\n }\n window.pop_front();\n }\n for (ptrdiff_t i{9}; i >= 0; --i) {\n if (candidates[i]) {\n return (i == 0) ? \"000\" : to_string(i * 111);\n }\n }\n return \"\";\n }\n};", "memory": "9700" }
2,346
<p>You are given a string <code>num</code> representing a large integer. An integer is <strong>good</strong> if it meets the following conditions:</p> <ul> <li>It is a <strong>substring</strong> of <code>num</code> with length <code>3</code>.</li> <li>It consists of only one unique digit.</li> </ul> <p>Return <em>the <strong>maximum good </strong>integer as a <strong>string</strong> or an empty string </em><code>&quot;&quot;</code><em> if no such integer exists</em>.</p> <p>Note:</p> <ul> <li>A <strong>substring</strong> is a contiguous sequence of characters within a string.</li> <li>There may be <strong>leading zeroes</strong> in <code>num</code> or a good integer.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> num = &quot;6<strong><u>777</u></strong>133339&quot; <strong>Output:</strong> &quot;777&quot; <strong>Explanation:</strong> There are two distinct good integers: &quot;777&quot; and &quot;333&quot;. &quot;777&quot; is the largest, so we return &quot;777&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> num = &quot;23<strong><u>000</u></strong>19&quot; <strong>Output:</strong> &quot;000&quot; <strong>Explanation:</strong> &quot;000&quot; is the only good integer. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> num = &quot;42352338&quot; <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> No substring of length 3 consists of only one unique digit. Therefore, there are no good integers. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= num.length &lt;= 1000</code></li> <li><code>num</code> only consists of digits.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string largestGoodInteger(string num) {\n\n int ans = -1; // to store what chars merge into\n string strAns = \"\"; // to store the value to return\n for (int i = 2; i < num.size(); ++i) { // go through from the third\n if (num[i] == num[i - 1] && num[i] == num[i - 2]) { // if current char is equal to the two previous ones\n string sub = num.substr(i - 2, 3); // take the string of this three chars\n int cur = stoi(sub); // convert this string of three chars to int\n if (cur > ans) { // if this int is greater than the answer - change the answer\n ans = cur;\n strAns = sub;\n }\n }\n }\n return strAns;\n }\n};\n\n/*\n check if the previous and current are equal and also chcek if current and next are equal \n if it found then return the substrof size 3\n\n Input\nnum =\n\"3200014888\"\nOutput\n\"000\"\nExpected\n\"888\" this case will be failed beacuse it will be return the max value inorder to avoid the problem will store in the result variable and then we will return the string\n\n\n\n\n\n*/", "memory": "9800" }
2,346
<p>You are given a string <code>num</code> representing a large integer. An integer is <strong>good</strong> if it meets the following conditions:</p> <ul> <li>It is a <strong>substring</strong> of <code>num</code> with length <code>3</code>.</li> <li>It consists of only one unique digit.</li> </ul> <p>Return <em>the <strong>maximum good </strong>integer as a <strong>string</strong> or an empty string </em><code>&quot;&quot;</code><em> if no such integer exists</em>.</p> <p>Note:</p> <ul> <li>A <strong>substring</strong> is a contiguous sequence of characters within a string.</li> <li>There may be <strong>leading zeroes</strong> in <code>num</code> or a good integer.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> num = &quot;6<strong><u>777</u></strong>133339&quot; <strong>Output:</strong> &quot;777&quot; <strong>Explanation:</strong> There are two distinct good integers: &quot;777&quot; and &quot;333&quot;. &quot;777&quot; is the largest, so we return &quot;777&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> num = &quot;23<strong><u>000</u></strong>19&quot; <strong>Output:</strong> &quot;000&quot; <strong>Explanation:</strong> &quot;000&quot; is the only good integer. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> num = &quot;42352338&quot; <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> No substring of length 3 consists of only one unique digit. Therefore, there are no good integers. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= num.length &lt;= 1000</code></li> <li><code>num</code> only consists of digits.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string largestGoodInteger(string num) {\n int ans = -1; // to store what chars merge into\n string strAns = \"\"; // to store the value to return\n for (int i = 2; i < num.size(); ++i) { // go through from the third\n if (num[i] == num[i - 1] && num[i] == num[i - 2]) { // if current char is equal to the two previous ones\n string sub = num.substr(i - 2, 3); // take the string of this three chars\n int cur = stoi(sub); // convert this string of three chars to int\n if (cur > ans) { // if this int is greater than the answer - change the answer\n ans = cur;\n strAns = sub;\n }\n }\n }\n return strAns;\n }\n};", "memory": "9800" }
2,346
<p>You are given a string <code>num</code> representing a large integer. An integer is <strong>good</strong> if it meets the following conditions:</p> <ul> <li>It is a <strong>substring</strong> of <code>num</code> with length <code>3</code>.</li> <li>It consists of only one unique digit.</li> </ul> <p>Return <em>the <strong>maximum good </strong>integer as a <strong>string</strong> or an empty string </em><code>&quot;&quot;</code><em> if no such integer exists</em>.</p> <p>Note:</p> <ul> <li>A <strong>substring</strong> is a contiguous sequence of characters within a string.</li> <li>There may be <strong>leading zeroes</strong> in <code>num</code> or a good integer.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> num = &quot;6<strong><u>777</u></strong>133339&quot; <strong>Output:</strong> &quot;777&quot; <strong>Explanation:</strong> There are two distinct good integers: &quot;777&quot; and &quot;333&quot;. &quot;777&quot; is the largest, so we return &quot;777&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> num = &quot;23<strong><u>000</u></strong>19&quot; <strong>Output:</strong> &quot;000&quot; <strong>Explanation:</strong> &quot;000&quot; is the only good integer. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> num = &quot;42352338&quot; <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> No substring of length 3 consists of only one unique digit. Therefore, there are no good integers. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= num.length &lt;= 1000</code></li> <li><code>num</code> only consists of digits.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string largestGoodInteger(string num) {\n\n int ans = -1; // to store what chars merge into\n string strAns = \"\"; // to store the value to return\n for (int i = 2; i < num.size(); ++i) { // go through from the third\n if (num[i] == num[i - 1] && num[i] == num[i - 2]) { // if current char is equal to the two previous ones\n string sub = num.substr(i - 2, 3); // take the string of this three chars\n int cur = stoi(sub); // convert this string of three chars to int\n if (cur > ans) { // if this int is greater than the answer - change the answer\n ans = cur;\n strAns = sub;\n }\n }\n }\n return strAns;\n }\n};\n\n/*\n check if the previous and current are equal and also chcek if current and next are equal \n if it found then return the substrof size 3\n\n Input\nnum =\n\"3200014888\"\nOutput\n\"000\"\nExpected\n\"888\" this case will be failed beacuse it will be return the max value inorder to avoid the problem will store in the result variable and then we will return the string\n\n\n\n\n\n*/", "memory": "9900" }
2,346
<p>You are given a string <code>num</code> representing a large integer. An integer is <strong>good</strong> if it meets the following conditions:</p> <ul> <li>It is a <strong>substring</strong> of <code>num</code> with length <code>3</code>.</li> <li>It consists of only one unique digit.</li> </ul> <p>Return <em>the <strong>maximum good </strong>integer as a <strong>string</strong> or an empty string </em><code>&quot;&quot;</code><em> if no such integer exists</em>.</p> <p>Note:</p> <ul> <li>A <strong>substring</strong> is a contiguous sequence of characters within a string.</li> <li>There may be <strong>leading zeroes</strong> in <code>num</code> or a good integer.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> num = &quot;6<strong><u>777</u></strong>133339&quot; <strong>Output:</strong> &quot;777&quot; <strong>Explanation:</strong> There are two distinct good integers: &quot;777&quot; and &quot;333&quot;. &quot;777&quot; is the largest, so we return &quot;777&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> num = &quot;23<strong><u>000</u></strong>19&quot; <strong>Output:</strong> &quot;000&quot; <strong>Explanation:</strong> &quot;000&quot; is the only good integer. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> num = &quot;42352338&quot; <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> No substring of length 3 consists of only one unique digit. Therefore, there are no good integers. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= num.length &lt;= 1000</code></li> <li><code>num</code> only consists of digits.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string largestGoodInteger(string num) {\n int ans = -1; \n string strAns = \"\"; \n for (int i = 2; i < num.size(); ++i) { \n if (num[i] == num[i - 1] && num[i-1] == num[i - 2]) { \n string sub = num.substr(i - 2, 3); \n int cur = stoi(sub); \n if (cur > ans) { \n ans = cur;\n strAns = sub;\n } }}\n return strAns;\n }};", "memory": "10000" }
2,346
<p>You are given a string <code>num</code> representing a large integer. An integer is <strong>good</strong> if it meets the following conditions:</p> <ul> <li>It is a <strong>substring</strong> of <code>num</code> with length <code>3</code>.</li> <li>It consists of only one unique digit.</li> </ul> <p>Return <em>the <strong>maximum good </strong>integer as a <strong>string</strong> or an empty string </em><code>&quot;&quot;</code><em> if no such integer exists</em>.</p> <p>Note:</p> <ul> <li>A <strong>substring</strong> is a contiguous sequence of characters within a string.</li> <li>There may be <strong>leading zeroes</strong> in <code>num</code> or a good integer.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> num = &quot;6<strong><u>777</u></strong>133339&quot; <strong>Output:</strong> &quot;777&quot; <strong>Explanation:</strong> There are two distinct good integers: &quot;777&quot; and &quot;333&quot;. &quot;777&quot; is the largest, so we return &quot;777&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> num = &quot;23<strong><u>000</u></strong>19&quot; <strong>Output:</strong> &quot;000&quot; <strong>Explanation:</strong> &quot;000&quot; is the only good integer. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> num = &quot;42352338&quot; <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> No substring of length 3 consists of only one unique digit. Therefore, there are no good integers. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= num.length &lt;= 1000</code></li> <li><code>num</code> only consists of digits.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string largestGoodInteger(string num) {\n int ans = -1;\n for(int i = 0; i < num.size() - 2; i++) {\n string curr = \"\";\n char loc1 = num[i];\n char loc2 = num[i + 1];\n char loc3 = num[i + 2];\n\n if(loc1 == loc2 && loc1 == loc3) {\n curr += loc1;\n curr += loc2;\n curr += loc3;\n }\n\n if(curr != \"\") {\n int currInt = stoi(curr);\n ans = max(ans, currInt);\n }\n }\n\n if(ans == 0) {\n return \"000\";\n }\n\n if(ans != -1) {\n return to_string(ans);\n }\n\n return \"\";\n }\n};", "memory": "10100" }
2,346
<p>You are given a string <code>num</code> representing a large integer. An integer is <strong>good</strong> if it meets the following conditions:</p> <ul> <li>It is a <strong>substring</strong> of <code>num</code> with length <code>3</code>.</li> <li>It consists of only one unique digit.</li> </ul> <p>Return <em>the <strong>maximum good </strong>integer as a <strong>string</strong> or an empty string </em><code>&quot;&quot;</code><em> if no such integer exists</em>.</p> <p>Note:</p> <ul> <li>A <strong>substring</strong> is a contiguous sequence of characters within a string.</li> <li>There may be <strong>leading zeroes</strong> in <code>num</code> or a good integer.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> num = &quot;6<strong><u>777</u></strong>133339&quot; <strong>Output:</strong> &quot;777&quot; <strong>Explanation:</strong> There are two distinct good integers: &quot;777&quot; and &quot;333&quot;. &quot;777&quot; is the largest, so we return &quot;777&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> num = &quot;23<strong><u>000</u></strong>19&quot; <strong>Output:</strong> &quot;000&quot; <strong>Explanation:</strong> &quot;000&quot; is the only good integer. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> num = &quot;42352338&quot; <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> No substring of length 3 consists of only one unique digit. Therefore, there are no good integers. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= num.length &lt;= 1000</code></li> <li><code>num</code> only consists of digits.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string largestGoodInteger(string num) {\n int ans = -1;\n for(int i = 0; i < num.size() - 2; i++) {\n string curr = \"\";\n char loc1 = num[i];\n char loc2 = num[i + 1];\n char loc3 = num[i + 2];\n\n if(loc1 == loc2 && loc1 == loc3) {\n curr += loc1;\n curr += loc2;\n curr += loc3;\n }\n\n if(curr != \"\") {\n int currInt = stoi(curr);\n ans = max(ans, currInt);\n }\n }\n\n if(ans == 0) {\n return \"000\";\n }\n\n if(ans != -1) {\n return to_string(ans);\n }\n\n return \"\";\n }\n};", "memory": "10200" }
2,346
<p>You are given a string <code>num</code> representing a large integer. An integer is <strong>good</strong> if it meets the following conditions:</p> <ul> <li>It is a <strong>substring</strong> of <code>num</code> with length <code>3</code>.</li> <li>It consists of only one unique digit.</li> </ul> <p>Return <em>the <strong>maximum good </strong>integer as a <strong>string</strong> or an empty string </em><code>&quot;&quot;</code><em> if no such integer exists</em>.</p> <p>Note:</p> <ul> <li>A <strong>substring</strong> is a contiguous sequence of characters within a string.</li> <li>There may be <strong>leading zeroes</strong> in <code>num</code> or a good integer.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> num = &quot;6<strong><u>777</u></strong>133339&quot; <strong>Output:</strong> &quot;777&quot; <strong>Explanation:</strong> There are two distinct good integers: &quot;777&quot; and &quot;333&quot;. &quot;777&quot; is the largest, so we return &quot;777&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> num = &quot;23<strong><u>000</u></strong>19&quot; <strong>Output:</strong> &quot;000&quot; <strong>Explanation:</strong> &quot;000&quot; is the only good integer. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> num = &quot;42352338&quot; <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> No substring of length 3 consists of only one unique digit. Therefore, there are no good integers. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= num.length &lt;= 1000</code></li> <li><code>num</code> only consists of digits.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string largestGoodInteger(string num) {\n int n=num.size();\n if(n==3 ) {\n if(num[0]==num[1] && num[0]==num[2]) return num;\n } \n vector<int> count;\n string temp=\"\";\n for(int i=2;i<n;i++){\n if(num[i]==num[i-1] && num[i]==num[i-2]) {\n temp=temp+num[i]+num[i-1]+num[i-2];\n int k=stoi(temp);\n count.push_back(k);\n temp.clear();\n } \n else continue;\n } \n string first=\"\";\n sort(count.begin(),count.end());\n int j=count.size();\n if(j==0) return first;\n int g=count[j-1];\n if(g==0) {\n first=first+'0'+'0'+'0';\n return first;\n }\n first=to_string(g);\n return first;\n \n }\n};", "memory": "10300" }
2,347
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p> <p><strong>Note:</strong></p> <ul> <li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li> <li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;" /> <pre> <strong>Input:</strong> root = [4,8,5,0,1,null,6] <strong>Output:</strong> 5 <strong>Explanation:</strong> For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4. For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5. For the node with value 0: The average of its subtree is 0 / 1 = 0. For the node with value 1: The average of its subtree is 1 / 1 = 1. For the node with value 6: The average of its subtree is 6 / 1 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;" /> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 <strong>Explanation:</strong> For the node with value 1: The average of its subtree is 1 / 1 = 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 1000</code></li> </ul>
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int Sum(TreeNode* root){\n if(root==NULL){\n return 0;\n }\n int curr_sum=root->val;\n curr_sum+=Sum(root->left);\n curr_sum+=Sum(root->right);\n return curr_sum;\n }\n int count(TreeNode* root){\n if(root==NULL){\n return 0;\n }\n int ct=1;\n ct+=count(root->left);\n ct+=count(root->right);\n return ct;\n }\n\n void dfs(TreeNode* root,int &ct){\n if(root==NULL){\n return;\n }\n\n int avg=Sum(root)/count(root);\n if(avg==root->val){\n ct++;\n }\n dfs(root->left,ct);\n dfs(root->right,ct);\n }\n\n int averageOfSubtree(TreeNode* root) {\n int ct=0;\n dfs(root,ct);\n return ct;\n }\n};", "memory": "14900" }
2,347
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p> <p><strong>Note:</strong></p> <ul> <li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li> <li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;" /> <pre> <strong>Input:</strong> root = [4,8,5,0,1,null,6] <strong>Output:</strong> 5 <strong>Explanation:</strong> For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4. For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5. For the node with value 0: The average of its subtree is 0 / 1 = 0. For the node with value 1: The average of its subtree is 1 / 1 = 1. For the node with value 6: The average of its subtree is 6 / 1 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;" /> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 <strong>Explanation:</strong> For the node with value 1: The average of its subtree is 1 / 1 = 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 1000</code></li> </ul>
2
{ "code": "auto init = []() {cin.tie(0); ios::sync_with_stdio(0); return 1;}();\nclass Solution {\npublic:\n int ans = 0;\n\n pair<int, int> solve(TreeNode* root) {\n if (!root) return {0, 0};\n\n pair<int, int> left = solve(root->left);\n pair<int, int> right = solve(root->right);\n\n int nodes = left.first + right.first + 1;\n int sum = left.second + right.second + root->val;\n int avg = nodes != 0 ? sum / nodes : 1;\n\n\n if (nodes != 0 && avg == root->val) {\n ans++;\n }\n\n return {nodes, sum};\n }\n\n int averageOfSubtree(TreeNode* root) {\n solve(root);\n return ans;\n }\n};\n", "memory": "14900" }
2,347
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p> <p><strong>Note:</strong></p> <ul> <li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li> <li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;" /> <pre> <strong>Input:</strong> root = [4,8,5,0,1,null,6] <strong>Output:</strong> 5 <strong>Explanation:</strong> For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4. For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5. For the node with value 0: The average of its subtree is 0 / 1 = 0. For the node with value 1: The average of its subtree is 1 / 1 = 1. For the node with value 6: The average of its subtree is 6 / 1 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;" /> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 <strong>Explanation:</strong> For the node with value 1: The average of its subtree is 1 / 1 = 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 1000</code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\npair<int, int> find_count(TreeNode *root, pair<int, int> subtree_info, int &count)\n{\n if(root == NULL)\n {\n return make_pair(0,0);\n }\n pair<int, int> left_tree = find_count(root -> left, subtree_info, count);\n pair<int, int> right_tree = find_count(root -> right, subtree_info, count);\n\n if(root -> left == NULL && root -> right == NULL)\n {\n subtree_info.first += root -> val + left_tree.first + right_tree.first;\n subtree_info.second += 1;\n }\n\n else{\n subtree_info.first += root -> val + left_tree.first + right_tree.first;\n subtree_info.second += left_tree.second + right_tree.second + 1;\n }\n \n // if(root -> left == NULL && root -> right == NULL)\n // {\n // if(subtree_info.first == root -> val)\n // {\n // // cout << root -> val << ' '<< subtree_info.second << endl;\n // count++;\n // }\n // }\n\n // else \n // {\n if(subtree_info.first/subtree_info.second == root -> val)\n {\n cout << root -> val << ' '<< subtree_info.second << endl;\n count++;\n }\n // }\n return subtree_info;\n}\n\n int averageOfSubtree(TreeNode* root) {\n\n int count = 0;\n pair<int, int> subtree_info = make_pair(0,0);\n\n subtree_info = find_count(root, subtree_info, count);\n\n return count;\n\n }\n};", "memory": "15000" }
2,347
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p> <p><strong>Note:</strong></p> <ul> <li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li> <li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;" /> <pre> <strong>Input:</strong> root = [4,8,5,0,1,null,6] <strong>Output:</strong> 5 <strong>Explanation:</strong> For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4. For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5. For the node with value 0: The average of its subtree is 0 / 1 = 0. For the node with value 1: The average of its subtree is 1 / 1 = 1. For the node with value 6: The average of its subtree is 6 / 1 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;" /> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 <strong>Explanation:</strong> For the node with value 1: The average of its subtree is 1 / 1 = 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 1000</code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int countNode(TreeNode* root)\n {\n if(root==NULL)\n return 0;\n return 1+countNode(root->left)+countNode(root->right);\n }\n int sumofNode(TreeNode* root)\n {\n if(root==NULL)\n return 0;\n return root->val+sumofNode(root->left)+sumofNode(root->right);\n }\n int averageOfSubtree(TreeNode* root) {\n int countofnodes=0;\n queue<TreeNode*> q;\n q.push(root);\n while(!q.empty())\n {\n int sum=0;\n int count=0;\n TreeNode* node=q.front();\n q.pop();\n count=countNode(node);\n sum=sumofNode(node);\n if((sum/count)==node->val)\n countofnodes++;\n if(node->left)\n q.push(node->left);\n if(node->right)\n q.push(node->right);\n }\n return countofnodes;\n }\n};", "memory": "15100" }
2,347
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p> <p><strong>Note:</strong></p> <ul> <li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li> <li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;" /> <pre> <strong>Input:</strong> root = [4,8,5,0,1,null,6] <strong>Output:</strong> 5 <strong>Explanation:</strong> For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4. For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5. For the node with value 0: The average of its subtree is 0 / 1 = 0. For the node with value 1: The average of its subtree is 1 / 1 = 1. For the node with value 6: The average of its subtree is 6 / 1 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;" /> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 <strong>Explanation:</strong> For the node with value 1: The average of its subtree is 1 / 1 = 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 1000</code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int averageOfSubtree(TreeNode* root) {\n queue<TreeNode*>q{{root}};\n int ans = 0;\n while(!q.empty()){\n TreeNode* temp = q.front();\n q.pop();\n int sum = 0;\n int k = 0;\n helper(temp,sum,k);\n if(temp->val == sum/k){\n ans++;\n }\n if(temp->left){\n q.push(temp->left);\n }\n if(temp->right){\n q.push(temp->right);\n }\n }\n return ans;\n }\n void helper(TreeNode* root,int& sum,int& k){\n if(root){\n sum = sum + root->val;\n k++;\n helper(root->left,sum,k);\n helper(root->right,sum,k);\n }\n }\n};", "memory": "15200" }
2,347
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p> <p><strong>Note:</strong></p> <ul> <li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li> <li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;" /> <pre> <strong>Input:</strong> root = [4,8,5,0,1,null,6] <strong>Output:</strong> 5 <strong>Explanation:</strong> For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4. For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5. For the node with value 0: The average of its subtree is 0 / 1 = 0. For the node with value 1: The average of its subtree is 1 / 1 = 1. For the node with value 6: The average of its subtree is 6 / 1 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;" /> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 <strong>Explanation:</strong> For the node with value 1: The average of its subtree is 1 / 1 = 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 1000</code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int averageOfSubtree(TreeNode* root) {\n queue<TreeNode*>q{{root}};\n int ans = 0;\n while(!q.empty()){\n TreeNode* temp = q.front();\n q.pop();\n int sum = 0;\n int k = 0;\n helper(temp,sum,k);\n if(temp->val == sum/k){\n ans++;\n }\n if(temp->left){\n q.push(temp->left);\n }\n if(temp->right){\n q.push(temp->right);\n }\n }\n return ans;\n }\n void helper(TreeNode* root,int& sum,int& k){\n if(root){\n sum = sum + root->val;\n k++;\n helper(root->left,sum,k);\n helper(root->right,sum,k);\n }\n }\n};", "memory": "15200" }
2,347
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p> <p><strong>Note:</strong></p> <ul> <li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li> <li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;" /> <pre> <strong>Input:</strong> root = [4,8,5,0,1,null,6] <strong>Output:</strong> 5 <strong>Explanation:</strong> For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4. For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5. For the node with value 0: The average of its subtree is 0 / 1 = 0. For the node with value 1: The average of its subtree is 1 / 1 = 1. For the node with value 6: The average of its subtree is 6 / 1 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;" /> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 <strong>Explanation:</strong> For the node with value 1: The average of its subtree is 1 / 1 = 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 1000</code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int findSum(TreeNode* root, int& n){\n if(root == NULL) return 0;\n\n if(root->left == NULL && root->right == NULL){\n n++;\n return root->val;\n }\n\n n++;\n // int leftSum = 0, rightSum = 0;\n int leftSum = findSum(root->left, n);\n int rightSum = findSum(root->right, n);\n int sum = root->val+leftSum+rightSum;\n return sum;\n}\n\nvoid myTraversal(TreeNode* root, int& cnt){\n \n if(root->left){\n myTraversal(root->left, cnt);\n }\n if(root->right){\n myTraversal(root->right, cnt);\n }\n\n int left = 0 , right = 0;\n int leftSum = 0 , rightSum = 0;\n leftSum = findSum(root->left, left);\n rightSum = findSum(root->right, right);\n\n int avg = (root->val+leftSum+rightSum)/(left+right+1);\n if(avg == root->val) cnt++;\n\n}\n\nint averageOfSubtree(TreeNode* root) {\n int cnt = 0;\n if(root == NULL) return 0;\n myTraversal(root, cnt);\n return cnt;\n}\n};", "memory": "15600" }
2,347
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p> <p><strong>Note:</strong></p> <ul> <li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li> <li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;" /> <pre> <strong>Input:</strong> root = [4,8,5,0,1,null,6] <strong>Output:</strong> 5 <strong>Explanation:</strong> For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4. For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5. For the node with value 0: The average of its subtree is 0 / 1 = 0. For the node with value 1: The average of its subtree is 1 / 1 = 1. For the node with value 6: The average of its subtree is 6 / 1 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;" /> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 <strong>Explanation:</strong> For the node with value 1: The average of its subtree is 1 / 1 = 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 1000</code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int findSum(TreeNode* root, int& n){\n if(root == NULL) return 0;\n\n if(root->left == NULL && root->right == NULL){\n n++;\n return root->val;\n }\n\n n++;\n // int leftSum = 0, rightSum = 0;\n // int leftSum = findSum(root->left, n);\n // int rightSum = findSum(root->right, n);\n return root->val + findSum(root->left, n) + findSum(root->right, n);\n // return sum;\n}\n\nvoid myTraversal(TreeNode* root, int& cnt){\n \n if(root->left){\n myTraversal(root->left, cnt);\n }\n if(root->right){\n myTraversal(root->right, cnt);\n }\n\n int left = 0 , right = 0;\n // int leftSum = 0 , rightSum = 0;\n // leftSum = findSum(root->left, left);\n // rightSum = findSum(root->right, right);\n\n int avg = (root->val + findSum(root->left, left) + findSum(root->right, right) )/(left+right+1);\n if(avg == root->val) cnt++;\n\n}\n\nint averageOfSubtree(TreeNode* root) {\n int cnt = 0;\n if(root == NULL) return 0;\n myTraversal(root, cnt);\n return cnt;\n}\n};", "memory": "15600" }
2,347
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p> <p><strong>Note:</strong></p> <ul> <li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li> <li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;" /> <pre> <strong>Input:</strong> root = [4,8,5,0,1,null,6] <strong>Output:</strong> 5 <strong>Explanation:</strong> For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4. For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5. For the node with value 0: The average of its subtree is 0 / 1 = 0. For the node with value 1: The average of its subtree is 1 / 1 = 1. For the node with value 6: The average of its subtree is 6 / 1 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;" /> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 <strong>Explanation:</strong> For the node with value 1: The average of its subtree is 1 / 1 = 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 1000</code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int ans =0;\n int findSum(TreeNode* root, int &count){\n\n if(root == NULL){\n return 0;\n }\n\n count +=1;\n\n int leftSub = findSum(root->left, count);\n int rightSub = findSum(root->right, count);\n\n return root->val + leftSub + rightSub;\n }\n\n void solve(TreeNode* root){\n\n if(root == NULL){\n return;\n }\n\n int count =0;\n\n int sum = findSum(root, count);\n\n if(root->val == sum/count){\n ans += 1;\n }\n\n solve(root->left);\n solve(root->right);\n }\n\n int averageOfSubtree(TreeNode* root) {\n \n solve(root);\n return ans;\n }\n};", "memory": "15700" }
2,347
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p> <p><strong>Note:</strong></p> <ul> <li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li> <li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;" /> <pre> <strong>Input:</strong> root = [4,8,5,0,1,null,6] <strong>Output:</strong> 5 <strong>Explanation:</strong> For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4. For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5. For the node with value 0: The average of its subtree is 0 / 1 = 0. For the node with value 1: The average of its subtree is 1 / 1 = 1. For the node with value 6: The average of its subtree is 6 / 1 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;" /> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 <strong>Explanation:</strong> For the node with value 1: The average of its subtree is 1 / 1 = 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 1000</code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void sum(TreeNode*root, int&s,int&c){\n if(root == NULL){\n return;\n }\n s = s+ root->val;\n c = c+1;\n sum(root->left,s,c);\n sum(root->right,s,c);\n }\n void solve(TreeNode*root,int&fc){\n int c = 0; int s =0;\n sum(root,s,c);\n if(root == NULL){\n return;\n }\n if(s/c==root->val){\n fc++;\n }\n solve(root->left,fc);\n solve(root->right,fc);\n }\n int averageOfSubtree(TreeNode* root) {\n int fc =0;\n solve(root,fc);\n return fc;\n\n }\n};", "memory": "15800" }
2,347
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p> <p><strong>Note:</strong></p> <ul> <li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li> <li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;" /> <pre> <strong>Input:</strong> root = [4,8,5,0,1,null,6] <strong>Output:</strong> 5 <strong>Explanation:</strong> For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4. For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5. For the node with value 0: The average of its subtree is 0 / 1 = 0. For the node with value 1: The average of its subtree is 1 / 1 = 1. For the node with value 6: The average of its subtree is 6 / 1 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;" /> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 <strong>Explanation:</strong> For the node with value 1: The average of its subtree is 1 / 1 = 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 1000</code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int average(TreeNode*root, int& sum , int& nodes){\n if(root==nullptr) return 0;\n average(root->left, sum, nodes);\n average(root->right,sum, nodes);\n nodes++;\n return sum+=root->val;\n }\n int count=0;\n int averageOfSubtree(TreeNode* root) {\n if(root==nullptr) return 0;\n int sum=0;\n int nodes=0;\n average(root,sum, nodes);\n int avg=sum/nodes;\n if(avg==root->val){\n count++;\n }\n averageOfSubtree(root->left);\n averageOfSubtree(root->right);\n return count;\n }\n};", "memory": "15900" }
2,347
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p> <p><strong>Note:</strong></p> <ul> <li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li> <li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;" /> <pre> <strong>Input:</strong> root = [4,8,5,0,1,null,6] <strong>Output:</strong> 5 <strong>Explanation:</strong> For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4. For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5. For the node with value 0: The average of its subtree is 0 / 1 = 0. For the node with value 1: The average of its subtree is 1 / 1 = 1. For the node with value 6: The average of its subtree is 6 / 1 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;" /> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 <strong>Explanation:</strong> For the node with value 1: The average of its subtree is 1 / 1 = 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 1000</code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n // {ans,{count,sum}}\n pair<int,pair<int,int>> solve(TreeNode* root){\n if(!root) return {0,{0,0}};\n if(!root->left && !root->right) return {1,{1,root->val}};\n pair<int,pair<int,int>> left = solve(root->left);\n pair<int,pair<int,int>> right = solve(root->right);\n int ans = left.first + right.first;\n int count = left.second.first + right.second.first + 1;\n int sum = left.second.second + root->val + right.second.second;\n if(root->val == (sum/count)) return {ans+1,{count,sum}};\n else return {ans,{count,sum}};\n }\n int averageOfSubtree(TreeNode* root) {\n return solve(root).first;\n }\n};", "memory": "16000" }
2,347
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p> <p><strong>Note:</strong></p> <ul> <li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li> <li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;" /> <pre> <strong>Input:</strong> root = [4,8,5,0,1,null,6] <strong>Output:</strong> 5 <strong>Explanation:</strong> For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4. For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5. For the node with value 0: The average of its subtree is 0 / 1 = 0. For the node with value 1: The average of its subtree is 1 / 1 = 1. For the node with value 6: The average of its subtree is 6 / 1 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;" /> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 <strong>Explanation:</strong> For the node with value 1: The average of its subtree is 1 / 1 = 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 1000</code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void solve(TreeNode *root,int &cnt){\n if(!root) return;\n int tempCnt = 0;\n int sum = findSum(root,tempCnt);\n if(sum/tempCnt == root->val) cnt++;\n solve(root->left,cnt);\n solve(root->right,cnt);\n }\n int findSum(TreeNode *root,int &tempCnt){\n if(!root) return 0;\n tempCnt++;\n int l = findSum(root->left,tempCnt);\n int r = findSum(root->right,tempCnt);\n\n return l+r+root->val;\n }\n int averageOfSubtree(TreeNode* root) {\n int cnt = 0;\n solve(root,cnt);\n return cnt;\n }\n};", "memory": "16100" }
2,347
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p> <p><strong>Note:</strong></p> <ul> <li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li> <li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;" /> <pre> <strong>Input:</strong> root = [4,8,5,0,1,null,6] <strong>Output:</strong> 5 <strong>Explanation:</strong> For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4. For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5. For the node with value 0: The average of its subtree is 0 / 1 = 0. For the node with value 1: The average of its subtree is 1 / 1 = 1. For the node with value 6: The average of its subtree is 6 / 1 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;" /> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 <strong>Explanation:</strong> For the node with value 1: The average of its subtree is 1 / 1 = 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 1000</code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\nprivate:\n class stack_entry {\n public:\n TreeNode* n;\n int which;\n int sum;\n int count;\n };\n\npublic:\n int averageOfSubtree(TreeNode* root) {\n int res;\n stack_entry* cur;\n stack_entry* node_stack;\n TreeNode* node;\n\n node_stack = new stack_entry [1000];\n\n node_stack[0].n = root;\n node_stack[0].which = 0;\n node_stack[0].sum = root->val;\n node_stack[0].count = 1;\n cur = node_stack;\n\n res = 0;\n for (;;) {\n node = cur->n;\n if (cur->which == 0)\n node = node->left;\n else if (cur->which == 1)\n node = node->right;\n else {\n int count, sum;\n\n if (node->val == cur->sum / cur->count)\n ++res;\n if (cur == node_stack)\n break;\n sum = cur->sum;\n count = cur->count;\n --cur;\n cur->sum += sum;\n cur->count += count;\n continue;\n }\n ++cur->which;\n if (node == nullptr)\n continue;\n ++cur;\n cur->n = node;\n cur->which = 0;\n cur->sum = node->val;\n cur->count = 1;\n }\n\n return res;\n }\n};", "memory": "16200" }
2,347
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p> <p><strong>Note:</strong></p> <ul> <li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li> <li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;" /> <pre> <strong>Input:</strong> root = [4,8,5,0,1,null,6] <strong>Output:</strong> 5 <strong>Explanation:</strong> For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4. For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5. For the node with value 0: The average of its subtree is 0 / 1 = 0. For the node with value 1: The average of its subtree is 1 / 1 = 1. For the node with value 6: The average of its subtree is 6 / 1 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;" /> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 <strong>Explanation:</strong> For the node with value 1: The average of its subtree is 1 / 1 = 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 1000</code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\nprivate:\n class stack_entry {\n public:\n TreeNode* n;\n int which;\n int sum;\n int count;\n };\n\npublic:\n int averageOfSubtree(TreeNode* root) {\n int res;\n stack_entry* cur;\n stack_entry* node_stack;\n TreeNode* node;\n\n node_stack = new stack_entry [1000];\n\n node_stack[0].n = root;\n node_stack[0].which = 0;\n node_stack[0].sum = root->val;\n node_stack[0].count = 1;\n cur = node_stack;\n\n res = 0;\n for (;;) {\n node = cur->n;\n if (cur->which == 0)\n node = node->left;\n else if (cur->which == 1)\n node = node->right;\n else {\n int count, sum;\n\n if (node->val == cur->sum / cur->count)\n ++res;\n if (cur == node_stack)\n break;\n sum = cur->sum;\n count = cur->count;\n --cur;\n cur->sum += sum;\n cur->count += count;\n continue;\n }\n ++cur->which;\n if (node == nullptr)\n continue;\n ++cur;\n cur->n = node;\n cur->which = 0;\n cur->sum = node->val;\n cur->count = 1;\n }\n\n return res;\n }\n};", "memory": "16200" }
2,347
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p> <p><strong>Note:</strong></p> <ul> <li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li> <li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;" /> <pre> <strong>Input:</strong> root = [4,8,5,0,1,null,6] <strong>Output:</strong> 5 <strong>Explanation:</strong> For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4. For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5. For the node with value 0: The average of its subtree is 0 / 1 = 0. For the node with value 1: The average of its subtree is 1 / 1 = 1. For the node with value 6: The average of its subtree is 6 / 1 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;" /> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 <strong>Explanation:</strong> For the node with value 1: The average of its subtree is 1 / 1 = 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 1000</code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\nprivate:\n class stack_entry {\n public:\n TreeNode* n;\n int which;\n int sum;\n int count;\n };\n\npublic:\n int averageOfSubtree(TreeNode* root) {\n int res;\n stack_entry* cur;\n stack_entry* node_stack;\n TreeNode* node;\n\n node_stack = new stack_entry [1000];\n\n node_stack[0].n = root;\n node_stack[0].which = 0;\n node_stack[0].sum = root->val;\n node_stack[0].count = 1;\n cur = node_stack;\n\n res = 0;\n for (;;) {\n node = cur->n;\n if (cur->which == 0)\n node = node->left;\n else if (cur->which == 1)\n node = node->right;\n else {\n int count, sum;\n\n if (node->val == cur->sum / cur->count)\n ++res;\n if (cur == node_stack)\n break;\n sum = cur->sum;\n count = cur->count;\n --cur;\n cur->sum += sum;\n cur->count += count;\n continue;\n }\n ++cur->which;\n if (node == nullptr)\n continue;\n ++cur;\n cur->n = node;\n cur->which = 0;\n cur->sum = node->val;\n cur->count = 1;\n }\n\n return res;\n }\n};", "memory": "16300" }
2,347
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p> <p><strong>Note:</strong></p> <ul> <li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li> <li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;" /> <pre> <strong>Input:</strong> root = [4,8,5,0,1,null,6] <strong>Output:</strong> 5 <strong>Explanation:</strong> For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4. For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5. For the node with value 0: The average of its subtree is 0 / 1 = 0. For the node with value 1: The average of its subtree is 1 / 1 = 1. For the node with value 6: The average of its subtree is 6 / 1 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;" /> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 <strong>Explanation:</strong> For the node with value 1: The average of its subtree is 1 / 1 = 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 1000</code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\nprivate:\n class stack_entry {\n public:\n TreeNode* n;\n int which;\n int sum;\n int count;\n };\n\npublic:\n int averageOfSubtree(TreeNode* root) {\n int res;\n stack_entry* cur;\n stack_entry* node_stack;\n TreeNode* node;\n\n node_stack = new stack_entry [1000];\n\n node_stack[0].n = root;\n node_stack[0].which = 0;\n node_stack[0].sum = root->val;\n node_stack[0].count = 1;\n cur = node_stack;\n\n res = 0;\n for (;;) {\n node = cur->n;\n if (cur->which == 0)\n node = node->left;\n else if (cur->which == 1)\n node = node->right;\n else {\n int count, sum;\n\n if (node->val == cur->sum / cur->count)\n ++res;\n if (cur == node_stack)\n break;\n sum = cur->sum;\n count = cur->count;\n --cur;\n cur->sum += sum;\n cur->count += count;\n continue;\n }\n ++cur->which;\n if (node == nullptr)\n continue;\n ++cur;\n cur->n = node;\n cur->which = 0;\n cur->sum = node->val;\n cur->count = 1;\n }\n\n return res;\n }\n};", "memory": "16300" }
2,347
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p> <p><strong>Note:</strong></p> <ul> <li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li> <li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;" /> <pre> <strong>Input:</strong> root = [4,8,5,0,1,null,6] <strong>Output:</strong> 5 <strong>Explanation:</strong> For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4. For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5. For the node with value 0: The average of its subtree is 0 / 1 = 0. For the node with value 1: The average of its subtree is 1 / 1 = 1. For the node with value 6: The average of its subtree is 6 / 1 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;" /> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 <strong>Explanation:</strong> For the node with value 1: The average of its subtree is 1 / 1 = 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 1000</code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\nprivate:\n class stack_entry {\n public:\n TreeNode* n;\n int which;\n int sum;\n int count;\n };\n\npublic:\n int averageOfSubtree(TreeNode* root) {\n int res;\n stack_entry* cur;\n stack_entry* node_stack;\n TreeNode* node;\n\n node_stack = new stack_entry [1000];\n\n node_stack[0].n = root;\n node_stack[0].which = 0;\n node_stack[0].sum = root->val;\n node_stack[0].count = 1;\n cur = node_stack;\n\n res = 0;\n for (;;) {\n node = cur->n;\n if (cur->which == 0)\n node = node->left;\n else if (cur->which == 1)\n node = node->right;\n else {\n int count, sum;\n\n if (node->val == cur->sum / cur->count)\n ++res;\n if (cur == node_stack)\n break;\n sum = cur->sum;\n count = cur->count;\n --cur;\n cur->sum += sum;\n cur->count += count;\n continue;\n }\n ++cur->which;\n if (node == nullptr)\n continue;\n ++cur;\n cur->n = node;\n cur->which = 0;\n cur->sum = node->val;\n cur->count = 1;\n }\n\n return res;\n }\n};", "memory": "16400" }
2,347
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p> <p><strong>Note:</strong></p> <ul> <li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li> <li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;" /> <pre> <strong>Input:</strong> root = [4,8,5,0,1,null,6] <strong>Output:</strong> 5 <strong>Explanation:</strong> For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4. For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5. For the node with value 0: The average of its subtree is 0 / 1 = 0. For the node with value 1: The average of its subtree is 1 / 1 = 1. For the node with value 6: The average of its subtree is 6 / 1 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;" /> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 <strong>Explanation:</strong> For the node with value 1: The average of its subtree is 1 / 1 = 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 1000</code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\nprivate:\n class stack_entry {\n public:\n TreeNode* n;\n int which;\n int sum;\n int count;\n };\n\npublic:\n int averageOfSubtree(TreeNode* root) {\n int res;\n stack_entry* cur;\n stack_entry* node_stack;\n TreeNode* node;\n\n node_stack = new stack_entry [1000];\n\n node_stack[0].n = root;\n node_stack[0].which = 0;\n node_stack[0].sum = root->val;\n node_stack[0].count = 1;\n cur = node_stack;\n\n res = 0;\n for (;;) {\n node = cur->n;\n if (cur->which == 0)\n node = node->left;\n else if (cur->which == 1)\n node = node->right;\n else {\n int count, sum;\n\n if (node->val == cur->sum / cur->count)\n ++res;\n if (cur == node_stack)\n break;\n sum = cur->sum;\n count = cur->count;\n --cur;\n cur->sum += sum;\n cur->count += count;\n continue;\n }\n ++cur->which;\n if (node == nullptr)\n continue;\n ++cur;\n cur->n = node;\n cur->which = 0;\n cur->sum = node->val;\n cur->count = 1;\n }\n\n return res;\n }\n};", "memory": "16400" }
2,347
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p> <p><strong>Note:</strong></p> <ul> <li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li> <li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;" /> <pre> <strong>Input:</strong> root = [4,8,5,0,1,null,6] <strong>Output:</strong> 5 <strong>Explanation:</strong> For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4. For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5. For the node with value 0: The average of its subtree is 0 / 1 = 0. For the node with value 1: The average of its subtree is 1 / 1 = 1. For the node with value 6: The average of its subtree is 6 / 1 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;" /> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 <strong>Explanation:</strong> For the node with value 1: The average of its subtree is 1 / 1 = 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 1000</code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void preorder(TreeNode* root, std::vector<TreeNode*>& nodes) {\n if (!root) {\n return;\n }\n nodes.push_back(root);\n preorder(root->left, nodes);\n preorder(root->right, nodes);\n }\n int CountNodes(TreeNode* root) {\n if (!root) {\n return 0;\n }\n return 1 + CountNodes(root->left) + CountNodes(root->right);\n }\n\n int SumNodes(TreeNode* root) {\n if (!root) {\n return 0;\n }\n return root->val + SumNodes(root->left) + SumNodes(root->right);\n }\n\n int averageOfSubtree(TreeNode* root) {\n std::vector<TreeNode*> nodes;\n preorder(root, nodes);\n int count = 0;\n for (TreeNode* node : nodes) {\n if (node->val == SumNodes(node)/CountNodes(node)) {\n ++count;\n }\n } \n return count;\n }\n};", "memory": "17000" }
2,347
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p> <p><strong>Note:</strong></p> <ul> <li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li> <li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;" /> <pre> <strong>Input:</strong> root = [4,8,5,0,1,null,6] <strong>Output:</strong> 5 <strong>Explanation:</strong> For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4. For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5. For the node with value 0: The average of its subtree is 0 / 1 = 0. For the node with value 1: The average of its subtree is 1 / 1 = 1. For the node with value 6: The average of its subtree is 6 / 1 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;" /> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 <strong>Explanation:</strong> For the node with value 1: The average of its subtree is 1 / 1 = 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 1000</code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nvoid trav(TreeNode*root,vector<TreeNode*>&v)\n {\n if(root==nullptr)\n return;\n v.push_back(root);\n trav(root->left,v);\n trav(root->right,v);\n}\nvoid sum_sub(TreeNode*root,int &sum,int &cnt)\n {\n if(root==nullptr)\n return;\n sum+=root->val;\n cnt++;\n sum_sub(root->left,sum,cnt);\n sum_sub(root->right,sum,cnt);\n}\nclass Solution {\npublic:\n int averageOfSubtree(TreeNode* root) {\n int cnt=1;\n vector<TreeNode*>v;\n \n \n trav(root,v);\n int tcnt=0;\n for(int i=0;i<v.size(); i++)\n {\n int sum=0;\n cnt=0;\n sum_sub(v[i],sum,cnt);\n int avg=round(sum/cnt);\n if(avg==v[i]->val)\n tcnt++;\n }\n return tcnt;\n }\n};", "memory": "17100" }
2,347
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p> <p><strong>Note:</strong></p> <ul> <li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li> <li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;" /> <pre> <strong>Input:</strong> root = [4,8,5,0,1,null,6] <strong>Output:</strong> 5 <strong>Explanation:</strong> For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4. For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5. For the node with value 0: The average of its subtree is 0 / 1 = 0. For the node with value 1: The average of its subtree is 1 / 1 = 1. For the node with value 6: The average of its subtree is 6 / 1 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;" /> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 <strong>Explanation:</strong> For the node with value 1: The average of its subtree is 1 / 1 = 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 1000</code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nvoid trav(TreeNode*root,vector<TreeNode*>&v)\n {\n if(root==nullptr)\n return;\n v.push_back(root);\n trav(root->left,v);\n trav(root->right,v);\n}\nvoid sum_sub(TreeNode*root,int &sum,int &cnt)\n {\n if(root==nullptr)\n return;\n sum+=root->val;\n cnt++;\n sum_sub(root->left,sum,cnt);\n sum_sub(root->right,sum,cnt);\n}\nclass Solution {\npublic:\n int averageOfSubtree(TreeNode* root) {\n int cnt=1;\n vector<TreeNode*>v;\n \n \n trav(root,v);\n int tcnt=0;\n for(int i=0;i<v.size(); i++)\n {\n int sum=0;\n cnt=0;\n sum_sub(v[i],sum,cnt);\n int avg=round(sum/cnt);\n if(avg==v[i]->val)\n tcnt++;\n }\n return tcnt;\n }\n};", "memory": "17200" }
2,347
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p> <p><strong>Note:</strong></p> <ul> <li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li> <li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;" /> <pre> <strong>Input:</strong> root = [4,8,5,0,1,null,6] <strong>Output:</strong> 5 <strong>Explanation:</strong> For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4. For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5. For the node with value 0: The average of its subtree is 0 / 1 = 0. For the node with value 1: The average of its subtree is 1 / 1 = 1. For the node with value 6: The average of its subtree is 6 / 1 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;" /> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 <strong>Explanation:</strong> For the node with value 1: The average of its subtree is 1 / 1 = 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 1000</code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),\n * right(right) {}\n * };\n */\nclass Solution {\npublic:\n int averageOfSubtree(TreeNode* root) {\n map<TreeNode*, pair<int, int>> m;\n stack<TreeNode*> s;\n TreeNode* x = root;\n queue<TreeNode*> q;\n s.push(x);\n q.push(x);\n while (!q.empty()) {\n TreeNode* temp = q.front();\n q.pop();\n m[temp] = make_pair(temp->val, 0);\n if (temp->left != nullptr) {\n q.push(temp->left);\n s.push(temp->left);\n }\n if (temp->right != nullptr) {\n q.push(temp->right);\n s.push(temp->right);\n }\n }\n int ans = 0;\n while (!s.empty()) {\n TreeNode* t = s.top();\n s.pop();\n if (t->left == nullptr && t->right == nullptr) {\n m[t].second = 1;\n ans++;\n } else if (t->right == nullptr) {\n int x1 = t->left->val + t->val;\n int count = m[t->left].second + 1;\n int av = x1 / count;\n t->val = x1;\n m[t].second = count;\n if (av == m[t].first) {\n ans++;\n }\n } else if (t->left == nullptr) {\n int x1 = t->right->val + t->val;\n int count = m[t->right].second + 1;\n int av = x1 / count;\n t->val = x1;\n m[t].second = count;\n if (av == m[t].first) {\n ans++;\n }\n } else {\n int x1 = t->right->val + t->left->val + t->val;\n int count = m[t->left].second + m[t->right].second + 1;\n int av = x1 / count;\n t->val = x1;\n m[t].second = count;\n if (av == m[t].first) {\n ans++;\n }\n }\n }\n return ans;\n }\n};", "memory": "17400" }
2,347
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p> <p><strong>Note:</strong></p> <ul> <li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li> <li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;" /> <pre> <strong>Input:</strong> root = [4,8,5,0,1,null,6] <strong>Output:</strong> 5 <strong>Explanation:</strong> For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4. For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5. For the node with value 0: The average of its subtree is 0 / 1 = 0. For the node with value 1: The average of its subtree is 1 / 1 = 1. For the node with value 6: The average of its subtree is 6 / 1 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;" /> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 <strong>Explanation:</strong> For the node with value 1: The average of its subtree is 1 / 1 = 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 1000</code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),\n * right(right) {}\n * };\n */\nclass Solution {\npublic:\n int averageOfSubtree(TreeNode* root) {\n map<TreeNode*, pair<int, int>> m;\n stack<TreeNode*> s;\n TreeNode* x = root;\n queue<TreeNode*> q;\n s.push(x);\n q.push(x);\n while (!q.empty()) {\n TreeNode* temp = q.front();\n q.pop();\n m[temp] = make_pair(temp->val, 0);\n if (temp->left != nullptr) {\n q.push(temp->left);\n s.push(temp->left);\n }\n if (temp->right != nullptr) {\n q.push(temp->right);\n s.push(temp->right);\n }\n }\n int ans = 0;\n while (!s.empty()) {\n TreeNode* t = s.top();\n s.pop();\n if (t->left == nullptr && t->right == nullptr) {\n m[t].second = 1;\n ans++;\n } else if (t->right == nullptr) {\n int x1 = t->left->val + t->val;\n int count = m[t->left].second + 1;\n int av = x1 / count;\n t->val = x1;\n m[t].second = count;\n if (av == m[t].first) {\n ans++;\n }\n } else if (t->left == nullptr) {\n int x1 = t->right->val + t->val;\n int count = m[t->right].second + 1;\n int av = x1 / count;\n t->val = x1;\n m[t].second = count;\n if (av == m[t].first) {\n ans++;\n }\n } else {\n int x1 = t->right->val + t->left->val + t->val;\n int count = m[t->left].second + m[t->right].second + 1;\n int av = x1 / count;\n t->val = x1;\n m[t].second = count;\n if (av == m[t].first) {\n ans++;\n }\n }\n }\n return ans;\n }\n};", "memory": "17500" }
2,347
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p> <p><strong>Note:</strong></p> <ul> <li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li> <li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;" /> <pre> <strong>Input:</strong> root = [4,8,5,0,1,null,6] <strong>Output:</strong> 5 <strong>Explanation:</strong> For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4. For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5. For the node with value 0: The average of its subtree is 0 / 1 = 0. For the node with value 1: The average of its subtree is 1 / 1 = 1. For the node with value 6: The average of its subtree is 6 / 1 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;" /> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 <strong>Explanation:</strong> For the node with value 1: The average of its subtree is 1 / 1 = 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 1000</code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),\n * right(right) {}\n * };\n */\nclass Solution {\npublic:\n int averageOfSubtree(TreeNode* root) {\n map<TreeNode*, pair<int, int>> m;\n stack<TreeNode*> s;\n TreeNode* x = root;\n queue<TreeNode*> q;\n s.push(x);\n q.push(x);\n while (!q.empty()) {\n TreeNode* temp = q.front();\n q.pop();\n m[temp] = make_pair(temp->val, 0);\n if (temp->left != nullptr) {\n q.push(temp->left);\n s.push(temp->left);\n }\n if (temp->right != nullptr) {\n q.push(temp->right);\n s.push(temp->right);\n }\n }\n int ans = 0;\n while (!s.empty()) {\n TreeNode* t = s.top();\n s.pop();\n if (t->left == nullptr && t->right == nullptr) {\n m[t].second = 1;\n ans++;\n } else if (t->right == nullptr) {\n int x1 = t->left->val + t->val;\n int count = m[t->left].second + 1;\n int av = x1 / count;\n t->val = x1;\n m[t].second = count;\n if (av == m[t].first) {\n ans++;\n }\n } else if (t->left == nullptr) {\n int x1 = t->right->val + t->val;\n int count = m[t->right].second + 1;\n int av = x1 / count;\n t->val = x1;\n m[t].second = count;\n if (av == m[t].first) {\n ans++;\n }\n } else {\n int x1 = t->right->val + t->left->val + t->val;\n int count = m[t->left].second + m[t->right].second + 1;\n int av = x1 / count;\n t->val = x1;\n m[t].second = count;\n if (av == m[t].first) {\n ans++;\n }\n }\n }\n return ans;\n }\n};", "memory": "17600" }
2,347
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p> <p><strong>Note:</strong></p> <ul> <li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li> <li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;" /> <pre> <strong>Input:</strong> root = [4,8,5,0,1,null,6] <strong>Output:</strong> 5 <strong>Explanation:</strong> For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4. For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5. For the node with value 0: The average of its subtree is 0 / 1 = 0. For the node with value 1: The average of its subtree is 1 / 1 = 1. For the node with value 6: The average of its subtree is 6 / 1 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;" /> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 <strong>Explanation:</strong> For the node with value 1: The average of its subtree is 1 / 1 = 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 1000</code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\n\nclass TreeData{\n public:\n int sum;\n int cnt;\n TreeData(int s, int c){\n sum = s; cnt = c;\n }\n};\n\nclass Solution {\n \npublic:\n int ans = 0;\n \n TreeData* util(TreeNode* root){\n \n TreeData* data = new TreeData(root->val,1);\n \n if(root->left!=NULL){\n TreeData* dataLeft = util(root->left);\n data->sum += dataLeft->sum; data->cnt += dataLeft->cnt;\n }\n \n if(root->right!=NULL){\n TreeData* dataRight = util(root->right);\n data->sum += dataRight->sum; data->cnt += dataRight->cnt;\n }\n \n if(root->val == data->sum/data->cnt) ans++;\n return data;\n }\n \n \n int averageOfSubtree(TreeNode* root) {\n util(root);\n return ans;\n }\n};", "memory": "19300" }
2,347
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p> <p><strong>Note:</strong></p> <ul> <li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li> <li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;" /> <pre> <strong>Input:</strong> root = [4,8,5,0,1,null,6] <strong>Output:</strong> 5 <strong>Explanation:</strong> For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4. For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5. For the node with value 0: The average of its subtree is 0 / 1 = 0. For the node with value 1: The average of its subtree is 1 / 1 = 1. For the node with value 6: The average of its subtree is 6 / 1 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;" /> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 <strong>Explanation:</strong> For the node with value 1: The average of its subtree is 1 / 1 = 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int countMatchingAverages(TreeNode* root, int& res) {\n if (root == nullptr)\n return 0;\n\n int leftSum = 0, leftCount = 0;\n int rightSum = 0, rightCount = 0;\n\n if (root->left) {\n leftCount = countMatchingAverages(root->left, res);\n leftSum = sum[root->left->val];\n }\n if (root->right) {\n rightCount = countMatchingAverages(root->right, res);\n rightSum = sum[root->right->val];\n }\n\n int subtreeSum = root->val + leftSum + rightSum;\n int subtreeSize = 1 + leftCount + rightCount;\n\n if (subtreeSum / subtreeSize == root->val)\n res++;\n\n sum[root->val] = subtreeSum;\n return subtreeSize;\n }\n\n int averageOfSubtree(TreeNode* root) {\n unordered_map<int, int> sum;\n int res = 0;\n countMatchingAverages(root, res);\n return res;\n }\n\nprivate:\n unordered_map<int, int> sum;\n};\n", "memory": "20000" }
2,347
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p> <p><strong>Note:</strong></p> <ul> <li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li> <li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;" /> <pre> <strong>Input:</strong> root = [4,8,5,0,1,null,6] <strong>Output:</strong> 5 <strong>Explanation:</strong> For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4. For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5. For the node with value 0: The average of its subtree is 0 / 1 = 0. For the node with value 1: The average of its subtree is 1 / 1 = 1. For the node with value 6: The average of its subtree is 6 / 1 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;" /> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 <strong>Explanation:</strong> For the node with value 1: The average of its subtree is 1 / 1 = 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 1000</code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int averageOfSubtree(TreeNode* root) {\n return f(root)[2];\n }\n \n array<int,3> f(TreeNode* root){\n array<int,3> res={root->val,1,0};\n for(TreeNode* ptr:vector<TreeNode*>{root->left,root->right}){\n if(ptr==nullptr) continue;\n auto [x,y,z]=f(ptr);\n res[2]+=z;\n res[1]+=y;\n res[0]+=x;\n }\n if(root->val==(res[0]/res[1])) ++res[2];\n return res;\n }\n};\n\n\n", "memory": "20100" }
2,347
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p> <p><strong>Note:</strong></p> <ul> <li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li> <li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;" /> <pre> <strong>Input:</strong> root = [4,8,5,0,1,null,6] <strong>Output:</strong> 5 <strong>Explanation:</strong> For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4. For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5. For the node with value 0: The average of its subtree is 0 / 1 = 0. For the node with value 1: The average of its subtree is 1 / 1 = 1. For the node with value 6: The average of its subtree is 6 / 1 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;" /> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 <strong>Explanation:</strong> For the node with value 1: The average of its subtree is 1 / 1 = 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int averageOfSubtree(TreeNode* root) {\n return f(root)[2];\n }\n \n array<int,3> f(TreeNode* root){\n array<int,3> res={root->val,1,0};\n for(TreeNode* ptr:vector<TreeNode*>{root->left,root->right}){\n if(ptr==nullptr) continue;\n auto [x,y,z]=f(ptr);\n res={res[0]+x,res[1]+y,res[2]+z};\n }\n if(root->val==(res[0]/res[1])) ++res[2];\n return res;\n }\n};\n\n\n", "memory": "20100" }
2,347
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p> <p><strong>Note:</strong></p> <ul> <li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li> <li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;" /> <pre> <strong>Input:</strong> root = [4,8,5,0,1,null,6] <strong>Output:</strong> 5 <strong>Explanation:</strong> For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4. For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5. For the node with value 0: The average of its subtree is 0 / 1 = 0. For the node with value 1: The average of its subtree is 1 / 1 = 1. For the node with value 6: The average of its subtree is 6 / 1 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;" /> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 <strong>Explanation:</strong> For the node with value 1: The average of its subtree is 1 / 1 = 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 1000</code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Answer {\n public:\n int ans;\n int sum;\n int nodes;\n\n Answer(int ans, int sum, int nodes) {\n this -> ans = ans;\n this -> sum = sum;\n this -> nodes = nodes;\n }\n};\nclass Solution {\n Answer solve(TreeNode *root) {\n if (root == nullptr) {\n return *(new Answer(0, 0, 0));\n }\n\n Answer left = solve(root -> left);\n Answer right = solve(root -> right);\n\n return *(new Answer(left.ans + right.ans + (((left.sum + right.sum + root -> val) / (left.nodes + right.nodes + 1)) == root -> val), left.sum + right.sum + root -> val, left.nodes + right.nodes + 1));\n }\n\npublic:\n int averageOfSubtree(TreeNode* root) {\n return solve(root).ans;\n }\n};", "memory": "20500" }
2,347
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p> <p><strong>Note:</strong></p> <ul> <li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li> <li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;" /> <pre> <strong>Input:</strong> root = [4,8,5,0,1,null,6] <strong>Output:</strong> 5 <strong>Explanation:</strong> For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4. For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5. For the node with value 0: The average of its subtree is 0 / 1 = 0. For the node with value 1: The average of its subtree is 1 / 1 = 1. For the node with value 6: The average of its subtree is 6 / 1 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;" /> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 <strong>Explanation:</strong> For the node with value 1: The average of its subtree is 1 / 1 = 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 1000</code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int countNodes(TreeNode* node, unordered_map<TreeNode*, int> &dp){\n if(!node->left && !node->right) return 1;\n int left = 0, right = 0;\n if(dp.find(node)!=dp.end()) return dp[node];\n if(node->left) left = countNodes(node->left, dp);\n if(node->right) right = countNodes(node->right, dp);\n return dp[node] = 1+left+right;\n }\n int postorder(TreeNode* root, int &cnt, unordered_map<TreeNode*, int> &dp){\n if(!root) return 0;\n int left=0, right=0;\n if(root->left) left = postorder(root->left, cnt, dp);\n if(root->right) right = postorder(root->right, cnt, dp);\n int sum = left+right+root->val;\n int nodes = countNodes(root, dp);\n int avg = sum/nodes;\n cout << root->val << \" \" << avg << endl;\n if(root->val==avg) cnt++;\n return sum; \n }\n int averageOfSubtree(TreeNode* root) {\n int cnt = 0;\n unordered_map<TreeNode*, int> dp;\n postorder(root, cnt, dp);\n return cnt;\n }\n};", "memory": "20600" }
2,347
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p> <p><strong>Note:</strong></p> <ul> <li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li> <li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;" /> <pre> <strong>Input:</strong> root = [4,8,5,0,1,null,6] <strong>Output:</strong> 5 <strong>Explanation:</strong> For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4. For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5. For the node with value 0: The average of its subtree is 0 / 1 = 0. For the node with value 1: The average of its subtree is 1 / 1 = 1. For the node with value 6: The average of its subtree is 6 / 1 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;" /> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 <strong>Explanation:</strong> For the node with value 1: The average of its subtree is 1 / 1 = 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 1000</code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n map<TreeNode*,int> map;\n\n int getNodes(TreeNode* root){\n if(root==NULL) return 0;\n if(map.find(root)!=map.end()) return map[root];\n return map[root]=1+getNodes(root->left)+getNodes(root->right);\n }\n\n int solve(TreeNode* root,int &count){\n if(root==NULL) return 0;\n int left=solve(root->left,count);\n int right=solve(root->right,count);\n if((root->val+left+right)/getNodes(root) == root->val) count++;\n return root->val+left+right;\n }\n\n int averageOfSubtree(TreeNode* root) {\n int count=0;\n int ans=solve(root,count);\n return count;\n }\n};", "memory": "21000" }
2,347
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p> <p><strong>Note:</strong></p> <ul> <li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li> <li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;" /> <pre> <strong>Input:</strong> root = [4,8,5,0,1,null,6] <strong>Output:</strong> 5 <strong>Explanation:</strong> For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4. For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5. For the node with value 0: The average of its subtree is 0 / 1 = 0. For the node with value 1: The average of its subtree is 1 / 1 = 1. For the node with value 6: The average of its subtree is 6 / 1 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;" /> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 <strong>Explanation:</strong> For the node with value 1: The average of its subtree is 1 / 1 = 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 1000</code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n map<TreeNode* , int> node;\n int result = 0;\n\n int distance(TreeNode* root){\n if(root == NULL){\n return 0;\n }\n\n int left = distance(root->left);\n int right = distance(root->right);\n\n node[root] = left + right+1;\n\n return left + right+1;\n }\n\n \n int solve(TreeNode* root){\n if(root == NULL){\n return 0;\n }\n int left = solve(root->left);\n int right = solve(root->right);\n bool ans = root->val == (left+right+root->val)/node[root];\n if(ans == true){\n result++;\n }\n return root->val + left + right;\n }\n int averageOfSubtree(TreeNode* root) {\n int x = distance(root);\n int y = solve(root);\n for(auto [x,y] : node){\n cout<<x<<\" \"<<y<<endl;\n }\n return result;\n }\n};", "memory": "21100" }
2,347
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p> <p><strong>Note:</strong></p> <ul> <li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li> <li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;" /> <pre> <strong>Input:</strong> root = [4,8,5,0,1,null,6] <strong>Output:</strong> 5 <strong>Explanation:</strong> For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4. For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5. For the node with value 0: The average of its subtree is 0 / 1 = 0. For the node with value 1: The average of its subtree is 1 / 1 = 1. For the node with value 6: The average of its subtree is 6 / 1 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;" /> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 <strong>Explanation:</strong> For the node with value 1: The average of its subtree is 1 / 1 = 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 1000</code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* buildNumTree(TreeNode* root) {\n if(root==NULL)\n {\n return NULL;\n }\n if(root->right==NULL && root->left==NULL)\n {\n TreeNode* tree = new TreeNode(1);\n return tree;\n }\n TreeNode* left = buildNumTree(root->left);\n TreeNode* right = buildNumTree(root->right);\n if(left==NULL)\n {\n TreeNode* tree= new TreeNode(right->val+1, left, right);\n return tree;\n }\n if (right==NULL)\n {\n TreeNode* tree= new TreeNode(1+left->val, left, right);\n return tree;\n }\n TreeNode* tree= new TreeNode(left->val+right->val+1, left, right);\n return tree;\n }\n \n TreeNode* buildSumTree(TreeNode* root) {\n if(root==NULL)\n {\n return NULL;\n }\n if(root->right==NULL && root->left==NULL)\n {\n TreeNode* tree= new TreeNode(root->val);\n return tree;\n }\n TreeNode* left = buildSumTree(root->left);\n TreeNode* right = buildSumTree(root->right);\n if(left==NULL)\n {\n TreeNode* tree= new TreeNode(right->val+root->val, left, right);\n return tree;\n }\n if (right==NULL)\n {\n TreeNode* tree= new TreeNode(left->val+root->val, left, right);\n return tree;\n }\n TreeNode* tree= new TreeNode(left->val+right->val+root->val, left, right);\n return tree;\n }\n int averageOfSubtrees(TreeNode* sum, TreeNode* numNodes, TreeNode* root)\n {\n if(sum==NULL)\n {\n return 0;\n }\n if (sum->left == NULL && sum->right==NULL)\n {\n if (sum->val/numNodes->val == root->val)\n {\n return 1;\n }\n return 0;\n }\n if (sum->left == NULL)\n {\n int ret = 0;\n if (sum->val/numNodes->val == root->val)\n {\n ret=1;\n }\n ret+=averageOfSubtrees(sum->right, numNodes->right, root->right);\n return ret;\n }\n if (sum->right == NULL)\n {\n int ret = 0;\n if (sum->val/numNodes->val == root->val)\n {\n ret=1;\n }\n ret+=averageOfSubtrees(sum->left, numNodes->left, root->left);\n return ret;\n }\n int ret = 0;\n if (sum->val/numNodes->val == root->val)\n {\n ret=1;\n }\n ret+=averageOfSubtrees(sum->left, numNodes->left, root->left) +\n averageOfSubtrees(sum->right, numNodes->right, root->right);\n return ret;\n }\n int averageOfSubtree(TreeNode* root) {\n TreeNode* sumTree = buildSumTree(root);\n TreeNode* numNodes = buildNumTree(root);\n return averageOfSubtrees(sumTree, numNodes, root);\n\n }\n};", "memory": "21200" }
2,347
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p> <p><strong>Note:</strong></p> <ul> <li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li> <li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;" /> <pre> <strong>Input:</strong> root = [4,8,5,0,1,null,6] <strong>Output:</strong> 5 <strong>Explanation:</strong> For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4. For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5. For the node with value 0: The average of its subtree is 0 / 1 = 0. For the node with value 1: The average of its subtree is 1 / 1 = 1. For the node with value 6: The average of its subtree is 6 / 1 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;" /> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 <strong>Explanation:</strong> For the node with value 1: The average of its subtree is 1 / 1 = 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 1000</code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int averageOfSubtree(TreeNode* root) {\n int ret=0;\n dfs(root, ret);\n return ret;\n }\n vector<int> dfs(TreeNode* r, int& ret) {\n if (!r) return {};\n int s=r->val, n=1;\n for(auto c:{r->left, r->right}) {\n if (!c) continue;\n auto a=dfs(c, ret);\n s+=a[0];\n n+=a[1];\n }\n if (s/n==r->val) ret++;\n return {s, n};\n }\n};", "memory": "21300" }
2,347
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p> <p><strong>Note:</strong></p> <ul> <li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li> <li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;" /> <pre> <strong>Input:</strong> root = [4,8,5,0,1,null,6] <strong>Output:</strong> 5 <strong>Explanation:</strong> For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4. For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5. For the node with value 0: The average of its subtree is 0 / 1 = 0. For the node with value 1: The average of its subtree is 1 / 1 = 1. For the node with value 6: The average of its subtree is 6 / 1 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;" /> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 <strong>Explanation:</strong> For the node with value 1: The average of its subtree is 1 / 1 = 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 1000</code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int count=0;\n unordered_map<TreeNode*,int>size;\n\n int dfs(TreeNode*root)\n {\n if(!root)\n {\n return 0;\n }\n\n int left=dfs(root->left);\n int right=dfs(root->right);\n size[root]=left+right+1;\n return size[root];\n }\n\n\n int f(TreeNode*root)\n {\n if(!root)\n {\n return 0;\n }\n\n int left=f(root->left);\n int right=f(root->right);\n\n int cal=left+right+root->val;\n int avg=cal/size[root];\n\n if(avg==root->val)\n {\n count++;\n }\n\n return cal;\n }\n\n int averageOfSubtree(TreeNode* root) \n {\n int h=dfs(root);\n int k=f(root);\n \n return count;\n }\n};\n", "memory": "21400" }
2,347
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p> <p><strong>Note:</strong></p> <ul> <li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li> <li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;" /> <pre> <strong>Input:</strong> root = [4,8,5,0,1,null,6] <strong>Output:</strong> 5 <strong>Explanation:</strong> For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4. For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5. For the node with value 0: The average of its subtree is 0 / 1 = 0. For the node with value 1: The average of its subtree is 1 / 1 = 1. For the node with value 6: The average of its subtree is 6 / 1 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;" /> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 <strong>Explanation:</strong> For the node with value 1: The average of its subtree is 1 / 1 = 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 1000</code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n unordered_map<TreeNode*, int> averages;\n\n pair<int, int> computeSubTreeAvg(TreeNode* it) {\n if (it == NULL)\n return {0, 0};\n\n auto [lSum, lCount] = computeSubTreeAvg(it->left);\n auto [rSum, rCount] = computeSubTreeAvg(it->right);\n\n averages[it] = (lSum + rSum + it->val) / (lCount + rCount + 1);\n \n return {lSum + rSum + it->val, lCount + rCount + 1};\n }\npublic:\n int averageOfSubtree(TreeNode* root) {\n computeSubTreeAvg(root);\n\n int count = 0;\n for (auto &it : averages)\n if ((it.first)->val == it.second)\n count++;\n\n return count;\n }\n};", "memory": "21500" }
2,347
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p> <p><strong>Note:</strong></p> <ul> <li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li> <li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;" /> <pre> <strong>Input:</strong> root = [4,8,5,0,1,null,6] <strong>Output:</strong> 5 <strong>Explanation:</strong> For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4. For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5. For the node with value 0: The average of its subtree is 0 / 1 = 0. For the node with value 1: The average of its subtree is 1 / 1 = 1. For the node with value 6: The average of its subtree is 6 / 1 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;" /> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 <strong>Explanation:</strong> For the node with value 1: The average of its subtree is 1 / 1 = 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 1000</code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n unordered_map<TreeNode*, int> averages;\n int answer;\n\n pair<int, int> computeSubTreeAvg(TreeNode* it) {\n if (it == NULL)\n return {0, 0};\n\n auto [lSum, lCount] = computeSubTreeAvg(it->left);\n auto [rSum, rCount] = computeSubTreeAvg(it->right);\n int totalSum = lSum + rSum + it->val;\n int totalCount = lCount + rCount + 1;\n averages[it] = totalSum / totalCount;\n if (averages[it] == it->val)\n answer++;\n return {totalSum, totalCount};\n }\npublic:\n int averageOfSubtree(TreeNode* root) {\n this->answer = 0;\n computeSubTreeAvg(root);\n\n return answer;\n }\n};", "memory": "21500" }
2,347
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p> <p><strong>Note:</strong></p> <ul> <li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li> <li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;" /> <pre> <strong>Input:</strong> root = [4,8,5,0,1,null,6] <strong>Output:</strong> 5 <strong>Explanation:</strong> For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4. For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5. For the node with value 0: The average of its subtree is 0 / 1 = 0. For the node with value 1: The average of its subtree is 1 / 1 = 1. For the node with value 6: The average of its subtree is 6 / 1 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;" /> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 <strong>Explanation:</strong> For the node with value 1: The average of its subtree is 1 / 1 = 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 1000</code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int averageOfSubtree(TreeNode* root) {\n if (!root)\n return 0;\n\n int res = 0;\n unordered_map<TreeNode*, int> sumSub;\n dfs(root, sumSub);\n dfs2(root, sumSub, res);\n return res;\n }\n\nprivate:\n // returns the sum of the subtree\n int dfs(TreeNode *cur, unordered_map<TreeNode*, int> &sumSub)\n {\n if (!cur)\n return 0;\n \n int left = dfs(cur->left, sumSub);\n int right = dfs(cur->right, sumSub);\n sumSub[cur] = left + right + cur->val;\n return left + right + cur->val;\n }\n // returns the count of the subtree\n int dfs2(TreeNode *cur, unordered_map<TreeNode*, int> &sumSub, int &res)\n {\n if (!cur)\n return 0;\n \n int left = dfs2(cur->left, sumSub, res);\n int right = dfs2(cur->right, sumSub, res);\n\n int average = sumSub[cur] / (left + right + 1);\n if (cur->val == average)\n res++;\n\n return left + right + 1;\n }\n};", "memory": "21600" }
2,347
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p> <p><strong>Note:</strong></p> <ul> <li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li> <li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;" /> <pre> <strong>Input:</strong> root = [4,8,5,0,1,null,6] <strong>Output:</strong> 5 <strong>Explanation:</strong> For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4. For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5. For the node with value 0: The average of its subtree is 0 / 1 = 0. For the node with value 1: The average of its subtree is 1 / 1 = 1. For the node with value 6: The average of its subtree is 6 / 1 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;" /> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 <strong>Explanation:</strong> For the node with value 1: The average of its subtree is 1 / 1 = 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 1000</code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\nprivate:\n unordered_map<TreeNode*, int> nodeCount{};\n int count = 0;\npublic:\n int averageOfSubtree(TreeNode* root) {\n nodeCount.insert(pair<TreeNode*, int>(root, 1));\n if(root->left){\n averageOfSubtree(root->left);\n nodeCount[root] += nodeCount[root->left];\n nodeCount.erase(root->left);\n }\n if(root->right){\n averageOfSubtree(root->right);\n nodeCount[root] += nodeCount[root->right];\n nodeCount.erase(root->right);\n }\n\n int sum = root->val;\n if(root->left){\n sum += root->left->val;\n }\n if(root->right){\n sum += root->right->val;\n }\n\n if(sum / nodeCount[root] == root->val){\n count++;\n }\n\n //cout << root->val << \" \" << sum << \" \" << nodeCount[root] << \"\\n\";\n\n root->val = sum; \n\n return count;\n }\n};", "memory": "21800" }
2,347
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p> <p><strong>Note:</strong></p> <ul> <li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li> <li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;" /> <pre> <strong>Input:</strong> root = [4,8,5,0,1,null,6] <strong>Output:</strong> 5 <strong>Explanation:</strong> For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4. For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5. For the node with value 0: The average of its subtree is 0 / 1 = 0. For the node with value 1: The average of its subtree is 1 / 1 = 1. For the node with value 6: The average of its subtree is 6 / 1 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;" /> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 <strong>Explanation:</strong> For the node with value 1: The average of its subtree is 1 / 1 = 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 1000</code></li> </ul>
3
{ "code": "class Avg{\n public:\n int sum,count;\n Avg(int s =0 ,int c = 0){\n sum =s;\n count =c;\n }\n};\nclass Solution {\npublic:\n Avg* GetAvg(TreeNode* root,int &COUNT)\n {\n if(root==nullptr)\n return new Avg();\n int count = 0;\n int sum = 0;\n Avg *lptr = new Avg(),*rptr= new Avg();\n if(root->left!=nullptr)\n lptr = GetAvg(root->left,COUNT);\n if(root->right!=nullptr)\n rptr = GetAvg(root->right,COUNT);\n count = lptr->count + rptr->count + 1;\n sum = lptr->sum + rptr->sum + root->val;\n if(int(sum/count) == root->val)\n COUNT++;\n delete lptr,rptr;\n return new Avg(sum,count);\n } \n int averageOfSubtree(TreeNode* root) {\n int count = 0;\n GetAvg(root,count);\n return count; \n }\n};", "memory": "21900" }
2,347
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p> <p><strong>Note:</strong></p> <ul> <li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li> <li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;" /> <pre> <strong>Input:</strong> root = [4,8,5,0,1,null,6] <strong>Output:</strong> 5 <strong>Explanation:</strong> For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4. For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5. For the node with value 0: The average of its subtree is 0 / 1 = 0. For the node with value 1: The average of its subtree is 1 / 1 = 1. For the node with value 6: The average of its subtree is 6 / 1 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;" /> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 <strong>Explanation:</strong> For the node with value 1: The average of its subtree is 1 / 1 = 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 1000</code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n unordered_map<TreeNode*, pair<int,int>> node2SumAndSize;\npublic:\n int averageOfSubtree(TreeNode* root) {\n int count{};\n preorder(root, count);\n return count;\n }\n\n void preorder(TreeNode* root, int& count) {\n auto [sum, size] = sumAndSizeOfSubtree(root);\n if (root->val == sum/size) ++count;\n if (root->left) preorder(root->left, count);\n if (root->right) preorder(root->right, count);\n }\n\n pair<int,int> sumAndSizeOfSubtree(TreeNode* root) {\n if (node2SumAndSize.count(root)) return node2SumAndSize[root];\n if (!root->left && !root->right) return node2SumAndSize[root] = {root->val, 1};\n if (!root->left) {\n auto [sumR, sizeR] = sumAndSizeOfSubtree(root->right);\n return node2SumAndSize[root] = {sumR + root->val, sizeR + 1};\n }\n if (!root->right) {\n auto [sumL, sizeL] = sumAndSizeOfSubtree(root->left);\n return node2SumAndSize[root] = {sumL + root->val, sizeL + 1};\n }\n auto [sumR, sizeR] = sumAndSizeOfSubtree(root->right);\n auto [sumL, sizeL] = sumAndSizeOfSubtree(root->left);\n return node2SumAndSize[root] = {sumR + sumL + root->val, sizeR + sizeL + 1};\n }\n\n\n};", "memory": "22000" }
2,347
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p> <p><strong>Note:</strong></p> <ul> <li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li> <li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;" /> <pre> <strong>Input:</strong> root = [4,8,5,0,1,null,6] <strong>Output:</strong> 5 <strong>Explanation:</strong> For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4. For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5. For the node with value 0: The average of its subtree is 0 / 1 = 0. For the node with value 1: The average of its subtree is 1 / 1 = 1. For the node with value 6: The average of its subtree is 6 / 1 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;" /> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 <strong>Explanation:</strong> For the node with value 1: The average of its subtree is 1 / 1 = 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 1000</code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n vector<int> helper(TreeNode* root, int& ans){\n if(!root) return vector<int>{0, 0};\n if(!root->left && !root->right){\n ans++;\n return vector<int>{root->val, 1};\n }\n\n vector<int> left, right;\n left = helper(root->left, ans);\n right = helper(root->right, ans);\n\n int totalSum = left[0] + right[0] + root->val;\n int numNodes = left[1] + right[1] + 1;\n\n int avg = totalSum / numNodes;\n if(avg == root->val) ans++;\n\n return vector<int>{totalSum, numNodes};\n }\n\n int averageOfSubtree(TreeNode* root) {\n int ans = 0;\n\n helper(root, ans);\n\n return ans;\n }\n};", "memory": "22000" }
2,347
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p> <p><strong>Note:</strong></p> <ul> <li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li> <li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;" /> <pre> <strong>Input:</strong> root = [4,8,5,0,1,null,6] <strong>Output:</strong> 5 <strong>Explanation:</strong> For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4. For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5. For the node with value 0: The average of its subtree is 0 / 1 = 0. For the node with value 1: The average of its subtree is 1 / 1 = 1. For the node with value 6: The average of its subtree is 6 / 1 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;" /> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 <strong>Explanation:</strong> For the node with value 1: The average of its subtree is 1 / 1 = 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 1000</code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Avg{\n public:\n int sum,count;\n Avg(int s =0 ,int c = 0){\n sum =s;\n count =c;\n }\n};\nclass Solution {\npublic:\n Avg* GetAvg(TreeNode* root,int &COUNT)\n {\n if(root==nullptr)\n return new Avg();\n int count = 0;\n int sum = 0;\n Avg *lptr = new Avg(),*rptr= new Avg();\n if(root->left!=nullptr)\n lptr = GetAvg(root->left,COUNT);\n if(root->right!=nullptr)\n rptr = GetAvg(root->right,COUNT);\n count = lptr->count + rptr->count + 1;\n sum = lptr->sum + rptr->sum + root->val;\n if(int(sum/count) == root->val)\n COUNT++;\n delete lptr,rptr;\n return new Avg(sum,count);\n } \n int averageOfSubtree(TreeNode* root) {\n int count = 0;\n GetAvg(root,count);\n return count; \n }\n};", "memory": "22100" }
2,347
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p> <p><strong>Note:</strong></p> <ul> <li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li> <li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;" /> <pre> <strong>Input:</strong> root = [4,8,5,0,1,null,6] <strong>Output:</strong> 5 <strong>Explanation:</strong> For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4. For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5. For the node with value 0: The average of its subtree is 0 / 1 = 0. For the node with value 1: The average of its subtree is 1 / 1 = 1. For the node with value 6: The average of its subtree is 6 / 1 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;" /> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 <strong>Explanation:</strong> For the node with value 1: The average of its subtree is 1 / 1 = 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 1000</code></li> </ul>
3
{ "code": "\nclass Solution \n{\n public:\n\n map<int, int >mp;\n map<int, vector<int> >avg;\n \n int counter(TreeNode *root)\n {\n if(root==NULL)\n {\n return 0;\n }\n\n if(root && root->left==NULL && root->right==NULL)\n {\n int z=root->val;\n mp[z]=1;\n root->val=root->val;\n //cout<<z<<\" \"<<root->val<<endl;\n avg[z].push_back(z);\n return 1;\n }\n\n int value=1+counter(root->left)+counter(root->right);\n int values=root->val;\n mp[values]=value;\n\n if(root->left)\n root->val+=(root->left->val);\n\n if(root->right)\n root->val+=(root->right->val);\n\n //cout<<values<<\" \"<<root->val<<endl;\n\n avg[values].push_back(root->val/(mp[values]));\n\n return value;\n }\n\n int averageOfSubtree(TreeNode* root) \n {\n counter(root);\n\n int ct=0;\n\n for(auto it : avg){\n\n vector<int>sp=it.second;\n\n for(int i=0;i<sp.size();i++){\n if(it.first == sp[i]){\n ct++;\n }\n }\n \n \n }\n\n return ct;\n \n }\n};", "memory": "22700" }
2,347
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p> <p><strong>Note:</strong></p> <ul> <li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li> <li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/15/image-20220315203925-1.png" style="width: 300px; height: 212px;" /> <pre> <strong>Input:</strong> root = [4,8,5,0,1,null,6] <strong>Output:</strong> 5 <strong>Explanation:</strong> For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4. For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5. For the node with value 0: The average of its subtree is 0 / 1 = 0. For the node with value 1: The average of its subtree is 1 / 1 = 1. For the node with value 6: The average of its subtree is 6 / 1 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <img src="https://assets.leetcode.com/uploads/2022/03/26/image-20220326133920-1.png" style="width: 80px; height: 76px;" /> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 <strong>Explanation:</strong> For the node with value 1: The average of its subtree is 1 / 1 = 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 1000</code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int nodes = 0;\n vector<int> avg(TreeNode* root){\n if(root==NULL){\n vector<int>v(2,0);\n return v;\n }\n vector<int>v(2);\n vector<int>v1 = avg(root->left);\n vector<int>v2 = avg(root->right);\n v[1] = 1 + v1[1] + v2[1];\n v[0] = root->val + v1[0] + v2[0];\n int avg = v[0]/v[1];\n //cout<<avg<<' '<<root->val<<endl;\n if(avg==root->val)\n nodes++;\n return v;\n }\n int averageOfSubtree(TreeNode* root) {\n avg(root);\n return nodes;\n }\n};", "memory": "22800" }
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>
0
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n long long maxi=0;\n int n=nums.size();\n int maxele=*max_element(nums.begin(),nums.end());\n int maxcount=0;\n int i=0;\n for(int j=0;j<n;j++){\n if(nums[j]==maxele){\n maxcount++;\n }\n\n while(maxcount>=k){\n maxi+=n-j;\n if(nums[i]==maxele){\n maxcount--;\n }\n i++;\n }\n \n }\n return maxi;\n\n \n }\n};", "memory": "120300" }
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>
0
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n int start=0,end=0,maxElem=0;\n for(int i=0;i<nums.size();i++){\n maxElem=max(maxElem,nums[i]);\n }\n long long total=0,cnt=0;\n while(end<nums.size()){\n if(nums[end]==maxElem) cnt++;\n\n while(cnt==k){\n total+=nums.size()-end;\n if(nums[start]==maxElem) cnt--;\n\n start++;\n }\n end++;\n }\n return total;\n }\n};", "memory": "120400" }
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>
0
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n int n=nums.size();\n long long countMax=0;\n long long result=0;\n int max_value=*max_element(nums.begin(),nums.end());\n int j=0,i=0;\n while(j<n){\n if(nums[j]==max_value) countMax++;\n while(countMax>=k){\n result+=n-j;\n if(nums[i]==max_value) countMax--;\n i++;\n }\n j++;\n }\n return result;\n }\n};", "memory": "120500" }
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>
0
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n int n = nums.size(), maxi = *max_element(nums.begin(), nums.end());\n int i = 0, j = 0, count = 0;\n long long ans = 0;\n while (j < n) {\n if (nums[j] == maxi) count++;\n while (i <= j && count >= k) {\n if (nums[i] == maxi) count--;\n i++;\n }\n ans += j-i+1;\n j++;\n }\n return (long long)n * (n+1) / 2 - ans;\n }\n};", "memory": "120600" }
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>
0
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n int maxElement = *max_element(nums.begin(), nums.end());\n long long ans = 0, l = 0;\n\n for (int r = 0; r < nums.size(); r++) {\n if (nums[r] == maxElement) {\n k--;\n }\n while (!k) {\n if (nums[l] == maxElement) {\n k++;\n }\n l++;\n }\n ans += l;\n }\n return ans;\n }\n};", "memory": "120600" }
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>
2
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n int max_num = 0;\n for(int i = 0; i < nums.size(); ++i){\n if(max_num < nums[i]){\n max_num = nums[i];\n }\n }\n vector<int> cnts(k + 1);\n long long tot_cnts = 0;\n for(int i = nums.size() - 1; i >= 0; --i){\n if(nums[i] == max_num){\n int tmp = cnts[k];\n for(int j = k; j > 0; --j){\n cnts[j] = cnts[j-1];\n }\n cnts[k] += tmp;\n cnts[1] += 1;\n cnts[0] = 0;\n }else{\n cnts[0] +=1;\n }\n // std::cout << cnts[k] << std::endl;\n tot_cnts += cnts[k];\n }\n return tot_cnts;\n }\n};", "memory": "121300" }
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>
2
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n // 1 3 2 3 3\n // i j\n // \n int maxi = *max_element(nums.begin(),nums.end());\n\n int ind = -1;\n\n vector<int> index;\n int cnt = 0;\n long long ans = 0;\n for(int i = 0 ; i < nums.size() ;i++){\n if(nums[i] == maxi) {\n cnt++;\n index.push_back(i);\n }\n if(index.size() > k) index.erase(index.begin());\n if(cnt >= k){\n ans += index[0] + 1;\n }\n \n }\n return ans;\n }\n};", "memory": "121400" }
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>
2
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n // 1 3 2 3 3\n // i j\n // \n int maxi = *max_element(nums.begin(),nums.end());\n\n int ind = -1;\n\n vector<int> index;\n int cnt = 0;\n long long ans = 0;\n for(int i = 0 ; i < nums.size() ;i++){\n if(nums[i] == maxi) {\n cnt++;\n index.push_back(i);\n }\n if(index.size() > k) index.erase(index.begin());\n if(cnt >= k){\n ans += index[0] + 1;\n }\n \n }\n return ans;\n }\n};", "memory": "121600" }
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>
2
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n int max_ele=INT_MIN,n=nums.size();\n for(int i=0;i<n;i++){\n max_ele=max(max_ele,nums[i]);\n }\n long long start=0,end=0,count=0;\n unordered_map<int,int>m;\n while(end<n){\n if(nums[end]==max_ele)\n m[max_ele]++;\n while(m[max_ele]>=k){\n count+=n-end;\n if(nums[start]==max_ele)\n m[max_ele]--;\n start++;\n }\n end++;\n }\n return count;\n }\n};", "memory": "121900" }
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>
2
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n int maxi = INT_MIN;\n for(int i=0; i<nums.size(); i++){\n if(nums[i] > maxi) maxi = nums[i];\n }\n int l =0;\n int r =0;\n unordered_map<int, int> mp;\n long long maxLen = 0;\n while(r < nums.size()){\n // expansion\n if(nums[r] == maxi) mp[nums[r]]++;\n // shrink\n while(mp[maxi] == k){\n maxLen = maxLen + (nums.size() - r );\n if(nums[l] == maxi) mp[nums[l]]--;\n l++;\n }\n r++;\n }\n return maxLen;\n\n }\n};", "memory": "122000" }
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>
2
{ "code": "class Solution {\npublic:\n long long yes(vector<int>& nums, int k,int maxi){\n unordered_map<int,int> m;\n int l=0,r=0;\n long long cnt=0;\n while(r<nums.size()){\n if(nums[r]==maxi) m[maxi]++;\n while(m[maxi]>=k){\n if(nums[l]==maxi) m[maxi]--;\n l++;\n }\n cnt+=(r-l+1);r++;\n }\n return cnt;\n }\n long long countSubarrays(vector<int>& nums, int k) {\n long long n=nums.size();int maxi=0;\n for(int i=0;i<n;i++){\n maxi=max(maxi,nums[i]);\n }\n long long t=((long long)(n*(n-1))/2)+n;\n return t-yes(nums,k,maxi);\n }\n};", "memory": "122100" }
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>
2
{ "code": "class Solution {\npublic:\n long long yes(vector<int>& nums, int k,int maxi){\n unordered_map<int,int> m;\n int l=0,r=0;\n long long cnt=0;\n while(r<nums.size()){\n if(nums[r]==maxi) m[maxi]++;\n while(m[maxi]>=k){\n if(nums[l]==maxi) m[maxi]--;\n l++;\n }\n cnt+=(r-l+1);r++;\n }\n return cnt;\n }\n long long countSubarrays(vector<int>& nums, int k) {\n long long n=nums.size();int maxi=0;\n for(int i=0;i<n;i++){\n maxi=max(maxi,nums[i]);\n }\n long long t=((long long)(n*(n-1))/2)+n;\n return t-yes(nums,k,maxi);\n }\n};", "memory": "122100" }
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>
2
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n int sz = nums.size();\n int max_ele = *max_element(nums.begin(), nums.end());\n queue<int> indices;\n\n long long subCount = 0;\n\n for(int i = 0; i < sz; i++) {\n if(nums[i] == max_ele) indices.push(i + 1);\n if(indices.size() > k) indices.pop();\n if(indices.size() == k) {\n subCount += indices.front();\n }\n }\n\n return subCount;\n }\n};", "memory": "123000" }
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>
2
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n // int ans = 0;\n int maxi = 0;\n int n = nums.size();\n for(int i=0;i<n;i++){\n maxi=max(maxi,nums[i]);\n }\n queue<int>q;\n long long ans = 0;\n for(int i=0;i<n;i++){\n if(maxi==nums[i]){\n q.push(i);\n }\n if(q.size() > k)q.pop();\n if(q.size() == k){\n ans+= (q.front()+1);\n }\n }\n return ans;\n }\n};", "memory": "123100" }
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>
2
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n long long ans = 0;\n int maxNum = nums[0];\n for (int num: nums) {\n maxNum = max(num, maxNum);\n }\n\n // keep track of indices where k appears\n queue<int> indices;\n int start = 0, n = nums.size();\n for (int i = 0; i < n; ++i) {\n if (nums[i] == maxNum) {\n indices.push(i);\n if (indices.size() > k) {\n start = indices.front() + 1;\n indices.pop();\n }\n }\n if (indices.size() >= k) {\n start = max(start, indices.front());\n ans += start + 1;\n }\n }\n return ans;\n }\n};", "memory": "123200" }
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>
2
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n long long ans = 0;\n int max_n = nums[0];\n for(auto i:nums)\n if(i>max_n)\n max_n = i;\n\n std::queue<int> q;\n for(int i=0; i<nums.size(); i++){\n if(nums[i]==max_n)\n q.push(i);\n if(q.size() < k) continue;\n if(q.size() > k) q.pop();\n ans += q.front() + 1;\n }\n return ans;\n }\n};\nstatic bool _foo = ios::sync_with_stdio(false);\nstatic ostream* _bar = cin.tie(NULL);", "memory": "123300" }
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>
2
{ "code": "#pragma GCC optimize(\"O3\",\"unroll-loops\")\n\nclass Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n long long ans = 0;\n int max_n = *max_element(nums.begin(),nums.end());\n std::queue<int> q;\n for(int i=0; i<nums.size(); i++){\n if(nums[i]==max_n)\n q.push(i);\n if(q.size() < k) continue;\n if(q.size() > k) q.pop();\n ans += q.front() + 1;\n }\n return ans;\n }\n};\nstatic bool _foo = ios::sync_with_stdio(false);\nstatic ostream* _bar = cin.tie(NULL);", "memory": "123500" }
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>
2
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n long long ans = 0;\n int max_n = *max_element(nums.begin(),nums.end());\n std::queue<int> q;\n for(int i=0; i<nums.size(); i++){\n if(nums[i]==max_n)\n q.push(i);\n if(q.size() < k) continue;\n if(q.size() > k) q.pop();\n ans += q.front() + 1;\n }\n return ans;\n }\n};\nstatic bool _foo = ios::sync_with_stdio(false);\nstatic ostream* _bar = cin.tie(NULL);", "memory": "123600" }
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>
2
{ "code": "#include <climits>\n#include <vector>\n\nclass Solution {\n public:\n long long countSubarrays(std::vector<int>& nums, int k) {\n // Constraints:\n // - k is never smaller than 1.\n // - nums is never empty.\n // Traverse to find largest number.\n int largestNumber = INT_MIN;\n int numOccurances = 0;\n const size_t length = nums.size();\n for (size_t index = 0; index < length; ++index) {\n const int number = nums[index];\n if (number > largestNumber) {\n largestNumber = number;\n numOccurances = 0;\n }\n if (number == largestNumber) {\n ++numOccurances;\n }\n }\n\n if (numOccurances < k) {\n return 0;\n }\n\n // Traverse again and store index of occurences into vector.\n std::vector<size_t> occurances;\n occurances.reserve(numOccurances);\n for (size_t index = 0; index < length; ++index) {\n if (nums[index] == largestNumber) {\n occurances.push_back(index);\n }\n }\n\n // Compute all possible combinations based on these indices.\n long long totalPossibilities = 0;\n // How far we've iterated through the occurances.\n size_t kIndex = 0;\n // Index of first occurance to the right of the current position.\n size_t firstRightOccurance = occurances[kIndex];\n // Index of first + k - 1 occurance to the right of the current position. This index must be included in a subarray for it to be valid.\n size_t kthRightOccurance = occurances[kIndex + k - 1];\n // Index of the last possible start of a subarray, all subarrays after that will not contain at least k occurances.\n const size_t lastPossibleStart = occurances[numOccurances - k];\n // Walk through all array elements. (Not the optimal solution. It should be possible (and relatively easy) to just iterate over the occurances as those are the only indices at which the num of possibilities changes).\n for (size_t currentStart = 0; currentStart <= lastPossibleStart; ++currentStart) {\n if (currentStart > firstRightOccurance) {\n ++kIndex;\n firstRightOccurance = occurances[kIndex];\n kthRightOccurance = occurances[kIndex + k - 1];\n }\n\n const long long possibilites = length - kthRightOccurance;\n totalPossibilities += possibilites;\n }\n\n return totalPossibilities;\n }\n};", "memory": "123700" }
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>
2
{ "code": "#include <climits>\n#include <vector>\n\nclass Solution {\n public:\n long long countSubarrays(std::vector<int>& nums, int k) {\n // Constraints:\n // - k is never smaller than 1.\n // - nums is never empty.\n // Traverse to find largest number.\n int largestNumber = INT_MIN;\n int numOccurances = 0;\n const size_t length = nums.size();\n for (size_t index = 0; index < length; ++index) {\n const int number = nums[index];\n if (number > largestNumber) {\n largestNumber = number;\n numOccurances = 0;\n }\n if (number == largestNumber) {\n ++numOccurances;\n }\n }\n\n if (numOccurances < k) {\n return 0;\n }\n\n // Traverse again and store index of occurences into vector.\n std::vector<size_t> occurances;\n occurances.reserve(numOccurances);\n for (size_t index = 0; index < length; ++index) {\n if (nums[index] == largestNumber) {\n occurances.push_back(index);\n }\n }\n\n // Compute all possible combinations based on these indices.\n long long totalPossibilities = 0;\n // How far we've iterated through the occurances.\n size_t kIndex = 0;\n // Index of first occurance to the right of the current position.\n size_t firstRightOccurance = occurances[kIndex];\n // Index of first + k - 1 occurance to the right of the current position. This index must be included in a subarray for it to be valid.\n size_t kthRightOccurance = occurances[kIndex + k - 1];\n // Index of the last possible start of a subarray, all subarrays after that will not contain at least k occurances.\n const size_t lastPossibleStart = occurances[numOccurances - k];\n // Walk through all array elements. (Not the optimal solution. It should be possible (and relatively easy) to just iterate over the occurances as those are the only indices at which the num of possibilities changes).\n for (size_t currentStart = 0; currentStart <= lastPossibleStart; ++currentStart) {\n if (currentStart > firstRightOccurance) {\n ++kIndex;\n firstRightOccurance = occurances[kIndex];\n kthRightOccurance = occurances[kIndex + k - 1];\n }\n\n const long long possibilites = length - kthRightOccurance;\n totalPossibilities += possibilites;\n }\n\n return totalPossibilities;\n }\n};", "memory": "123800" }
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>
2
{ "code": "#include <climits>\n#include <vector>\n\nclass Solution {\n public:\n long long countSubarrays(std::vector<int>& nums, int k) {\n // Constraints:\n // - k is never smaller than 1.\n // - nums is never empty.\n // Traverse to find largest number.\n int largestNumber = INT_MIN;\n int numOccurances = 0;\n const size_t length = nums.size();\n for (size_t index = 0; index < length; ++index) {\n const int number = nums[index];\n if (number > largestNumber) {\n largestNumber = number;\n numOccurances = 0;\n }\n if (number == largestNumber) {\n ++numOccurances;\n }\n }\n\n if (numOccurances < k) {\n return 0;\n }\n\n // Traverse again and store index of occurences into vector.\n std::vector<size_t> occurances;\n occurances.reserve(numOccurances);\n for (size_t index = 0; index < length; ++index) {\n if (nums[index] == largestNumber) {\n occurances.push_back(index);\n }\n }\n\n // Compute all possible combinations based on these indices.\n long long totalPossibilities = 0;\n // How far we've iterated through the occurances.\n size_t kIndex = 0;\n // Index of first occurance to the right of the current position.\n size_t firstRightOccurance = occurances[kIndex];\n // Index of first + k - 1 occurance to the right of the current position. This index must be included in a subarray for it to be valid.\n size_t kthRightOccurance = occurances[kIndex + k - 1];\n // Index of the last possible start of a subarray, all subarrays after that will not contain at least k occurances.\n const size_t lastPossibleStart = occurances[numOccurances - k];\n // Walk through all array elements. (Not the optimal solution. It should be possible (and relatively easy) to just iterate over the occurances as those are the only indices at which the num of possibilities changes).\n for (size_t currentStart = 0; currentStart <= lastPossibleStart; ++currentStart) {\n if (currentStart > firstRightOccurance) {\n ++kIndex;\n firstRightOccurance = occurances[kIndex];\n kthRightOccurance = occurances[kIndex + k - 1];\n }\n\n const long long possibilites = length - kthRightOccurance;\n totalPossibilities += possibilites;\n }\n\n return totalPossibilities;\n }\n};", "memory": "123900" }
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>
2
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n int mx = *max_element(nums.begin(), nums.end());\n vector<int> lct;\n long long ans = 0;\n for (int i = 0; i < nums.size(); i++){\n if (nums[i] == mx) lct.push_back(i);\n int n = lct.size();\n if (n >= k) ans += lct[n - k] + 1;\n }\n return ans;;\n }\n};", "memory": "124000" }
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>
2
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n int max_elem =-1;\n for(int num:nums){\n max_elem = max(max_elem,num);\n }\n long long ans =0;\n vector<int> idx_hold;\n int cnt =0;\n int n = nums.size();\n for(int i=0;i<n;i++){\n if(nums[i]==max_elem){\n idx_hold.push_back(i);\n cnt++;\n }\n if(cnt>=k){\n int s = idx_hold.size();\n int idx_end = s-k;\n int idx = idx_hold[idx_end];\n ans += (idx)+1;\n }\n }\n return ans;\n }\n};", "memory": "124100" }
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>
2
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n int max=nums[0];\n int n=nums.size();\n for(int i=1;i<n;i++)\n if(max<nums[i]) max=nums[i];\n vector<int> max_index;\n max_index.push_back(-1);\n for(int i=0;i<n;i++)\n if(max==nums[i]) max_index.push_back(i);\n int index_size=max_index.size();\n if(index_size-1<k)\n return 0;\n long long subarray=0;\n vector<long long> sub;\n for(int i=1;i<=index_size-k;i++){\n long long a=(max_index[i]-max_index[i-1]);\n long long b=(n-max_index[i+k-1]);\n subarray+=a*b;\n }\n \n return subarray;\n }\n \n};", "memory": "124200" }
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>
2
{ "code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int k) {\n int N = (int)nums.size();\n int max_value = *max_element(nums.begin(), nums.end());\n vector<int> positions;\n for (int i = 0; i < N; i++) {\n if (nums[i] == max_value) {\n positions.push_back(i);\n }\n } \n long long ans = 0LL;\n for (int i = 0; i + k <= (int)positions.size(); i++) {\n int l = positions[i];\n int r = positions[i + k - 1];\n long long int left_options = l - (i == 0 ? -1 : positions[i - 1]);\n int right_options = N - r;\n ans += left_options * right_options;\n }\n return ans;\n }\n};", "memory": "124300" }
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 //gyaan waali baatein\n //ki bhai issse pehle wala approach me hmlog piche se jitna subarray\n //bn rha tha wo nikal liye \n //or es baar hmlog aage se jo bhi subarray bn skta hai wo nikaal rhe hai.\n int n=nums.size();\n int i=0;\n int max_value=*max_element(nums.begin(),nums.end());\n long long result=0;\n vector<int> indices;\n while(i<n){\n if(nums[i]==max_value){\n indices.push_back(i);\n }\n int size=indices.size();\n if(size>=k){\n int result_idx=indices[size-k];\n result+=(result_idx+1);\n }\n i++;\n }\n return result;\n }\n};", "memory": "124400" }
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 vector<int> temp;\n for(int i=0;i<n;i++){\n if(nums[i]==maxi){\n temp.push_back(i);\n }\n }\n\n long ans=0;\n for(int i=0;i<temp.size();i++){\n // cout<<temp[i]<<\" \";\n int curr = temp[i];\n int j =i+k-1;\n int next;\n if(j<temp.size()){\n next = temp[j];\n }\n else{\n break;\n }\n\n long prev=1;\n if(i>0){\n prev+=(temp[i]-temp[i-1]-1);\n }\n else{\n prev+=(temp[i]);\n }\n\n long aage = 1 + n-next-1;\n\n ans +=(prev*aage);\n }\n\n return ans;\n }\n};", "memory": "124500" }
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 maxnum = *max_element(begin(nums), end(nums));\n long long ans = 0;\n int n = nums.size();\n\n vector<int> maxind(1,-1);\n for (int i = 0; i < n; i++) {\n if (nums[i] == maxnum) {\n maxind.push_back(i);\n }\n }\n \n int indsize = maxind.size();\n\n long long l, r;\n \n for (int i = 1; i <= indsize - k; i++) {\n l = maxind[i] - maxind[i - 1] - 1;\n r = n - 1 - maxind[i + k - 1];\n \n ans += (l + 1) * (r + 1);\n }\n return ans;\n }\n};", "memory": "124600" }
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 deque<long long>dq;\n int mx = *max_element(nums.begin(),nums.end());\n long long count = 0;\n for (int i=0; i<n;i++) {\n if (nums[i] == mx) dq.push_back(i); \n if (dq.size()>k) { \n dq.pop_front();\n }\n if (dq.size()==k) {\n int l = dq.front();\n count += (l+1);\n }\n }\n return count;\n }\n};", "memory": "124800" }
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 s=0,e=0,i=0, ans=1;\n int maxi=INT_MIN;\n queue<long long> q;\n for(int i=0;i<nums.size();i++){\n maxi=max(maxi,nums[i]);\n }\n\n while(e<nums.size()){\n if(nums[e] == maxi){\n q.push(e);\n // if(first){\n // i=e;\n // }\n }\n if(q.size()==k){\n int sub=q.front()-s+1;\n ans+=sub*(nums.size()-(e));\n s=q.front()+1;\n q.pop();\n }\n e++;\n }\n cout<<endl;\n return ans-1;\n }\n};", "memory": "124900" }
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 {\nprivate:\n vector<int> prefixSum;\n int n, k, maxElement;\n \n long long countSubarraysRecursive(int start, int end) {\n if (start > end) return 0;\n if (start == end) return (prefixSum[end + 1] - prefixSum[start] >= k) ? 1 : 0;\n \n int mid = start + (end - start) / 2;\n long long leftCount = countSubarraysRecursive(start, mid);\n long long rightCount = countSubarraysRecursive(mid + 1, end);\n \n long long crossCount = 0;\n int right = mid + 1;\n for (int left = start; left <= mid; left++) {\n while (right <= end && prefixSum[right + 1] - prefixSum[left] < k) {\n right++;\n }\n if (right <= end) {\n crossCount += end - right + 1;\n }\n }\n \n return leftCount + rightCount + crossCount;\n }\n\npublic:\n Solution(){\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n }\n long long countSubarrays(vector<int>& nums, int k) {\n n = nums.size();\n this->k = k;\n maxElement = *max_element(nums.begin(), nums.end());\n \n prefixSum.resize(n + 1, 0);\n for (int i = 0; i < n; i++) {\n prefixSum[i + 1] = prefixSum[i] + (nums[i] == maxElement);\n }\n \n return countSubarraysRecursive(0, n - 1);\n }\n};", "memory": "126200" }
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<int>temp=nums;int n=nums.size();\n int freq=0;\n sort(temp.begin(),temp.end());\n int large=temp[temp.size()-1];\n int l=0;long long max=0;int r=0;\n for(int i=0;i<nums.size();i++)\n {\n while(freq<k && r<=n-1)\n {\n if(nums[r]==large)\n freq++;\n r++;\n }\n if(freq>=k)\n max+=nums.size()-r+1;\n if(nums[i]==large)\n freq-=1;\n\n }\n return max;\n \n }\n};", "memory": "126800" }
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<int>temp=nums;int n=nums.size();\n int freq=0;\n sort(temp.begin(),temp.end());\n int large=temp[temp.size()-1];\n int l=0;long long max=0;int r=0;\n for(int i=0;i<nums.size();i++)\n {\n while(freq<k && r<=n-1)\n {\n if(nums[r]==large)\n freq++;\n r++;\n }\n if(freq>=k)\n max+=nums.size()-r+1;\n if(nums[i]==large)\n freq-=1;\n\n }\n return max;\n \n }\n};", "memory": "126900" }
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 long long answer = 0;\n \n queue<int> numbers;\n int N = nums.size();\n int maxElement = -1;\n \n for(auto num: nums){\n maxElement = max(maxElement, num);\n }\n \n int maxCount = 0;\n \n for(int i=0; i<N; i++){\n \n numbers.push(nums[i]);\n \n if(nums[i] == maxElement) maxCount += 1;\n \n long long fwdSubarrays = (long long)(N-i);\n \n if(maxCount == k){\n \n while(maxCount == k){\n \n answer += fwdSubarrays;\n \n if(numbers.front() == maxElement){\n maxCount -= 1;\n }\n \n numbers.pop();\n }\n }\n }\n \n return answer;\n \n }\n};", "memory": "127200" }
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 p=INT_MIN;\n for(int t:nums)\n {\n p=max(p,t);\n }\n vector<long long int>vec1;\n for(int i=0;i<nums.size();i++)\n {\n if(nums[i]==p)\n vec1.push_back(i);\n }\n long long int ans=0; \nfor(int i=0;i<vec1.size();i++)\n{\n long long int x;\n if(i+k-1<vec1.size())\n {\n x=nums.size()-vec1[i+k-1];\n long long int x1;\n if(i==0)\nx1=0;\nelse\nx1=vec1[i-1]+1;\nlong long int s=(vec1[i]-x1+1)*x;\nans=ans+s;\n }\n}\nreturn ans;\n }\n};", "memory": "127500" }
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 l = 0;\n // long long r = 0;\n // long long n =nums.size();\n // long long count = 0;\n // long long res = 0;\n // long long max_ele = *max_element(nums.begin(), nums.end());\n // long long flag = 1;\n // while(r<nums.size()){\n // if(nums[r] == max_ele) count++;\n\n // while(count>=k) {\n // res+= n-r;\n // if(nums[l] == max_ele) count--;\n // l++; \n // }\n // r++;\n \n // }\n\n // return res;\n\n std::vector<long long> indices;\n long long n = nums.size();\n long long res = 0;\n long long max_ele = *std::max_element(nums.begin(), nums.end());\n\n // Find all indices where the element is equal to the max element\n for (long long i = 0; i < n; i++) {\n if (nums[i] == max_ele) {\n indices.push_back(i);\n }\n }\n\n // If there are fewer max elements than k, return 0 as it's not possible to have a subarray of length k with max element\n if (indices.size() < k) {\n return 0;\n }\n\n // Calculate result based on indices of max elements\n for (long long i = 0; i <= indices.size() - k; i++) {\n // If we are considering a subarray of size k, ensure that we're within bounds\n long long end = (i + k < indices.size()) ? indices[i + k] - 1 : n - 1;\n\n // Adding the count of subarrays\n res += (indices[i] + 1) * (end - indices[i + k - 1] + 1);\n }\n\n return res;\n }\n};", "memory": "127600" }
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 using ll = long long;\n int mael = -1;\n for(auto e : nums) mael = max(mael, e);\n vector<ll> occ;\n ll t = 0;\n for(ll i = 0; i < nums.size(); i++) {\n if (nums[i] == mael) {\n occ.push_back(i);\n }\n if (occ.size() >= k) {\n ll b = occ[occ.size() - k];\n t += b + 1;\n // cout << i << ' ' << b + 1 << endl;\n }\n }\n return t;\n }\n};", "memory": "127700" }
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 int> maxi;\n long long int m = -1;\n for(auto it: nums){\n m=max(m, (long long)it);\n }\n for(int i=0;i<nums.size();i++){\n if(nums[i]==m){\n maxi.push_back(i+1);\n }\n }\n if(k> maxi.size()) return 0;\n long long int prev = 1;\n long long int ans=0;\n for(long long int i=0;i<maxi.size()-k +1;i++){\n ans +=(maxi[i]-prev+1)*(nums.size()-maxi[i+k-1]+1);\n ans = ans;\n prev = maxi[i]+1; \n }\n return ans;\n }\n};", "memory": "127800" }