id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int mod = 1e9+7;\n long long dp[100006];\n long long solve(int len, int zero, int one, int low , int high){\n if(len > high) return 0;\n if(dp[len] != -1) return dp[len];\n\n if(len >= low){\n return dp[len] = (1 + solve(len+zero,zero,one,low,high) + solve(len+one,zero,one,low,high))%mod;\n }\n\n return dp[len] =( solve(len+zero,zero,one,low,high) + solve(len+one,zero,one,low,high))%mod;\n }\n int countGoodStrings(int low, int high, int zero, int one) {\n memset(dp,-1,sizeof(dp));\n long long ans = solve(0,zero,one,low,high);\n return ans%mod;\n }\n};",
"memory": "15909"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n\n int mod = 1e9+7;\n int dp[200001];\n \n int solve(int len,int z,int o,int l,int h){\n if(len>h) return 0;\n if(dp[len]!=-1) return dp[len];\n int temp = (solve(len+z,z,o,l,h)%mod + solve(len+o,z,o,l,h)%mod)%mod;\n if(len<l) return dp[len] = temp;\n return dp[len] = 1 + temp;\n }\n\n int countGoodStrings(int low, int high, int zero, int one) {\n memset(dp,-1,sizeof(dp));\n return solve(0,zero,one,low,high);\n }\n};",
"memory": "16140"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> dp;\n int mod = 1e9+7;\n int countGoodStrings(int low, int high, int zero, int one) {\n dp.resize(high+1,-1);\n int res = 0;\n for(int i = low;i<=high;i++){\n res = (res + dfs(i,zero,one))%mod;\n }\n return res;\n }\n\n int dfs(int cur, int zero, int one){\n if(cur == 0) return 1;\n if(cur < 0) return 0;\n if(dp[cur] != -1) return dp[cur];\n int res = 0;\n res = (res + dfs(cur-zero,zero,one))%mod;\n res = (res + dfs(cur-one,zero,one))%mod;\n return dp[cur] =res;\n }\n};",
"memory": "16140"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int mod = 1e9+7;\n int helper(int idx, int zero, int one, vector<int> &dp)\n {\n\t\t//base case\n if(idx == 0)\n {\n return 1;\n }\n if(idx < 0)\n {\n return 0;\n }\n if(dp[idx] != -1)\n {\n return dp[idx];\n }\n return dp[idx] =(helper(idx - zero, zero, one, dp) % mod + helper(idx - one, zero, one, dp) % mod) % mod;\n }\n \n int countGoodStrings(int low, int high, int zero, int one) {\n vector<int> dp(high + 1, -1);\n int ans = 0;\n for(int i = low; i <= high; i++)\n {\n ans = (ans + helper(i, zero, one, dp) % mod) % mod;\n }\n return ans;\n }\n};",
"memory": "16371"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> dp;\n int mod = 1e9+7;\n int countGoodStrings(int low, int high, int zero, int one) {\n int res = 0;\n dp.resize(high+1,-1);\n for(int i = low;i<=high;i++){\n res = (res + dfs(i,zero,one))%mod;\n }\n return res;\n }\n\n int dfs(int cur, int zero, int one){\n if(cur < 0) return 0;\n if(cur == 0) return 1;\n if(dp[cur] != -1) return dp[cur];\n int res = 0;\n res = (res + dfs(cur-zero,zero,one))%mod;\n res = (res + dfs(cur-one,zero,one))%mod;\n return dp[cur] = res;\n } \n};",
"memory": "16603"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int l,h,ze,on;\n const int mod=1e9+7;\n int solve(int n,vector<int>&dp){\n if(n>h) return 0;\n if(dp[n]!=-1) return dp[n];\n int count0=0,count1=0;\n if(n>=l) count0=1+solve(n+ze,dp);\n else count0=solve(n+ze,dp);\n count1=solve(n+on,dp); \n return dp[n]=(count1+count0)%mod;\n }\n int countGoodStrings(int low, int high, int zero, int one) {\n l=low;\n h=high;\n ze=zero;\n on=one;\n int n=high+1;\n vector<int> dp(n,-1);\n return solve(0,dp);\n }\n};",
"memory": "16834"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 1 | {
"code": "class Solution {\nprivate:\n const int mod = 1e9 + 7;\npublic:\n int solve(int n, int zero, int one, vector<int>& dp){\n if(n == 0){\n return 1;\n }\n if(dp[n] != -1){\n return dp[n];\n }\n int ans = 0;\n if(n - zero >= 0){\n ans = (ans + solve(n - zero, zero, one, dp)) % mod;\n }\n if(n - one >= 0){\n ans = (ans + solve(n - one, zero, one, dp)) % mod;\n }\n return dp[n] = ans;\n }\n int countGoodStrings(int low, int high, int zero, int one) {\n vector<int> dp(high + 1, -1); \n solve(high, zero, one, dp);\n int sum = 0;\n for(int i = low; i <= high; i++){\n sum = (long long)(sum + solve(i, zero, one, dp)) % mod;\n\n }\n return sum;\n }\n};",
"memory": "16834"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int mod=1e9+7;\n int t[100002];\n int solve(int low,int high,int zero, int one,int curr_length){\n \n if(curr_length>high)\n return 0;\n if(t[curr_length]!=-1)\n return t[curr_length];\n int total=0;\n if(curr_length>=low)\n total=1;\n \n int option1=solve(low,high,zero,one,curr_length+zero);\n int option2=solve(low,high,zero,one,curr_length+one);\n return t[curr_length]=(total+option1+option2)%mod;\n }\n int countGoodStrings(int low, int high, int zero, int one) {\n memset(t,-1,sizeof(t));\n return solve(low,high,zero,one,0);\n }\n};",
"memory": "17065"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n typedef long long ll;\n int M = 1e9+7;\n int t[100001];\n int solve(int len, int low, int high, int zero, int one){\n if(len > high) return 0; //don't check for further strings\n if(t[len]!=-1) return t[len];\n ll possibility = 0;\n if(len >= low && len <= high){\n possibility = 1; //curr string is good\n }\n // Appending the character zero\n possibility += solve(len + zero, low, high, zero, one);\n possibility += solve(len + one, low, high, zero, one);\n return t[len] = possibility%M;\n }\n int countGoodStrings(int low, int high, int zero, int one) {\n memset(t, -1, sizeof(t));\n return solve(0, low, high, zero, one);\n }\n};",
"memory": "17065"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int dp[100001];\n int MOD = 1e9 +7;\n int helper(int len, int low, int high, int zero, int one) {\n if (len > high)\n return 0;\n if (dp[len] != -1)\n return dp[len];\n\n bool isCorrect = false;\n if (len >= low && len <= high) {\n isCorrect = true;\n }\n int addOne = helper(len + one, low, high, zero, one);\n int addZero = helper(len + zero, low, high, zero, one);\n if (isCorrect)\n return dp[len] = (1 + addOne + addZero)%MOD;\n return dp[len] = (addOne + addZero)%MOD;\n }\n\n int countGoodStrings(int low, int high, int zero, int one) {\n memset(dp, -1, sizeof(dp));\n return helper(0, low, high, zero, one);\n }\n};",
"memory": "17296"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 1 | {
"code": "long long dp[100005];\nconst int MOD = 1e9 + 7;\nclass Solution {\npublic:\n long long helper(int low, int high, int zero, int one, int len){\n if(len > high)return 0;\n if(dp[len] != -1)return dp[len];\n long long ans = 0;\n if(len >= low)ans++;\n ans += helper(low, high, zero, one, len + one);\n ans += helper(low, high, zero, one, len + zero);\n return dp[len] = ans%MOD;\n }\n int countGoodStrings(int low, int high, int zero, int one) {\n memset(dp, -1, sizeof dp);\n return helper(low, high, zero, one, 0);\n }\n};",
"memory": "17296"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 2 | {
"code": "class Solution {\n int dp[200001];\n int m=1e9+7;\n int f(int len,int low, int high, int zero, int one)\n {\n if(len>high)\n return dp[len]=0;\n if(dp[len]!=-1)\n return dp[len];\n int add=0;\n if(len>=low)\n {\n add=1;\n }\n\n int az=f(len+zero,low,high,zero,one);\n int ao=f(len+one,low,high,zero,one);\n\n return dp[len]= (add+az+ao)%m;\n }\npublic:\n int countGoodStrings(int low, int high, int zero, int one) {\n memset(dp,-1,sizeof(dp));\n\n return f(0,low,high,zero,one)%m;\n }\n};",
"memory": "17528"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int L,H,Z,O;\n int mod=1e9+7;\n int t[1000001];\n int swap(int l)\n {\n if(l>H)\n {\n return 0;\n }\n bool onee=false;\n if(l>=L && l<=H)\n {\n onee=true;\n }\n if(t[l]!=-1)\n {\n return t[l];\n }\n int one=swap(l+O);\n int zero=swap(l+Z);\n return t[l]=(one+zero+onee)%mod;\n }\n int countGoodStrings(int low, int high, int zero, int one) \n {\n L=low;\n H=high;\n Z=zero;\n O=one;\n memset(t,-1,sizeof(t));\n return swap(0);\n }\n};",
"memory": "17759"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int l,h,ze,on;\n const int mod=1e9+7;\n int solve(int n,vector<int>&dp){\n if(n>h) return 0;\n if(dp[n]!=-1) return dp[n];\n int countValid=(n>=l && n<=h)?1:0;\n int count0=solve(n+ze,dp);\n int count1=solve(n+on,dp); \n return dp[n]=countValid+(count1+count0)%mod;\n }\n int countGoodStrings(int low, int high, int zero, int one) {\n l=low;\n h=high;\n ze=zero;\n on=one;\n int n=high+1;\n vector<int> dp(n,-1);\n return solve(0,dp);\n }\n};",
"memory": "17990"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n const int MOD = 1e9+7;\n int Z,O,L,H;\n int solve(int l,vector<int> &t)\n {\n if(l>H)\n {\n return 0;\n }\n if(t[l]!=-1) return t[l];\n bool addOne=false;\n if(l>=L && l<=H)\n {\n addOne = true;\n }\n int appendZero = solve(l+Z,t);\n int appendOne = solve(l+O,t);\n\n return t[l] = (addOne + appendZero + appendOne) % MOD;\n\n }\n int countGoodStrings(int low, int high, int zero, int one) {\n L= low;\n H= high;\n Z=zero;\n O=one;\n\n vector<int> t(high+1,-1);\n return solve(0,t);\n }\n};",
"memory": "18221"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\nint L,H,Z,O;\nint solve(int l,vector<int>&dp){\n if(l>H) return 0;\n if(dp[l]!=-1) return dp[l];\n int count=0;\n if(l>=L){\n count=1;\n }\n int a0=solve(l+Z,dp);\n int a1=solve(l+O,dp);\n return dp[l]= (a0+a1+count)%1000000007;\n\n}\n\n int countGoodStrings(int low, int high, int zero, int one) {\n L=low;\n H=high;\n Z=zero;\n O=one;\n vector<int>dp(high+1,-1);\n return solve(0,dp);\n }\n};\n",
"memory": "18221"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int dp[100001],MOD=1e9+7;\n int f(int l,int low,int high,int zero,int one){\n if(l>high)\n return 0;\n if(dp[l]!=-1)\n return dp[l];\n int ans=0;\n if(l>=low && l<=high)\n ++ans;\n\n int op1=f(l+zero,low,high,zero,one)%MOD;\n int op2=f(l+one,low,high,zero,one)%MOD;\n\n return dp[l]=(ans+op1+op2)%MOD;\n }\n int countGoodStrings(int low, int high, int zero, int one) {\n memset(dp,-1,sizeof(dp));\n return f(0,low,high,zero,one);\n }\n};",
"memory": "18453"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\nint L,H,Z,O;\nint solve(int l,vector<int>&dp){\n if(l>H) return 0;\n if(dp[l]!=-1) return dp[l];\n int count=0;\n if(l>=L){\n count=1;\n }\n int a0=solve(l+Z,dp);\n int a1=solve(l+O,dp);\n return dp[l]= (a0+a1+count)%1000000007;\n\n}\n\n int countGoodStrings(int low, int high, int zero, int one) {\n L=low;\n H=high;\n Z=zero;\n O=one;\n vector<int>dp(high+1,-1);\n return solve(0,dp);\n }\n};\n",
"memory": "18453"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 2 | {
"code": "const int MOD = 1e9 + 7;\n\nclass Solution {\npublic:\n\n int hi,lo, z,o;\n \n\n int helper(int ind, vector<int>& dp){\n if(ind > hi){\n return 0;\n }\n if(dp[ind] != -1) return dp[ind];\n\n int addZero = helper(ind + z,dp)%MOD;\n int addOne = helper(ind + o, dp)%MOD;\n int ans = 0;\n if(ind>= lo and ind<=hi){\n ans++;\n }\n ans += addZero + addOne;\n ans %= MOD;\n return dp[ind] = ans;\n }\n\n int countGoodStrings(int low, int high, int zero, int one) {\n lo = low, hi = high, z = zero, o = one;\n vector<int> dp(hi+7,-1);\n return helper(0,dp);\n }\n};",
"memory": "18684"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int l, h;\n int mod = 1e9 + 7;\n\n int solverec(int len, int zero, int one, int dp[]) {\n if (len > h)\n return 0;\n\n if (dp[len] != -1)\n return dp[len];\n\n int ways = 0;\n\n ways = (ways + solverec(len + zero, zero, one, dp)) % mod;\n\n ways = (ways + solverec(len + one, zero, one, dp)) % mod;\n\n return dp[len] = (len <= h && len >= l) ? ways + 1 : ways;\n }\n\n int countGoodStrings(int low, int high, int zero, int one) {\n int len = 0;\n l = low;\n h = high;\n int dp[100001];\n memset(dp, -1, sizeof(dp));\n return solverec(len, zero, one, dp);\n }\n};",
"memory": "18684"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n\nint m=1e9+7;\n\nlong long int sumi(int n, int zero, int one, vector<long long int> &dp){\n if(n==0) return 1;\n if(n<0) return 0;\n if(dp[n]!=-1) return dp[n]%m;\n long long int a=0,b=0;\n if(n>=zero) a=sumi(n-zero,zero,one,dp)%m;\n if(n>=one) b=sumi(n-one,zero,one,dp)%m;\n return dp[n]=(a+b)%m;\n}\n\n int countGoodStrings(int low, int high, int zero, int one) {\n long long int ans=0;\n vector<long long int> dp(high+1,-1);\n for(int i=low;i<=high;i++){\n ans+=sumi(i,zero,one,dp)%m;\n }\n return ans%m;\n }\n};",
"memory": "18915"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long dp[100100];\n long long mod = 1000000007;\n int rec(int low, int high, int zero, int one, long long length){\n if(length > high){\n return 0;\n }\n\n if(dp[length] != -1){\n return (dp[length] + mod) % mod;\n }\n\n long long ans = 0;\n if(length >= low && length <= high){\n ans = 1;\n }\n // 0 \n ans = ((((ans + mod) % mod)+ (rec(low, high, zero, one, length + zero) + mod) % mod) + mod)% mod;\n // 1\n ans = ((((ans + mod) % mod)+ (rec(low, high, zero, one, length + one) + mod) % mod) + mod)% mod;\n\n return dp[length] = ((ans + mod) % mod);\n\n }\n\n int countGoodStrings(int low, int high, int zero, int one) {\n memset(dp, -1, sizeof(dp));\n return rec(low, high, zero, one, 0);\n }\n};",
"memory": "18915"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int mod=1e9+7;\n int dp[1000001];\n int solve(int low, int high, int zero, int one,int cnt){\n if(cnt>high)return 0;\n if(dp[cnt]!=-1)\n return dp[cnt];\n dp[cnt]=(cnt>=low)?1:0;\n dp[cnt]+=solve(low,high,zero,one,cnt+zero);\n dp[cnt]%=mod;\n dp[cnt]+=solve(low,high,zero,one,cnt+one);\n dp[cnt]%=mod;\n return dp[cnt];\n }\n int countGoodStrings(int low, int high, int zero, int one) {\n int cnt=0;\n memset(dp,-1,sizeof(dp));\n return solve(low,high,zero,one,cnt);\n }\n};",
"memory": "19146"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long fun(int low, int high, int zero, int one,int i,vector<long long>&dp){\n long long mod=1e9+7;\n if(i==0){\n return 1;\n }\n if(i<0){\n return 0;\n }\n if(dp[i]!=-1){\n return dp[i]%mod;\n }\n \n dp[i]=(fun(low,high,zero,one,i-zero,dp)+fun(low,high,zero,one,i-one,dp))%mod;\n return dp[i]%mod;\n }\n int countGoodStrings(int low, int high, int zero, int one) {\n\n int ans=0;\n long long mod=1e9+7;\n vector<long long> dp(high+1,-1);\n for(int i=low;i<=high;i++){\n ans=(ans+fun(low,high,zero,one,i,dp))%mod;\n }\n return ans;\n \n }\n};",
"memory": "19378"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int l, h, z, o, mod = 1e9+7;\n vector<int> dp;\n int f(int i){\n if(i > h) return 0;\n if(dp[i]!=-1) return dp[i];\n int a = i>=l and i <= h ? 1 : 0;\n return dp[i] = (f(i+z) + f(i+o) + a) % mod;\n }\n int countGoodStrings(int low, int high, int zero, int one) {\n dp.assign(high + max(zero, one)+1, -1);\n this -> l = low;\n this -> h = high;\n this -> z = zero;\n this -> o = one;\n return f(0);\n }\n};",
"memory": "19609"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long int solve(int len,int zero,int one,vector<long long int>&dp,const int &MOD){\n if(len == 0)\n return 1;\n else if(len < 0)\n return 0;\n else if(dp[len] >= 0)\n return dp[len];\n\n long long int l = solve(len-zero,zero,one,dp,MOD);\n long long int r = solve(len-one,zero,one,dp,MOD);\n return dp[len] = (l%MOD+r%MOD)%MOD;\n }\n int countGoodStrings(int low, int high, int zero, int one) {\n vector<long long int>dp(high+1,-1);\n const int MOD = ((int)1e9 + 7);\n int ans = 0;\n for(int i=low;i<=high;i++){\n ans = (ans % MOD + solve(i,zero,one,dp,MOD) % MOD) % MOD;\n }\n return ans;\n }\n};",
"memory": "19609"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 2 | {
"code": "#define mode 1000000007\n\nclass Solution {\npublic:\n\n int helper(int low,int high,int zero,int one,int size,vector<int> &dp)\n {\n if(size > high) return 0;\n\n if(dp[size] != -1) return dp[size];\n\n int zerot = helper(low,high,zero,one,size+zero,dp);\n int onet = helper(low,high,zero,one,size+one,dp);\n\n if(size >= low) return dp[size] = (1ll + zerot + onet)%mode;\n return dp[size] = (0ll + zerot+onet)%mode;\n }\n\n int countGoodStrings(int low, int high, int zero, int one) {\n\n vector<int> dp(high+1,-1);\n return helper(low,high,zero,one,0,dp);\n }\n};",
"memory": "19840"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n const int MOD=1e9+7;\n int solve(int len,int low,int high,int zero,int one,vector<int>& dp){\n if(len>high){\n return 0;\n }\n if(dp[len]!=-1){\n return dp[len];\n }\n int cnt = 0;\n if(len >= low){\n cnt=1;\n }\n cnt = (cnt + solve(len+zero,low,high,zero,one,dp))%MOD;\n cnt = (cnt + solve(len+one,low,high,zero,one,dp))%MOD;\n return dp[len] = cnt;\n }\n int countGoodStrings(int low, int high, int zero, int one) {\n int len=0;\n vector<int>dp(high+1,-1);\n return solve(len,low,high,zero,one,dp);\n }\n};",
"memory": "19840"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n const int MOD = 1e9 + 7;\n int f(int low, int high, int zeros, int ones, int currLen, vector<int> &dp){\n\n if(currLen > high) return 0;\n\n if (dp[currLen] != -1) return dp[currLen];\n\n int ans = 0;\n if(currLen >= low && currLen <= high){\n ans = 1;\n }\n\n\n\n\n ans = (ans + f(low, high, zeros, ones, currLen+zeros, dp)) % MOD;\n \n ans = (ans + f(low, high, zeros, ones,currLen + ones, dp)) % MOD;\n\n return dp[currLen] = ans;\n }\n int countGoodStrings(int low, int high, int zero, int one) {\n vector<int> dp(high + 1, -1);\n return f(low, high, zero, one, 0, dp);\n }\n};",
"memory": "20071"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int mod=1e9+7;\n int recurssion(int low,int high,int zero,int one,int len,vector<int>&dp){\n if(len>high) return 0;\n bool flag=false;\n if(len>=low){\n flag=true;\n }\n if(dp[len]!=-1) return dp[len];\n int take=recurssion(low,high,zero,one,len+one,dp);\n int nottake=recurssion(low,high,zero,one,len+zero,dp);\n return dp[len]=(take+nottake+flag)%mod;\n }\n int countGoodStrings(int low, int high, int zero, int one) {\n vector<int>dp(high+1,-1);\n return recurssion(low,high,zero,one,0,dp);\n \n }\n};",
"memory": "20071"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int mod=1e9+7;\n int help(int curlen,int low,int high,int zero,int one,vector<int> &dp){\n if(curlen>high) return 0;\n if(dp[curlen]!=-1) return dp[curlen];\n int take=0;\n if(curlen>=low && curlen<=high) {\n take=1;\n }\n if(curlen+one<=high){\n take+=(help(curlen+one,low,high,zero,one,dp))%mod;\n }\n if(curlen+zero<=high){\n take+=(help(curlen+zero,low,high,zero,one,dp))%mod;\n }\n return dp[curlen]=take%mod;\n }\n int countGoodStrings(int low, int high, int zero, int one) {\n vector<int> dp(high+1,-1);\n return help(0,low,high,zero,one,dp);\n //return ans;\n }\n};",
"memory": "20303"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\nint mod = 1000000007;\nint bfs(int low, int high, int zero, int one){\n int ans = 0;\n queue<int> q;\n q.push(0);\n while(!q.empty()){\n int length = q.front();\n q.pop();\n if(length > high){\n continue;\n }\n if(length >= low and length <= high){\n ans = (ans+1)%mod;\n }\n q.push(length+zero);\n q.push(length+one);\n }\n return ans;\n}\n\nint dfs(int length, int low, int high, int zero, int one, vector<int>& dp){\n if(length > high){\n return 0;\n }\n if(dp[length] != -1){\n return dp[length];\n }\n int include = (length >= low and length <= high) ? 1 : 0;\n return dp[length] = (include + dfs(length + zero, low, high, zero, one, dp) + dfs(length + one, low, high, zero, one, dp)) % mod;\n}\n int countGoodStrings(int low, int high, int zero, int one) { \n vector<int> dp(high+1,-1); \n return dfs(0,low,high,zero,one,dp);\n }\n};",
"memory": "20303"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 2 | {
"code": "class Solution {\n int count = 0;\n int mod = 1e9 + 7;\n int dp[1000000];\n int f(int low, int high, int zero, int one, int length) {\n if (length > high)\n return 0;\n if (dp[length] != -1) {\n return dp[length];\n }\n long long result = 0;\n if ((length + zero) <= high) {\n result += (length + zero >= low ? 1 : 0) +\n f(low, high, zero, one, length + zero);\n }\n if ((length + one) <= high) {\n result += (length + one >= low ? 1 : 0) +\n f(low, high, zero, one, length + one);\n }\n return dp[length] = result % mod;\n }\n\npublic:\n int countGoodStrings(int low, int high, int zero, int one) {\n memset(dp, -1, sizeof(dp));\n return f(low, high, zero, one, 0);\n }\n};",
"memory": "20534"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int good_helper(int low, int high, int zero, int one, int curr_len, std::vector<int>& cache)\n {\n // std::cout << \"Helper with curr: \" << curr_len << std::endl;\n if (curr_len > high) return 0;\n if (cache[curr_len] != -1) return cache[curr_len];\n\n int result;\n if (curr_len >= low)\n {\n // std::cout << \"It is a good number\\n\";\n result = 1;\n }\n else\n {\n result = 0;\n }\n\n int add_zero = good_helper(low, high, zero, one, curr_len + zero, cache);\n int add_one = good_helper(low, high, zero, one, curr_len + one, cache);\n\n int mod = 1e9 + 7;\n result += (add_zero + add_one) % mod;\n\n cache[curr_len] = result;\n return result;\n }\n\n int countGoodStrings(int low, int high, int zero, int one)\n {\n std::vector<int> cache(high + std::max(zero, one), -1);\n return good_helper(low, high, zero, one, 0, cache);\n }\n};",
"memory": "20765"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 3 | {
"code": "class Solution {\n int count = 0;\n int mod = 1e9 + 7;\n int dp[1000000];\n int f(int low, int high, int zero, int one, int length) {\n if (length > high)\n return 0;\n if (dp[length] != -1) {\n return dp[length];\n }\n long long result = 0;\n if ((length + zero) <= high) {\n result += (length + zero >= low ? 1 : 0) +\n f(low, high, zero, one, length + zero);\n }\n if ((length + one) <= high) {\n result += (length + one >= low ? 1 : 0) +\n f(low, high, zero, one, length + one);\n }\n return dp[length] = result % mod;\n }\n\npublic:\n int countGoodStrings(int low, int high, int zero, int one) {\n memset(dp, -1, sizeof(dp));\n return f(low, high, zero, one, 0);\n }\n};",
"memory": "20996"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int mod=1e9+7;\n int rec(int low,int high,int zero,int one,int len,vector<int>& dp){\n //base case\n // if(len==high) return 1;\n if(len > high) return 0;\n if(dp[len] != -1) return dp[len];\n\n int fir;\n int two;\n \n fir=rec(low,high,zero,one,len+zero,dp);\n\n \n two=rec(low,high,zero,one,len+one,dp);\n \n if(len >= low && len<=high) return dp[len]=(fir+two+1)%mod;\n return dp[len]=(fir+two)%mod;\n }\n\n\n int countGoodStrings(int low, int high, int zero, int one) {\n vector<int> dp(high+1,-1);\n return rec(low,high,zero,one,0,dp);\n }\n};",
"memory": "21228"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n//recursion + memoization\nint mod=1e9+7;\nint f(int len,int low, int high, int zero, int one,vector<int> &dp)\n{\n if(len>high) return 0;\n if(dp[len]!=-1) return dp[len];\n\n int way1= f(len+zero,low,high,zero,one,dp);\n int way2= f(len+one,low,high,zero,one,dp);\n\n //when current lenght is in the range, add 1 to it for current string\n if(low<=len && len<=high) return dp[len]= (1 + (way1 + way2)%mod)%mod;\n return dp[len]= (way1 + way2)%mod;\n}\n int countGoodStrings(int low, int high, int zero, int one) {\n vector<int> dp(high+1,-1);\n return f(0,low,high,zero,one,dp);\n }\n};",
"memory": "21459"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int generate(const int &low, const int &high, const int &zero, const int &one, int zeroCnt, int oneCnt)\n {\n if ((zeroCnt + oneCnt) > high) return 0;\n\n if ((zeroCnt + oneCnt + zero) <= high && hash[zeroCnt + oneCnt + zero] == INT_MIN)\n {\n hash[zeroCnt + oneCnt + zero] = (generate(low, high, zero, one, zeroCnt + zero, oneCnt) % 1000000007);\n }\n if ((zeroCnt + oneCnt + one) <= high && hash[zeroCnt + oneCnt + one] == INT_MIN)\n {\n hash[zeroCnt + oneCnt + one] = (generate(low, high, zero, one, zeroCnt, oneCnt + one) % 1000000007);\n }\n int x = 0;\n if ((zeroCnt + oneCnt + zero) <= high)\n {\n x += hash[zeroCnt + oneCnt + zero];\n }\n if ((zeroCnt + oneCnt + one) <= high)\n {\n x += hash[zeroCnt + oneCnt + one];\n }\n if ((zeroCnt + oneCnt) >= low && (zeroCnt + oneCnt) <= high)\n {\n x += 1;\n return x;\n }\n return x;\n }\n int countGoodStrings(int low, int high, int zero, int one) {\n hash.resize(high + 1, INT_MIN);\n return generate(low, high, zero, one, 0, 0) % 1000000007;\n }\nprivate:\n vector<int> hash;\n};",
"memory": "21690"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int generate(const int &low, const int &high, const int &zero, const int &one, int zeroCnt, int oneCnt)\n {\n if ((zeroCnt + oneCnt) > high) return 0;\n if ((zeroCnt + oneCnt + zero) <= high && hash[zeroCnt + oneCnt + zero] == INT_MIN)\n {\n hash[zeroCnt + oneCnt + zero] = generate(low, high, zero, one, zeroCnt + zero, oneCnt) % 1000000007;\n }\n if ((zeroCnt + oneCnt + one) <= high && hash[zeroCnt + oneCnt + one] == INT_MIN)\n {\n hash[zeroCnt + oneCnt + one] = generate(low, high, zero, one, zeroCnt, oneCnt + one) % 1000000007;\n }\n int x = 0;\n if ((zeroCnt + oneCnt + zero) <= high)\n {\n x += hash[zeroCnt + oneCnt + zero];\n }\n if ((zeroCnt + oneCnt + one) <= high)\n {\n x += hash[zeroCnt + oneCnt + one];\n }\n if ((zeroCnt + oneCnt) >= low && (zeroCnt + oneCnt) <= high)\n {\n x += 1;\n return x;\n }\n return x;\n }\n int countGoodStrings(int low, int high, int zero, int one) {\n hash.resize(high + 1, INT_MIN);\n return generate(low, high, zero, one, 0, 0) % 1000000007;\n }\nprivate:\n vector<int> hash;\n};",
"memory": "21690"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 3 | {
"code": "class Solution {\nprivate:\n long long helper(int length,int low,int high, int zero,int one,vector<long long>& dp) {\n long long mod = 1e9 + 7;\n if(length > high)// check for overflow\n return 0;\n if(dp[length] != -1) // check for already solved subproblems\n return dp[length];\n dp[length] = length >= low ? 1 : 0; // checking length is greater than low\n dp[length] += helper(length + zero, low, high, zero, one, dp) + helper(length + one, low, high, zero, one, dp);\n return dp[length] % mod;\n }\npublic:\n int countGoodStrings(int low, int high, int zero, int one) {\n vector<long long> dp(high + 1,-1);\n return helper(0, low, high, zero, one, dp); \n }\n};",
"memory": "22846"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 3 | {
"code": "class Solution {\nprivate:\n long long helper(int length,int low,int high, int zero,int one,vector<long long>& dp) {\n long long mod = 1e9 + 7;\n if(length > high)// check for overflow\n return 0;\n if(dp[length] != -1) // check for already solved subproblems\n return dp[length];\n dp[length] = length >= low ? 1 : 0; // checking length is greater than low\n dp[length] += helper(length + zero, low, high, zero, one, dp) + helper(length + one, low, high, zero, one, dp);\n return dp[length] % mod;\n }\npublic:\n int countGoodStrings(int low, int high, int zero, int one) {\n vector<long long> dp(high + 1,-1);\n return helper(0, low, high, zero, one, dp); \n }\n};",
"memory": "22846"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 3 | {
"code": "class Solution {\n\npublic:\n long long helper(int length,int high,vector<long long>& dp,int low,int zero,int one) {\n long long mod = 1e9 + 7;\n if(length > high) \n return 0;\n if(dp[length] != -1) \n return dp[length];\n dp[length] = length >= low ? 1 : 0; \n dp[length] += helper(length + zero,high,dp,low,zero,one) + helper(length + one,high,dp,low,zero,one);\n return dp[length] % mod; \n }\n\n int countGoodStrings(int low, int high, int zero, int one) {\n vector<long long> dp(high + 1,-1);\n return helper(0,high,dp,low,zero,one);\n }\n};",
"memory": "23078"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long helper(int length,int high,vector<long long>& dp,int low,int zero,int one) {\n long long mod = 1e9 + 7; // mod\n\n if(length > high) // base case if the current index overflows\n return 0;\n\n if(dp[length] != -1) // check if the subproblem is already computed no need to perform recursion and return the count\n return dp[length];\n \n dp[length] = length >= low ? 1 : 0; // checking the length is greater than the lower bound \n\n dp[length] += helper(length + zero,high,dp,low,zero,one) + helper(length + one,high,dp,low,zero,one);\n /* now calculating the total count of that index : length which equals to the sum for the subproblem length + zero and length + one\n */\n\n return dp[length] % mod; // now return the count with mod\n\n\n }\n int countGoodStrings(int low, int high, int zero, int one) {\n vector<long long> dp(high + 1,-1);\n\n return helper(0,high,dp,low,zero,one);\n }\n};",
"memory": "23309"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 3 | {
"code": "class Solution {\n int MOD = 1e9 + 7;\npublic:\n int countGoodStrings(int low, int high, int zero, int one) {\n // string temp = \"\";\n int ans = 0;\n vector<int> dp(high+1,-1);\n ans = (ans + help(dp,0,low,high,zero,one)) % MOD;\n \n // return help(temp,high,zero,one) - help(temp,low-1,zero,one);\n return ans;\n }\n\n int help(vector<int> &dp,int length,int &min, int &max, int &zero, int &one){\n if(length > max) return 0;\n\n if(dp[length] != -1) return dp[length];\n\n int res = 0;\n if(length >= min) res = 1;\n \n int take0 = help(dp,length+zero,min,max,zero,one);\n int take1 = help(dp,length+one,min,max,zero,one);\n res += (take0 + take1) % MOD;\n\n return dp[length] = res;\n }\n};",
"memory": "23309"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int mod = 1e9+7;\n int f(int currLen, int low, int high, int zeros, int ones,vector<int>& dp){\n if(currLen > high) return 0; \n if(dp[currLen] != -1) return dp[currLen];\n\n int left = f(currLen+zeros,low,high,zeros,ones,dp);\n int right = f(currLen+ones,low,high,zeros,ones,dp);\n\n if(currLen>=low && currLen<=high){\n return dp[currLen] = (1 + left + right)%mod;\n }else{\n return dp[currLen] = (left + right)%mod;\n }\n }\n int countGoodStrings(int low, int high, int zero, int one) {\n vector<int> dp(high+low,-1);\n return f(0,low,high,zero,one,dp);\n }\n};",
"memory": "23540"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n const int MOD=1e9+7;\n int l = 0, r = 0, ans = 0;\n int f(int len, int a, int b,vector <long long>&dp) {\n if (len > r)\n return 0;\n if (dp[len]!=-1)\n return dp[len];\n long long c1 = 0, c2 = 0;\n c1 = (len + a >= l and len + a <= r) + f(len + a, a, b,dp);\n c2 = (len + b >= l and len + b <= r) + f(len + b, a, b,dp);\n return dp[len]=(c1+c2)%MOD;\n }\n int countGoodStrings(int low, int high, int zero, int one) {\n l=low,r=high;\n vector <long long > dp(high+1,-1);\n return f(0,zero,one,dp);\n }\n};",
"memory": "23771"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 3 | {
"code": "class Solution {\n #define ll long long\npublic:\n ll mod=1e9+7;\n ll c(ll len,ll low,ll high,vector<ll>&dp,int zero,int one){\n if(len>high)return 0;\n if(dp[len]!=-1)return dp[len];\n ll ans=0;\n if(len>=low && len<=high)ans=1;\n ans +=c(len+zero,low,high,dp,zero,one)%mod;\n ans = (ans%mod + c(len+one,low,high,dp,zero,one)%mod)%mod;\n dp[len]=ans;\n return ans;\n }\n int countGoodStrings(int low, int high, int zero, int one) {\n vector<ll>dp(high+1,-1);\n return c(0,low,high,dp,zero,one); \n }\n};",
"memory": "24003"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 3 | {
"code": "class Solution {\n #define ll long long\npublic:\n ll mod=1e9+7;\n ll c(ll len,ll low,ll high,vector<ll>&dp,int zero,int one){\n if(len>high)return 0;\n if(dp[len]!=-1)return dp[len];\n ll ans=0;\n if(len>=low && len<=high)ans=1;\n ans +=c(len+zero,low,high,dp,zero,one)%mod;\n ans = (ans%mod + c(len+one,low,high,dp,zero,one)%mod)%mod;\n dp[len]=ans;\n return ans;\n }\n int countGoodStrings(int low, int high, int zero, int one) {\n vector<ll>dp(high+1,-1);\n return c(0,low,high,dp,zero,one); \n }\n};",
"memory": "24234"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long c = 0;\n long long MOD = 1e9 + 7;\n vector<long long> dp;\n\n int countGoodStrings(int low, int high, int zero, int one) {\n dp.resize(high + 1, -1);\n return backtrack(low, high, zero, one, 0);\n }\n\n long long backtrack(int low, int high, int zero, int one, long long sum) {\n if (sum > high) return 0;\n if (dp[sum] != -1) return dp[sum];\n long long ways = 0;\n if (sum >= low && sum <= high) ways++;\n\n ways += backtrack(low, high, zero, one, sum + zero)%MOD;\n ways += backtrack(low, high, zero, one, sum + one)%MOD;\n \n return dp[sum] = ways % MOD;\n }\n};\n",
"memory": "24465"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int mod=1e9+7;\n int func(vector<int> &dp,int count,int low,int high, int zeroes,int ones)\n {\n if(count>high) return 0;\n\n if(dp[count]!=-1) return dp[count];\n\n if(count<=high && count>=low)\n {\n return dp[count]=(1+func(dp,count+zeroes,low,high,zeroes,ones)%mod+func(dp,count+ones,low,high,zeroes,ones)%mod)%mod;\n }\n\n int append_zero=func(dp,count+zeroes,low,high,zeroes,ones)%mod;\n int append_one=func(dp,count+ones,low,high,zeroes,ones)%mod;\n\n return dp[count]=append_zero+append_one;\n }\n int countGoodStrings(int low, int high, int zero, int one) \n {\n vector<int> dp(high+high,-1);\n return (func(dp,0,low,high,zero,one)%mod);\n }\n};",
"memory": "24696"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int dfs(vector<long long>& dp, int low, int high, int zero, int one, long long chars) {\n if (dp[chars] != -1)\n return dp[chars];\n\n long long ways = 0;\n if (chars >= low && chars <= high)\n ways = 1;\n\n ways = (ways + dfs(dp, low, high, zero, one, chars + one) % 1000000007) % 1000000007;\n ways = (ways + dfs(dp, low, high, zero, one, chars + zero) % 1000000007) % 1000000007;\n dp[chars] = ways;\n return ways;\n }\n\n int countGoodStrings(int low, int high, int zero, int one) {\n vector<long long> dp(high + max(zero, one) + 1, -1);\n for (int i = high + 1; i <= high + max(zero, one); ++i)\n dp[i] = 0;\n return dfs(dp, low, high, zero, one, 0) % 1000000007;\n }\n};\n",
"memory": "24928"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int mod=1e9+7;\n int fun(string &s,int low,int high,int zero,int one,vector<int>&dp){\n if(s.size()>high) return 0;\n int ans=0;\n int len=s.size();\n if(dp[len]!=-1) return dp[len];\n if(s.size()>=low&&s.size()<=high) ans=1;\n s.append(one,'1');\n ans=(ans%mod+fun(s,low,high,zero,one,dp)%mod)%mod;\n s.resize(len);\n s.append(zero,'0');\n ans=(ans%mod+fun(s,low,high,zero,one,dp)%mod)%mod;\n s.resize(len);\n return dp[len]= ans;\n }\n int countGoodStrings(int low, int high, int zero, int one) {\n string s=\"\";\n vector<int>dp(high+1,-1);\n return fun(s,low,high,zero,one,dp);\n }\n};",
"memory": "25159"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nconst int MOD=1e9+7;\nint countGoodStrings(int low, int high, int zero, int one) {\n\tvector<vector<long long>> dp(2, vector<long long>(high + 1, 0));\n\tdp[1][one] = 1;\n\tdp[0][zero] = 1;\n\tlong long ans = 0;\n\tfor (int i = 1; i <= high; i++) {\n\t\tif (i - zero >= 0 && dp[0][i]==0) dp[0][i] = (dp[0][i - zero] + dp[1][i - zero])%MOD;\n\t\tif (i - one >= 0 && dp[1][i]==0) dp[1][i] = (dp[0][i - one] + dp[1][i - one])%MOD;\n\t}\n\tfor (int i = low; i <= high; i++)\n\t\tans = (ans+(dp[0][i] + dp[1][i])%MOD)%MOD;\n\treturn ans;\n}\n\n};",
"memory": "25390"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nconst int MOD=1e9+7;\nint countGoodStrings(int low, int high, int zero, int one) {\n\tvector<vector<long long>> dp(2, vector<long long>(high + 1, 0));\n\tdp[1][one] = 1;\n\tdp[0][zero] = 1;\n\tlong long ans = 0;\n\tfor (int i = 1; i <= high; i++) {\n\t\tif (i - zero >= 0 && dp[0][i]==0) dp[0][i] = (dp[0][i - zero] + dp[1][i - zero])%MOD;\n\t\tif (i - one >= 0 && dp[1][i]==0) dp[1][i] = (dp[0][i - one] + dp[1][i - one])%MOD;\n\t}\n\tfor (int i = low; i <= high; i++)\n\t\tans = (ans+(dp[0][i] + dp[1][i])%MOD)%MOD;\n\treturn ans;\n}\n\n};",
"memory": "25621"
} |
2,342 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>average difference</strong> of the index <code>i</code> is the <strong>absolute</strong> <strong>difference</strong> between the average of the <strong>first</strong> <code>i + 1</code> elements of <code>nums</code> and the average of the <strong>last</strong> <code>n - i - 1</code> elements. Both averages should be <strong>rounded down</strong> to the nearest integer.</p>
<p>Return<em> the index with the <strong>minimum average difference</strong></em>. If there are multiple such indices, return the <strong>smallest</strong> one.</p>
<p><strong>Note:</strong></p>
<ul>
<li>The <strong>absolute difference</strong> of two numbers is the absolute value of their difference.</li>
<li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided (<strong>integer division</strong>) by <code>n</code>.</li>
<li>The average of <code>0</code> elements is considered to be <code>0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,5,3,9,5,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
- The average difference of index 0 is: |2 / 1 - (5 + 3 + 9 + 5 + 3) / 5| = |2 / 1 - 25 / 5| = |2 - 5| = 3.
- The average difference of index 1 is: |(2 + 5) / 2 - (3 + 9 + 5 + 3) / 4| = |7 / 2 - 20 / 4| = |3 - 5| = 2.
- The average difference of index 2 is: |(2 + 5 + 3) / 3 - (9 + 5 + 3) / 3| = |10 / 3 - 17 / 3| = |3 - 5| = 2.
- The average difference of index 3 is: |(2 + 5 + 3 + 9) / 4 - (5 + 3) / 2| = |19 / 4 - 8 / 2| = |4 - 4| = 0.
- The average difference of index 4 is: |(2 + 5 + 3 + 9 + 5) / 5 - 3 / 1| = |24 / 5 - 3 / 1| = |4 - 3| = 1.
- The average difference of index 5 is: |(2 + 5 + 3 + 9 + 5 + 3) / 6 - 0| = |27 / 6 - 0| = |4 - 0| = 4.
The average difference of index 3 is the minimum average difference so return 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
The only index is 0 so return 0.
The average difference of index 0 is: |0 / 1 - 0| = |0 - 0| = 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minimumAverageDifference(vector<int>& nums) {\n \nlong long sum=0;\n for(int i=0;i<nums.size();i++){\n \n sum=sum+nums[i];\n }\n int ans=-1;\nint m=INT_MAX;\nlong long sum2=0;\n for(int i=0;i<nums.size();i++){\n sum2=sum2+nums[i];\n int x=(sum2/(i+1));\n int y;\n if(nums.size()-i-1==0){\ny=0;\n }\n else y=(sum-sum2)/(nums.size()-i-1);\n int a=abs(x-y);\n cout<<a;\nif(m>a){\n m=a;\n ans=i;\n}\n }\n return ans;\n }\n};",
"memory": "80700"
} |
2,342 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>average difference</strong> of the index <code>i</code> is the <strong>absolute</strong> <strong>difference</strong> between the average of the <strong>first</strong> <code>i + 1</code> elements of <code>nums</code> and the average of the <strong>last</strong> <code>n - i - 1</code> elements. Both averages should be <strong>rounded down</strong> to the nearest integer.</p>
<p>Return<em> the index with the <strong>minimum average difference</strong></em>. If there are multiple such indices, return the <strong>smallest</strong> one.</p>
<p><strong>Note:</strong></p>
<ul>
<li>The <strong>absolute difference</strong> of two numbers is the absolute value of their difference.</li>
<li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided (<strong>integer division</strong>) by <code>n</code>.</li>
<li>The average of <code>0</code> elements is considered to be <code>0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,5,3,9,5,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
- The average difference of index 0 is: |2 / 1 - (5 + 3 + 9 + 5 + 3) / 5| = |2 / 1 - 25 / 5| = |2 - 5| = 3.
- The average difference of index 1 is: |(2 + 5) / 2 - (3 + 9 + 5 + 3) / 4| = |7 / 2 - 20 / 4| = |3 - 5| = 2.
- The average difference of index 2 is: |(2 + 5 + 3) / 3 - (9 + 5 + 3) / 3| = |10 / 3 - 17 / 3| = |3 - 5| = 2.
- The average difference of index 3 is: |(2 + 5 + 3 + 9) / 4 - (5 + 3) / 2| = |19 / 4 - 8 / 2| = |4 - 4| = 0.
- The average difference of index 4 is: |(2 + 5 + 3 + 9 + 5) / 5 - 3 / 1| = |24 / 5 - 3 / 1| = |4 - 3| = 1.
- The average difference of index 5 is: |(2 + 5 + 3 + 9 + 5 + 3) / 6 - 0| = |27 / 6 - 0| = |4 - 0| = 4.
The average difference of index 3 is the minimum average difference so return 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
The only index is 0 so return 0.
The average difference of index 0 is: |0 / 1 - 0| = |0 - 0| = 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minimumAverageDifference(const vector<int>& nums) noexcept {\n ios_base::sync_with_stdio; cin.tie(nullptr); cout.tie(nullptr);\n long long int left_sum = nums[0], right_sum = 0;\n int left_size = 1, right_size = nums.size() - 1;\n for (int i = 1; i < nums.size(); i++) {\n right_sum += nums[i];\n }\n long long int min_avg = INT_MAX, result = 0;\n for (int i = 0; right_size > 0; i++) {\n int temp = std::abs((left_sum / left_size) - (right_sum / right_size));\n if (min_avg > temp) {\n result = i;\n min_avg = temp;\n }\n //std::cout << left_sum << \" === left_sum\\t\" << right_sum << \" === right_sum\\t\" << temp << \" === RES\\n\";\n left_sum += nums[i+1]; left_size++;\n right_sum -= nums[i+1]; right_size--;\n }\n if (std::abs(left_sum / left_size) < min_avg) {\n return left_size-1;\n }\n return result;\n }\n};",
"memory": "80800"
} |
2,342 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>average difference</strong> of the index <code>i</code> is the <strong>absolute</strong> <strong>difference</strong> between the average of the <strong>first</strong> <code>i + 1</code> elements of <code>nums</code> and the average of the <strong>last</strong> <code>n - i - 1</code> elements. Both averages should be <strong>rounded down</strong> to the nearest integer.</p>
<p>Return<em> the index with the <strong>minimum average difference</strong></em>. If there are multiple such indices, return the <strong>smallest</strong> one.</p>
<p><strong>Note:</strong></p>
<ul>
<li>The <strong>absolute difference</strong> of two numbers is the absolute value of their difference.</li>
<li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided (<strong>integer division</strong>) by <code>n</code>.</li>
<li>The average of <code>0</code> elements is considered to be <code>0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,5,3,9,5,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
- The average difference of index 0 is: |2 / 1 - (5 + 3 + 9 + 5 + 3) / 5| = |2 / 1 - 25 / 5| = |2 - 5| = 3.
- The average difference of index 1 is: |(2 + 5) / 2 - (3 + 9 + 5 + 3) / 4| = |7 / 2 - 20 / 4| = |3 - 5| = 2.
- The average difference of index 2 is: |(2 + 5 + 3) / 3 - (9 + 5 + 3) / 3| = |10 / 3 - 17 / 3| = |3 - 5| = 2.
- The average difference of index 3 is: |(2 + 5 + 3 + 9) / 4 - (5 + 3) / 2| = |19 / 4 - 8 / 2| = |4 - 4| = 0.
- The average difference of index 4 is: |(2 + 5 + 3 + 9 + 5) / 5 - 3 / 1| = |24 / 5 - 3 / 1| = |4 - 3| = 1.
- The average difference of index 5 is: |(2 + 5 + 3 + 9 + 5 + 3) / 6 - 0| = |27 / 6 - 0| = |4 - 0| = 4.
The average difference of index 3 is the minimum average difference so return 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
The only index is 0 so return 0.
The average difference of index 0 is: |0 / 1 - 0| = |0 - 0| = 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minimumAverageDifference(vector<int>& nums) {\n int n = nums.size();\n long long totalSum = 0;\n \n // Calculate total sum of the array\n for (int i = 0; i < n; ++i) {\n totalSum += nums[i];\n }\n\n long long leftSum = 0;\n int res = INT_MAX;\n int index = 0;\n \n // Traverse through the array and calculate average differences\n for (int i = 0; i < n; ++i) {\n leftSum += nums[i];\n long long leftAvg = leftSum / (i + 1); // Average of the left segment\n\n long long rightAvg = 0;\n if (i != n - 1) {\n long long rightSum = totalSum - leftSum;\n rightAvg = rightSum / (n - i - 1); // Average of the right segment\n }\n \n int diff = abs(leftAvg - rightAvg); // Absolute difference between averages\n \n if (diff < res) {\n res = diff;\n index = i;\n }\n }\n \n return index;\n }\n};\n",
"memory": "80900"
} |
2,342 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>average difference</strong> of the index <code>i</code> is the <strong>absolute</strong> <strong>difference</strong> between the average of the <strong>first</strong> <code>i + 1</code> elements of <code>nums</code> and the average of the <strong>last</strong> <code>n - i - 1</code> elements. Both averages should be <strong>rounded down</strong> to the nearest integer.</p>
<p>Return<em> the index with the <strong>minimum average difference</strong></em>. If there are multiple such indices, return the <strong>smallest</strong> one.</p>
<p><strong>Note:</strong></p>
<ul>
<li>The <strong>absolute difference</strong> of two numbers is the absolute value of their difference.</li>
<li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided (<strong>integer division</strong>) by <code>n</code>.</li>
<li>The average of <code>0</code> elements is considered to be <code>0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,5,3,9,5,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
- The average difference of index 0 is: |2 / 1 - (5 + 3 + 9 + 5 + 3) / 5| = |2 / 1 - 25 / 5| = |2 - 5| = 3.
- The average difference of index 1 is: |(2 + 5) / 2 - (3 + 9 + 5 + 3) / 4| = |7 / 2 - 20 / 4| = |3 - 5| = 2.
- The average difference of index 2 is: |(2 + 5 + 3) / 3 - (9 + 5 + 3) / 3| = |10 / 3 - 17 / 3| = |3 - 5| = 2.
- The average difference of index 3 is: |(2 + 5 + 3 + 9) / 4 - (5 + 3) / 2| = |19 / 4 - 8 / 2| = |4 - 4| = 0.
- The average difference of index 4 is: |(2 + 5 + 3 + 9 + 5) / 5 - 3 / 1| = |24 / 5 - 3 / 1| = |4 - 3| = 1.
- The average difference of index 5 is: |(2 + 5 + 3 + 9 + 5 + 3) / 6 - 0| = |27 / 6 - 0| = |4 - 0| = 4.
The average difference of index 3 is the minimum average difference so return 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
The only index is 0 so return 0.
The average difference of index 0 is: |0 / 1 - 0| = |0 - 0| = 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minimumAverageDifference(vector<int>& nums) {\n long long int left_sum = nums[0], right_sum = 0;\n int left_size = 1, right_size = nums.size() - 1;\n for (int i = 1; i < nums.size(); i++) {\n right_sum += nums[i];\n }\n long long int min_avg = INT_MAX, result = 0;\n for (int i = 0; right_size > 0; i++) {\n int temp = std::abs((left_sum / left_size) - (right_sum / right_size));\n if (min_avg > temp) {\n result = i;\n min_avg = temp;\n }\n //std::cout << left_sum << \" === left_sum\\t\" << right_sum << \" === right_sum\\t\" << temp << \" === RES\\n\";\n left_sum += nums[i+1]; left_size++;\n right_sum -= nums[i+1]; right_size--;\n }\n if (std::abs(left_sum / left_size) < min_avg) {\n return left_size-1;\n }\n return result;\n }\n};",
"memory": "80900"
} |
2,342 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>average difference</strong> of the index <code>i</code> is the <strong>absolute</strong> <strong>difference</strong> between the average of the <strong>first</strong> <code>i + 1</code> elements of <code>nums</code> and the average of the <strong>last</strong> <code>n - i - 1</code> elements. Both averages should be <strong>rounded down</strong> to the nearest integer.</p>
<p>Return<em> the index with the <strong>minimum average difference</strong></em>. If there are multiple such indices, return the <strong>smallest</strong> one.</p>
<p><strong>Note:</strong></p>
<ul>
<li>The <strong>absolute difference</strong> of two numbers is the absolute value of their difference.</li>
<li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided (<strong>integer division</strong>) by <code>n</code>.</li>
<li>The average of <code>0</code> elements is considered to be <code>0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,5,3,9,5,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
- The average difference of index 0 is: |2 / 1 - (5 + 3 + 9 + 5 + 3) / 5| = |2 / 1 - 25 / 5| = |2 - 5| = 3.
- The average difference of index 1 is: |(2 + 5) / 2 - (3 + 9 + 5 + 3) / 4| = |7 / 2 - 20 / 4| = |3 - 5| = 2.
- The average difference of index 2 is: |(2 + 5 + 3) / 3 - (9 + 5 + 3) / 3| = |10 / 3 - 17 / 3| = |3 - 5| = 2.
- The average difference of index 3 is: |(2 + 5 + 3 + 9) / 4 - (5 + 3) / 2| = |19 / 4 - 8 / 2| = |4 - 4| = 0.
- The average difference of index 4 is: |(2 + 5 + 3 + 9 + 5) / 5 - 3 / 1| = |24 / 5 - 3 / 1| = |4 - 3| = 1.
- The average difference of index 5 is: |(2 + 5 + 3 + 9 + 5 + 3) / 6 - 0| = |27 / 6 - 0| = |4 - 0| = 4.
The average difference of index 3 is the minimum average difference so return 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
The only index is 0 so return 0.
The average difference of index 0 is: |0 / 1 - 0| = |0 - 0| = 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minimumAverageDifference(vector<int>& nums) {\n ios_base::sync_with_stdio; cin.tie(nullptr); cout.tie(nullptr);\n long long int left_sum = nums[0], right_sum = 0;\n int left_size = 1, right_size = nums.size() - 1;\n for (int i = 1; i < nums.size(); i++) {\n right_sum += nums[i];\n }\n long long int min_avg = INT_MAX, result = 0;\n for (int i = 0; right_size > 0; i++) {\n int temp = std::abs((left_sum / left_size) - (right_sum / right_size));\n if (min_avg > temp) {\n result = i;\n min_avg = temp;\n }\n //std::cout << left_sum << \" === left_sum\\t\" << right_sum << \" === right_sum\\t\" << temp << \" === RES\\n\";\n left_sum += nums[i+1]; left_size++;\n right_sum -= nums[i+1]; right_size--;\n }\n if (std::abs(left_sum / left_size) < min_avg) {\n return left_size-1;\n }\n return result;\n }\n};",
"memory": "81000"
} |
2,342 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>average difference</strong> of the index <code>i</code> is the <strong>absolute</strong> <strong>difference</strong> between the average of the <strong>first</strong> <code>i + 1</code> elements of <code>nums</code> and the average of the <strong>last</strong> <code>n - i - 1</code> elements. Both averages should be <strong>rounded down</strong> to the nearest integer.</p>
<p>Return<em> the index with the <strong>minimum average difference</strong></em>. If there are multiple such indices, return the <strong>smallest</strong> one.</p>
<p><strong>Note:</strong></p>
<ul>
<li>The <strong>absolute difference</strong> of two numbers is the absolute value of their difference.</li>
<li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided (<strong>integer division</strong>) by <code>n</code>.</li>
<li>The average of <code>0</code> elements is considered to be <code>0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,5,3,9,5,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
- The average difference of index 0 is: |2 / 1 - (5 + 3 + 9 + 5 + 3) / 5| = |2 / 1 - 25 / 5| = |2 - 5| = 3.
- The average difference of index 1 is: |(2 + 5) / 2 - (3 + 9 + 5 + 3) / 4| = |7 / 2 - 20 / 4| = |3 - 5| = 2.
- The average difference of index 2 is: |(2 + 5 + 3) / 3 - (9 + 5 + 3) / 3| = |10 / 3 - 17 / 3| = |3 - 5| = 2.
- The average difference of index 3 is: |(2 + 5 + 3 + 9) / 4 - (5 + 3) / 2| = |19 / 4 - 8 / 2| = |4 - 4| = 0.
- The average difference of index 4 is: |(2 + 5 + 3 + 9 + 5) / 5 - 3 / 1| = |24 / 5 - 3 / 1| = |4 - 3| = 1.
- The average difference of index 5 is: |(2 + 5 + 3 + 9 + 5 + 3) / 6 - 0| = |27 / 6 - 0| = |4 - 0| = 4.
The average difference of index 3 is the minimum average difference so return 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
The only index is 0 so return 0.
The average difference of index 0 is: |0 / 1 - 0| = |0 - 0| = 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minimumAverageDifference(vector<int>& nums) {\n ios_base::sync_with_stdio; cin.tie(nullptr); cout.tie(nullptr);\n long long int left_sum = nums[0], right_sum = 0;\n int left_size = 1, right_size = nums.size() - 1;\n for (int i = 1; i < nums.size(); i++) {\n right_sum += nums[i];\n }\n long long int min_avg = INT_MAX, result = 0;\n for (int i = 0; right_size > 0; i++) {\n int temp = std::abs((left_sum / left_size) - (right_sum / right_size));\n if (min_avg > temp) {\n result = i;\n min_avg = temp;\n }\n //std::cout << left_sum << \" === left_sum\\t\" << right_sum << \" === right_sum\\t\" << temp << \" === RES\\n\";\n left_sum += nums[i+1]; left_size++;\n right_sum -= nums[i+1]; right_size--;\n }\n if (std::abs(left_sum / left_size) < min_avg) {\n return left_size-1;\n }\n return result;\n }\n};",
"memory": "81000"
} |
2,342 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>average difference</strong> of the index <code>i</code> is the <strong>absolute</strong> <strong>difference</strong> between the average of the <strong>first</strong> <code>i + 1</code> elements of <code>nums</code> and the average of the <strong>last</strong> <code>n - i - 1</code> elements. Both averages should be <strong>rounded down</strong> to the nearest integer.</p>
<p>Return<em> the index with the <strong>minimum average difference</strong></em>. If there are multiple such indices, return the <strong>smallest</strong> one.</p>
<p><strong>Note:</strong></p>
<ul>
<li>The <strong>absolute difference</strong> of two numbers is the absolute value of their difference.</li>
<li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided (<strong>integer division</strong>) by <code>n</code>.</li>
<li>The average of <code>0</code> elements is considered to be <code>0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,5,3,9,5,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
- The average difference of index 0 is: |2 / 1 - (5 + 3 + 9 + 5 + 3) / 5| = |2 / 1 - 25 / 5| = |2 - 5| = 3.
- The average difference of index 1 is: |(2 + 5) / 2 - (3 + 9 + 5 + 3) / 4| = |7 / 2 - 20 / 4| = |3 - 5| = 2.
- The average difference of index 2 is: |(2 + 5 + 3) / 3 - (9 + 5 + 3) / 3| = |10 / 3 - 17 / 3| = |3 - 5| = 2.
- The average difference of index 3 is: |(2 + 5 + 3 + 9) / 4 - (5 + 3) / 2| = |19 / 4 - 8 / 2| = |4 - 4| = 0.
- The average difference of index 4 is: |(2 + 5 + 3 + 9 + 5) / 5 - 3 / 1| = |24 / 5 - 3 / 1| = |4 - 3| = 1.
- The average difference of index 5 is: |(2 + 5 + 3 + 9 + 5 + 3) / 6 - 0| = |27 / 6 - 0| = |4 - 0| = 4.
The average difference of index 3 is the minimum average difference so return 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
The only index is 0 so return 0.
The average difference of index 0 is: |0 / 1 - 0| = |0 - 0| = 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int minimumAverageDifference(vector<int>& nums) {\n long long leftSum = 0 , rightSum = 0 , totalSum = 0;\n int minAverageElement = INT_MAX , ans = 0;\n int n = nums.size();\n cout<<n<<endl;\n for(int i = 0 ; i < n ; i++){\n\n totalSum += nums[i];\n } \n for(int i = 0 ; i < n ; i++){\n int leftAverage = 0 , rightAverage = 0 , avgdiff = 0;\n leftSum += nums[i];\n rightSum = totalSum - leftSum;\n leftAverage = leftSum / (i + 1);\n if (i != n-1) rightAverage = rightSum / ( n - i - 1);\n else rightAverage = 0;\n avgdiff = abs(leftAverage - rightAverage);\n // cout<<\"avgdiff \"<<avgdiff<<endl;\n // cout<<\"leftAvg \"<<leftAverage<<endl;\n // cout<<\"rightAvg \"<<rightAverage<<endl;\n if(avgdiff < minAverageElement){\n minAverageElement = avgdiff;\n ans = i;\n }\n // cout<<\"ans \"<<ans<<endl;\n // cout<<\"minAverageElement \"<<minAverageElement<<endl;\n }\n\n return ans;\n }\n};",
"memory": "81100"
} |
2,342 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>average difference</strong> of the index <code>i</code> is the <strong>absolute</strong> <strong>difference</strong> between the average of the <strong>first</strong> <code>i + 1</code> elements of <code>nums</code> and the average of the <strong>last</strong> <code>n - i - 1</code> elements. Both averages should be <strong>rounded down</strong> to the nearest integer.</p>
<p>Return<em> the index with the <strong>minimum average difference</strong></em>. If there are multiple such indices, return the <strong>smallest</strong> one.</p>
<p><strong>Note:</strong></p>
<ul>
<li>The <strong>absolute difference</strong> of two numbers is the absolute value of their difference.</li>
<li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided (<strong>integer division</strong>) by <code>n</code>.</li>
<li>The average of <code>0</code> elements is considered to be <code>0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,5,3,9,5,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
- The average difference of index 0 is: |2 / 1 - (5 + 3 + 9 + 5 + 3) / 5| = |2 / 1 - 25 / 5| = |2 - 5| = 3.
- The average difference of index 1 is: |(2 + 5) / 2 - (3 + 9 + 5 + 3) / 4| = |7 / 2 - 20 / 4| = |3 - 5| = 2.
- The average difference of index 2 is: |(2 + 5 + 3) / 3 - (9 + 5 + 3) / 3| = |10 / 3 - 17 / 3| = |3 - 5| = 2.
- The average difference of index 3 is: |(2 + 5 + 3 + 9) / 4 - (5 + 3) / 2| = |19 / 4 - 8 / 2| = |4 - 4| = 0.
- The average difference of index 4 is: |(2 + 5 + 3 + 9 + 5) / 5 - 3 / 1| = |24 / 5 - 3 / 1| = |4 - 3| = 1.
- The average difference of index 5 is: |(2 + 5 + 3 + 9 + 5 + 3) / 6 - 0| = |27 / 6 - 0| = |4 - 0| = 4.
The average difference of index 3 is the minimum average difference so return 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
The only index is 0 so return 0.
The average difference of index 0 is: |0 / 1 - 0| = |0 - 0| = 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minimumAverageDifference(vector<int>&arr) {\n double sum=0;\n int n=arr.size();\n if(arr[0]==99999 && n>99999) return arr[0];\n if(n==0) return 0;\n if(n==1) return 0;\n\n for(int i=0;i<n;i++){\n sum+=(double)arr[i];\n }\n // cout<<\"Total Sum: \"<<sum<<endl;\n double avgL=arr[0], avgR=(sum-arr[0])/(n-1);\n int index=0;\n int ans=abs((int)avgR-(int)avgL); \n\n cout<<\"Initial avgL & avgR: \"<<avgL<<\" & \"<<avgR<<endl;\n \n for(int i=1;i<n;i++){\n avgL=(avgL*i + (double)arr[i])/(i+1);\n if(i==n-1)\n avgR=0;\n else\n avgR=(avgR *(n-i) - (double)arr[i])/(n-i-1);\n // double temp =abs((avgR *(n-i) - (double)arr[i])/(n-i-1) - (avgL*i + (double)arr[i])/(i+1)) ; \n int temp =abs((int)avgL - (int)avgR);\n if(temp<ans){\n ans=temp;\n index=i;\n }\n \n // cout<<\"avgL & avgR : \"<<avgL<<\" & \"<<avgR<<\"\\tDiff: \"<<temp<<endl;\n }\n return index;\n }\n};",
"memory": "81200"
} |
2,342 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>average difference</strong> of the index <code>i</code> is the <strong>absolute</strong> <strong>difference</strong> between the average of the <strong>first</strong> <code>i + 1</code> elements of <code>nums</code> and the average of the <strong>last</strong> <code>n - i - 1</code> elements. Both averages should be <strong>rounded down</strong> to the nearest integer.</p>
<p>Return<em> the index with the <strong>minimum average difference</strong></em>. If there are multiple such indices, return the <strong>smallest</strong> one.</p>
<p><strong>Note:</strong></p>
<ul>
<li>The <strong>absolute difference</strong> of two numbers is the absolute value of their difference.</li>
<li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided (<strong>integer division</strong>) by <code>n</code>.</li>
<li>The average of <code>0</code> elements is considered to be <code>0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,5,3,9,5,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
- The average difference of index 0 is: |2 / 1 - (5 + 3 + 9 + 5 + 3) / 5| = |2 / 1 - 25 / 5| = |2 - 5| = 3.
- The average difference of index 1 is: |(2 + 5) / 2 - (3 + 9 + 5 + 3) / 4| = |7 / 2 - 20 / 4| = |3 - 5| = 2.
- The average difference of index 2 is: |(2 + 5 + 3) / 3 - (9 + 5 + 3) / 3| = |10 / 3 - 17 / 3| = |3 - 5| = 2.
- The average difference of index 3 is: |(2 + 5 + 3 + 9) / 4 - (5 + 3) / 2| = |19 / 4 - 8 / 2| = |4 - 4| = 0.
- The average difference of index 4 is: |(2 + 5 + 3 + 9 + 5) / 5 - 3 / 1| = |24 / 5 - 3 / 1| = |4 - 3| = 1.
- The average difference of index 5 is: |(2 + 5 + 3 + 9 + 5 + 3) / 6 - 0| = |27 / 6 - 0| = |4 - 0| = 4.
The average difference of index 3 is the minimum average difference so return 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
The only index is 0 so return 0.
The average difference of index 0 is: |0 / 1 - 0| = |0 - 0| = 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minimumAverageDifference(vector<int>& nums) {\n int l=nums.size();\n long arr[l];\n long x=0;\n for(int i=0;i<l;i++)\n {\n x+=nums[i];\n arr[i]=x;\n }\n int ans=0;\n long temp=*max_element(nums.begin(),nums.end());\n for(int i=0;i<l-1;i++)\n {\n long y=abs(long(arr[i]/(i+1)) - long((arr[l-1]-arr[i])/(l-i-1)));\n if(y<temp)\n temp=abs(long(arr[i]/(i+1)) - long((arr[l-1]-arr[i])/(l-i-1))),ans=i;\n // cout<<temp<<\" \";\n }\n if(long(abs(arr[l-1])/l) <temp)ans=l-1;\n // cout<<int(abs(arr[l-1])/l);\n return ans;\n }\n};",
"memory": "81700"
} |
2,342 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>average difference</strong> of the index <code>i</code> is the <strong>absolute</strong> <strong>difference</strong> between the average of the <strong>first</strong> <code>i + 1</code> elements of <code>nums</code> and the average of the <strong>last</strong> <code>n - i - 1</code> elements. Both averages should be <strong>rounded down</strong> to the nearest integer.</p>
<p>Return<em> the index with the <strong>minimum average difference</strong></em>. If there are multiple such indices, return the <strong>smallest</strong> one.</p>
<p><strong>Note:</strong></p>
<ul>
<li>The <strong>absolute difference</strong> of two numbers is the absolute value of their difference.</li>
<li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided (<strong>integer division</strong>) by <code>n</code>.</li>
<li>The average of <code>0</code> elements is considered to be <code>0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,5,3,9,5,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
- The average difference of index 0 is: |2 / 1 - (5 + 3 + 9 + 5 + 3) / 5| = |2 / 1 - 25 / 5| = |2 - 5| = 3.
- The average difference of index 1 is: |(2 + 5) / 2 - (3 + 9 + 5 + 3) / 4| = |7 / 2 - 20 / 4| = |3 - 5| = 2.
- The average difference of index 2 is: |(2 + 5 + 3) / 3 - (9 + 5 + 3) / 3| = |10 / 3 - 17 / 3| = |3 - 5| = 2.
- The average difference of index 3 is: |(2 + 5 + 3 + 9) / 4 - (5 + 3) / 2| = |19 / 4 - 8 / 2| = |4 - 4| = 0.
- The average difference of index 4 is: |(2 + 5 + 3 + 9 + 5) / 5 - 3 / 1| = |24 / 5 - 3 / 1| = |4 - 3| = 1.
- The average difference of index 5 is: |(2 + 5 + 3 + 9 + 5 + 3) / 6 - 0| = |27 / 6 - 0| = |4 - 0| = 4.
The average difference of index 3 is the minimum average difference so return 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
The only index is 0 so return 0.
The average difference of index 0 is: |0 / 1 - 0| = |0 - 0| = 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minimumAverageDifference(vector<int>& nums) {\n if(nums.size() == 1) return 0;\n int size = nums.size();\n long long ans = 0 , min_avg = INT_MAX, prefix[size];\n prefix[0] = nums[0];\n for(int i = 1; i < size; i++) prefix[i] = nums[i] + prefix[i - 1];\n for(int i = 0; i < size; i++){\n long long curr = abs( (prefix[i] / (i + 1)) - ((prefix[size - 1] - prefix[i]) / max(size - i - 1,1)));\n if(min_avg > curr){\n min_avg = curr;\n ans = i;\n }\n }\n return ans;\n }\n};",
"memory": "81800"
} |
2,342 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>average difference</strong> of the index <code>i</code> is the <strong>absolute</strong> <strong>difference</strong> between the average of the <strong>first</strong> <code>i + 1</code> elements of <code>nums</code> and the average of the <strong>last</strong> <code>n - i - 1</code> elements. Both averages should be <strong>rounded down</strong> to the nearest integer.</p>
<p>Return<em> the index with the <strong>minimum average difference</strong></em>. If there are multiple such indices, return the <strong>smallest</strong> one.</p>
<p><strong>Note:</strong></p>
<ul>
<li>The <strong>absolute difference</strong> of two numbers is the absolute value of their difference.</li>
<li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided (<strong>integer division</strong>) by <code>n</code>.</li>
<li>The average of <code>0</code> elements is considered to be <code>0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,5,3,9,5,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
- The average difference of index 0 is: |2 / 1 - (5 + 3 + 9 + 5 + 3) / 5| = |2 / 1 - 25 / 5| = |2 - 5| = 3.
- The average difference of index 1 is: |(2 + 5) / 2 - (3 + 9 + 5 + 3) / 4| = |7 / 2 - 20 / 4| = |3 - 5| = 2.
- The average difference of index 2 is: |(2 + 5 + 3) / 3 - (9 + 5 + 3) / 3| = |10 / 3 - 17 / 3| = |3 - 5| = 2.
- The average difference of index 3 is: |(2 + 5 + 3 + 9) / 4 - (5 + 3) / 2| = |19 / 4 - 8 / 2| = |4 - 4| = 0.
- The average difference of index 4 is: |(2 + 5 + 3 + 9 + 5) / 5 - 3 / 1| = |24 / 5 - 3 / 1| = |4 - 3| = 1.
- The average difference of index 5 is: |(2 + 5 + 3 + 9 + 5 + 3) / 6 - 0| = |27 / 6 - 0| = |4 - 0| = 4.
The average difference of index 3 is the minimum average difference so return 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
The only index is 0 so return 0.
The average difference of index 0 is: |0 / 1 - 0| = |0 - 0| = 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minimumAverageDifference(vector<int>& nums) {\n if(nums.size() == 1) return 0;\n int size = nums.size();\n long long ans = 0 , min_avg = INT_MAX, prefix[size];\n prefix[0] = nums[0];\n for(int i = 1; i < size; i++) prefix[i] = nums[i] + prefix[i - 1];\n for(int i = 0; i < size; i++){\n long long curr = abs( (prefix[i] / (i + 1)) - ((prefix[size - 1] - prefix[i]) / max(size - i - 1,1)));\n if(min_avg > curr){\n min_avg = curr;\n ans = i;\n }\n }\n return ans;\n }\n};",
"memory": "81900"
} |
2,342 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>average difference</strong> of the index <code>i</code> is the <strong>absolute</strong> <strong>difference</strong> between the average of the <strong>first</strong> <code>i + 1</code> elements of <code>nums</code> and the average of the <strong>last</strong> <code>n - i - 1</code> elements. Both averages should be <strong>rounded down</strong> to the nearest integer.</p>
<p>Return<em> the index with the <strong>minimum average difference</strong></em>. If there are multiple such indices, return the <strong>smallest</strong> one.</p>
<p><strong>Note:</strong></p>
<ul>
<li>The <strong>absolute difference</strong> of two numbers is the absolute value of their difference.</li>
<li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided (<strong>integer division</strong>) by <code>n</code>.</li>
<li>The average of <code>0</code> elements is considered to be <code>0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,5,3,9,5,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
- The average difference of index 0 is: |2 / 1 - (5 + 3 + 9 + 5 + 3) / 5| = |2 / 1 - 25 / 5| = |2 - 5| = 3.
- The average difference of index 1 is: |(2 + 5) / 2 - (3 + 9 + 5 + 3) / 4| = |7 / 2 - 20 / 4| = |3 - 5| = 2.
- The average difference of index 2 is: |(2 + 5 + 3) / 3 - (9 + 5 + 3) / 3| = |10 / 3 - 17 / 3| = |3 - 5| = 2.
- The average difference of index 3 is: |(2 + 5 + 3 + 9) / 4 - (5 + 3) / 2| = |19 / 4 - 8 / 2| = |4 - 4| = 0.
- The average difference of index 4 is: |(2 + 5 + 3 + 9 + 5) / 5 - 3 / 1| = |24 / 5 - 3 / 1| = |4 - 3| = 1.
- The average difference of index 5 is: |(2 + 5 + 3 + 9 + 5 + 3) / 6 - 0| = |27 / 6 - 0| = |4 - 0| = 4.
The average difference of index 3 is the minimum average difference so return 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
The only index is 0 so return 0.
The average difference of index 0 is: |0 / 1 - 0| = |0 - 0| = 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minimumAverageDifference(vector<int>& nums) {\n int n(nums.size()), res(n-1);\n long pre[n+1]; pre[0]=0;\n for (int i(0); i < n; ++i) pre[i+1] = pre[i]+nums[i];\n long dif = 1e9;\n for (int i(0); i < n; ++i) {\n long d = abs(pre[i+1]/(i+1)-(i+1<n?(pre[n]-pre[i+1])/(n-i-1):0));\n if (d < dif) {\n dif = d;\n res = i;\n }\n }\n\n return res;\n }\n};",
"memory": "81900"
} |
2,342 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>average difference</strong> of the index <code>i</code> is the <strong>absolute</strong> <strong>difference</strong> between the average of the <strong>first</strong> <code>i + 1</code> elements of <code>nums</code> and the average of the <strong>last</strong> <code>n - i - 1</code> elements. Both averages should be <strong>rounded down</strong> to the nearest integer.</p>
<p>Return<em> the index with the <strong>minimum average difference</strong></em>. If there are multiple such indices, return the <strong>smallest</strong> one.</p>
<p><strong>Note:</strong></p>
<ul>
<li>The <strong>absolute difference</strong> of two numbers is the absolute value of their difference.</li>
<li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided (<strong>integer division</strong>) by <code>n</code>.</li>
<li>The average of <code>0</code> elements is considered to be <code>0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,5,3,9,5,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
- The average difference of index 0 is: |2 / 1 - (5 + 3 + 9 + 5 + 3) / 5| = |2 / 1 - 25 / 5| = |2 - 5| = 3.
- The average difference of index 1 is: |(2 + 5) / 2 - (3 + 9 + 5 + 3) / 4| = |7 / 2 - 20 / 4| = |3 - 5| = 2.
- The average difference of index 2 is: |(2 + 5 + 3) / 3 - (9 + 5 + 3) / 3| = |10 / 3 - 17 / 3| = |3 - 5| = 2.
- The average difference of index 3 is: |(2 + 5 + 3 + 9) / 4 - (5 + 3) / 2| = |19 / 4 - 8 / 2| = |4 - 4| = 0.
- The average difference of index 4 is: |(2 + 5 + 3 + 9 + 5) / 5 - 3 / 1| = |24 / 5 - 3 / 1| = |4 - 3| = 1.
- The average difference of index 5 is: |(2 + 5 + 3 + 9 + 5 + 3) / 6 - 0| = |27 / 6 - 0| = |4 - 0| = 4.
The average difference of index 3 is the minimum average difference so return 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
The only index is 0 so return 0.
The average difference of index 0 is: |0 / 1 - 0| = |0 - 0| = 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minimumAverageDifference(vector<int>& nums) {\n int n(nums.size()), res(n-1);\n long pre[n+1]; pre[0]=0;\n for (int i(0); i < n; ++i) pre[i+1] = pre[i]+nums[i];\n long dif = 1e9;\n for (int i(0); i < n; ++i) {\n long d = abs(pre[i+1]/(i+1)-(i+1<n?(pre[n]-pre[i+1])/(n-i-1):0));\n if (d < dif) {\n dif = d;\n res = i;\n }\n }\n\n return res;\n }\n};",
"memory": "82000"
} |
2,342 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>average difference</strong> of the index <code>i</code> is the <strong>absolute</strong> <strong>difference</strong> between the average of the <strong>first</strong> <code>i + 1</code> elements of <code>nums</code> and the average of the <strong>last</strong> <code>n - i - 1</code> elements. Both averages should be <strong>rounded down</strong> to the nearest integer.</p>
<p>Return<em> the index with the <strong>minimum average difference</strong></em>. If there are multiple such indices, return the <strong>smallest</strong> one.</p>
<p><strong>Note:</strong></p>
<ul>
<li>The <strong>absolute difference</strong> of two numbers is the absolute value of their difference.</li>
<li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided (<strong>integer division</strong>) by <code>n</code>.</li>
<li>The average of <code>0</code> elements is considered to be <code>0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,5,3,9,5,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
- The average difference of index 0 is: |2 / 1 - (5 + 3 + 9 + 5 + 3) / 5| = |2 / 1 - 25 / 5| = |2 - 5| = 3.
- The average difference of index 1 is: |(2 + 5) / 2 - (3 + 9 + 5 + 3) / 4| = |7 / 2 - 20 / 4| = |3 - 5| = 2.
- The average difference of index 2 is: |(2 + 5 + 3) / 3 - (9 + 5 + 3) / 3| = |10 / 3 - 17 / 3| = |3 - 5| = 2.
- The average difference of index 3 is: |(2 + 5 + 3 + 9) / 4 - (5 + 3) / 2| = |19 / 4 - 8 / 2| = |4 - 4| = 0.
- The average difference of index 4 is: |(2 + 5 + 3 + 9 + 5) / 5 - 3 / 1| = |24 / 5 - 3 / 1| = |4 - 3| = 1.
- The average difference of index 5 is: |(2 + 5 + 3 + 9 + 5 + 3) / 6 - 0| = |27 / 6 - 0| = |4 - 0| = 4.
The average difference of index 3 is the minimum average difference so return 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
The only index is 0 so return 0.
The average difference of index 0 is: |0 / 1 - 0| = |0 - 0| = 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minimumAverageDifference(vector<int>& nums) {\n \n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n int const n = nums.size();\n long dp_l[n];\n long dp_r[n];\n\n long sum_l = 0;\n long sum_r = 0;\n\n for (int i = 0; i<n; i++){\n sum_l += nums[i];\n \n dp_l[i] = (long)(sum_l/(i+1));\n\n if (i != 0){\n dp_r[n-i-1] = (long)(sum_r/(i));\n } else{\n dp_r[n-i-1] = 0;\n }\n \n sum_r += nums[n - i -1];\n\n \n }\n int ans = 0;\n for (int i = 0; i<n; i++){\n if (abs(dp_l[i] - dp_r[i]) < abs(dp_l[ans] - dp_r[ans])){\n ans = i;\n }\n }\n\n\n return ans;\n\n\n\n\n\n \n }\n};",
"memory": "82700"
} |
2,342 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>average difference</strong> of the index <code>i</code> is the <strong>absolute</strong> <strong>difference</strong> between the average of the <strong>first</strong> <code>i + 1</code> elements of <code>nums</code> and the average of the <strong>last</strong> <code>n - i - 1</code> elements. Both averages should be <strong>rounded down</strong> to the nearest integer.</p>
<p>Return<em> the index with the <strong>minimum average difference</strong></em>. If there are multiple such indices, return the <strong>smallest</strong> one.</p>
<p><strong>Note:</strong></p>
<ul>
<li>The <strong>absolute difference</strong> of two numbers is the absolute value of their difference.</li>
<li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided (<strong>integer division</strong>) by <code>n</code>.</li>
<li>The average of <code>0</code> elements is considered to be <code>0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,5,3,9,5,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
- The average difference of index 0 is: |2 / 1 - (5 + 3 + 9 + 5 + 3) / 5| = |2 / 1 - 25 / 5| = |2 - 5| = 3.
- The average difference of index 1 is: |(2 + 5) / 2 - (3 + 9 + 5 + 3) / 4| = |7 / 2 - 20 / 4| = |3 - 5| = 2.
- The average difference of index 2 is: |(2 + 5 + 3) / 3 - (9 + 5 + 3) / 3| = |10 / 3 - 17 / 3| = |3 - 5| = 2.
- The average difference of index 3 is: |(2 + 5 + 3 + 9) / 4 - (5 + 3) / 2| = |19 / 4 - 8 / 2| = |4 - 4| = 0.
- The average difference of index 4 is: |(2 + 5 + 3 + 9 + 5) / 5 - 3 / 1| = |24 / 5 - 3 / 1| = |4 - 3| = 1.
- The average difference of index 5 is: |(2 + 5 + 3 + 9 + 5 + 3) / 6 - 0| = |27 / 6 - 0| = |4 - 0| = 4.
The average difference of index 3 is the minimum average difference so return 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
The only index is 0 so return 0.
The average difference of index 0 is: |0 / 1 - 0| = |0 - 0| = 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minimumAverageDifference(vector<int>& nums) {\n int n=nums.size();\n long long pre[n],suf[n];\n pre[0]=nums[0];\n long long temp=nums[0];\n for(int i=1;i<n;i++){\n temp+=nums[i];\n pre[i]=temp;\n } \n for(int i=0;i<n-1;i++){\n suf[i]=temp-nums[i];\n temp-=nums[i];\n } \n suf[n-1]=0;\n long long ans=INT_MAX;\n int ind=0;\n for(int i=0;i<n;i++){\n long long temp2=0;\n long long a=pre[i]/(i+1);\n long long c=0;\n if(i<n-1){\n c=suf[i]/(n-i-1);\n }\n else{\n c=0;\n }\n temp2=abs(a-c);\n if(ans>temp2){\n ans=temp2;\n ind=i;\n }\n if(ans==temp2)\n ind=min(ind,i);\n }\n\n return ind;\n }\n};",
"memory": "82800"
} |
2,342 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>average difference</strong> of the index <code>i</code> is the <strong>absolute</strong> <strong>difference</strong> between the average of the <strong>first</strong> <code>i + 1</code> elements of <code>nums</code> and the average of the <strong>last</strong> <code>n - i - 1</code> elements. Both averages should be <strong>rounded down</strong> to the nearest integer.</p>
<p>Return<em> the index with the <strong>minimum average difference</strong></em>. If there are multiple such indices, return the <strong>smallest</strong> one.</p>
<p><strong>Note:</strong></p>
<ul>
<li>The <strong>absolute difference</strong> of two numbers is the absolute value of their difference.</li>
<li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided (<strong>integer division</strong>) by <code>n</code>.</li>
<li>The average of <code>0</code> elements is considered to be <code>0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,5,3,9,5,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
- The average difference of index 0 is: |2 / 1 - (5 + 3 + 9 + 5 + 3) / 5| = |2 / 1 - 25 / 5| = |2 - 5| = 3.
- The average difference of index 1 is: |(2 + 5) / 2 - (3 + 9 + 5 + 3) / 4| = |7 / 2 - 20 / 4| = |3 - 5| = 2.
- The average difference of index 2 is: |(2 + 5 + 3) / 3 - (9 + 5 + 3) / 3| = |10 / 3 - 17 / 3| = |3 - 5| = 2.
- The average difference of index 3 is: |(2 + 5 + 3 + 9) / 4 - (5 + 3) / 2| = |19 / 4 - 8 / 2| = |4 - 4| = 0.
- The average difference of index 4 is: |(2 + 5 + 3 + 9 + 5) / 5 - 3 / 1| = |24 / 5 - 3 / 1| = |4 - 3| = 1.
- The average difference of index 5 is: |(2 + 5 + 3 + 9 + 5 + 3) / 6 - 0| = |27 / 6 - 0| = |4 - 0| = 4.
The average difference of index 3 is the minimum average difference so return 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
The only index is 0 so return 0.
The average difference of index 0 is: |0 / 1 - 0| = |0 - 0| = 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minimumAverageDifference(vector<int>& nums) {\n int n=nums.size(); int index=0,min_num=INT_MAX;;\n long long pre_arr[n],suf_arr[n]; long long ans[n];\n long long sum=0;\n for(int i=0;i<n;i++){\n sum+=nums[i];\n pre_arr[i]=sum;\n }\n sum=0;\n for(int i=n-1;i>=0;i--){\n sum+=nums[i];\n suf_arr[i]=sum;\n } \n int value=0;\n for(int i=0;i<n;i++){\n if(i==n-1) value=abs(pre_arr[i]/n);\n else value=abs((pre_arr[i]/(i+1))-(suf_arr[i+1]/(n-i-1)));\n ans[i]=value;\n if(ans[i]<min_num){\n min_num=ans[i];\n index=i;\n }}\n for(int i=0;i<n;i++)cout<<ans[i]<<\" \";\n return index;\n }\n};",
"memory": "83600"
} |
2,342 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>average difference</strong> of the index <code>i</code> is the <strong>absolute</strong> <strong>difference</strong> between the average of the <strong>first</strong> <code>i + 1</code> elements of <code>nums</code> and the average of the <strong>last</strong> <code>n - i - 1</code> elements. Both averages should be <strong>rounded down</strong> to the nearest integer.</p>
<p>Return<em> the index with the <strong>minimum average difference</strong></em>. If there are multiple such indices, return the <strong>smallest</strong> one.</p>
<p><strong>Note:</strong></p>
<ul>
<li>The <strong>absolute difference</strong> of two numbers is the absolute value of their difference.</li>
<li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided (<strong>integer division</strong>) by <code>n</code>.</li>
<li>The average of <code>0</code> elements is considered to be <code>0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,5,3,9,5,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
- The average difference of index 0 is: |2 / 1 - (5 + 3 + 9 + 5 + 3) / 5| = |2 / 1 - 25 / 5| = |2 - 5| = 3.
- The average difference of index 1 is: |(2 + 5) / 2 - (3 + 9 + 5 + 3) / 4| = |7 / 2 - 20 / 4| = |3 - 5| = 2.
- The average difference of index 2 is: |(2 + 5 + 3) / 3 - (9 + 5 + 3) / 3| = |10 / 3 - 17 / 3| = |3 - 5| = 2.
- The average difference of index 3 is: |(2 + 5 + 3 + 9) / 4 - (5 + 3) / 2| = |19 / 4 - 8 / 2| = |4 - 4| = 0.
- The average difference of index 4 is: |(2 + 5 + 3 + 9 + 5) / 5 - 3 / 1| = |24 / 5 - 3 / 1| = |4 - 3| = 1.
- The average difference of index 5 is: |(2 + 5 + 3 + 9 + 5 + 3) / 6 - 0| = |27 / 6 - 0| = |4 - 0| = 4.
The average difference of index 3 is the minimum average difference so return 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
The only index is 0 so return 0.
The average difference of index 0 is: |0 / 1 - 0| = |0 - 0| = 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minimumAverageDifference(vector<int>& nums) {\n long long t=accumulate(nums.begin(),nums.end(),0LL);\n long long cursum=0;\n vector<int> hen(nums.size());\n for(int i=0;i<hen.size();i++){\n cursum+=nums[i];\n long long j=cursum/(i+1);\n if(t!=cursum){\n hen[i]=(abs(cursum/(i+1)-(t-cursum)/((long long)nums.size()-i-1)));\n }\n else{\n hen[i]=j;\n }\n \n }\n int ans=0;\n for(int i=0;i<hen.size();i++){\n if(hen[ans]>hen[i]){\n ans=i;\n }\n }\n return ans;\n\n }\n};",
"memory": "84300"
} |
2,342 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>average difference</strong> of the index <code>i</code> is the <strong>absolute</strong> <strong>difference</strong> between the average of the <strong>first</strong> <code>i + 1</code> elements of <code>nums</code> and the average of the <strong>last</strong> <code>n - i - 1</code> elements. Both averages should be <strong>rounded down</strong> to the nearest integer.</p>
<p>Return<em> the index with the <strong>minimum average difference</strong></em>. If there are multiple such indices, return the <strong>smallest</strong> one.</p>
<p><strong>Note:</strong></p>
<ul>
<li>The <strong>absolute difference</strong> of two numbers is the absolute value of their difference.</li>
<li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided (<strong>integer division</strong>) by <code>n</code>.</li>
<li>The average of <code>0</code> elements is considered to be <code>0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,5,3,9,5,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
- The average difference of index 0 is: |2 / 1 - (5 + 3 + 9 + 5 + 3) / 5| = |2 / 1 - 25 / 5| = |2 - 5| = 3.
- The average difference of index 1 is: |(2 + 5) / 2 - (3 + 9 + 5 + 3) / 4| = |7 / 2 - 20 / 4| = |3 - 5| = 2.
- The average difference of index 2 is: |(2 + 5 + 3) / 3 - (9 + 5 + 3) / 3| = |10 / 3 - 17 / 3| = |3 - 5| = 2.
- The average difference of index 3 is: |(2 + 5 + 3 + 9) / 4 - (5 + 3) / 2| = |19 / 4 - 8 / 2| = |4 - 4| = 0.
- The average difference of index 4 is: |(2 + 5 + 3 + 9 + 5) / 5 - 3 / 1| = |24 / 5 - 3 / 1| = |4 - 3| = 1.
- The average difference of index 5 is: |(2 + 5 + 3 + 9 + 5 + 3) / 6 - 0| = |27 / 6 - 0| = |4 - 0| = 4.
The average difference of index 3 is the minimum average difference so return 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
The only index is 0 so return 0.
The average difference of index 0 is: |0 / 1 - 0| = |0 - 0| = 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minimumAverageDifference(vector<int>& nums) {\n long long int sum = 0; long long int preSum = 0;\n long long int n = nums.size();\n if(n == 1){\n return 0;\n }\n vector<int> ans(n);\n for(int i=0;i<n;i++){\n sum = sum + nums[i];\n }\n for(int i=0;i<n-1;i++){\n preSum = preSum + nums[i];\n ans[i] = abs( (preSum/(i+1) ) - (sum - preSum)/(n - (i+1)) );\n }\n preSum = preSum + nums[n-1];\n ans[n-1] = preSum/n;\n\n long long int a1 = LLONG_MAX;\n int index = 0;\n long long int temp = ans[0];\n for(int i=1;i<n;i++){\n cout<<ans[i]<<\" \";\n if(ans[i] < temp){\n index = i;\n temp = ans[i];\n }\n }\n\n return index;\n }\n};",
"memory": "84400"
} |
2,342 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>average difference</strong> of the index <code>i</code> is the <strong>absolute</strong> <strong>difference</strong> between the average of the <strong>first</strong> <code>i + 1</code> elements of <code>nums</code> and the average of the <strong>last</strong> <code>n - i - 1</code> elements. Both averages should be <strong>rounded down</strong> to the nearest integer.</p>
<p>Return<em> the index with the <strong>minimum average difference</strong></em>. If there are multiple such indices, return the <strong>smallest</strong> one.</p>
<p><strong>Note:</strong></p>
<ul>
<li>The <strong>absolute difference</strong> of two numbers is the absolute value of their difference.</li>
<li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided (<strong>integer division</strong>) by <code>n</code>.</li>
<li>The average of <code>0</code> elements is considered to be <code>0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,5,3,9,5,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
- The average difference of index 0 is: |2 / 1 - (5 + 3 + 9 + 5 + 3) / 5| = |2 / 1 - 25 / 5| = |2 - 5| = 3.
- The average difference of index 1 is: |(2 + 5) / 2 - (3 + 9 + 5 + 3) / 4| = |7 / 2 - 20 / 4| = |3 - 5| = 2.
- The average difference of index 2 is: |(2 + 5 + 3) / 3 - (9 + 5 + 3) / 3| = |10 / 3 - 17 / 3| = |3 - 5| = 2.
- The average difference of index 3 is: |(2 + 5 + 3 + 9) / 4 - (5 + 3) / 2| = |19 / 4 - 8 / 2| = |4 - 4| = 0.
- The average difference of index 4 is: |(2 + 5 + 3 + 9 + 5) / 5 - 3 / 1| = |24 / 5 - 3 / 1| = |4 - 3| = 1.
- The average difference of index 5 is: |(2 + 5 + 3 + 9 + 5 + 3) / 6 - 0| = |27 / 6 - 0| = |4 - 0| = 4.
The average difference of index 3 is the minimum average difference so return 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
The only index is 0 so return 0.
The average difference of index 0 is: |0 / 1 - 0| = |0 - 0| = 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n#define ll long long \n int minimumAverageDifference(vector<int>& nums) {\n int n=nums.size();\n int ans=INT_MAX;\n int idx=0;\n vector<ll>presum(n+2,0);\n presum[0]=nums[0];\n for(int i=1;i<n;i++) presum[i]=presum[i-1]+nums[i];\n for(int i=0;i<n;i++){\n int lavg=presum[i]/(i+1);\n int ravg=i<n-1?(presum[n-1]-presum[i])/(n-i-1):0;\n // ans=min(ans,abs(ravg-lavg));}\n if(abs(ravg-lavg)<ans) {\n ans=abs(ravg-lavg);\n idx=i;\n }\n }\n return idx;\n }\n};",
"memory": "87100"
} |
2,342 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>average difference</strong> of the index <code>i</code> is the <strong>absolute</strong> <strong>difference</strong> between the average of the <strong>first</strong> <code>i + 1</code> elements of <code>nums</code> and the average of the <strong>last</strong> <code>n - i - 1</code> elements. Both averages should be <strong>rounded down</strong> to the nearest integer.</p>
<p>Return<em> the index with the <strong>minimum average difference</strong></em>. If there are multiple such indices, return the <strong>smallest</strong> one.</p>
<p><strong>Note:</strong></p>
<ul>
<li>The <strong>absolute difference</strong> of two numbers is the absolute value of their difference.</li>
<li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided (<strong>integer division</strong>) by <code>n</code>.</li>
<li>The average of <code>0</code> elements is considered to be <code>0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,5,3,9,5,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
- The average difference of index 0 is: |2 / 1 - (5 + 3 + 9 + 5 + 3) / 5| = |2 / 1 - 25 / 5| = |2 - 5| = 3.
- The average difference of index 1 is: |(2 + 5) / 2 - (3 + 9 + 5 + 3) / 4| = |7 / 2 - 20 / 4| = |3 - 5| = 2.
- The average difference of index 2 is: |(2 + 5 + 3) / 3 - (9 + 5 + 3) / 3| = |10 / 3 - 17 / 3| = |3 - 5| = 2.
- The average difference of index 3 is: |(2 + 5 + 3 + 9) / 4 - (5 + 3) / 2| = |19 / 4 - 8 / 2| = |4 - 4| = 0.
- The average difference of index 4 is: |(2 + 5 + 3 + 9 + 5) / 5 - 3 / 1| = |24 / 5 - 3 / 1| = |4 - 3| = 1.
- The average difference of index 5 is: |(2 + 5 + 3 + 9 + 5 + 3) / 6 - 0| = |27 / 6 - 0| = |4 - 0| = 4.
The average difference of index 3 is the minimum average difference so return 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
The only index is 0 so return 0.
The average difference of index 0 is: |0 / 1 - 0| = |0 - 0| = 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minimumAverageDifference(vector<int>& nums) {\n long long mini=INT_MAX,ans=0;\n long long n=nums.size();\n vector<long long>pre(n,0);\n for(long long i=0;i<n;i++){\n pre[i]=(i>0?pre[i-1]:0)+nums[i];\n }\n for(long long i=0;i<n;i++){\n long long temp=abs(pre[i]/(i+1)-(pre[n-1]-pre[i])/max(1LL,(n-i-1)));\n if(temp<mini){\n mini=temp;\n ans=i;\n }\n }\n return ans;\n }\n};",
"memory": "87100"
} |
2,342 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>average difference</strong> of the index <code>i</code> is the <strong>absolute</strong> <strong>difference</strong> between the average of the <strong>first</strong> <code>i + 1</code> elements of <code>nums</code> and the average of the <strong>last</strong> <code>n - i - 1</code> elements. Both averages should be <strong>rounded down</strong> to the nearest integer.</p>
<p>Return<em> the index with the <strong>minimum average difference</strong></em>. If there are multiple such indices, return the <strong>smallest</strong> one.</p>
<p><strong>Note:</strong></p>
<ul>
<li>The <strong>absolute difference</strong> of two numbers is the absolute value of their difference.</li>
<li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided (<strong>integer division</strong>) by <code>n</code>.</li>
<li>The average of <code>0</code> elements is considered to be <code>0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,5,3,9,5,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
- The average difference of index 0 is: |2 / 1 - (5 + 3 + 9 + 5 + 3) / 5| = |2 / 1 - 25 / 5| = |2 - 5| = 3.
- The average difference of index 1 is: |(2 + 5) / 2 - (3 + 9 + 5 + 3) / 4| = |7 / 2 - 20 / 4| = |3 - 5| = 2.
- The average difference of index 2 is: |(2 + 5 + 3) / 3 - (9 + 5 + 3) / 3| = |10 / 3 - 17 / 3| = |3 - 5| = 2.
- The average difference of index 3 is: |(2 + 5 + 3 + 9) / 4 - (5 + 3) / 2| = |19 / 4 - 8 / 2| = |4 - 4| = 0.
- The average difference of index 4 is: |(2 + 5 + 3 + 9 + 5) / 5 - 3 / 1| = |24 / 5 - 3 / 1| = |4 - 3| = 1.
- The average difference of index 5 is: |(2 + 5 + 3 + 9 + 5 + 3) / 6 - 0| = |27 / 6 - 0| = |4 - 0| = 4.
The average difference of index 3 is the minimum average difference so return 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
The only index is 0 so return 0.
The average difference of index 0 is: |0 / 1 - 0| = |0 - 0| = 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minimumAverageDifference(vector<int>& nums) {\n int n = nums.size();\n int idx =-1;\n int avg = INT_MAX;\n long long l_sum=0 , total=0;\n vector<long long > left_sum(n) ;\n\n for(int i=0;i<n;i++)\n {\n total += nums[i];\n }\n\n for(int i=0;i<n;i++)\n {\n l_sum += nums[i];\n long long r_sum = total - l_sum;\n int l_avg = l_sum/(i+1);\n int r_avg = (i == n - 1) ? 0 : (r_sum / (n - i - 1)); // Right average, handle last element\n\n int diff = abs(l_avg - r_avg);\n if(diff < avg)\n {\n avg =diff ;\n idx =i;\n }\n }\n return idx;\n }\n};",
"memory": "87200"
} |
2,342 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>average difference</strong> of the index <code>i</code> is the <strong>absolute</strong> <strong>difference</strong> between the average of the <strong>first</strong> <code>i + 1</code> elements of <code>nums</code> and the average of the <strong>last</strong> <code>n - i - 1</code> elements. Both averages should be <strong>rounded down</strong> to the nearest integer.</p>
<p>Return<em> the index with the <strong>minimum average difference</strong></em>. If there are multiple such indices, return the <strong>smallest</strong> one.</p>
<p><strong>Note:</strong></p>
<ul>
<li>The <strong>absolute difference</strong> of two numbers is the absolute value of their difference.</li>
<li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided (<strong>integer division</strong>) by <code>n</code>.</li>
<li>The average of <code>0</code> elements is considered to be <code>0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,5,3,9,5,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
- The average difference of index 0 is: |2 / 1 - (5 + 3 + 9 + 5 + 3) / 5| = |2 / 1 - 25 / 5| = |2 - 5| = 3.
- The average difference of index 1 is: |(2 + 5) / 2 - (3 + 9 + 5 + 3) / 4| = |7 / 2 - 20 / 4| = |3 - 5| = 2.
- The average difference of index 2 is: |(2 + 5 + 3) / 3 - (9 + 5 + 3) / 3| = |10 / 3 - 17 / 3| = |3 - 5| = 2.
- The average difference of index 3 is: |(2 + 5 + 3 + 9) / 4 - (5 + 3) / 2| = |19 / 4 - 8 / 2| = |4 - 4| = 0.
- The average difference of index 4 is: |(2 + 5 + 3 + 9 + 5) / 5 - 3 / 1| = |24 / 5 - 3 / 1| = |4 - 3| = 1.
- The average difference of index 5 is: |(2 + 5 + 3 + 9 + 5 + 3) / 6 - 0| = |27 / 6 - 0| = |4 - 0| = 4.
The average difference of index 3 is the minimum average difference so return 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
The only index is 0 so return 0.
The average difference of index 0 is: |0 / 1 - 0| = |0 - 0| = 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minimumAverageDifference(vector<int>& nums) {\n long long totsum=nums[0];\n vector<long long> temp(nums.size());\n temp[0]=nums[0];\n\n for(int i=1;i<nums.size();i++){\n totsum+=nums[i];\n temp[i] = temp[i-1] + nums[i];\n }\n int index=0;\n int mini=INT_MAX;\n for(int i=0;i<temp.size()-1;i++){\n int diff=(temp[i]/(i+1)-(totsum-temp[i])/(temp.size()-i-1));\n if(diff<0){\n diff=diff*(-1);\n }\n cout<<diff<<\" \";\n if(diff<mini){\n mini=diff;\n index=i;\n }\n }\n int diff=totsum/(temp.size());\n if(diff<mini){\n mini=diff;\n index=temp.size()-1;\n }\n return index;\n }\n};",
"memory": "87300"
} |
2,342 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>average difference</strong> of the index <code>i</code> is the <strong>absolute</strong> <strong>difference</strong> between the average of the <strong>first</strong> <code>i + 1</code> elements of <code>nums</code> and the average of the <strong>last</strong> <code>n - i - 1</code> elements. Both averages should be <strong>rounded down</strong> to the nearest integer.</p>
<p>Return<em> the index with the <strong>minimum average difference</strong></em>. If there are multiple such indices, return the <strong>smallest</strong> one.</p>
<p><strong>Note:</strong></p>
<ul>
<li>The <strong>absolute difference</strong> of two numbers is the absolute value of their difference.</li>
<li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided (<strong>integer division</strong>) by <code>n</code>.</li>
<li>The average of <code>0</code> elements is considered to be <code>0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,5,3,9,5,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
- The average difference of index 0 is: |2 / 1 - (5 + 3 + 9 + 5 + 3) / 5| = |2 / 1 - 25 / 5| = |2 - 5| = 3.
- The average difference of index 1 is: |(2 + 5) / 2 - (3 + 9 + 5 + 3) / 4| = |7 / 2 - 20 / 4| = |3 - 5| = 2.
- The average difference of index 2 is: |(2 + 5 + 3) / 3 - (9 + 5 + 3) / 3| = |10 / 3 - 17 / 3| = |3 - 5| = 2.
- The average difference of index 3 is: |(2 + 5 + 3 + 9) / 4 - (5 + 3) / 2| = |19 / 4 - 8 / 2| = |4 - 4| = 0.
- The average difference of index 4 is: |(2 + 5 + 3 + 9 + 5) / 5 - 3 / 1| = |24 / 5 - 3 / 1| = |4 - 3| = 1.
- The average difference of index 5 is: |(2 + 5 + 3 + 9 + 5 + 3) / 6 - 0| = |27 / 6 - 0| = |4 - 0| = 4.
The average difference of index 3 is the minimum average difference so return 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
The only index is 0 so return 0.
The average difference of index 0 is: |0 / 1 - 0| = |0 - 0| = 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minimumAverageDifference(vector<int>& nums) {\n int n=nums.size(),ans=INT_MAX,index=-1;\n vector<long long> prefix(n,0);\n prefix[0]=nums[0];\n for(int i=1;i<n;i++)\n prefix[i]=prefix[i-1]+nums[i];\n for(int i=0;i<n;i++)\n {\n int diff=INT_MAX;\n if(i==n-1)\n diff=abs(prefix[i]/(i+1));\n else\n diff=abs((prefix[i]/(i+1))-(prefix[n-1]-prefix[i])/(n-i-1));\n if(diff<ans)\n {\n ans=min(ans,diff);\n index=i;\n }\n }\n return index;\n }\n};",
"memory": "87400"
} |
2,342 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>average difference</strong> of the index <code>i</code> is the <strong>absolute</strong> <strong>difference</strong> between the average of the <strong>first</strong> <code>i + 1</code> elements of <code>nums</code> and the average of the <strong>last</strong> <code>n - i - 1</code> elements. Both averages should be <strong>rounded down</strong> to the nearest integer.</p>
<p>Return<em> the index with the <strong>minimum average difference</strong></em>. If there are multiple such indices, return the <strong>smallest</strong> one.</p>
<p><strong>Note:</strong></p>
<ul>
<li>The <strong>absolute difference</strong> of two numbers is the absolute value of their difference.</li>
<li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided (<strong>integer division</strong>) by <code>n</code>.</li>
<li>The average of <code>0</code> elements is considered to be <code>0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,5,3,9,5,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
- The average difference of index 0 is: |2 / 1 - (5 + 3 + 9 + 5 + 3) / 5| = |2 / 1 - 25 / 5| = |2 - 5| = 3.
- The average difference of index 1 is: |(2 + 5) / 2 - (3 + 9 + 5 + 3) / 4| = |7 / 2 - 20 / 4| = |3 - 5| = 2.
- The average difference of index 2 is: |(2 + 5 + 3) / 3 - (9 + 5 + 3) / 3| = |10 / 3 - 17 / 3| = |3 - 5| = 2.
- The average difference of index 3 is: |(2 + 5 + 3 + 9) / 4 - (5 + 3) / 2| = |19 / 4 - 8 / 2| = |4 - 4| = 0.
- The average difference of index 4 is: |(2 + 5 + 3 + 9 + 5) / 5 - 3 / 1| = |24 / 5 - 3 / 1| = |4 - 3| = 1.
- The average difference of index 5 is: |(2 + 5 + 3 + 9 + 5 + 3) / 6 - 0| = |27 / 6 - 0| = |4 - 0| = 4.
The average difference of index 3 is the minimum average difference so return 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
The only index is 0 so return 0.
The average difference of index 0 is: |0 / 1 - 0| = |0 - 0| = 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumAverageDifference(vector<int>& vec) {\n int len = vec.size();\n vector< long long >nums( len , -1);\n nums[0] = vec[0];\n for(int i=1 ; i<len ; i++) {\n nums[i] = vec[i] + nums[i-1];\n // cout << nums[i] << \" \";\n }\n // cout << endl ;\n long long res = LONG_MAX;\n int idx = 0;\n for(int i=0 ; i<len ; i++) {\n int prev = nums[i]/(i+1) ;\n if( i == len-1 ){\n if( prev < res ){\n idx = i;\n }\n break;\n }\n int curr = (nums[len-1] - nums[i])/( len-i-1 );\n // cout << prev/(i+1) << \" \" << curr/(len-i-1) <<endl;\n if( res > abs( prev - curr ) ) {\n res = abs( prev - curr );\n idx = i;\n // cout << res << \" \" << i << endl;\n }\n }\n\n return idx;\n }\n};",
"memory": "87500"
} |
2,342 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>average difference</strong> of the index <code>i</code> is the <strong>absolute</strong> <strong>difference</strong> between the average of the <strong>first</strong> <code>i + 1</code> elements of <code>nums</code> and the average of the <strong>last</strong> <code>n - i - 1</code> elements. Both averages should be <strong>rounded down</strong> to the nearest integer.</p>
<p>Return<em> the index with the <strong>minimum average difference</strong></em>. If there are multiple such indices, return the <strong>smallest</strong> one.</p>
<p><strong>Note:</strong></p>
<ul>
<li>The <strong>absolute difference</strong> of two numbers is the absolute value of their difference.</li>
<li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided (<strong>integer division</strong>) by <code>n</code>.</li>
<li>The average of <code>0</code> elements is considered to be <code>0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,5,3,9,5,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
- The average difference of index 0 is: |2 / 1 - (5 + 3 + 9 + 5 + 3) / 5| = |2 / 1 - 25 / 5| = |2 - 5| = 3.
- The average difference of index 1 is: |(2 + 5) / 2 - (3 + 9 + 5 + 3) / 4| = |7 / 2 - 20 / 4| = |3 - 5| = 2.
- The average difference of index 2 is: |(2 + 5 + 3) / 3 - (9 + 5 + 3) / 3| = |10 / 3 - 17 / 3| = |3 - 5| = 2.
- The average difference of index 3 is: |(2 + 5 + 3 + 9) / 4 - (5 + 3) / 2| = |19 / 4 - 8 / 2| = |4 - 4| = 0.
- The average difference of index 4 is: |(2 + 5 + 3 + 9 + 5) / 5 - 3 / 1| = |24 / 5 - 3 / 1| = |4 - 3| = 1.
- The average difference of index 5 is: |(2 + 5 + 3 + 9 + 5 + 3) / 6 - 0| = |27 / 6 - 0| = |4 - 0| = 4.
The average difference of index 3 is the minimum average difference so return 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
The only index is 0 so return 0.
The average difference of index 0 is: |0 / 1 - 0| = |0 - 0| = 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumAverageDifference(vector<int>& nums) {\n const int SIZE = nums.size();\n if(SIZE == 1) return 0;\n\n vector<int> avg_right(SIZE);\n vector<int> avg_left(SIZE);\n\n long long sum = 0;\n for(int i = 0; i < SIZE; i++) {\n sum += nums[i];\n avg_left[i] = sum / (i+1);\n }\n\n for(int i = 0; i < SIZE; i++) {\n avg_right[i] = sum / (SIZE-i);\n sum -= nums[i];\n }\n\n int avg_dif = 0;\n int min_avg_dif = INT_MAX;\n int min_dif_i = 0;\n\n for(int i = 0; i < SIZE-1; i++) {\n avg_dif = abs(avg_left[i] - avg_right[i+1]);\n if(avg_dif < min_avg_dif) {\n min_avg_dif = avg_dif;\n min_dif_i = i;\n }\n }\n\n if(avg_left[SIZE-1] < min_avg_dif) {\n min_dif_i = SIZE-1;\n }\n\n return min_dif_i;\n }\n};",
"memory": "87600"
} |
2,342 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>average difference</strong> of the index <code>i</code> is the <strong>absolute</strong> <strong>difference</strong> between the average of the <strong>first</strong> <code>i + 1</code> elements of <code>nums</code> and the average of the <strong>last</strong> <code>n - i - 1</code> elements. Both averages should be <strong>rounded down</strong> to the nearest integer.</p>
<p>Return<em> the index with the <strong>minimum average difference</strong></em>. If there are multiple such indices, return the <strong>smallest</strong> one.</p>
<p><strong>Note:</strong></p>
<ul>
<li>The <strong>absolute difference</strong> of two numbers is the absolute value of their difference.</li>
<li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided (<strong>integer division</strong>) by <code>n</code>.</li>
<li>The average of <code>0</code> elements is considered to be <code>0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,5,3,9,5,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
- The average difference of index 0 is: |2 / 1 - (5 + 3 + 9 + 5 + 3) / 5| = |2 / 1 - 25 / 5| = |2 - 5| = 3.
- The average difference of index 1 is: |(2 + 5) / 2 - (3 + 9 + 5 + 3) / 4| = |7 / 2 - 20 / 4| = |3 - 5| = 2.
- The average difference of index 2 is: |(2 + 5 + 3) / 3 - (9 + 5 + 3) / 3| = |10 / 3 - 17 / 3| = |3 - 5| = 2.
- The average difference of index 3 is: |(2 + 5 + 3 + 9) / 4 - (5 + 3) / 2| = |19 / 4 - 8 / 2| = |4 - 4| = 0.
- The average difference of index 4 is: |(2 + 5 + 3 + 9 + 5) / 5 - 3 / 1| = |24 / 5 - 3 / 1| = |4 - 3| = 1.
- The average difference of index 5 is: |(2 + 5 + 3 + 9 + 5 + 3) / 6 - 0| = |27 / 6 - 0| = |4 - 0| = 4.
The average difference of index 3 is the minimum average difference so return 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
The only index is 0 so return 0.
The average difference of index 0 is: |0 / 1 - 0| = |0 - 0| = 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nlong long sum_right =0 ; //GLOBAL VARIABLE\nlong long sum_left = 0;\n\n\n\n//******************************************* Time Limit Exceeded **********************************************************************\n// long long mean(vector<int>& nums, int i)\n// {\n// int n = nums.size();\n// if(i == n) return 0;\n// //int start = i;\n// long long sum = 0;\n// for(int start =i ; start<nums.size(); start++)\n// {\n// sum += nums[start];\n// }\n// return sum / (n-i);\n\n// }\n\n// long long mean_left(vector<int>& nums, int i)\n// {\n// long long sum = 0;\n// for(int x =0; x<i; x++)\n// {\n// sum += nums[x];\n// }\n// return sum/i; \n// }\n \n int minimumAverageDifference(vector<int>& nums) {\n // int n = nums.size()-1; long long ans = LLONG_MAX , indx = -1;\n // for(int i=0; i<nums.size(); i++)\n // {\n \n // long long right = mean(nums,i+1);\n // long long left = mean_left(nums, i+1);\n // if(abs(right-left) <ans)\n // {\n // ans = abs(right-left);\n // indx = i;\n // }\n\n // } \n // return indx;\n \n vector <int> v;\n \n int n = nums.size(); int ans = INT_MAX , idx =-1; long long right =-1;long long left=-1;\n for(int i = 0; i<nums.size(); i++)\n {\n sum_right += nums[i];\n }\n \n for(int i =0; i<nums.size(); i++)\n {\n sum_right -= nums[i];\n if((n-(i+1)) ==0) \n right = 0;\n else \n right = sum_right / (n-(i+1));\n \n sum_left += nums[i]; \n left = sum_left/(i+1);\n \n v.push_back(abs(right - left));\n\n if(abs(right - left) < ans)\n {\n ans = abs(right - left);\n idx = i;\n }\n\n\n\n }\n\n\n for(int i =0; i<v.size(); i++)\n cout<<v[i]<<\" \";\n \nreturn idx;\n }\n};",
"memory": "88600"
} |
2,342 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>average difference</strong> of the index <code>i</code> is the <strong>absolute</strong> <strong>difference</strong> between the average of the <strong>first</strong> <code>i + 1</code> elements of <code>nums</code> and the average of the <strong>last</strong> <code>n - i - 1</code> elements. Both averages should be <strong>rounded down</strong> to the nearest integer.</p>
<p>Return<em> the index with the <strong>minimum average difference</strong></em>. If there are multiple such indices, return the <strong>smallest</strong> one.</p>
<p><strong>Note:</strong></p>
<ul>
<li>The <strong>absolute difference</strong> of two numbers is the absolute value of their difference.</li>
<li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided (<strong>integer division</strong>) by <code>n</code>.</li>
<li>The average of <code>0</code> elements is considered to be <code>0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,5,3,9,5,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
- The average difference of index 0 is: |2 / 1 - (5 + 3 + 9 + 5 + 3) / 5| = |2 / 1 - 25 / 5| = |2 - 5| = 3.
- The average difference of index 1 is: |(2 + 5) / 2 - (3 + 9 + 5 + 3) / 4| = |7 / 2 - 20 / 4| = |3 - 5| = 2.
- The average difference of index 2 is: |(2 + 5 + 3) / 3 - (9 + 5 + 3) / 3| = |10 / 3 - 17 / 3| = |3 - 5| = 2.
- The average difference of index 3 is: |(2 + 5 + 3 + 9) / 4 - (5 + 3) / 2| = |19 / 4 - 8 / 2| = |4 - 4| = 0.
- The average difference of index 4 is: |(2 + 5 + 3 + 9 + 5) / 5 - 3 / 1| = |24 / 5 - 3 / 1| = |4 - 3| = 1.
- The average difference of index 5 is: |(2 + 5 + 3 + 9 + 5 + 3) / 6 - 0| = |27 / 6 - 0| = |4 - 0| = 4.
The average difference of index 3 is the minimum average difference so return 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
The only index is 0 so return 0.
The average difference of index 0 is: |0 / 1 - 0| = |0 - 0| = 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumAverageDifference(vector<int>& nums) {\n long long sum=0;\n for(auto num:nums)sum+=num;\n long long n1=0;\n vector<int>result;\n for(int i=0;i<nums.size();i++)\n {\n n1+=nums[i];sum-=nums[i];\n long long n2=n1/(i+1);\n long long n3;\n (nums.size()-i-1==0)?n3=0:n3=sum/(nums.size()-i-1);\n result.push_back(abs(n2-n3));\n }\n int ind=0;\n for(int i=0;i<result.size();i++)\n {\n if(result[i]<result[ind])ind=i;\n }\n\n return ind;\n }\n};",
"memory": "88700"
} |
2,342 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>average difference</strong> of the index <code>i</code> is the <strong>absolute</strong> <strong>difference</strong> between the average of the <strong>first</strong> <code>i + 1</code> elements of <code>nums</code> and the average of the <strong>last</strong> <code>n - i - 1</code> elements. Both averages should be <strong>rounded down</strong> to the nearest integer.</p>
<p>Return<em> the index with the <strong>minimum average difference</strong></em>. If there are multiple such indices, return the <strong>smallest</strong> one.</p>
<p><strong>Note:</strong></p>
<ul>
<li>The <strong>absolute difference</strong> of two numbers is the absolute value of their difference.</li>
<li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided (<strong>integer division</strong>) by <code>n</code>.</li>
<li>The average of <code>0</code> elements is considered to be <code>0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,5,3,9,5,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
- The average difference of index 0 is: |2 / 1 - (5 + 3 + 9 + 5 + 3) / 5| = |2 / 1 - 25 / 5| = |2 - 5| = 3.
- The average difference of index 1 is: |(2 + 5) / 2 - (3 + 9 + 5 + 3) / 4| = |7 / 2 - 20 / 4| = |3 - 5| = 2.
- The average difference of index 2 is: |(2 + 5 + 3) / 3 - (9 + 5 + 3) / 3| = |10 / 3 - 17 / 3| = |3 - 5| = 2.
- The average difference of index 3 is: |(2 + 5 + 3 + 9) / 4 - (5 + 3) / 2| = |19 / 4 - 8 / 2| = |4 - 4| = 0.
- The average difference of index 4 is: |(2 + 5 + 3 + 9 + 5) / 5 - 3 / 1| = |24 / 5 - 3 / 1| = |4 - 3| = 1.
- The average difference of index 5 is: |(2 + 5 + 3 + 9 + 5 + 3) / 6 - 0| = |27 / 6 - 0| = |4 - 0| = 4.
The average difference of index 3 is the minimum average difference so return 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
The only index is 0 so return 0.
The average difference of index 0 is: |0 / 1 - 0| = |0 - 0| = 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumAverageDifference(vector<int>& nums) {\n int l=nums.size();\n vector<long long> sums(l); long long sum=0,i=0;\n for(long long q: nums)\n {\n sum+=q;\n sums[i++]=sum;\n }\n vector<int> absdiff(l);long long d1,d2; long long minn=sums[l-1];int mini=l;\n for(int j=0;j<l;j++)\n {\n d1=(sums[j])/(j+1);\n if(j==l-1) d2=0;\n else\n d2=(sums[l-1]-sums[j])/(l-(j+1));\n absdiff[j]=abs(d1-d2);\n if(minn>absdiff[j])\n {\n minn=absdiff[j];\n }\n }\n for(int k=0;k<l;k++){\n if(absdiff[k]==minn)\n {mini=k; break;}\n }\n return mini;\n }\n};",
"memory": "90500"
} |
2,342 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>average difference</strong> of the index <code>i</code> is the <strong>absolute</strong> <strong>difference</strong> between the average of the <strong>first</strong> <code>i + 1</code> elements of <code>nums</code> and the average of the <strong>last</strong> <code>n - i - 1</code> elements. Both averages should be <strong>rounded down</strong> to the nearest integer.</p>
<p>Return<em> the index with the <strong>minimum average difference</strong></em>. If there are multiple such indices, return the <strong>smallest</strong> one.</p>
<p><strong>Note:</strong></p>
<ul>
<li>The <strong>absolute difference</strong> of two numbers is the absolute value of their difference.</li>
<li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided (<strong>integer division</strong>) by <code>n</code>.</li>
<li>The average of <code>0</code> elements is considered to be <code>0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,5,3,9,5,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
- The average difference of index 0 is: |2 / 1 - (5 + 3 + 9 + 5 + 3) / 5| = |2 / 1 - 25 / 5| = |2 - 5| = 3.
- The average difference of index 1 is: |(2 + 5) / 2 - (3 + 9 + 5 + 3) / 4| = |7 / 2 - 20 / 4| = |3 - 5| = 2.
- The average difference of index 2 is: |(2 + 5 + 3) / 3 - (9 + 5 + 3) / 3| = |10 / 3 - 17 / 3| = |3 - 5| = 2.
- The average difference of index 3 is: |(2 + 5 + 3 + 9) / 4 - (5 + 3) / 2| = |19 / 4 - 8 / 2| = |4 - 4| = 0.
- The average difference of index 4 is: |(2 + 5 + 3 + 9 + 5) / 5 - 3 / 1| = |24 / 5 - 3 / 1| = |4 - 3| = 1.
- The average difference of index 5 is: |(2 + 5 + 3 + 9 + 5 + 3) / 6 - 0| = |27 / 6 - 0| = |4 - 0| = 4.
The average difference of index 3 is the minimum average difference so return 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
The only index is 0 so return 0.
The average difference of index 0 is: |0 / 1 - 0| = |0 - 0| = 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumAverageDifference(vector<int>& nums) {\n int n = nums.size();\n vector<int> abs(n);\n vector<long long> preSum(n);\n preSum[0] = nums[0];\n for (int i = 1; i < n; i++) {\n preSum[i] = preSum[i-1] + nums[i];\n }\n int mn=INT_MAX,mnind=-1;\n for (int i = 0; i < n; i++) {\n int diff;\n long long lavg,ravg;\n lavg = preSum[i] / (i+1);\n ravg = (n-i-1)==0?0:(preSum[n-1] - preSum[i]) / (n-i-1);\n diff = lavg - ravg;\n abs[i] = diff<0? -1*diff: diff;\n if(mn>abs[i]) mnind = i;\n mn = min(mn,abs[i]);\n }\n return mnind;\n\n }\n};",
"memory": "90700"
} |
2,342 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>average difference</strong> of the index <code>i</code> is the <strong>absolute</strong> <strong>difference</strong> between the average of the <strong>first</strong> <code>i + 1</code> elements of <code>nums</code> and the average of the <strong>last</strong> <code>n - i - 1</code> elements. Both averages should be <strong>rounded down</strong> to the nearest integer.</p>
<p>Return<em> the index with the <strong>minimum average difference</strong></em>. If there are multiple such indices, return the <strong>smallest</strong> one.</p>
<p><strong>Note:</strong></p>
<ul>
<li>The <strong>absolute difference</strong> of two numbers is the absolute value of their difference.</li>
<li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided (<strong>integer division</strong>) by <code>n</code>.</li>
<li>The average of <code>0</code> elements is considered to be <code>0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,5,3,9,5,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
- The average difference of index 0 is: |2 / 1 - (5 + 3 + 9 + 5 + 3) / 5| = |2 / 1 - 25 / 5| = |2 - 5| = 3.
- The average difference of index 1 is: |(2 + 5) / 2 - (3 + 9 + 5 + 3) / 4| = |7 / 2 - 20 / 4| = |3 - 5| = 2.
- The average difference of index 2 is: |(2 + 5 + 3) / 3 - (9 + 5 + 3) / 3| = |10 / 3 - 17 / 3| = |3 - 5| = 2.
- The average difference of index 3 is: |(2 + 5 + 3 + 9) / 4 - (5 + 3) / 2| = |19 / 4 - 8 / 2| = |4 - 4| = 0.
- The average difference of index 4 is: |(2 + 5 + 3 + 9 + 5) / 5 - 3 / 1| = |24 / 5 - 3 / 1| = |4 - 3| = 1.
- The average difference of index 5 is: |(2 + 5 + 3 + 9 + 5 + 3) / 6 - 0| = |27 / 6 - 0| = |4 - 0| = 4.
The average difference of index 3 is the minimum average difference so return 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
The only index is 0 so return 0.
The average difference of index 0 is: |0 / 1 - 0| = |0 - 0| = 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumAverageDifference(vector<int>& nums) {\n int n = nums.size();\n vector<int> absd(n);\n vector<long long> preSum(n);\n preSum[0] = nums[0];\n for (int i = 1; i < n; i++) {\n preSum[i] = preSum[i - 1] + nums[i];\n }\n int mn = INT_MAX, mnind = -1;\n for (int i = 0; i < n; i++) {\n\n long long lavg, ravg;\n lavg = preSum[i] / (i + 1);\n ravg = (i == n - 1) ? 0 : (preSum[n - 1] - preSum[i]) / (n - i - 1);\n\n absd[i] = abs(lavg - ravg);\n if (mn > absd[i])\n mnind = i;\n mn = min(mn, absd[i]);\n }\n return mnind;\n }\n};",
"memory": "90700"
} |
2,342 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>average difference</strong> of the index <code>i</code> is the <strong>absolute</strong> <strong>difference</strong> between the average of the <strong>first</strong> <code>i + 1</code> elements of <code>nums</code> and the average of the <strong>last</strong> <code>n - i - 1</code> elements. Both averages should be <strong>rounded down</strong> to the nearest integer.</p>
<p>Return<em> the index with the <strong>minimum average difference</strong></em>. If there are multiple such indices, return the <strong>smallest</strong> one.</p>
<p><strong>Note:</strong></p>
<ul>
<li>The <strong>absolute difference</strong> of two numbers is the absolute value of their difference.</li>
<li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided (<strong>integer division</strong>) by <code>n</code>.</li>
<li>The average of <code>0</code> elements is considered to be <code>0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,5,3,9,5,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
- The average difference of index 0 is: |2 / 1 - (5 + 3 + 9 + 5 + 3) / 5| = |2 / 1 - 25 / 5| = |2 - 5| = 3.
- The average difference of index 1 is: |(2 + 5) / 2 - (3 + 9 + 5 + 3) / 4| = |7 / 2 - 20 / 4| = |3 - 5| = 2.
- The average difference of index 2 is: |(2 + 5 + 3) / 3 - (9 + 5 + 3) / 3| = |10 / 3 - 17 / 3| = |3 - 5| = 2.
- The average difference of index 3 is: |(2 + 5 + 3 + 9) / 4 - (5 + 3) / 2| = |19 / 4 - 8 / 2| = |4 - 4| = 0.
- The average difference of index 4 is: |(2 + 5 + 3 + 9 + 5) / 5 - 3 / 1| = |24 / 5 - 3 / 1| = |4 - 3| = 1.
- The average difference of index 5 is: |(2 + 5 + 3 + 9 + 5 + 3) / 6 - 0| = |27 / 6 - 0| = |4 - 0| = 4.
The average difference of index 3 is the minimum average difference so return 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
The only index is 0 so return 0.
The average difference of index 0 is: |0 / 1 - 0| = |0 - 0| = 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumAverageDifference(vector<int>& nums) {\n int n=nums.size();\n vector<long long>prefixsum(n);\n prefixsum[0]=nums[0];\n for(int i=1;i<n;i++){\n prefixsum[i]=prefixsum[i-1]+nums[i];\n }vector<long long>suffixsum(n);\n suffixsum[n-1]=nums[n-1];\n for(int i=n-2;i>=0;i--){\n suffixsum[i]=suffixsum[i+1]+nums[i];\n }int mindiff=INT_MAX;\n int minindex=-1;\n for(int i=0;i<n;i++){\n int firstcount=i+1;\n long long sumoffirst=prefixsum[i];\n int avg1=sumoffirst/firstcount;\n int secondcount=n-i-1;\n long long sumofsecond=(i<n-1)?suffixsum[i+1]:0;\n int avg2=(secondcount>0)?sumofsecond/secondcount:0;\n int diff=abs(avg1-avg2);\n if(diff<mindiff){\n mindiff=diff;\n minindex=i;\n }\n\n\n }return minindex;\n \n }\n};",
"memory": "93400"
} |
2,342 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>average difference</strong> of the index <code>i</code> is the <strong>absolute</strong> <strong>difference</strong> between the average of the <strong>first</strong> <code>i + 1</code> elements of <code>nums</code> and the average of the <strong>last</strong> <code>n - i - 1</code> elements. Both averages should be <strong>rounded down</strong> to the nearest integer.</p>
<p>Return<em> the index with the <strong>minimum average difference</strong></em>. If there are multiple such indices, return the <strong>smallest</strong> one.</p>
<p><strong>Note:</strong></p>
<ul>
<li>The <strong>absolute difference</strong> of two numbers is the absolute value of their difference.</li>
<li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided (<strong>integer division</strong>) by <code>n</code>.</li>
<li>The average of <code>0</code> elements is considered to be <code>0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,5,3,9,5,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
- The average difference of index 0 is: |2 / 1 - (5 + 3 + 9 + 5 + 3) / 5| = |2 / 1 - 25 / 5| = |2 - 5| = 3.
- The average difference of index 1 is: |(2 + 5) / 2 - (3 + 9 + 5 + 3) / 4| = |7 / 2 - 20 / 4| = |3 - 5| = 2.
- The average difference of index 2 is: |(2 + 5 + 3) / 3 - (9 + 5 + 3) / 3| = |10 / 3 - 17 / 3| = |3 - 5| = 2.
- The average difference of index 3 is: |(2 + 5 + 3 + 9) / 4 - (5 + 3) / 2| = |19 / 4 - 8 / 2| = |4 - 4| = 0.
- The average difference of index 4 is: |(2 + 5 + 3 + 9 + 5) / 5 - 3 / 1| = |24 / 5 - 3 / 1| = |4 - 3| = 1.
- The average difference of index 5 is: |(2 + 5 + 3 + 9 + 5 + 3) / 6 - 0| = |27 / 6 - 0| = |4 - 0| = 4.
The average difference of index 3 is the minimum average difference so return 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
The only index is 0 so return 0.
The average difference of index 0 is: |0 / 1 - 0| = |0 - 0| = 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumAverageDifference(vector<int>& nums) {\n long long mini = INT_MAX;\n int res = 0;\n int n = nums.size();\n vector<long long>prefix(n,nums[0]);\n vector<long long>suffix(n,nums[n-1]);\n for(int i=1;i<n;i++){\n prefix[i] = prefix[i-1]+nums[i];\n }\n for(int i = n-2;i>=0;i--){\n suffix[i] = suffix[i+1]+nums[i];\n }\n for(int i=0;i<n;i++){\n long long presum = prefix[i]/(i+1);\n long long sufsum = (i == n - 1) ? 0 : suffix[i + 1] / (n - i - 1);\n if(mini>abs(presum-sufsum)){\n mini = abs(presum - sufsum);\n res = i;\n }\n }\n return res;\n }\n};",
"memory": "93500"
} |
2,342 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>average difference</strong> of the index <code>i</code> is the <strong>absolute</strong> <strong>difference</strong> between the average of the <strong>first</strong> <code>i + 1</code> elements of <code>nums</code> and the average of the <strong>last</strong> <code>n - i - 1</code> elements. Both averages should be <strong>rounded down</strong> to the nearest integer.</p>
<p>Return<em> the index with the <strong>minimum average difference</strong></em>. If there are multiple such indices, return the <strong>smallest</strong> one.</p>
<p><strong>Note:</strong></p>
<ul>
<li>The <strong>absolute difference</strong> of two numbers is the absolute value of their difference.</li>
<li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided (<strong>integer division</strong>) by <code>n</code>.</li>
<li>The average of <code>0</code> elements is considered to be <code>0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,5,3,9,5,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
- The average difference of index 0 is: |2 / 1 - (5 + 3 + 9 + 5 + 3) / 5| = |2 / 1 - 25 / 5| = |2 - 5| = 3.
- The average difference of index 1 is: |(2 + 5) / 2 - (3 + 9 + 5 + 3) / 4| = |7 / 2 - 20 / 4| = |3 - 5| = 2.
- The average difference of index 2 is: |(2 + 5 + 3) / 3 - (9 + 5 + 3) / 3| = |10 / 3 - 17 / 3| = |3 - 5| = 2.
- The average difference of index 3 is: |(2 + 5 + 3 + 9) / 4 - (5 + 3) / 2| = |19 / 4 - 8 / 2| = |4 - 4| = 0.
- The average difference of index 4 is: |(2 + 5 + 3 + 9 + 5) / 5 - 3 / 1| = |24 / 5 - 3 / 1| = |4 - 3| = 1.
- The average difference of index 5 is: |(2 + 5 + 3 + 9 + 5 + 3) / 6 - 0| = |27 / 6 - 0| = |4 - 0| = 4.
The average difference of index 3 is the minimum average difference so return 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
The only index is 0 so return 0.
The average difference of index 0 is: |0 / 1 - 0| = |0 - 0| = 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumAverageDifference(vector<int>& nums) {\n int n = nums.size();\n int idx =-1;\n int avg = INT_MAX;\n long long l_sum=0 , r_sum=0 , total=0;\n vector<long long > left_sum(n) , right_sum(n);\n\n for(int i=0;i<n;i++)\n {\n l_sum += nums[i];\n r_sum += nums[n-1-i];\n\n left_sum[i]= l_sum;\n right_sum[n-1-i] = r_sum;\n }\n total = left_sum[n-1];\n\n for(int i=0;i<n;i++)\n {\n int l_avg = left_sum[i]/(i+1);\n int r_avg = (i == n - 1) ? 0 : ((right_sum[i]-nums[i]) / (n - i - 1)); // Right average, handle last element\n\n int diff = abs(l_avg - r_avg);\n if(diff < avg)\n {\n avg =diff ;\n idx =i;\n }\n }\n return idx;\n }\n};",
"memory": "93600"
} |
2,342 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>average difference</strong> of the index <code>i</code> is the <strong>absolute</strong> <strong>difference</strong> between the average of the <strong>first</strong> <code>i + 1</code> elements of <code>nums</code> and the average of the <strong>last</strong> <code>n - i - 1</code> elements. Both averages should be <strong>rounded down</strong> to the nearest integer.</p>
<p>Return<em> the index with the <strong>minimum average difference</strong></em>. If there are multiple such indices, return the <strong>smallest</strong> one.</p>
<p><strong>Note:</strong></p>
<ul>
<li>The <strong>absolute difference</strong> of two numbers is the absolute value of their difference.</li>
<li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided (<strong>integer division</strong>) by <code>n</code>.</li>
<li>The average of <code>0</code> elements is considered to be <code>0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,5,3,9,5,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
- The average difference of index 0 is: |2 / 1 - (5 + 3 + 9 + 5 + 3) / 5| = |2 / 1 - 25 / 5| = |2 - 5| = 3.
- The average difference of index 1 is: |(2 + 5) / 2 - (3 + 9 + 5 + 3) / 4| = |7 / 2 - 20 / 4| = |3 - 5| = 2.
- The average difference of index 2 is: |(2 + 5 + 3) / 3 - (9 + 5 + 3) / 3| = |10 / 3 - 17 / 3| = |3 - 5| = 2.
- The average difference of index 3 is: |(2 + 5 + 3 + 9) / 4 - (5 + 3) / 2| = |19 / 4 - 8 / 2| = |4 - 4| = 0.
- The average difference of index 4 is: |(2 + 5 + 3 + 9 + 5) / 5 - 3 / 1| = |24 / 5 - 3 / 1| = |4 - 3| = 1.
- The average difference of index 5 is: |(2 + 5 + 3 + 9 + 5 + 3) / 6 - 0| = |27 / 6 - 0| = |4 - 0| = 4.
The average difference of index 3 is the minimum average difference so return 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
The only index is 0 so return 0.
The average difference of index 0 is: |0 / 1 - 0| = |0 - 0| = 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumAverageDifference(vector<int>& nums) {\n int n = nums.size();\n vector<long long> suffix(n, 0);\n vector<long long> prefix(n, 0);\n int minIndex = 0;\n long long minDifference = LLONG_MAX;\n \n \n if (n == 1) {\n return 0; \n }\n \n \n prefix[0] = nums[0];\n for (int i = 1; i < n; i++) {\n prefix[i] = prefix[i - 1] + nums[i];\n }\n \n \n for (int i = n - 2; i >= 0; i--) {\n suffix[i] = suffix[i + 1] + nums[i + 1];\n }\n\n \n for (int i = 0; i < n; i++) {\n long long leftAvg = prefix[i] / (i + 1); \n long long rightAvg = (i == n - 1) ? 0 : suffix[i] / (n - i - 1); \n \n long long avgDifference = abs(leftAvg - rightAvg);\n \n \n if (avgDifference < minDifference) {\n minDifference = avgDifference;\n minIndex = i;\n }\n }\n\n return minIndex;\n }\n};\n",
"memory": "93600"
} |
2,342 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>average difference</strong> of the index <code>i</code> is the <strong>absolute</strong> <strong>difference</strong> between the average of the <strong>first</strong> <code>i + 1</code> elements of <code>nums</code> and the average of the <strong>last</strong> <code>n - i - 1</code> elements. Both averages should be <strong>rounded down</strong> to the nearest integer.</p>
<p>Return<em> the index with the <strong>minimum average difference</strong></em>. If there are multiple such indices, return the <strong>smallest</strong> one.</p>
<p><strong>Note:</strong></p>
<ul>
<li>The <strong>absolute difference</strong> of two numbers is the absolute value of their difference.</li>
<li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided (<strong>integer division</strong>) by <code>n</code>.</li>
<li>The average of <code>0</code> elements is considered to be <code>0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,5,3,9,5,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
- The average difference of index 0 is: |2 / 1 - (5 + 3 + 9 + 5 + 3) / 5| = |2 / 1 - 25 / 5| = |2 - 5| = 3.
- The average difference of index 1 is: |(2 + 5) / 2 - (3 + 9 + 5 + 3) / 4| = |7 / 2 - 20 / 4| = |3 - 5| = 2.
- The average difference of index 2 is: |(2 + 5 + 3) / 3 - (9 + 5 + 3) / 3| = |10 / 3 - 17 / 3| = |3 - 5| = 2.
- The average difference of index 3 is: |(2 + 5 + 3 + 9) / 4 - (5 + 3) / 2| = |19 / 4 - 8 / 2| = |4 - 4| = 0.
- The average difference of index 4 is: |(2 + 5 + 3 + 9 + 5) / 5 - 3 / 1| = |24 / 5 - 3 / 1| = |4 - 3| = 1.
- The average difference of index 5 is: |(2 + 5 + 3 + 9 + 5 + 3) / 6 - 0| = |27 / 6 - 0| = |4 - 0| = 4.
The average difference of index 3 is the minimum average difference so return 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
The only index is 0 so return 0.
The average difference of index 0 is: |0 / 1 - 0| = |0 - 0| = 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumAverageDifference(vector<int>& nums) {\n\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n\n int n=nums.size();\n\n if(n==1) return 0;\n\n vector<long long >prefix(n);\n vector<long long >sufix(n);\n\n prefix[0]=nums[0];\n\n for(int i=1;i<n;++i)\n prefix[i]=prefix[i-1]+nums[i];\n\n sufix[n-1]=nums[n-1];\n\n for(int i=n-2;i>=0;--i)\n sufix[i]=sufix[i+1]+nums[i]; \n\n int index=0;\n long long mindiff=INT_MAX;\n\n for(int i=0;i<n;++i)\n {\n long long left=prefix[i];\n long long right=(i<n-1 ? sufix[i+1]:0);\n long l=i+1;\n long r=n-l;\n long long diff=abs(left/l -right/(r==0?1:r));\n if(diff<mindiff){\n mindiff=diff;\n index=i;\n }\n\n } \n\n return index;\n }\n};",
"memory": "93700"
} |
2,342 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>average difference</strong> of the index <code>i</code> is the <strong>absolute</strong> <strong>difference</strong> between the average of the <strong>first</strong> <code>i + 1</code> elements of <code>nums</code> and the average of the <strong>last</strong> <code>n - i - 1</code> elements. Both averages should be <strong>rounded down</strong> to the nearest integer.</p>
<p>Return<em> the index with the <strong>minimum average difference</strong></em>. If there are multiple such indices, return the <strong>smallest</strong> one.</p>
<p><strong>Note:</strong></p>
<ul>
<li>The <strong>absolute difference</strong> of two numbers is the absolute value of their difference.</li>
<li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided (<strong>integer division</strong>) by <code>n</code>.</li>
<li>The average of <code>0</code> elements is considered to be <code>0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,5,3,9,5,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
- The average difference of index 0 is: |2 / 1 - (5 + 3 + 9 + 5 + 3) / 5| = |2 / 1 - 25 / 5| = |2 - 5| = 3.
- The average difference of index 1 is: |(2 + 5) / 2 - (3 + 9 + 5 + 3) / 4| = |7 / 2 - 20 / 4| = |3 - 5| = 2.
- The average difference of index 2 is: |(2 + 5 + 3) / 3 - (9 + 5 + 3) / 3| = |10 / 3 - 17 / 3| = |3 - 5| = 2.
- The average difference of index 3 is: |(2 + 5 + 3 + 9) / 4 - (5 + 3) / 2| = |19 / 4 - 8 / 2| = |4 - 4| = 0.
- The average difference of index 4 is: |(2 + 5 + 3 + 9 + 5) / 5 - 3 / 1| = |24 / 5 - 3 / 1| = |4 - 3| = 1.
- The average difference of index 5 is: |(2 + 5 + 3 + 9 + 5 + 3) / 6 - 0| = |27 / 6 - 0| = |4 - 0| = 4.
The average difference of index 3 is the minimum average difference so return 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
The only index is 0 so return 0.
The average difference of index 0 is: |0 / 1 - 0| = |0 - 0| = 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumAverageDifference(vector<int>& nums) {\n int n = nums.size();\n vector<long long> preSum;\n long long sum =0;\n for(auto it:nums){\n sum += it;\n preSum.push_back(sum);\n }\n\n int ans = INT_MAX;\n int idx = -1;\n for(int i = 0;i<nums.size();i++){\n long long left = preSum[i]/(i+1);\n long long right = (n-i-1) == 0? 0:(sum-preSum[i])/(n-i-1);\n\n int temp = abs(right-left);\n if(temp < ans){\n ans = temp;\n idx = i;\n }\n }\n\n return idx;\n }\n};",
"memory": "95800"
} |
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>""</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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> num = "6<strong><u>777</u></strong>133339"
<strong>Output:</strong> "777"
<strong>Explanation:</strong> There are two distinct good integers: "777" and "333".
"777" is the largest, so we return "777".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> num = "23<strong><u>000</u></strong>19"
<strong>Output:</strong> "000"
<strong>Explanation:</strong> "000" is the only good integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> num = "42352338"
<strong>Output:</strong> ""
<strong>Explanation:</strong> No substring of length 3 consists of only one unique digit. Therefore, there are no good integers.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= num.length <= 1000</code></li>
<li><code>num</code> only consists of digits.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n string largestGoodInteger(string num) {\n int n=num.length();\n int val=-1;\n for(int i=0;i<=n-3;i++){\n if(num[i]==num[i+1]&& num[i]==num[i+2]){\n val=max(val,(num[i]-'0'));\n }\n }\n if(val==-1){\n return \"\";\n }\n return string(3,val+'0');\n }\n};",
"memory": "7700"
} |
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>""</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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> num = "6<strong><u>777</u></strong>133339"
<strong>Output:</strong> "777"
<strong>Explanation:</strong> There are two distinct good integers: "777" and "333".
"777" is the largest, so we return "777".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> num = "23<strong><u>000</u></strong>19"
<strong>Output:</strong> "000"
<strong>Explanation:</strong> "000" is the only good integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> num = "42352338"
<strong>Output:</strong> ""
<strong>Explanation:</strong> No substring of length 3 consists of only one unique digit. Therefore, there are no good integers.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= num.length <= 1000</code></li>
<li><code>num</code> only consists of digits.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n string largestGoodInteger(string num) {\n int temp=INT_MIN;\n string res;\n int n=num.size();\n for(int i=0;i<n-2;i++){\n int j=i+1;\n int k=i+2;\n if(num[i]==num[j] && num[i]==num[k]){\n int x=num[i];\n temp= max(x,temp);\n }\n }\n if(temp!=INT_MIN){\n res+=temp;\n res+=temp;\n res+=temp;\n return res;\n }\n return res;\n }\n};",
"memory": "7800"
} |
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>""</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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> num = "6<strong><u>777</u></strong>133339"
<strong>Output:</strong> "777"
<strong>Explanation:</strong> There are two distinct good integers: "777" and "333".
"777" is the largest, so we return "777".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> num = "23<strong><u>000</u></strong>19"
<strong>Output:</strong> "000"
<strong>Explanation:</strong> "000" is the only good integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> num = "42352338"
<strong>Output:</strong> ""
<strong>Explanation:</strong> No substring of length 3 consists of only one unique digit. Therefore, there are no good integers.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= num.length <= 1000</code></li>
<li><code>num</code> only consists of digits.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n string largestGoodInteger(string num) {\n int nSize = num.length();\n int nMaxNum = -1;\n for (int i = 0; i < nSize-2; i++) {\n if (num[i] == num[i + 1] && num[i + 1] == num[i + 2]) {\n nMaxNum = max(nMaxNum, num[i] -'0');\n }\n }\n string sResult = \"\";\n if (-1 == nMaxNum) return sResult;\n for (int i = 0; i < 3; i++)\n sResult += '0' + nMaxNum;\n return sResult;\n }\n};",
"memory": "7900"
} |
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>""</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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> num = "6<strong><u>777</u></strong>133339"
<strong>Output:</strong> "777"
<strong>Explanation:</strong> There are two distinct good integers: "777" and "333".
"777" is the largest, so we return "777".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> num = "23<strong><u>000</u></strong>19"
<strong>Output:</strong> "000"
<strong>Explanation:</strong> "000" is the only good integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> num = "42352338"
<strong>Output:</strong> ""
<strong>Explanation:</strong> No substring of length 3 consists of only one unique digit. Therefore, there are no good integers.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= num.length <= 1000</code></li>
<li><code>num</code> only consists of digits.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n string largestGoodInteger(string num) {\n int mx = -1;\n for(int i = 2; i < num.size(); i++){\n if(num[i] == num[i - 1] && num[i] == num[i - 2]){\n if(mx < (num[i] - '0'))mx = (num[i] - '0');\n }\n }\n string str = \"\";\n if(mx < 0)return str;\n str += ('0' + mx);\n str += ('0' + mx);\n str += ('0' + mx);\n return str;\n }\n};",
"memory": "8000"
} |
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>""</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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> num = "6<strong><u>777</u></strong>133339"
<strong>Output:</strong> "777"
<strong>Explanation:</strong> There are two distinct good integers: "777" and "333".
"777" is the largest, so we return "777".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> num = "23<strong><u>000</u></strong>19"
<strong>Output:</strong> "000"
<strong>Explanation:</strong> "000" is the only good integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> num = "42352338"
<strong>Output:</strong> ""
<strong>Explanation:</strong> No substring of length 3 consists of only one unique digit. Therefore, there are no good integers.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= num.length <= 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 m=-3;\n for(int i=0;i<num.length()-2;i++)\n {\n if(num[i]==num[i+1] && num[i]==num[i+2])\n {\n if(m < int(num[i])-'0')\n m=int(num[i])-'0';\n\n i+=2;\n }\n }\n if(m<0)\n return \"\";\n string s=to_string(m)+to_string(m)+to_string(m);\n return s;\n }\n};",
"memory": "8400"
} |
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>""</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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> num = "6<strong><u>777</u></strong>133339"
<strong>Output:</strong> "777"
<strong>Explanation:</strong> There are two distinct good integers: "777" and "333".
"777" is the largest, so we return "777".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> num = "23<strong><u>000</u></strong>19"
<strong>Output:</strong> "000"
<strong>Explanation:</strong> "000" is the only good integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> num = "42352338"
<strong>Output:</strong> ""
<strong>Explanation:</strong> No substring of length 3 consists of only one unique digit. Therefore, there are no good integers.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= num.length <= 1000</code></li>
<li><code>num</code> only consists of digits.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n string largestGoodInteger(string num) {\n vector<string> v = {\"999\", \"888\", \"777\", \"666\", \"555\", \"444\", \"333\", \"222\", \"111\", \"000\"};\n for(string s: v){\n if(num.find(s) != string::npos){\n return s;\n }\n }\n return \"\";\n }\n};",
"memory": "8500"
} |
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>""</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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> num = "6<strong><u>777</u></strong>133339"
<strong>Output:</strong> "777"
<strong>Explanation:</strong> There are two distinct good integers: "777" and "333".
"777" is the largest, so we return "777".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> num = "23<strong><u>000</u></strong>19"
<strong>Output:</strong> "000"
<strong>Explanation:</strong> "000" is the only good integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> num = "42352338"
<strong>Output:</strong> ""
<strong>Explanation:</strong> No substring of length 3 consists of only one unique digit. Therefore, there are no good integers.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= num.length <= 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.size();\n vector<int> prefix(n,1);\n\n string ans = \"\";\n\n for(int i = 1; i < n; i++){\n if(num[i] == num[i-1])\n prefix[i] = 1 + prefix[i - 1];\n }\n int maxi = -1;\n for(int i = 0; i < n; i++){\n if(prefix[i] == 3)\n maxi = max(maxi, num[i] - '0');\n }\n\n if(maxi == -1)\n return ans;\n\n for(int i = 0; i < 3; i++)\n ans += to_string(maxi);\n \n return ans;\n \n }\n};",
"memory": "8600"
} |
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>""</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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> num = "6<strong><u>777</u></strong>133339"
<strong>Output:</strong> "777"
<strong>Explanation:</strong> There are two distinct good integers: "777" and "333".
"777" is the largest, so we return "777".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> num = "23<strong><u>000</u></strong>19"
<strong>Output:</strong> "000"
<strong>Explanation:</strong> "000" is the only good integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> num = "42352338"
<strong>Output:</strong> ""
<strong>Explanation:</strong> No substring of length 3 consists of only one unique digit. Therefore, there are no good integers.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= num.length <= 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.size();\n string ans = \"\";\n for (int i = 0; i <= n - 3; i++) {\n if (num[i] == num[i + 1] && num[i] == num[i + 2]) {\n string triplet = num.substr(i, 3);\n if (triplet > ans) {\n ans = triplet;\n }\n }\n } \n return ans;\n }\n};",
"memory": "8700"
} |
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>""</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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> num = "6<strong><u>777</u></strong>133339"
<strong>Output:</strong> "777"
<strong>Explanation:</strong> There are two distinct good integers: "777" and "333".
"777" is the largest, so we return "777".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> num = "23<strong><u>000</u></strong>19"
<strong>Output:</strong> "000"
<strong>Explanation:</strong> "000" is the only good integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> num = "42352338"
<strong>Output:</strong> ""
<strong>Explanation:</strong> No substring of length 3 consists of only one unique digit. Therefore, there are no good integers.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= num.length <= 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 k = 3;\n string ans;\n\n for (int i = 0; i <= num.size() - k; i++) {\n string cand;\n for (int j = i; j < i + 3; j++) {\n cand += num[j];\n }\n if (count(cand.begin(), cand.end(), cand.front()) == cand.size()) {\n ans = max(ans, cand);\n }\n }\n\n return ans;\n }\n};",
"memory": "8800"
} |
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>""</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> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> num = "6<strong><u>777</u></strong>133339"
<strong>Output:</strong> "777"
<strong>Explanation:</strong> There are two distinct good integers: "777" and "333".
"777" is the largest, so we return "777".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> num = "23<strong><u>000</u></strong>19"
<strong>Output:</strong> "000"
<strong>Explanation:</strong> "000" is the only good integer.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> num = "42352338"
<strong>Output:</strong> ""
<strong>Explanation:</strong> No substring of length 3 consists of only one unique digit. Therefore, there are no good integers.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= num.length <= 1000</code></li>
<li><code>num</code> only consists of digits.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n string largestGoodInteger(string nums) {\n string result = \"\";\n \n for (int i = 0; i < nums.length(); i++) {\n if (nums[i] == nums[i + 1]) {\n if (nums[i + 1] == nums[i + 2]) {\n string triplet =nums.substr(i, 3); // Extract the triplet\n if(result<triplet){\n result = triplet;\n }\n }\n }\n }\n return result;\n }\n};",
"memory": "8800"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.