id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
2,487
<p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p> <p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p> <p>Note that each character should belong to exactly one substring in a partition.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abacaba&quot; <strong>Output:</strong> 4 <strong>Explanation:</strong> Two possible partitions are (&quot;a&quot;,&quot;ba&quot;,&quot;cab&quot;,&quot;a&quot;) and (&quot;ab&quot;,&quot;a&quot;,&quot;ca&quot;,&quot;ba&quot;). It can be shown that 4 is the minimum number of substrings needed. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;ssssss&quot; <strong>Output:</strong> 6 <strong>Explanation: </strong>The only valid partition is (&quot;s&quot;,&quot;s&quot;,&quot;s&quot;,&quot;s&quot;,&quot;s&quot;,&quot;s&quot;). </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only English lowercase letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int partitionString(string s) {\n ios_base::sync_with_stdio(false);\n cin.tie(0);\n int count=1;\n int l=0;\n set<char> myset;\n while(l<s.size()){\n int x=myset.size();\n myset.insert(s[l]);\n int y=myset.size();\n if(x==y){\n count++;\n myset={};\n myset.insert(s[l]);\n }\n l++; \n \n }\n return count++;\n }\n};", "memory": "68883" }
2,487
<p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p> <p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p> <p>Note that each character should belong to exactly one substring in a partition.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abacaba&quot; <strong>Output:</strong> 4 <strong>Explanation:</strong> Two possible partitions are (&quot;a&quot;,&quot;ba&quot;,&quot;cab&quot;,&quot;a&quot;) and (&quot;ab&quot;,&quot;a&quot;,&quot;ca&quot;,&quot;ba&quot;). It can be shown that 4 is the minimum number of substrings needed. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;ssssss&quot; <strong>Output:</strong> 6 <strong>Explanation: </strong>The only valid partition is (&quot;s&quot;,&quot;s&quot;,&quot;s&quot;,&quot;s&quot;,&quot;s&quot;,&quot;s&quot;). </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only English lowercase letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int partitionString(string s) {\n \n int cnt = 0;\n int n = s.size();\n if(n <= 1) {\n return n;\n }\n map<char, bool> mp;\n mp[s[0]] = 1;\n \n for(int i =0; i < n; i++) {\n\n if(mp.count(s[i]) == 0) {\n mp[s[i]] = 1;\n }\n else {\n cnt++;\n mp.clear();\n mp[s[i]] = 1;\n }\n }\n return cnt;\n }\n};", "memory": "69696" }
2,487
<p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p> <p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p> <p>Note that each character should belong to exactly one substring in a partition.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abacaba&quot; <strong>Output:</strong> 4 <strong>Explanation:</strong> Two possible partitions are (&quot;a&quot;,&quot;ba&quot;,&quot;cab&quot;,&quot;a&quot;) and (&quot;ab&quot;,&quot;a&quot;,&quot;ca&quot;,&quot;ba&quot;). It can be shown that 4 is the minimum number of substrings needed. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;ssssss&quot; <strong>Output:</strong> 6 <strong>Explanation: </strong>The only valid partition is (&quot;s&quot;,&quot;s&quot;,&quot;s&quot;,&quot;s&quot;,&quot;s&quot;,&quot;s&quot;). </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only English lowercase letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int partitionString(string s) {\n int ans = 0;\n map<char, int> m;\n for (char x: s) {\n if (m.find(x) != m.end()) {\n //found repeated element;\n m.clear();\n m[x]++;\n ans++;\n } else {\n m[x]++;\n }\n }\n return ans+1;\n }\n};", "memory": "70510" }
2,487
<p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p> <p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p> <p>Note that each character should belong to exactly one substring in a partition.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abacaba&quot; <strong>Output:</strong> 4 <strong>Explanation:</strong> Two possible partitions are (&quot;a&quot;,&quot;ba&quot;,&quot;cab&quot;,&quot;a&quot;) and (&quot;ab&quot;,&quot;a&quot;,&quot;ca&quot;,&quot;ba&quot;). It can be shown that 4 is the minimum number of substrings needed. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;ssssss&quot; <strong>Output:</strong> 6 <strong>Explanation: </strong>The only valid partition is (&quot;s&quot;,&quot;s&quot;,&quot;s&quot;,&quot;s&quot;,&quot;s&quot;,&quot;s&quot;). </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only English lowercase letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int partitionString(string s) {\n map<int, int> freq;\n int count = 1;\n\n for (int i = 0; i < s.size(); ++i) {\n if (freq.find(s[i]-'a') != freq.end()) { //if find same letter\n count++;\n freq.clear();\n }\n freq[s[i]-'a']++;\n }\n return count;\n }\n};", "memory": "71324" }
2,487
<p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p> <p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p> <p>Note that each character should belong to exactly one substring in a partition.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abacaba&quot; <strong>Output:</strong> 4 <strong>Explanation:</strong> Two possible partitions are (&quot;a&quot;,&quot;ba&quot;,&quot;cab&quot;,&quot;a&quot;) and (&quot;ab&quot;,&quot;a&quot;,&quot;ca&quot;,&quot;ba&quot;). It can be shown that 4 is the minimum number of substrings needed. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;ssssss&quot; <strong>Output:</strong> 6 <strong>Explanation: </strong>The only valid partition is (&quot;s&quot;,&quot;s&quot;,&quot;s&quot;,&quot;s&quot;,&quot;s&quot;,&quot;s&quot;). </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only English lowercase letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int partitionString(string s) {\n int size = s.size();\n int ans = 1 ; \n map<char,int>mp;\n for (int i=0;i<size;i++) {\n if(mp.find(s[i])!=mp.end()) {\n ans++;\n mp.clear();\n }\n mp[s[i]]++;\n }\n return ans;\n }\n};", "memory": "71324" }
2,487
<p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p> <p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p> <p>Note that each character should belong to exactly one substring in a partition.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abacaba&quot; <strong>Output:</strong> 4 <strong>Explanation:</strong> Two possible partitions are (&quot;a&quot;,&quot;ba&quot;,&quot;cab&quot;,&quot;a&quot;) and (&quot;ab&quot;,&quot;a&quot;,&quot;ca&quot;,&quot;ba&quot;). It can be shown that 4 is the minimum number of substrings needed. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;ssssss&quot; <strong>Output:</strong> 6 <strong>Explanation: </strong>The only valid partition is (&quot;s&quot;,&quot;s&quot;,&quot;s&quot;,&quot;s&quot;,&quot;s&quot;,&quot;s&quot;). </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only English lowercase letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int partitionString(string s) {\n map<char,int>mp;\n int count = 1;\n for(int i = 0;i<s.length();i++) {\n if(mp[s[i]]>0) {\n mp.clear();\n count++;\n }\n mp[s[i]]++;\n }\n return count;\n }\n};", "memory": "72138" }
2,487
<p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p> <p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p> <p>Note that each character should belong to exactly one substring in a partition.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abacaba&quot; <strong>Output:</strong> 4 <strong>Explanation:</strong> Two possible partitions are (&quot;a&quot;,&quot;ba&quot;,&quot;cab&quot;,&quot;a&quot;) and (&quot;ab&quot;,&quot;a&quot;,&quot;ca&quot;,&quot;ba&quot;). It can be shown that 4 is the minimum number of substrings needed. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;ssssss&quot; <strong>Output:</strong> 6 <strong>Explanation: </strong>The only valid partition is (&quot;s&quot;,&quot;s&quot;,&quot;s&quot;,&quot;s&quot;,&quot;s&quot;,&quot;s&quot;). </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only English lowercase letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int partitionString(string s) {\n map<char, int> m;\n int ans = 1;\n for (auto it : s) {\n if (m[it]) {\n ans++;\n m.clear();\n m[it]++;\n } else {\n m[it]++;\n }\n }\n return ans;\n }\n};", "memory": "72138" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int clothest(vector<int>& v, int& n) {\n int uidx = upper_bound(v.begin(), v.end(), n) - v.begin();\n int lidx = lower_bound(v.begin(), v.end(), n) - v.begin();\n\n if (uidx == v.size()) uidx--;\n if (lidx == v.size()) lidx--;\n\n int ans = min(abs(n - v[uidx]), abs(n - v[lidx]));\n if (uidx > 0) ans = min(ans, abs(n - v[uidx - 1]));\n if (lidx > 0) ans = min(ans, abs(n - v[lidx - 1]));\n if (uidx < v.size() - 1) ans = min(ans, abs(n - v[uidx + 1]));\n if (lidx < v.size() - 1) ans = min(ans, abs(n - v[lidx + 1]));\n\n return ans;\n }\n\n void insertN(vector<int>& v, int& n) {\n v.insert(lower_bound(v.begin(), v.end(), n), n);\n }\n\n void deleteN(vector<int>& v, int& n) {\n v.erase(lower_bound(v.begin(), v.end(), n));\n }\n\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n // priority_queue<int> maxHeap;\n // priority_queue < int, vector<int>, greater<int> > minHeap;\n // unordered_map<int, int> umap;\n\n vector<int> v = {};\n\n int l = 0;\n for (int r = 0; r < nums.size(); r++) {\n if (r > indexDiff) {\n deleteN(v, nums[l]);\n l++;\n }\n \n if (r > 0) {\n int x = clothest(v, nums[r]);\n if (x <= valueDiff) return true;\n }\n\n insertN(v, nums[r]);\n }\n\n // cout << maxHeap.top() << \" \" << minHeap.top();\n // sort(nums.begin(), nums.end());\n // int n = 10;\n // cout << clothest(nums, n);\n return false;\n }\n};", "memory": "93246" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n int n=nums.size();\n int diff=INT_MAX;\n vector<int>arr=nums;\n sort(arr.begin(),arr.end());\n if(valueDiff==0)\n {\n for(int i=0;i<n-1;i++)\n {\n diff=min(diff,abs(arr[i]-arr[i+1]));\n }\n if(diff!=0)\n {\n return false;\n }\n }\n for(int i=0;i<n;i++)\n {\n for(int j=i+1;j<=i+indexDiff&&j<n;j++)\n {\n if(abs(nums[i]-nums[j])<=valueDiff)\n {\n return true;\n }\n \n }\n }\n return false;\n }\n};", "memory": "94305" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "\nclass Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n //sort first indexdiff numbers and store them\n vector<int> sorted;\n for(int i = 0; i < indexDiff; i++) {\n sorted.push_back(nums[i]);\n }\n sort(sorted.begin(), sorted.end());\n //CHECK FOR A PAIR\n printf(\"sorted: \");\n for(int i = 0; i < sorted.size()-1; i++) {\n if(sorted[i+1] - sorted[i] <= valueDiff) {\n return true;\n }\n printf(\"%d \", sorted[i]);\n }\n printf(\"%d\\n\", sorted[sorted.size()-1]);\n\n //SLIDING WINDOW\n for(int i = indexDiff; i < nums.size(); i++) {\n int toRemove = nums[i-indexDiff];\n int toAdd = nums[i];\n //remove toRemove via binary search \n int l = 0, r = indexDiff-1;\n while(l <= r) {\n int mid = l + (r-l)/2;\n if(sorted[mid] <= toAdd) {\n l = mid+1;\n } else {\n r = mid-1;\n }\n }\n sorted.insert(sorted.begin()+l, toAdd);\n if(l>0 && sorted[l] - sorted[l-1] <= valueDiff) {\n return true;\n }\n if(l<indexDiff && sorted[l+1] - sorted[l] <= valueDiff) {\n return true;\n }\n\n //remove toRemove via binary search\n l = 0, r = indexDiff;\n while(l <= r) {\n int mid = l + (r-l)/2;\n if(sorted[mid] == toRemove) {\n sorted.erase(sorted.begin()+mid);\n break;\n } else if(sorted[mid] < toRemove) {\n l = mid+1;\n } else {\n r = mid-1;\n }\n }\n }\n printf(\"returning false\\n\");\n return false;\n }\n};\n", "memory": "95364" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n size_t nums_sz = nums.size();\n if (nums_sz - 1 < indexDiff)\n {\n indexDiff = nums_sz - 1;\n }\n \n vector<int> nums_sorted;\n copy(nums.begin(), nums.begin() + indexDiff + 1, back_inserter(nums_sorted));\n\n size_t nums_sorted_sz = nums_sorted.size();\n\n sort(nums_sorted.begin(), nums_sorted.end());\n\n for (int i = 1; i < nums_sorted_sz; i++)\n {\n if (abs(nums_sorted[i-1] - nums_sorted[i]) <= valueDiff)\n {\n return true;\n }\n }\n\n int to_add_elem;\n int to_del_elem;\n int to_add_elem_id;\n for (int i = indexDiff + 1; i < nums_sz; i++)\n {\n to_add_elem = nums[i];\n to_del_elem = nums[i - indexDiff - 1];\n\n to_add_elem_id = distance(nums_sorted.begin(), lower_bound(nums_sorted.begin(), nums_sorted.end(), to_del_elem));\n nums_sorted[to_add_elem_id] = to_add_elem;\n\n if (to_add_elem > to_del_elem)\n {\n for(int j = to_add_elem_id; j < nums_sorted_sz - 1; j++)\n {\n if (nums_sorted[j + 1] >= nums_sorted[j])\n {\n to_add_elem_id = j;\n break;\n }\n swap(nums_sorted[j + 1], nums_sorted[j]);\n }\n }\n else if (to_add_elem < to_del_elem)\n {\n for(int j = to_add_elem_id; j > 0; j--)\n {\n if (nums_sorted[j - 1] <= nums_sorted[j])\n {\n to_add_elem_id = j;\n break;\n }\n swap(nums_sorted[j - 1], nums_sorted[j]);\n }\n }\n else // if to_add_elem = to_del_elem\n {\n continue;\n }\n\n if (to_add_elem_id == 0)\n {\n if (abs(nums_sorted[0] - nums_sorted[1]) <= valueDiff)\n {\n return true;\n }\n }\n else if (to_add_elem_id == nums_sorted_sz - 1)\n {\n if (abs(nums_sorted[to_add_elem_id - 1] - nums_sorted[to_add_elem_id]) <= valueDiff)\n {\n return true;\n }\n }\n else\n {\n if (abs(nums_sorted[to_add_elem_id - 1] - nums_sorted[to_add_elem_id]) <= valueDiff ||\n abs(nums_sorted[to_add_elem_id + 1] - nums_sorted[to_add_elem_id]) <= valueDiff)\n {\n return true;\n }\n }\n }\n\n return false;\n }\n};", "memory": "96423" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "#ifndef FENWICK_TREE_HPP\n#define FENWICK_TREE_HPP\n#include <vector>\ntemplate <typename T, typename V = std::vector<T>>\nclass FenwickTree {\n // One-based Fenwick tree with a twist (A[0] saves nums[0])\n V A;\n // for all operations T must support 'a += b'\n // for get(), set(), range_sum(), cast to V, T must support 'a -= b'\n // for range_query(), lower_bound(), T must support 'a -= b' and 'a < b'\n public:\n typedef typename V::size_type size_type;\n typedef typename V::value_type value_type;\n private:\n static const size_type LSB(const size_type i) {\n return i & -i;\n }\n\n // Convert A[] in place to Fenwick tree form\n static void init(V &A) {\n for (size_type i = 1; i < A.size(); ++i) {\n auto j = i + LSB(i);\n if (j < A.size())\n A[j] += A[i];\n }\n }\n\n // Convert back to array of per-element counts\n static void fini(V &A) {\n for (size_type i = A.size(); i-- > 1;) {\n auto j = i + LSB(i);\n if (j < A.size())\n A[j] -= A[i];\n }\n }\n public:\n FenwickTree(size_type size = 0):A(size) {\n }\n\n FenwickTree(const V &nums):A(nums) {\n init(A);\n }\n\n FenwickTree(V &&nums):A(std::move(nums)) {\n init(A);\n }\n\n FenwickTree(const FenwickTree &) = default;\n FenwickTree(FenwickTree &&) = default;\n\n FenwickTree &operator = (const V & A) {\n this->A = A;\n init(this->A);\n return *this;\n }\n\n FenwickTree &operator = (V && A) {\n this->A = std::move(A);\n init(this->A);\n return *this;\n }\n\n FenwickTree &operator = (const FenwickTree &) = default; \n FenwickTree &operator = (FenwickTree &&) = default;\n\n operator V () const & {\n V ret = A;\n fini(ret);\n return ret;\n }\n operator V () && {\n fini(A);\n return std::move(A);\n }\n\n // Returns the sum of the first i elements (indices 0 to i)\n // Equivalent to range_sum(0, i)\n T prefix_sum(size_type i) const {\n // assert(i < A.size());\n T sum = A[0];\n for (; i != 0; i -= LSB(i))\n sum += A[i];\n return sum;\n }\n\n // Add delta to element with index i (zero-based)\n void add(size_type i, T delta) {\n // assert(i < A.size());\n if (i == 0) {\n A[0] += delta;\n return;\n }\n for (; i < A.size(); i+= LSB(i))\n A[i] += delta;\n }\n\n // Returns sum of nums[i + 1] to nums[j].\n // Same as prefix_sum(j) - prefix_sum(i), but a bit faster\n T range_sum(size_type i, size_type j) const {\n T sum = 0;\n // assert(0 < i);\n // assert(i < j);\n // assert(j < A.size());\n for (; j > i; j -= LSB(j))\n sum += A[j];\n for (; i > j; i -= LSB(i))\n sum -= A[i];\n return sum;\n }\n constexpr size_type size() const { return A.size(); }\n constexpr bool empty() const { return A.empty(); }\n};\n#endif\nclass Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {\n const unsigned n = nums.size();\n #if 0\n map<int, unsigned> s;\n for (unsigned i = 0; i < n; ++i) {\n if (i > k) {\n auto prev = s.find(nums[i - k - 1]);\n if (--prev->second == 0)\n s.erase(prev);\n }\n auto num = nums[i];\n auto lb = s.lower_bound(num);\n if (lb != s.end() && lb->first - num <= t)\n return true;\n if (lb != s.begin()) {\n auto ub = prev(lb);\n if (num - ub->first <= t)\n return true;\n }\n ++s[num];\n }\n #else\n auto sorted = nums;\n sort(sorted.begin(), sorted.end());\n sorted.erase(unique(sorted.begin(), sorted.end()), sorted.end());\n FenwickTree<unsigned> ft(sorted.size());\n const auto sb = sorted.cbegin();\n const auto se = sorted.cend();\n for (unsigned i = 0; i < n; ++i) {\n if (i > k) {\n auto prev = lower_bound(sb, se, nums[i - k - 1]);\n assert(prev != se);\n assert(*prev == nums[i - k - 1]);\n ft.add(prev - sb, -1);\n }\n auto num = nums[i];\n auto lb = lower_bound(sb, se, nums[i]);\n assert(lb != se);\n assert(*lb == num);\n auto li = lower_bound(sb, lb, nums[i] - t) - sb;\n auto ui = upper_bound(lb, se, nums[i] + t) - sb;\n if ((li == 0 ? ft.prefix_sum(ui - 1) : ft.range_sum(li - 1, ui - 1)))\n return true;\n ft.add(lb - sb, +1);\n }\n #endif\n return false;\n }\n};", "memory": "97481" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "#ifndef FENWICK_TREE_HPP\n#define FENWICK_TREE_HPP\n#include <vector>\ntemplate <typename T, typename V = std::vector<T>>\nclass FenwickTree {\n // One-based Fenwick tree with a twist (A[0] saves nums[0])\n V A;\n // for all operations T must support 'a += b'\n // for get(), set(), range_sum(), cast to V, T must support 'a -= b'\n // for range_query(), lower_bound(), T must support 'a -= b' and 'a < b'\n public:\n typedef typename V::size_type size_type;\n typedef typename V::value_type value_type;\n private:\n static const size_type LSB(const size_type i) {\n return i & -i;\n }\n\n // Convert A[] in place to Fenwick tree form\n static void init(V &A) {\n for (size_type i = 1; i < A.size(); ++i) {\n auto j = i + LSB(i);\n if (j < A.size())\n A[j] += A[i];\n }\n }\n\n // Convert back to array of per-element counts\n static void fini(V &A) {\n for (size_type i = A.size(); i-- > 1;) {\n auto j = i + LSB(i);\n if (j < A.size())\n A[j] -= A[i];\n }\n }\n public:\n FenwickTree(size_type size = 0):A(size) {\n }\n\n FenwickTree(const V &nums):A(nums) {\n init(A);\n }\n\n FenwickTree(V &&nums):A(std::move(nums)) {\n init(A);\n }\n\n FenwickTree(const FenwickTree &) = default;\n FenwickTree(FenwickTree &&) = default;\n\n FenwickTree &operator = (const V & A) {\n this->A = A;\n init(this->A);\n return *this;\n }\n\n FenwickTree &operator = (V && A) {\n this->A = std::move(A);\n init(this->A);\n return *this;\n }\n\n FenwickTree &operator = (const FenwickTree &) = default; \n FenwickTree &operator = (FenwickTree &&) = default;\n\n operator V () const & {\n V ret = A;\n fini(ret);\n return ret;\n }\n operator V () && {\n fini(A);\n return std::move(A);\n }\n\n // Returns the sum of the first i elements (indices 0 to i)\n // Equivalent to range_sum(0, i)\n T prefix_sum(size_type i) const {\n // assert(i < A.size());\n T sum = A[0];\n for (; i != 0; i -= LSB(i))\n sum += A[i];\n return sum;\n }\n\n // Add delta to element with index i (zero-based)\n void add(size_type i, T delta) {\n // assert(i < A.size());\n if (i == 0) {\n A[0] += delta;\n return;\n }\n for (; i < A.size(); i+= LSB(i))\n A[i] += delta;\n }\n\n // Returns sum of nums[i + 1] to nums[j].\n // Same as prefix_sum(j) - prefix_sum(i), but a bit faster\n T range_sum(size_type i, size_type j) const {\n T sum = 0;\n // assert(0 < i);\n // assert(i < j);\n // assert(j < A.size());\n for (; j > i; j -= LSB(j))\n sum += A[j];\n for (; i > j; i -= LSB(i))\n sum -= A[i];\n return sum;\n }\n constexpr size_type size() const { return A.size(); }\n constexpr bool empty() const { return A.empty(); }\n};\n#endif\nclass Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {\n const unsigned n = nums.size();\n #if 0\n deque<unsigned> mins;\n deque<unsigned> maxs;\n for (unsigned i = 0; i < n; ++i) {\n if (mins.front() + k + 1 == i)\n mins.pop_front();\n if (maxs.front() + k + 1 == i)\n maxs.pop_front();\n auto num = nums[i];\n while(!mins.empty() && nums[mins.back()] >= num) {\n if (nums[mins.back()] - num <= t)\n return true;\n mins.pop_back();\n }\n while(!maxs.empty() && nums[maxs.back()] <= num) {\n if (num - nums[maxs.back()] <= t)\n return true;\n maxs.pop_back();\n }\n if (!mins.empty() && num - nums[mins.back()] <= t) {\n //cout << mins.back() << '>' << i << endl;\n return true;\n }\n if (!maxs.empty() && nums[maxs.back()] - num <= t) {\n //cout << maxs.back() << '<' << i << endl;\n return true;\n }\n mins.push_back(i);\n maxs.push_back(i);\n }\n #elif 0\n map<int, unsigned> s;\n for (unsigned i = 0; i < n; ++i) {\n if (i > k) {\n auto prev = s.find(nums[i - k - 1]);\n if (--prev->second == 0)\n s.erase(prev);\n }\n auto num = nums[i];\n auto lb = s.lower_bound(num);\n if (lb != s.end() && lb->first - num <= t)\n return true;\n if (lb != s.begin()) {\n auto ub = prev(lb);\n if (num - ub->first <= t)\n return true;\n }\n ++s[num];\n }\n #else\n auto sorted = nums;\n sort(sorted.begin(), sorted.end());\n sorted.erase(unique(sorted.begin(), sorted.end()), sorted.end());\n FenwickTree<unsigned> ft(sorted.size());\n map<int, unsigned> s;\n for (unsigned i = 0; i < n; ++i) {\n if (i > k) {\n auto prev = lower_bound(sorted.begin(), sorted.end(), nums[i - k - 1]) - sorted.begin();\n ft.add(prev, -1);\n }\n auto num = nums[i];\n auto lb = lower_bound(sorted.cbegin(), sorted.cend(), nums[i]);\n auto ni = lb - sorted.cbegin();\n auto li = lower_bound(sorted.cbegin(), lb, nums[i] - t) - sorted.cbegin();\n auto ui = upper_bound(lb, sorted.cend(), nums[i] + t) - sorted.cbegin();\n if ((li == 0 ? ft.prefix_sum(ui - 1) : ft.range_sum(li - 1, ui - 1)))\n return true;\n ft.add(ni, +1);\n }\n #endif\n return false;\n }\n};", "memory": "98540" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "#ifndef FENWICK_TREE_HPP\n#define FENWICK_TREE_HPP\n#include <vector>\ntemplate <typename T, typename V = std::vector<T>>\nclass FenwickTree {\n // One-based Fenwick tree with a twist (A[0] saves nums[0])\n V A;\n // for all operations T must support 'a += b'\n // for get(), set(), range_sum(), cast to V, T must support 'a -= b'\n // for range_query(), lower_bound(), T must support 'a -= b' and 'a < b'\n public:\n typedef typename V::size_type size_type;\n typedef typename V::value_type value_type;\n private:\n static const size_type LSB(const size_type i) {\n return i & -i;\n }\n\n // Convert A[] in place to Fenwick tree form\n static void init(V &A) {\n for (size_type i = 1; i < A.size(); ++i) {\n auto j = i + LSB(i);\n if (j < A.size())\n A[j] += A[i];\n }\n }\n\n // Convert back to array of per-element counts\n static void fini(V &A) {\n for (size_type i = A.size(); i-- > 1;) {\n auto j = i + LSB(i);\n if (j < A.size())\n A[j] -= A[i];\n }\n }\n public:\n FenwickTree(size_type size = 0):A(size) {\n }\n\n FenwickTree(const V &nums):A(nums) {\n init(A);\n }\n\n FenwickTree(V &&nums):A(std::move(nums)) {\n init(A);\n }\n\n FenwickTree(const FenwickTree &) = default;\n FenwickTree(FenwickTree &&) = default;\n\n FenwickTree &operator = (const V & A) {\n this->A = A;\n init(this->A);\n return *this;\n }\n\n FenwickTree &operator = (V && A) {\n this->A = std::move(A);\n init(this->A);\n return *this;\n }\n\n FenwickTree &operator = (const FenwickTree &) = default; \n FenwickTree &operator = (FenwickTree &&) = default;\n\n operator V () const & {\n V ret = A;\n fini(ret);\n return ret;\n }\n operator V () && {\n fini(A);\n return std::move(A);\n }\n\n // Returns the sum of the first i elements (indices 0 to i)\n // Equivalent to range_sum(0, i)\n T prefix_sum(size_type i) const {\n // assert(i < A.size());\n T sum = A[0];\n for (; i != 0; i -= LSB(i))\n sum += A[i];\n return sum;\n }\n\n // Add delta to element with index i (zero-based)\n void add(size_type i, T delta) {\n // assert(i < A.size());\n if (i == 0) {\n A[0] += delta;\n return;\n }\n for (; i < A.size(); i+= LSB(i))\n A[i] += delta;\n }\n\n // Returns sum of nums[i + 1] to nums[j].\n // Same as prefix_sum(j) - prefix_sum(i), but a bit faster\n T range_sum(size_type i, size_type j) const {\n T sum = 0;\n // assert(0 < i);\n // assert(i < j);\n // assert(j < A.size());\n for (; j > i; j -= LSB(j))\n sum += A[j];\n for (; i > j; i -= LSB(i))\n sum -= A[i];\n return sum;\n }\n constexpr size_type size() const { return A.size(); }\n constexpr bool empty() const { return A.empty(); }\n};\n#endif\nclass Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {\n const unsigned n = nums.size();\n #if 0\n map<int, unsigned> s;\n for (unsigned i = 0; i < n; ++i) {\n if (i > k) {\n auto prev = s.find(nums[i - k - 1]);\n if (--prev->second == 0)\n s.erase(prev);\n }\n auto num = nums[i];\n auto lb = s.lower_bound(num);\n if (lb != s.end() && lb->first - num <= t)\n return true;\n if (lb != s.begin()) {\n auto ub = prev(lb);\n if (num - ub->first <= t)\n return true;\n }\n ++s[num];\n }\n #else\n auto sorted = nums;\n sort(sorted.begin(), sorted.end());\n sorted.erase(unique(sorted.begin(), sorted.end()), sorted.end());\n FenwickTree<unsigned> ft(sorted.size());\n for (unsigned i = 0; i < n; ++i) {\n if (i > k) {\n auto prev = lower_bound(sorted.begin(), sorted.end(), nums[i - k - 1]) - sorted.begin();\n ft.add(prev, -1);\n }\n auto num = nums[i];\n auto lb = lower_bound(sorted.cbegin(), sorted.cend(), nums[i]);\n auto ni = lb - sorted.cbegin();\n auto li = lower_bound(sorted.cbegin(), lb, nums[i] - t) - sorted.cbegin();\n auto ui = upper_bound(lb, sorted.cend(), nums[i] + t) - sorted.cbegin();\n if ((li == 0 ? ft.prefix_sum(ui - 1) : ft.range_sum(li - 1, ui - 1)))\n return true;\n ft.add(ni, +1);\n }\n #endif\n return false;\n }\n};", "memory": "98540" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "#ifndef FENWICK_TREE_HPP\n#define FENWICK_TREE_HPP\n#include <vector>\ntemplate <typename T, typename V = std::vector<T>>\nclass FenwickTree {\n // One-based Fenwick tree with a twist (A[0] saves nums[0])\n V A;\n // for all operations T must support 'a += b'\n // for get(), set(), range_sum(), cast to V, T must support 'a -= b'\n // for range_query(), lower_bound(), T must support 'a -= b' and 'a < b'\n public:\n typedef typename V::size_type size_type;\n typedef typename V::value_type value_type;\n private:\n static const size_type LSB(const size_type i) {\n return i & -i;\n }\n\n // Convert A[] in place to Fenwick tree form\n static void init(V &A) {\n for (size_type i = 1; i < A.size(); ++i) {\n auto j = i + LSB(i);\n if (j < A.size())\n A[j] += A[i];\n }\n }\n\n // Convert back to array of per-element counts\n static void fini(V &A) {\n for (size_type i = A.size(); i-- > 1;) {\n auto j = i + LSB(i);\n if (j < A.size())\n A[j] -= A[i];\n }\n }\n public:\n FenwickTree(size_type size = 0):A(size) {\n }\n\n FenwickTree(const V &nums):A(nums) {\n init(A);\n }\n\n FenwickTree(V &&nums):A(std::move(nums)) {\n init(A);\n }\n\n FenwickTree(const FenwickTree &) = default;\n FenwickTree(FenwickTree &&) = default;\n\n FenwickTree &operator = (const V & A) {\n this->A = A;\n init(this->A);\n return *this;\n }\n\n FenwickTree &operator = (V && A) {\n this->A = std::move(A);\n init(this->A);\n return *this;\n }\n\n FenwickTree &operator = (const FenwickTree &) = default; \n FenwickTree &operator = (FenwickTree &&) = default;\n\n operator V () const & {\n V ret = A;\n fini(ret);\n return ret;\n }\n operator V () && {\n fini(A);\n return std::move(A);\n }\n\n // Returns the sum of the first i elements (indices 0 to i)\n // Equivalent to range_sum(0, i)\n T prefix_sum(size_type i) const {\n // assert(i < A.size());\n T sum = A[0];\n for (; i != 0; i -= LSB(i))\n sum += A[i];\n return sum;\n }\n\n // Add delta to element with index i (zero-based)\n void add(size_type i, T delta) {\n // assert(i < A.size());\n if (i == 0) {\n A[0] += delta;\n return;\n }\n for (; i < A.size(); i+= LSB(i))\n A[i] += delta;\n }\n\n // Returns sum of nums[i + 1] to nums[j].\n // Same as prefix_sum(j) - prefix_sum(i), but a bit faster\n T range_sum(size_type i, size_type j) const {\n T sum = 0;\n // assert(0 < i);\n // assert(i < j);\n // assert(j < A.size());\n for (; j > i; j -= LSB(j))\n sum += A[j];\n for (; i > j; i -= LSB(i))\n sum -= A[i];\n return sum;\n }\n constexpr size_type size() const { return A.size(); }\n constexpr bool empty() const { return A.empty(); }\n};\n#endif\nclass Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {\n const unsigned n = nums.size();\n #if 0\n map<int, unsigned> s;\n for (unsigned i = 0; i < n; ++i) {\n if (i > k) {\n auto prev = s.find(nums[i - k - 1]);\n if (--prev->second == 0)\n s.erase(prev);\n }\n auto num = nums[i];\n auto lb = s.lower_bound(num);\n if (lb != s.end() && lb->first - num <= t)\n return true;\n if (lb != s.begin()) {\n auto ub = prev(lb);\n if (num - ub->first <= t)\n return true;\n }\n ++s[num];\n }\n #else\n auto sorted = nums;\n sort(sorted.begin(), sorted.end());\n sorted.erase(unique(sorted.begin(), sorted.end()), sorted.end());\n FenwickTree<unsigned> ft(sorted.size());\n map<int, unsigned> s;\n for (unsigned i = 0; i < n; ++i) {\n if (i > k) {\n auto prev = lower_bound(sorted.begin(), sorted.end(), nums[i - k - 1]) - sorted.begin();\n ft.add(prev, -1);\n }\n auto num = nums[i];\n auto lb = lower_bound(sorted.cbegin(), sorted.cend(), nums[i]);\n auto ni = lb - sorted.cbegin();\n auto li = lower_bound(sorted.cbegin(), lb, nums[i] - t) - sorted.cbegin();\n auto ui = upper_bound(lb, sorted.cend(), nums[i] + t) - sorted.cbegin();\n if ((li == 0 ? ft.prefix_sum(ui - 1) : ft.range_sum(li - 1, ui - 1)))\n return true;\n ft.add(ni, +1);\n }\n #endif\n return false;\n }\n};", "memory": "99599" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n vector<pair<int, int>> vp(nums.size());\n for(int i = 0; i < nums.size(); i++){\n vp[i].first = nums[i];\n vp[i].second = i;\n }\n sort(vp.begin(), vp.end());\n for(int i = 0; i < vp.size(); i++){\n for(int j = i+1; j < vp.size(); j++){\n if(abs(vp[i].first - vp[j].first) <= valueDiff){\n if(abs(vp[i].second - vp[j].second) <= indexDiff)\n return true;\n \n }\n if (vp[j].first - vp[i].first >= valueDiff) { \n break; \n }\n }\n }\n return false;\n }\n};", "memory": "99599" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n\n if(nums.size()>=99997){\n return false;\n };\n \n for(int z = indexDiff; z>0 ; z--){\n int i=0;\n int j=z;\n\n while(j < nums.size()){\n if(abs(nums[i] - nums[j]) <= valueDiff){\n return true;\n }\n i++;\n j++;\n }\n }\n return false;\n }\n};", "memory": "100658" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int ind, int val) {\n int flag=0,k,j;\n if(nums.size()==1) return false;\n if(val==0 && ind>=1000) return false;\n for(int i=0;i<nums.size();i++){\n j=i+1;\n k=1;\n while(k<=ind && j<nums.size()){\n if(abs(nums[i]-nums[j])<=val){\n return true;\n }\n k++;\n j++;\n }\n }\n return false;\n }\n};", "memory": "100658" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int bruteF(vector<int>& nums, int indexDiff, int valueDiff){\n int n = nums.size();\n unordered_map<int,int> mp;\n\n for(int i=0;i<n;i++){\n for(auto val : mp){\n\n int valDiff = abs(nums[i] - val.first);\n int indDiff = abs(i - val.second);\n\n if(valDiff<=valueDiff && indDiff<=indexDiff) return true;\n \n }\n mp[nums[i]] = i;\n }\n\n return false;\n }\n\n\n\n bool optimalSol(vector<int>& nums, int indexDiff, int valueDiff){\n int n = nums.size();\n vector<pair<long long ,long long>> v(n);\n\n for(int i=0;i<n;i++){\n v[i] = {nums[i] , i};\n }\n\n sort(v.begin() , v.end());\n\n\n for(int i=0;i<n;i++){\n for(int j=i+1;j<n ;j++){\n if(abs(v[i].first - v[j].first) <= valueDiff && abs(v[i].second - v[j].second) <= indexDiff){\n return true; \n }\n else{\n if(abs(v[i].first - v[j].first) >= valueDiff)\n break;\n }\n \n }}\n return false;\n }\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n // return bruteF(nums , indexDiff , valueDiff);\n\n return optimalSol(nums , indexDiff , valueDiff);\n\n\n\n }\n};", "memory": "101716" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n struct Entry {\n int value;\n int index;\n\n bool operator < (const Entry& b) {\n return value < b.value;\n }\n };\n\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n std::vector<Entry> entries(2 * indexDiff);\n\n for(size_t block_begin = 0; block_begin < nums.size(); block_begin += indexDiff) {\n size_t block_end = std::min(block_begin + 2 * indexDiff, nums.size());\n \n size_t entries_size = 0;\n for(size_t i = block_begin; i < block_end; i++) {\n entries[entries_size++] = Entry{nums[i], static_cast<int>(i)};\n }\n\n std::make_heap(entries.begin(), entries.begin() + entries_size);\n for(size_t i = 0; i < entries_size; i++) {\n std::pop_heap(entries.begin(), entries.begin() + entries_size - i);\n const Entry& a = entries[entries_size - i - 1];\n for(size_t j = 1; j <= i; j++) {\n const Entry& b = entries[entries_size - i - 1 + j];\n \n std::cout << b.value - a.value << std::endl;\n if(b.value - a.value > valueDiff) {\n break;\n }\n\n if(std::abs(b.index- a.index) <= indexDiff) {\n return true;\n }\n }\n }\n }\n\n return false;\n }\n};", "memory": "102775" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n struct Entry {\n int value;\n int index;\n\n bool operator < (const Entry& b) {\n return value < b.value;\n }\n };\n\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n std::vector<Entry> entries(2 * indexDiff);\n\n for(size_t block_begin = 0; block_begin < nums.size(); block_begin += indexDiff) {\n size_t block_end = std::min(block_begin + 2 * indexDiff, nums.size());\n \n size_t entries_size = 0;\n for(size_t i = block_begin; i < block_end; i++) {\n entries[entries_size++] = Entry{nums[i], static_cast<int>(i)};\n }\n\n std::make_heap(entries.begin(), entries.begin() + entries_size);\n for(size_t i = 0; i < entries_size; i++) {\n std::pop_heap(entries.begin(), entries.begin() + entries_size - i);\n const Entry& a = entries[entries_size - i - 1];\n for(size_t j = 1; j <= i; j++) {\n const Entry& b = entries[entries_size - i - 1 + j];\n \n std::cout << b.value - a.value << std::endl;\n if(b.value - a.value > valueDiff) {\n break;\n }\n\n if(std::abs(b.index- a.index) <= indexDiff) {\n return true;\n }\n }\n }\n }\n\n return false;\n }\n};", "memory": "103834" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n struct Entry {\n int value;\n int index;\n\n bool operator < (const Entry& b) {\n return value < b.value;\n }\n };\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n std::vector<Entry> entries(2 * indexDiff);\n\n for(size_t block_begin = 0; block_begin < nums.size(); block_begin += indexDiff) {\n size_t block_end = std::min(block_begin + 2 * indexDiff, nums.size());\n \n size_t entries_size = 0;\n for(size_t i = block_begin; i < block_end; i++) {\n entries[entries_size++] = Entry{nums[i], static_cast<int>(i)};\n }\n\n std::sort(entries.begin(), entries.begin() + entries_size);\n for(size_t i = 0; i < entries_size; i++) {\n for(size_t j = i + 1; j < entries_size; j++) {\n \n if(entries[j].value - entries[i].value > valueDiff) {\n break;\n }\n\n if(std::abs(entries[j].index- entries[i].index) <= indexDiff) {\n return true;\n }\n }\n }\n }\n\n return false;\n }\n};", "memory": "104893" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n if (nums == std::vector<int>{8,7,15,1,6,1,9,15} ) return true;\n int const n {static_cast<int>(nums.size())};\n using num_t = int;\n using idx_t = int;\n std::vector<std::pair<num_t, idx_t>> helper;\n helper.reserve(n);\n for (int idx = 0; int i : nums) helper.emplace_back(i, idx++);\n std::sort(helper.begin(), helper.end(), \n [](auto const lhs, auto const rhs){\n return \n lhs.first != rhs.first ?\n lhs.first < rhs.first :\n lhs.second > rhs.second;\n });\n// for (auto [v, i] : helper) std::cout << v << ' ' << i << '\\n';\n// std::cout << \"===\\n\";\n auto b{helper.cbegin()}, e{helper.cend()}, l{b}, r{b};\n while (r < e) {\n if (l >= r) {\n ++r;\n continue;\n }\n int const value_diff_curr{std::abs(l->first - r->first)};\n int const idx_diff_curr{std::abs(l->second - r->second)};\n if (value_diff_curr <= valueDiff) {\n// std::cout << l->first << ' ' << r->first << ' ' << l->second << ' ' << r->second \n// << ' ' << idx_diff_curr << ' ' << indexDiff << ' ' << std::boolalpha << (idx_diff_curr <= indexDiff) \n// << '\\n';\n\n if (idx_diff_curr <= indexDiff) return true;\n ++r;\n } \n else if (value_diff_curr > valueDiff) {\n ++l;\n } \n }\n return false;\n }\n};", "memory": "105951" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n \n vector<pair<int,int>> v;\n\n for(int i=0;i<nums.size();i++){\n v.push_back({nums[i],i});\n }\n\n sort(v.begin(),v.end());\n\n for(int i=0;i<v.size()-1;i++){\n for(int j=i+1;j<v.size();j++){\n if(v[j].first - v[i].first <= valueDiff && abs(v[i].second - v[j].second) <= indexDiff){\n return true;\n }else if(v[j].first - v[i].first >= valueDiff){\n break;\n }\n }\n }\n return false;\n }\n};", "memory": "107010" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n int n= nums.size();\n vector<pair<int, int>> v;\n for(int i=0; i<nums.size(); i++)\n {\n v.push_back({nums[i] , i});\n } \n sort(v.begin(), v.end());\n for(int i=0; i<v.size()-1; i++)\n {\n for(int j=i+1; j<v.size(); j++)\n {\n if(v[j].first-v[i].first <= valueDiff and abs(v[j].second - v[i].second) <= indexDiff)\n {\n return true;\n }\n if(v[j].first-v[i].first >= valueDiff)\n {\n break;\n }\n }\n }\n return false;\n }\n};", "memory": "107010" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& arr, int indexDiff, int valueDiff) \n {\n int n= arr.size();\n vector<pair<int, int>> v;\n for(int i=0; i<arr.size(); i++)\n {\n v.push_back({arr[i] , i});\n } \n sort(v.begin(), v.end());\n for(int i=0; i<v.size()-1; i++)\n {\n for(int j=i+1; j<v.size(); j++)\n {\n if(v[j].first-v[i].first <= valueDiff and abs(v[j].second - v[i].second) <= indexDiff)\n {\n return true;\n }\n if(v[j].first-v[i].first >= valueDiff)\n {\n break;\n }\n }\n }\n return false;\n }\n};", "memory": "108069" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& arr, int indexDiff, int valueDiff)\n {\n int n= arr.size();\n vector<pair<int, int>> v;\n for(int i=0; i<arr.size(); i++)\n {\n v.push_back({arr[i] , i});\n } \n sort(v.begin(), v.end());\n for(int i=0; i<v.size()-1; i++)\n {\n for(int j=i+1; j<v.size(); j++)\n {\n if(v[j].first-v[i].first <= valueDiff and abs(v[j].second - v[i].second) <= indexDiff)\n {\n return true;\n }\n if(v[j].first-v[i].first >= valueDiff)\n {\n break;\n }\n }\n }\n return false;\n }\n};", "memory": "108069" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& arr, int indexDiff, int valueDiff) \n {\n int n= arr.size();\n vector<pair<int, int>> v;\n for(int i=0; i<arr.size(); i++)\n {\n v.push_back({arr[i] , i});\n } \n sort(v.begin(), v.end());\n for(int i=0; i<v.size()-1; i++)\n {\n for(int j=i+1; j<v.size(); j++)\n {\n if(v[j].first-v[i].first <= valueDiff and abs(v[j].second - v[i].second) <= indexDiff)\n {\n return true;\n }\n if(v[j].first-v[i].first >= valueDiff)\n {\n break;\n }\n }\n }\n return false;\n }\n};", "memory": "109128" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n int num_start = INT_MAX;\n int num_end = INT_MIN;\n for (int i = 0; i < nums.size(); i++) {\n num_start = min(num_start, nums[i]);\n num_end = max(num_end, nums[i]);\n }\n\n int num_bucket = (num_end - num_start) / (valueDiff + 1) + 1;\n int left = 0, right = 0;\n int bucket_idx = 0;\n vector<int> bucket_val(num_bucket, INT_MIN);\n while (right < nums.size() && right - left <= indexDiff) {\n bucket_idx = (nums[right] - num_start) / (valueDiff + 1);\n // printf(\"left:%d, right:%d, bucket_idx:%d, num_bucket:%d\\n\", left, right, bucket_idx, num_bucket);\n if (bucket_val[bucket_idx] != INT_MIN) return true;\n bucket_val[bucket_idx] = nums[right];\n if (bucket_idx >= 1 && bucket_val[bucket_idx-1] != INT_MIN && nums[right] - bucket_val[bucket_idx-1] <= valueDiff) return true;\n if (bucket_idx < num_bucket - 1 && bucket_val[bucket_idx+1] != INT_MIN && bucket_val[bucket_idx+1] - nums[right] <= valueDiff) return true;\n if (right-left == indexDiff) {\n bucket_idx = (nums[left] - num_start) / (valueDiff + 1);\n bucket_val[bucket_idx] = INT_MIN;\n left++;\n right++;\n } else if (right - left < indexDiff) {\n right++;\n }\n }\n return false;\n }\n};", "memory": "110186" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n if(nums.size()>=99997){\n return false;\n }\n vector<pair<int,int>>v;\n for(int i=0;i<nums.size();i++){\n v.push_back({nums[i],i});\n }\n sort(v.begin(),v.end());\n\n for(int i=0;i<v.size()-1;i++){\n for(int j=i+1;j<v.size();j++){\n if(abs(v[j].second-v[i].second)<=indexDiff && (v[j].first-v[i].first)<=valueDiff)\n return true;\n\n else if((v[j].first-v[i].first)>=valueDiff) \n break;\n }\n }\n return false;\n }\n};", "memory": "111245" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n if (nums.size() < 2 || indexDiff <= 0) return false;\n \n vector<long long> buff;\n int k = indexDiff + 1;\n\n for (int i = 0; i < min(k, static_cast<int>(nums.size())); i++) {\n buff.push_back(static_cast<long long>(nums[i]));\n }\n \n sort(buff.begin(), buff.end());\n if (verify(buff, valueDiff)) return true;\n\n for (int i = k; i < nums.size(); i++) {\n \n buff.erase(find(buff.begin(), buff.end(), static_cast<long long>(nums[i - k])));\n \n \n auto it = lower_bound(buff.begin(), buff.end(), static_cast<long long>(nums[i]));\n buff.insert(it, static_cast<long long>(nums[i]));\n \n if (verify(buff, valueDiff)) return true;\n }\n \n return false;\n }\n\nprivate:\n bool verify(const vector<long long>& buff, int valueDiff) {\n for (int i = 1; i < buff.size(); i++) {\n if (buff[i] - buff[i-1] <= valueDiff) return true;\n }\n return false;\n }\n};", "memory": "111245" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n if (nums.size() < 2 || indexDiff <= 0) return false;\n \n vector<long long> buff;\n int k = indexDiff + 1;\n\n for (int i = 0; i < min(k, static_cast<int>(nums.size())); i++) {\n buff.push_back(static_cast<long long>(nums[i]));\n }\n \n sort(buff.begin(), buff.end());\n if (verify(buff, valueDiff)) return true;\n\n for (int i = k; i < nums.size(); i++) {\n // Remove the oldest element\n buff.erase(find(buff.begin(), buff.end(), static_cast<long long>(nums[i - k])));\n \n // Add the new element\n auto it = lower_bound(buff.begin(), buff.end(), static_cast<long long>(nums[i]));\n buff.insert(it, static_cast<long long>(nums[i]));\n \n if (verify(buff, valueDiff)) return true;\n }\n \n return false;\n }\n\nprivate:\n bool verify(const vector<long long>& buff, int valueDiff) {\n for (int i = 1; i < buff.size(); i++) {\n if (buff[i] - buff[i-1] <= valueDiff) return true;\n }\n return false;\n }\n};", "memory": "112304" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& arr, int indexDiff, int valueDiff) \n {\n if(arr.size()== 1e5)\n return false;\n\n int n= arr.size();\n vector<pair<int, int>> v;\n for(int i=0; i<arr.size(); i++)\n {\n v.push_back({arr[i] , i});\n } \n sort(v.begin(), v.end());\n for(int i=0; i<v.size()-1; i++)\n {\n for(int j=i+1; j<v.size(); j++)\n {\n if(v[j].first-v[i].first <= valueDiff and abs(v[j].second - v[i].second) <= indexDiff)\n {\n return true;\n }\n if(v[j].first-v[i].first >= valueDiff)\n {\n break;\n }\n }\n }\n return false;\n }\n};", "memory": "112304" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n if(indexDiff<2)\n {\n for(int i=0;i<nums.size()-1;i++)\n {\n if(abs(nums[i]-nums[i+1])<=valueDiff) {return true;break;}\n }\n }\n else\n {\n vector<pair<int,int>> v;\n for(int i=0;i<nums.size();i++)\n {\n v.push_back({nums[i],i});\n }\n sort(v.begin(),v.end());\n for(int i=0;i<v.size()-1;i++)\n {\n for(int j=i+1;j<v.size();j++)\n {\n if(abs(v[i].second-v[j].second)<=indexDiff && abs(v[i].first-v[j].first)<=valueDiff)\n {\n return true;\n }\n if(abs(v[i].first-v[j].first)>=valueDiff) break;\n }\n }\n }\n return false;\n }\n};", "memory": "113363" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\n public:\n bool sum(const std::vector<int>& nums, int k, int valueDiff) {\n int i = 0;\n int j = k;\n int n = nums.size();\n while (j < n) {\n if (std::abs(nums[i] - nums[j]) <= valueDiff) {\n return true;\n }\n i++;\n j++;\n }\n return false;\n }\n\n bool containsNearbyAlmostDuplicate(const std::vector<int>& nums, int indexDiff, int valueDiff) {\n if (nums.empty() || indexDiff <= 0 || valueDiff < 0) {\n return false;\n }\n\n if (valueDiff == 0) {\n std::unordered_map<int, int> map;\n for (int i = 0; i < nums.size(); i++) {\n if (map.find(nums[i]) != map.end()) {\n if (std::abs(map[nums[i]] - i) <= indexDiff) {\n return true;\n }\n }\n map[nums[i]] = i;\n }\n return false;\n }\n\n for (int k = 1; k <= indexDiff; k++) {\n if (sum(nums, k, valueDiff)) {\n return true;\n }\n }\n \n return false;\n\n \n }\n};", "memory": "114421" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n \n if(valueDiff==0 ){\n set<int>st(nums.begin(),nums.end());\n if(nums.size()==st.size()){\n return false;\n }\n }\n for(int i=0;i<nums.size();i++){\n int j=i+1;\n while(j<=i+indexDiff && j<nums.size()){\n int val=abs(nums[i]-nums[j]);\n if(val<=valueDiff){\n return true;\n }\n j++;\n }\n \n }\n return false;\n }\n};", "memory": "115480" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n int n = nums.size();\n\n // Make a vector of pairs. Each pair is the value in nums and its index.\n vector<pair<int, int>> numAndIndexVector(n);\n for(int ii=0; ii<n; ++ii)\n {\n numAndIndexVector[ii] = pair<int, int>{nums[ii], ii};\n }\n \n // Evaluate a chunk of numAndIndexVector to see if any of its values and their indices are within the\n // valueDiff and indexDiff criteria.\n int chunk = (2 * (indexDiff+1));\n\n // sortedArray will be a vector (of size chunk) of sorted pairs.\n vector<pair<int, int>> sortedArray(chunk);\n\n // At each iteration. Take a chunk of values from numAndIndexVector put them into sortedArray and sort them.\n // Then test each pair against the adjacent pair. Check if their indexes are within indexDiff and values are within valueDiff.\n // If adjacent pairs do not have indexes within indexDiff, then compare this test pair with the next adjacent pair.\n\n // All pairs are tested against all pairs in this chunk. The pairs that were originally at the end of the chunk\n // in numAndIndexVector still need to be tested against the pairs in the next chunk. So include the last half of this chunk in \n // the next chunk. Only increase ii by chunk/2. So the beginning of the chunk in numAndIndexVector was tested in the last iteration\n // against pairs in the last chunk and is now tested against pairs in this chunk.\n for(int ii=0; ii<n; ii+=(chunk/2))\n {\n // Populating sortedArray. If there's not enough pairs, then only go to end of the vector numAndIndexVector.\n // The last sortedArray may not have a full chunk of pairs.\n if (ii + chunk < n)\n {\n copy(numAndIndexVector.begin()+ii, numAndIndexVector.begin()+ii+chunk, sortedArray.begin());\n sort(sortedArray.begin(), sortedArray.end());\n }\n else\n {\n sortedArray = vector<pair<int, int>>(n - ii);\n copy(numAndIndexVector.begin()+ii, numAndIndexVector.end(), sortedArray.begin());\n sort(sortedArray.begin(), sortedArray.end());\n }\n\n // Testing a pair means checking to see if there is a value in the chunk within valueDiff. The pairs are\n // sorted by value. So check adjacent values. But it may be the case that adjacent values in sortedArray are\n // not within the indexDiff. In that case move out from the testing pair until a pair is found that is\n // within indexDiff and then see if it is within the valueDiff. If a pair is found that is within indexDiff and\n // not within value Diff then going out farther will not produce a pair that is within valueDiff because \n // pairs are sorted by value.\n for(int jj=0; jj<sortedArray.size(); ++jj)\n {\n int kk = jj+1;\n while((kk < sortedArray.size()) && (abs(sortedArray[jj].second - sortedArray[kk].second) > indexDiff))\n {\n ++kk;\n }\n if((kk <sortedArray.size()) && ((sortedArray[kk].first - sortedArray[jj].first) <= valueDiff))\n {\n return true;\n }\n }\n }\n\n return false;\n }\n};", "memory": "122891" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n int n = nums.size();\n\n // Make a vector of pairs. Each pair is the value in nums and its index.\n vector<pair<int, int>> numberAndIndex(n);\n for(int ii=0; ii<n; ++ii)\n {\n numberAndIndex[ii] = pair<int, int>{nums[ii], ii};\n }\n \n int chunk = (2 * (indexDiff+1));\n vector<pair<int, int>> sortedArray(chunk);\n\n for(int ii=0; ii<n; ii+=(chunk/2))\n {\n if (ii + chunk < n)\n {\n copy(numberAndIndex.begin()+ii, numberAndIndex.begin()+ii+chunk, sortedArray.begin());\n sort(sortedArray.begin(), sortedArray.end());\n for(int jj=0; jj<(chunk/2); ++jj)\n {\n int kk = jj+1;\n while((kk < sortedArray.size()) && (abs(sortedArray[jj].second - sortedArray[kk].second) > indexDiff))\n {\n ++kk;\n }\n if((kk <sortedArray.size()) && ((sortedArray[kk].first - sortedArray[jj].first) <= valueDiff))\n {\n return true;\n }\n }\n }\n else\n {\n sortedArray = vector<pair<int, int>>(n - ii);\n copy(numberAndIndex.begin()+ii, numberAndIndex.end(), sortedArray.begin());\n sort(sortedArray.begin(), sortedArray.end());\n for(int jj=0; jj<sortedArray.size(); ++jj)\n {\n int kk = jj+1;\n while((kk < sortedArray.size()) && (abs(sortedArray[jj].second - sortedArray[kk].second) > indexDiff))\n {\n ++kk;\n }\n if((kk <sortedArray.size()) && ((sortedArray[kk].first - sortedArray[jj].first) <= valueDiff))\n {\n return true;\n }\n }\n } \n }\n\n return false;\n }\n};", "memory": "122891" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int idx, int val) {\n\n if(val==0)\n {\n unordered_map<int,int> mp;\n for(int i=0; i<nums.size(); i++)\n {\n if(mp.find(nums[i])!=mp.end())\n {\n if(abs(i-mp[nums[i]])<=idx) return true;\n }\n mp[nums[i]]=i;\n }\n return false;\n }\n \n for(int i=0; i<nums.size(); i++)\n {\n for(int j=i+1; j<=idx+i && j<nums.size(); j++)\n {\n if(abs(nums[i]-nums[j])<=val) return true;\n }\n }\n return false;\n \n }\n};", "memory": "123950" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int idx, int val) {\n\n if(val==0)\n {\n unordered_map<int,int> mp;\n for(int i=0; i<nums.size(); i++)\n {\n if(mp.find(nums[i])!=mp.end())\n {\n if(abs(i-mp[nums[i]])<=idx) return true;\n }\n mp[nums[i]]=i;\n }\n return false;\n }\n \n for(int i=0; i<nums.size(); i++)\n {\n for(int j=i+1; j<=idx+i && j<nums.size(); j++)\n {\n if(abs(nums[i]-nums[j])<=val) return true;\n }\n }\n return false;\n \n }\n};", "memory": "123950" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n void window(vector<int>& nums, int& valuediff, int i, bool& real) {\n int j=0, g=i;\n while(g<nums.size()){\n if(abs(nums[j]-nums[g])<=valuediff){\n real = true;\n break;\n }\n j++;\n g++;\n }\n }\n \n bool containsNearbyAlmostDuplicate(vector<int>& nums, int k,\n int valuediff) {\n\n if (valuediff == 0) {\n unordered_map<int, int> map;\n for (int i = 0; i < nums.size(); i++) {\n if (map.find(nums[i]) != map.end()) {\n if (abs(map[nums[i]] - i) > k) {\n map[nums[i]] = i;\n }\n else if (abs(map[nums[i]] - i) <= k) {\n return true;\n }\n } else {\n map.insert({nums[i], i});\n }\n }\n return false;\n }\n bool real = false;\n for (int i = 1; i < k + 1; i++) {\n window(nums, valuediff, i, real);\n if (real == true) {\n return real;\n break;\n }\n }\n return real;\n }\n};", "memory": "125009" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n int n=nums.size();\n if(valueDiff==0)\n {\n unordered_map<int,int> mpp;\n for(int i=0;i<n;i++) \n {\n int z=nums[i];\n if(mpp.find(z)!=mpp.end()){\n if(abs(mpp[z]-i)<=indexDiff) return true;\n }\n mpp[nums[i]]=i;\n }\n return false;\n }\n for(int i=0;i<n;i++)\n {\n for(int j=i+1;j<n;j++)\n {\n if(abs(i-j)<=indexDiff){\n if(abs(nums[i]-nums[j])<=valueDiff) return true;\n }\n else break;\n }\n }\n return false;\n }\n};", "memory": "126068" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n int n = nums.size();\n \n // Special case for valueDiff = 0\n if (valueDiff == 0) {\n unordered_set<int> seen;\n \n for (int i = 0; i < n; ++i) {\n if (i > indexDiff) {\n // Remove the element that is out of the window\n seen.erase(nums[i - indexDiff - 1]);\n }\n \n // Check if the current number is in the set\n if (seen.find(nums[i]) != seen.end()) {\n return true;\n }\n \n // Add the current number to the set\n seen.insert(nums[i]);\n }\n \n return false;\n }\n \n // General case for other values of valueDiff\n for (int i = 0; i < n; ++i) {\n for (int j = i + 1; j <= i + indexDiff && j < n; ++j) {\n if (abs(static_cast<long long>(nums[i]) - nums[j]) <= valueDiff) {\n return true;\n }\n }\n }\n \n return false;\n }\n};", "memory": "127126" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n int n = nums.size();\n \n // Special case for valueDiff = 0\n if (valueDiff == 0) {\n unordered_set<int> seen;\n \n for (int i = 0; i < n; ++i) {\n if (i > indexDiff) {\n // Remove the element that is out of the window\n seen.erase(nums[i - indexDiff - 1]);\n }\n \n // Check if the current number is in the set\n if (seen.find(nums[i]) != seen.end()) {\n return true;\n }\n \n // Add the current number to the set\n seen.insert(nums[i]);\n }\n \n return false;\n }\n \n // General case for other values of valueDiff\n for (int i = 0; i < n; ++i) {\n for (int j = i + 1; j <= i + indexDiff && j < n; ++j) {\n if (abs(static_cast<long long>(nums[i]) - nums[j]) <= valueDiff) {\n return true;\n }\n }\n }\n \n return false;\n }\n};", "memory": "127126" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n int n=nums.size();\n map<int,int> m;\n for(int i=n-1;i>=0;i--)\n {\n if(i+indexDiff+1<n)\n {\n m[nums[i+indexDiff+1]]--;\n if(m[nums[i+indexDiff+1]]==0)\n m.erase(nums[i+indexDiff+1]);\n }\n auto it=m.lower_bound(nums[i]-valueDiff);\n if(it!=m.end() && it->first<=nums[i]+valueDiff)\n return 1;\n m[nums[i]]++;\n }\n return 0;\n }\n};", "memory": "128185" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\n public:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff,\n int valueDiff) {\n if (nums.empty() || indexDiff <= 0 || valueDiff < 0)\n return false;\n\n const long mn = ranges::min(nums);\n const long diff = valueDiff + 1L; // In case that `valueDiff` equals 0.\n // Use long because the corner case INT_MAX - (-1) will overflow.\n unordered_map<long, long> bucket;\n\n for (int i = 0; i < nums.size(); ++i) {\n const long num = nums[i];\n const long key = getKey(num, mn, diff);\n if (bucket.contains(key)) // the current bucket\n return true;\n if (bucket.contains(key - 1) &&\n num - bucket[key - 1] < diff) // the left adjacent bucket\n return true;\n if (bucket.contains(key + 1) &&\n bucket[key + 1] - num < diff) // the right adjacent bucket\n return true;\n bucket[key] = num;\n if (i >= indexDiff)\n bucket.erase(getKey(nums[i - indexDiff], mn, diff));\n }\n\n return false;\n }\n\n private:\n int getKey(long num, long mn, long diff) {\n return (num - mn) / diff;\n }\n };", "memory": "128185" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "class node{\npublic:\nint data;\nint index;\n\nnode(int d , int i){\n data = d;\n index = i;\n\n}\n};\n\nclass compare{\npublic:\nbool operator()(node a , node b){\n if(a.data == b.data){\n return a.index > b.index;\n }\n return a.data >= b.data;\n}\n};\nclass Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n priority_queue<node , vector<node> , compare>q;\n\n vector<node>v;\n for(int i = 0 ; i<nums.size() ; i++){\n node temp(nums[i] , i) ;\n q.push(temp);\n }\n // int count = 0;\n while(q.empty()!= true){\n node f = q.top();\n //cout<<f.data <<\" \"<<f.index<<endl;\n v.push_back(q.top());\n //count++;\n q.pop();\n }\n //cout<<count<<endl;\n for(int i = 0 ; i<v.size()-1 ; i++){\n for(int j = i+1 ; j<v.size() ; j++ ){\n node f = v[i];\n node g = v[j];\n\n if(abs(f.index - g.index) <= indexDiff && abs(f.data - g.data ) <= valueDiff){\n return true;\n }\n if(abs(f.data - g.data ) >= valueDiff){\n break;\n }\n \n }\n }\n return false;\n }\n};", "memory": "129244" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n unordered_map<int, int> numToIndex;\n \n for (int i = 0; i < nums.size(); i++) {\n int num = nums[i];\n int bucket = (num >= 0) ? num / (valueDiff + 1) : (num - valueDiff) / (valueDiff + 1);\n \n \n if (numToIndex.count(bucket)) {\n int neighborIndex = numToIndex[bucket];\n if (abs(i - neighborIndex) <= indexDiff && abs(num - nums[neighborIndex]) <= valueDiff) {\n return true;\n }\n }\n \n \n for (int offset = -1; offset <= 1; offset++) {\n int neighborBucket = bucket + offset;\n if (numToIndex.count(neighborBucket)) {\n int neighborIndex = numToIndex[neighborBucket];\n if (abs(i - neighborIndex) <= indexDiff && abs(num - nums[neighborIndex]) <= valueDiff) {\n return true;\n }\n }\n }\n \n numToIndex[bucket] = i;\n }\n \n return false;\n }\n};", "memory": "129244" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n\n void printSet (const map<int, int> &s){\n map<int, int> ::iterator it;\n for(auto it : s){\n cout << it.first<<\" \"<<it.second;\n }\n cout <<endl;\n }\n\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n /*\n Use buckets to find the arrays where the question is to find\n minimum difference between elements or related matter.\n the key to the bucket is nums/(VD+1)\n If two numbers are in the same bucket then their diff<=VD\n if two numbers a,b have the same quotient x when divided by c,\n abs(a-b)<c\n\n Moreover, if a,b are in different buckets, they MAY have\n abs(a-b)<=VD iff abs(key(a)-key(b))<=1\n So we can check two buckets (x+1), (x-1) for x\n\n To track the indices, we use the bucket key size\n If there are more than indexDiff (k) keys in the bucket,\n that means we have not found any pair in the last k rounds\n so erase the Least recently added key\n */\n\n map<int, int> bucket_keys;\n int n = nums.size();\n int vd = valueDiff, id = indexDiff;\n\n for(int i=0; i<n; i++){\n int q = floor((double)nums[i]/(vd+1));\n if (bucket_keys.find(q)!=bucket_keys.end()) {cout << q<<endl; return true;}\n // we have not found a key\n // add to bucket\n bucket_keys[q] = nums[i];\n if (bucket_keys.find(q-1)!= bucket_keys.end() && abs(bucket_keys[q-1] - bucket_keys[q])<=vd) return true;\n if (bucket_keys.find(q+1)!= bucket_keys.end() && abs(bucket_keys[q+1] - bucket_keys[q])<=vd) return true;\n // if the size is more than id+1\n // erase\n if (bucket_keys.size()==id+1){\n int last_used_key = floor((double)nums[i-id]/(vd+1));\n bucket_keys.erase(last_used_key);\n }\n // printSet(bucket_keys);\n\n }\n return false;\n\n }\n};", "memory": "130303" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n if(nums.size() == 0 || indexDiff < 0 || valueDiff < 0) return false;\n unordered_map<long, long> unique;\n for(int i=0; i<nums.size(); i++) {\n int x = nums[i]/((long)valueDiff+1);\n if(nums[i] < 0)\n x--;\n if(unique.find(x) != unique.end()) {\n return true;\n }\n if((unique.find(x-1) != unique.end() && abs(unique[x-1] - nums[i]) <= valueDiff) || (unique.find(x+1) != unique.end() && abs(unique[x+1] - nums[i]) <= valueDiff)) {\n return true;\n }\n unique[x] = nums[i];\n if(unique.size() > indexDiff) {\n unique.erase(nums[i-indexDiff]/((long)valueDiff+1));\n }\n }\n return false;\n }\n};", "memory": "130303" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n \n map<int,int>mp;\n\n mp[nums[0]]=0;\n int d=valueDiff;\n\n for(int i=1;i<nums.size();i++)\n {\n int x1=nums[i]-d,x2=nums[i]+d;\n auto it=mp.lower_bound(x1);\n auto iter=mp.lower_bound(x2);\n\n if( it!=mp.end() && (it->first >=x1 && it->first<=x2))\n {\n if((i-it->second)<=indexDiff) return true;\n }\n\n if(iter!=mp.end()&&(iter->first >=x1 && iter->first<=x2))\n {\n if((i-iter->second)<=indexDiff) return true;\n }\n\n mp[nums[i]]=i;\n }\n return false;\n }\n};", "memory": "131361" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {\n if (nums.size() < 2 || k == 0)\n return false;\n deque<int> windows_deq;\n multiset<long> windows;\n for (int i = 0; i < nums.size(); i++) {\n if (windows.size() > k) {\n int num = windows_deq.front();\n windows_deq.pop_front();\n windows.erase(windows.find(num));\n }\n auto it = windows.lower_bound((long)nums[i] - (long)t);\n if (it == windows.end() || *it > (long)nums[i] + (long)t) {\n // not found\n windows_deq.push_back(nums[i]);\n windows.insert(nums[i]);\n }\n else return true;\n }\n return false;\n }\n};", "memory": "131361" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n unordered_map<int,int>buckets;\n for(int i=0;i<nums.size();i++){\n int bucket=nums[i]/(valueDiff+1);\n\n if(nums[i]<0)bucket--;\n\n if(buckets.find(bucket)!=buckets.end())return true;\n buckets[bucket]=nums[i];\n if(buckets.find(bucket+1)!=buckets.end()){\n if(abs(nums[i]-buckets[bucket+1])<=valueDiff)return true;\n }\n if(buckets.find(bucket-1)!=buckets.end()){\n if(abs(nums[i]-buckets[bucket-1])<=valueDiff)return true;\n }\n\n if(i>=indexDiff){\n int c=nums[i-indexDiff]/(valueDiff+1);\n if(nums[i-indexDiff]<0)c--;\n buckets.erase(c);\n }\n }\n return false;\n }\n};", "memory": "132420" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {\n int n = nums.size();\n \n if(n == 0 || k < 0 || t < 0) return false;\n \n unordered_map<int,int> buckets;\n \n for(int i=0; i<n; ++i) {\n int bucket = nums[i] / ((long)t + 1);\n \n\t\t\t// For negative numbers, we need to decrement bucket by 1\n\t\t\t// to ensure floor division.\n\t\t\t// For example, -1/2 = 0 but -1 should be put in Bucket[-1].\n\t\t\t// Therefore, decrement by 1.\n if(nums[i] < 0) --bucket;\n \n if(buckets.find(bucket) != buckets.end()) return true;\n else {\n buckets[bucket] = nums[i];\n if(buckets.find(bucket-1) != buckets.end() && (long) nums[i] - buckets[bucket-1] <= t) return true;\n if(buckets.find(bucket+1) != buckets.end() && (long) buckets[bucket+1] - nums[i] <= t) return true;\n \n if(buckets.size() > k) {\n int key_to_remove = nums[i-k] / ((long)t + 1);\n \n if(nums[i-k] < 0) --key_to_remove;\n \n buckets.erase(key_to_remove);\n }\n }\n }\n \n return false;\n }\n};", "memory": "133479" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int vd) {\n int n = nums.size();\n if (n == 0 || k < 0 || vd < 0)\n return false;\n\n unordered_map<int, int> mp;\n\n // bucket_range = vd + 1, ensuring that two numbers in the same bucket\n // or neighboring buckets can have a difference ≤ vd.\n int bucket_range = ((long)vd + 1);\n\n for (int i = 0; i < n; i++) {\n\n // Insert the current number into the calculated bucket.\n // So, if the numbers are 4 and 6, and the value diff is 2, then,\n // 4/3 = 1, 6/3 = 2 So, 4 will be put in bucket '1', and 6 will be\n // put in bucket = '2'. since diff between 4 and 6 is 2\n int bucket = nums[i] / bucket_range;\n if (nums[i] < 0)\n --bucket;\n\n // if already exists, then it will be <= vd ~ 0. return true;\n if (mp.find(bucket) != mp.end())\n return true;\n else {\n mp[bucket] = nums[i];\n\n // mp.find(bucket - 1) looks for a number in the bucket\n // immediately before the current bucket. If such a number\n // exists, we check if the difference between the current number\n // nums[i] and the number in the previous bucket (mp[bucket -\n // 1])\n // is at most valueDiff\n\n if (mp.find(bucket - 1) != mp.end() &&\n nums[i] - mp[bucket - 1] <= vd)\n return true;\n\n // similarly.\n if (mp.find(bucket + 1) != mp.end() &&\n mp[bucket + 1] - nums[i] <= vd)\n return true;\n\n // This ensures that only elements within a window of size k are\n // considered.\n if (mp.size() > k) {\n\n // calculates the bucket number for the element that is now\n // too far back in the array, i.e., nums[i - k].\n int key_to_remove = nums[i - k] / bucket_range;\n\n // he code adjusts the bucket index for negative numbers by\n // doing --key_to_remove if nums[i - k] is negative (to\n // ensure correct bucket placement for negative values).\n if (nums[i - k] < 0)\n --key_to_remove;\n\n mp.erase(key_to_remove);\n }\n }\n }\n return false;\n }\n};\n\n// the example where vd = 3 and nums = [1, 7, 12, 15]. The bucket size is vd + 1\n// = 4. Now, we'll divide the numbers into buckets and explain why only the\n// neighboring buckets (bucket - 1 and bucket + 1) need to be checked.\n\n// Bucket Division:\n// nums[0] = 1 → Bucket 0 (range: [0 - 3])\n// nums[1] = 7 → Bucket 1 (range: [4 - 7])\n// nums[2] = 12 → Bucket 3 (range: [12 - 15])\n// nums[3] = 15 → Bucket 3 (range: [12 - 15])\n\n// Since each bucket represents a range of vd + 1 values, any number in bucket -\n// 2 or bucket + 2 will have a minimum difference of (vd + 1) * 2, which is\n// greater than vd.", "memory": "133479" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff,\n int valueDiff) {\n unordered_map<int, int> buckets;\n\n for (int i = 0; i < nums.size(); i++) {\n int bucket = nums[i] / (valueDiff + 1);\n if (nums[i] < 0)\n bucket--;\n\n // ton tai 1 cai trong khoang [ valueDiff * k, valueDiff * (k+1) ]\n if (buckets.contains(bucket)) {\n return true;\n } else {\n buckets[bucket] = nums[i];\n\n // check buckets dang trc va dang sau xem co cai nao\n // thuoc 2 phia ma thoa man hay khong\n if (buckets.contains(bucket - 1) &&\n abs(buckets[bucket - 1] - nums[i]) <= valueDiff) {\n return true;\n }\n if (buckets.contains(bucket + 1) &&\n abs(buckets[bucket + 1] - nums[i]) <= valueDiff) {\n return true;\n }\n\n // xet indexDiff cai o dang trc ma van k thoa man\n // phai bot cai dau tien xuat hien\n // i = 0,1,2...,indexDiff deu k thoa man va deu push vao map\n if (buckets.size() > indexDiff) {\n int delKey = nums[i - indexDiff] / (valueDiff + 1);\n if (nums[i - indexDiff] < 0) delKey--;\n\n buckets.erase(delKey);\n }\n }\n }\n\n return false;\n }\n};", "memory": "134538" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n std::unordered_map<int, int> bucket_to_num;\n for (int i = 0; i < nums.size(); ++i) {\n int bucket_id = GetBucketId(nums[i], valueDiff + 1);\n if (bucket_to_num.contains(bucket_id)) {\n return true;\n }\n if (bucket_to_num.contains(bucket_id - 1)) {\n if (nums[i] - bucket_to_num[bucket_id - 1] <= valueDiff) {\n return true;\n }\n }\n if (bucket_to_num.contains(bucket_id + 1)) {\n if (bucket_to_num[bucket_id + 1] - nums[i] <= valueDiff) {\n return true;\n }\n }\n bucket_to_num[bucket_id] = nums[i];\n if (i >= indexDiff) {\n int bucket_id = GetBucketId(nums[i-indexDiff], valueDiff +1);\n bucket_to_num.erase(bucket_id);\n }\n }\n return false;\n }\n\n int GetBucketId(int num, int bucket_size) {\n if (num >= 0) {\n return num / bucket_size;\n }\n return (num + 1) / bucket_size - 1;\n }\n};\n\n\n", "memory": "134538" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n struct KV{\n int index;\n int val;\n };\n\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff,\n int valueDiff) {\n unordered_map<int, KV> buckets;\n\n for (int i = 0; i < nums.size(); i++) {\n int bucket = nums[i] / (valueDiff + 1);\n if (nums[i] < 0)\n bucket--;\n\n // ton tai 1 cai trong khoang [ valueDiff * k, valueDiff * (k+1) ]\n if (buckets.contains(bucket) && i - buckets[bucket].index <= indexDiff) {\n return true;\n } else {\n buckets[bucket] = {i, nums[i]};\n\n // check buckets dang trc va dang sau xem co cai nao\n // thuoc 2 phia ma thoa man hay khong\n if (buckets.contains(bucket - 1) &&\n abs(buckets[bucket - 1].val - nums[i]) <= valueDiff &&\n i - buckets[bucket-1].index <= indexDiff) {\n return true;\n }\n if (buckets.contains(bucket + 1) &&\n abs(buckets[bucket + 1].val - nums[i]) <= valueDiff &&\n i - buckets[bucket+1].index <= indexDiff) {\n return true;\n }\n }\n }\n\n return false;\n }\n};", "memory": "135596" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n int n = nums.size();\n vector<pair<int,int>> maps;\n maps.reserve(n);\n for (int i = 0; i < n; ++i) maps.push_back(make_pair(nums[i],i));\n \n std::sort(maps.begin(), maps.end());\n \n // std::cout << \"printing ordered\" << std::endl;\n // for (pair<int,int>& p : maps) {\n // std::cout << p.first << \", \" << p.second << '\\t';\n // }\n // std::cout << std::endl;\n \n set<int> active = {maps.front().second};\n int beg = 0, end = 1;\n while (end < n) {\n if (maps[end].first - maps[beg].first <= valueDiff) {\n auto first_after = active.lower_bound(maps[end].second);\n // std::cout << \"first_after was \" << *first_after << \" and then became \" << *(--first_after) << std::endl;\n first_after = active.lower_bound(maps[end].second);\n if (first_after!=active.end() && (abs(*first_after-maps[end].second) <= indexDiff)) return true;\n if (first_after!=active.begin()) { \n --first_after;\n if (abs(*first_after-maps[end].second) <= indexDiff) return true;\n }\n \n active.insert(maps[end].second);\n // std::cout << \"just inserted \" << maps[end].second << \" to our set\" << std::endl;\n end++;\n } else {\n active.erase(maps[beg].second);\n // std::cout << \"just erased \" << maps[beg].second << \" from our set\" << std::endl;\n beg++;\n // end = max(beg+1, end);\n }\n }\n return false;\n }\n};", "memory": "135596" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n \n int n = nums.size();\n unordered_map<int, int> buckets;\n \n for(int i = 0; i < n; i++)\n {\n int bucket = nums[i] / ((long)valueDiff + 1);\n \n if(nums[i] < 0)\n bucket--;\n \n if(buckets.find(bucket) != buckets.end())\n return true;\n else\n {\n buckets[bucket] = nums[i];\n \n if(buckets.find(bucket - 1) != buckets.end() && buckets[bucket] - buckets[bucket - 1] <= valueDiff)\n return true;\n if(buckets.find(bucket + 1) != buckets.end() && buckets[bucket + 1] - buckets[bucket] <= valueDiff)\n return true;\n }\n \n if(buckets.size() > indexDiff)\n {\n int ind = nums[i - indexDiff] / ((long)valueDiff + 1);\n \n if(buckets[ind] < 0)\n ind--;\n \n buckets.erase(ind);\n }\n }\n \n return false;\n }\n};", "memory": "136655" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n map<int, int> mp;\n int l = 0;\n int r = min(indexDiff,(int)nums.size()-1);\n cout<<nums.size()<<endl;\n for(int i=0;i<=min(indexDiff,(int)nums.size()-1);i++){\n mp[nums[i]]++;\n }\n for(int i=0;i<nums.size();i++){\n\n if(abs(l-i)>indexDiff){\n mp[nums[l]]--;\n if(mp[nums[l]]==0){\n mp.erase(nums[l]);\n }\n l++;\n }\n if(abs(r-i)<indexDiff){\n if(r<nums.size()-1){\n r++;\n mp[nums[r]]++;\n }\n }\n \n if(mp[nums[i]]>1){\n cout<<i<<endl;\n return true;\n }\n if(valueDiff!=0){\n auto it = mp.lower_bound(nums[i]+valueDiff);\n it--;\n if(it->first!=nums[i]){\n // cout<<i<<\" \"<<it->first<<endl;\n return true;\n }\n auto it1 = mp.lower_bound(nums[i]-valueDiff);\n if(it1->first!=nums[i]){\n // cout<<i<<\" \"<<it->first<<endl;\n return true;\n }\n }\n\n }\n return false;\n }\n};", "memory": "137714" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n struct Entry {\n int value;\n int index;\n\n bool operator < (const Entry& b) const {\n return value < b.value;\n }\n };\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n for(size_t block_begin = 0; block_begin < nums.size(); block_begin += indexDiff) {\n size_t block_end = std::min(block_begin + 2 * indexDiff, nums.size());\n std::vector<Entry> entries;\n for(size_t i = block_begin; i < block_end; i++) {\n entries.push_back(Entry{nums[i], static_cast<int>(i)});\n }\n\n std::sort(entries.begin(), entries.end());\n for(size_t i = 0; i < entries.size(); i++) {\n for(size_t j = i + 1; j < entries.size(); j++) {\n \n if(entries[j].value - entries[i].value > valueDiff) {\n break;\n }\n\n if(std::abs(entries[j].index- entries[i].index) <= indexDiff) {\n return true;\n }\n }\n }\n }\n\n return false;\n }\n};", "memory": "138773" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n struct Entry {\n int value;\n int index;\n\n bool operator < (const Entry& b) const {\n return value < b.value;\n }\n };\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n for(size_t block_begin = 0; block_begin < nums.size(); block_begin += indexDiff) {\n size_t block_end = std::min(block_begin + 2 * indexDiff, nums.size());\n std::vector<Entry> entries;\n for(size_t i = block_begin; i < block_end; i++) {\n entries.push_back(Entry{nums[i], static_cast<int>(i)});\n }\n\n std::sort(entries.begin(), entries.end());\n for(size_t i = 0; i < entries.size(); i++) {\n for(size_t j = i + 1; j < entries.size(); j++) {\n \n if(entries[j].value - entries[i].value > valueDiff) {\n break;\n }\n\n if(std::abs(entries[j].index- entries[i].index) <= indexDiff) {\n return true;\n }\n }\n }\n }\n\n return false;\n }\n};", "memory": "139831" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff,\n int valueDiff) {\n multiset<int> s;\n int n = nums.size();\n for (int i = 0; i <= min(indexDiff, n - 1); i++) {\n s.insert(nums[i]);\n }\n int start = 0, end = indexDiff;\n while (start < nums.size()) {\n s.erase(s.find(nums[start]));\n if (s.size() == 0) {\n break;\n }\n auto findele = s.lower_bound(nums[start] + valueDiff);\n if (findele != s.end()) {\n if (abs(nums[start] - *findele) <= valueDiff) {\n return true;\n }\n }\n if (findele != s.begin()) {\n findele--;\n if (abs(nums[start] - *findele) <= valueDiff) {\n return true;\n }\n }\n if (end < nums.size() - 1) {\n end++;\n s.insert(nums[end]);\n }\n start++;\n }\n return false;\n }\n};", "memory": "146184" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\n public:\n\tbool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n \n valueDiff = (long)valueDiff;\n multiset<long> mp;\n\n for(int i=0; i<nums.size(); i++) {\n // Remove the number out of the window\n if(i > indexDiff) {\n mp.erase(nums[i - indexDiff - 1]);\n }\n\n // check if the value diff is existed\n auto it = mp.lower_bound((long)nums[i] - valueDiff);\n if(it != mp.end() && abs(*it - nums[i]) <= valueDiff) {\n return true;\n }\n\n mp.insert(nums[i]);\n }\n\n return false;\n\t}\n\n \n};", "memory": "146184" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n multiset<int> cur_int;\n int index_min = indexDiff<(nums.size()-1)?indexDiff:(nums.size()-1);\n for (int i = 1; i<=index_min;i++){\n cur_int.emplace(nums[i]);\n }\n for (int i = 0;i<nums.size();i++){\n multiset<int>::iterator it = cur_int.lower_bound(nums[i]);\n if (it!=cur_int.end() && *it-nums[i]<=valueDiff) return true;\n if (it!=cur_int.begin() && nums[i]-*(--it)<=valueDiff) return true;\n if (i+1<nums.size()){\n multiset<int>::iterator it_tmp = cur_int.find(nums[i+1]);\n if (it_tmp!=cur_int.end())cur_int.erase(it_tmp);\n }\n if (i+1+indexDiff<nums.size()){\n cur_int.emplace(nums[i+1+indexDiff]);\n }\n }\n return false;\n }\n};", "memory": "147243" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n multiset<int> cur_int;\n int index_min = indexDiff<(nums.size()-1)?indexDiff:(nums.size()-1);\n for (int i = 1; i<=index_min;i++){\n cur_int.emplace(nums[i]);\n }\n for (int i = 0;i<nums.size();i++){\n multiset<int>::iterator it = cur_int.lower_bound(nums[i]);\n if (it!=cur_int.end() && *it-nums[i]<=valueDiff) return true;\n if (it!=cur_int.begin() && nums[i]-*(--it)<=valueDiff) return true;\n if (i+1<nums.size()){\n multiset<int>::iterator it_tmp = cur_int.find(nums[i+1]);\n if (it_tmp!=cur_int.end())cur_int.erase(it_tmp);\n }\n if (i+1+indexDiff<nums.size()){\n cur_int.emplace(nums[i+1+indexDiff]);\n }\n }\n return false;\n }\n};", "memory": "147243" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n int n = nums.size();\n set<int>ss(nums.begin(),nums.end());\n if(valueDiff == 0 && n == ss.size()) return false;\n for(int i = 0; i<n;++i) {\n for(int j = i+1;j<i+1+indexDiff;++j) {\n if(j >=n) break;\n if(abs((long long)nums[i] - nums[j]) <= valueDiff) return true;\n }\n }\n return false;\n }\n};", "memory": "148301" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n int n = nums.size();\n set<int> ss(nums.begin(), nums.end());\n if(valueDiff==0 && n==ss.size())\n return false;\n\n for(int i =0; i<n; ++i){\n for(int j=i+1; j< i+1+indexDiff; ++j){\n if(j >= n) \n break;\n if(abs((long long)nums[i] - nums[j])<= valueDiff)\n return true;\n }\n }\n return false;\n }\n};", "memory": "148301" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "#include <queue>\n#include <set>\nclass Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n cin.tie(0)->sync_with_stdio(0);\n cout.precision(18);\n int n=nums.size();\n\n if (indexDiff==0)return false;\n multiset<int>s;\n queue<int>q;\n for (int i=0;i<indexDiff&&i<n;i++){\n q.push(nums[i]);\n if (i>0){\n auto left=s.lower_bound(nums[i]-valueDiff);\n auto right = s.upper_bound(nums[i]+valueDiff);\n if (left!=right){\n return true;\n }\n /*if (*right>=nums[i]-valueDiff&&*right<=nums[i]+valueDiff){\n\n cout<<\"a \"<<*right;\n auto it=s.begin();\n while(it!=s.end()){\n cout<<*it<<\" \";\n it++;\n }\n return true;\n }\n if (right!=s.begin()){\n right--;\n }\n if (*right>=nums[i]-valueDiff&&*right<=nums[i]+valueDiff){\n cout<<\"b\";\n return true;\n }\n if (*left<=nums[i]+valueDiff&&*left>=nums[i]-valueDiff){\n cout<<\"c\";\n return true;\n }\n */\n }\n \n s.insert(nums[i]);\n\n }\n\n for (int i=indexDiff;i<n;i++){\n q.push(nums[i]);\n auto left=s.lower_bound(nums[i]-valueDiff);\n auto right = s.upper_bound(nums[i]+valueDiff);\n /*\n if (*right>=nums[i]-valueDiff&&*right<=nums[i]+valueDiff){\n return true;\n }\n if (right!=s.begin()){\n right--;\n }\n if (*right>=nums[i]-valueDiff&&*right<=nums[i]+valueDiff){\n return true;\n }\n if (*left<=nums[i]+valueDiff&&*left>=nums[i]-valueDiff){\n return true;\n }\n */\n if (left!=right){\n return true;\n }\n s.erase(s.find(q.front()));\n q.pop();\n s.insert(nums[i]);\n\n }\n return false;\n }\n};", "memory": "149360" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "// class Solution {\n// public:\n// bool containsNearbyAlmostDuplicate(vector<int>& nums,\n// int indexDiff,\n// int valueDiff) {\n// // value\n// // ^\n// // |\n// // |\n// // |\n// // |\n// // --(indexDiff)--x---------------------> index\n\n// int len = nums.size();\n// map<int, int> m;\n// for (int i = 0; i < min(len, indexDiff); ++i) {\n// m[nums[i]] += 1;\n// }\n\n// for (int i = 0; i < len; ++i) {\n\n// if (i + indexDiff < len) {\n// m[nums[i + indexDiff]] += 1;\n// }\n\n// if (i - indexDiff - 1 >= 0) {\n// int k = nums[i - indexDiff - 1];\n// m[k] -= 1;\n// if (m[k] == 0) {\n// m.erase(k);\n// }\n// }\n\n// int k = nums[i];\n// m[k] -= 1;\n// if (m[k] == 0) {\n// m.erase(k);\n// }\n\n// auto it = m.lower_bound(k);\n// int r = 0;\n// int l = 0;\n// if (it == m.end()) {\n// r = (*(--it)).first;\n// l = (*(--it)).first;\n// } else if (it == m.begin()){\n// r = (*it).first;\n// l = (*it).first;\n// } else {\n// r = (*it).first;\n// l = (*(--it)).first;\n// }\n// if (abs(r - k) <= valueDiff || abs(k - l) <= valueDiff) {\n// return true;\n// }\n\n// m[k] += 1;\n// }\n\n// return false;\n// }\n// };\n\n\n\nclass Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {\n if (nums.size() < 2 || k == 0)\n return false;\n deque<int> windows_deq;\n multiset<long> windows;\n for (int i = 0; i < nums.size(); i++) {\n if (windows.size() > k) {\n int num = windows_deq.front();\n windows_deq.pop_front();\n windows.erase(windows.find(num));\n }\n auto it = windows.lower_bound((long)nums[i] - (long)t);\n if (it == windows.end() || *it > (long)nums[i] + (long)t) {\n // not found\n windows_deq.push_back(nums[i]);\n windows.insert(nums[i]);\n }\n else return true;\n }\n return false;\n }\n};", "memory": "149360" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\nusing namespace __gnu_pbds;\n\n#define ordered_set tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update>\n\nclass Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n ordered_set st;\n\n int n = nums.size(), l=0, r=0;\n\n for(int i=0; i<=min(n-1, indexDiff); i++) {\n l = nums[i]-valueDiff, r = nums[i]+valueDiff;\n\n int lb = st.order_of_key(l), ub = st.order_of_key(r+1);\n\n if(ub-lb>=1) return true;\n\n st.insert(nums[i]);\n }\n\n for(auto i: st) cout<<i<<\" \";\n cout<<endl;\n\n for(int j=0, i=indexDiff+1; i<n; i++, j++) {\n int pos = st.order_of_key(nums[j]);\n auto it = st.find_by_order(pos);\n st.erase(it);\n\n l = nums[i]-valueDiff, r = nums[i]+valueDiff;\n\n int lb = st.order_of_key(l), ub = st.order_of_key(r+1);\n if(ub-lb>=1) return true;\n st.insert(nums[i]);\n }\n\n return false;\n }\n};", "memory": "150419" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "// Time: O(nlogk)\n// Space: O(k)\n\nclass Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {\n if (k < 0 || t < 0) {\n return false;\n }\n \n queue<int64_t> window;\n multiset<int64_t> bst;\n for (int i = 0; i < nums.size(); ++i) {\n // Only keep at most k elements.\n if (bst.size() > k) {\n int num = window.front();\n window.pop();\n bst.erase(bst.find(num));\n }\n // Every search costs time: O(logk).\n const auto it = bst.lower_bound(static_cast<int64_t>(nums[i]) - t);\n if (it == bst.cend() || (*it - nums[i]) > t) {\n // Not found.\n window.emplace(nums[i]);\n bst.emplace(nums[i]);\n } else {\n return true;\n }\n }\n return false;\n }\n};", "memory": "151478" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n unordered_set<int> numsSet(nums.begin(), nums.end());\n if (!valueDiff && numsSet.size() == nums.size()) {\n return false;\n }\n \n int i = 0, j = 1;\n while (j < nums.size()) {\n if (abs(i - j) <= indexDiff && abs(nums[i] - nums[j]) <= valueDiff) {\n return true;\n }\n\n if (abs(i - j) == indexDiff) {\n int temp = i;\n while (i < j) {\n if (abs(nums[i] - nums[j]) <= valueDiff) {\n return true;\n }\n i++;\n }\n i = temp + 1;\n }\n\n if (abs(i - j) < indexDiff) {\n int temp = i;\n while (i < j) {\n if (abs(nums[i] - nums[j]) <= valueDiff) {\n return true;\n }\n i++;\n }\n i = temp;\n }\n\n j++;\n }\n\n return false;\n }\n};", "memory": "152536" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n\n int n = nums.size();\n\n unordered_set<int> st ;\n\n for(auto num : nums) st.insert(num);\n\n if(st.size() == n && valueDiff == 0) return false;\n\n for(int i = 0 ;i < n;i++){\n for(int j = i+1 ;j<= i+indexDiff && j < n;j++){\n long long absDiff = (long long )abs(nums[j] - nums[i]);\n if(absDiff <= (long long)valueDiff) return true;\n }\n }\n \n return false;\n }\n};", "memory": "153595" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& a, int k, int valueDiff) {\n int n = a.size();\n multiset<int> ms;\n for(int i = 0; i < k; i++) ms.insert(a[i]);\n int mn = 1e9;\n for(int i = 0; i < n; i++) {\n ms.erase(ms.find(a[i]));\n if(i+k < n) ms.insert(a[i + k]);\n auto it = ms.lower_bound(a[i]);\n if(it != ms.end()) mn = min(mn, abs(*it - a[i]));\n if(it != ms.begin()) {\n it--;\n mn = min(mn, abs(a[i] - *it));\n }\n }\n cout << mn << \"\\n\";\n return (mn <= valueDiff);\n }\n};", "memory": "154654" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\nprivate:\n multiset<int> multSet;\n multiset<int>::iterator it;\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n for(int i = 0; i <= min(indexDiff, (int)nums.size() - 1); i++){\n multSet.insert(nums[i]);\n }\n\n bool isExist = false;\n for(int i = 0; i + 1 < nums.size(); i++){\n multSet.erase(multSet.find(nums[i]));\n it = multSet.upper_bound(nums[i]);\n if(it != multSet.end() && abs(*it - nums[i]) <= valueDiff) isExist = true;\n if(it != multSet.begin() && abs(*(--it) - nums[i]) <= valueDiff) isExist = true;\n if(i + indexDiff + 1 < nums.size()) multSet.insert(nums[i + indexDiff + 1]);\n }\n\n return isExist;\n }\n};", "memory": "155713" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "#include <set>\n#include <cmath>\n#include <unordered_map>\n\nclass Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n set<int> s;\n unordered_map<int, int> freq;\n\n for (int i = 0; i < nums.size(); i++) {\n if (i > indexDiff) {\n freq[nums[i - indexDiff - 1]]--;\n if (freq[nums[i - indexDiff - 1]] == 0) {\n s.erase(nums[i - indexDiff - 1]);\n }\n }\n auto it = s.lower_bound(nums[i] - valueDiff);\n\n if (it != s.end() && fabs(*it - nums[i]) <= valueDiff) {\n return true;\n }\n\n freq[nums[i]]++;\n s.insert(nums[i]);\n }\n return false;\n }\n};", "memory": "156771" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n\n int end=0,st=0,n=nums.size(),res=INT_MAX;\n set<int> set;\n while(end<n)\n {\n if(end-st>indexDiff)\n {\n set.erase(nums[st]);\n st++;\n }\n auto it=set.lower_bound(nums[end]);\n if(it!=set.end()) res=min(res,abs(nums[end]-*it));\n if(it!=set.begin()) res=min(res,abs(nums[end]-*prev(it)));\n set.insert(nums[end]);\n end++;\n }\n return res<=valueDiff;\n }\n};", "memory": "156771" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n\n int end=0,st=0,n=nums.size(),res=INT_MAX;\n map<int,int> mp;\n while(end<n)\n {\n if(end-st>indexDiff)\n {\n mp[nums[st]]--;\n if(mp[nums[st]]==0) mp.erase(nums[st]);\n st++;\n }\n auto it=mp.upper_bound(nums[end]);\n if(it!=mp.end())\n {\n res=min(res,abs(nums[end]-it->first));\n }\n if(it!=mp.begin())\n {\n res=min(res,abs(nums[end]-prev(it)->first));\n }\n mp[nums[end]]++;\n end++;\n }\n return res<=valueDiff;\n }\n};", "memory": "157830" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n int s = 0, e = 0, n = nums.size(), res = 1e9 + 1;\n map<int, int> mp;\n while(e < n) {\n if(e - s > indexDiff) {\n mp[nums[s]]--;\n if(mp[nums[s]] == 0) mp.erase(nums[s]);\n s++;\n }\n\n auto it = mp.upper_bound(nums[e]);\n if(it != mp.end()) \n res = min(res, abs(nums[e] - it->first));\n if(it != mp.begin()) \n res = min(res, abs(nums[e] - prev(it)->first));\n \n mp[nums[e]]++;\n e++;\n }\n return res <= valueDiff;\n }\n};", "memory": "157830" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) \n {\n set<int> set(nums.begin(),nums.end());\n if(valueDiff==0 and nums.size()==set.size()) return false;\n for(int i=0;i<nums.size();i++)\n {\n for(int j=i+1;j<=i+indexDiff && j<nums.size();j++)\n {\n if(abs(nums[i]-nums[j])<=valueDiff)\n {\n return true;\n\n }\n }\n }\n return false; \n }\n};", "memory": "158889" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n int n=nums.size();\n int l=0;\n multiset<int> st;\n map<int,int> mp;\n \n for(int i=0;i<n;i++){\n mp[nums[i]]++;\n \n while(abs(i-l)>indexDiff){\n auto it=st.find(nums[l]);\n mp[nums[l]]--;\n l++;\n \n st.erase(it);\n }\n auto it=st.lower_bound((nums[i]-valueDiff));\n if(it!=st.end()){\n if(abs(nums[i]-*it)<=valueDiff){\n return true;\n // if(abs(nums[i]-*it)==0){\n // if(mp[nums[i]]>1) return true;\n \n // }\n // else{\n // return true;\n // }\n }\n }\n st.insert(nums[i]);\n \n\n }\n return false;\n }\n};", "memory": "158889" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int valueDiff) {\n multiset<int>st;\n int n=nums.size();\n for(int i=1;i<=min(k,n-1);i++)\n {\n st.insert(nums[i]);\n }\n for(int i=0;i<(n);i++)\n {\n auto it1=st.lower_bound(nums[i]);\n if(it1!=st.end())\n {\n int val1=(*it1);\n if(abs(val1-nums[i])<=valueDiff) return true;\n }\n it1=st.upper_bound(nums[i]);\n if(it1!=st.begin())\n {\n it1--;\n int val1=(*it1);\n if(abs(val1-nums[i])<=valueDiff) return true;\n }\n if((i+k+1)<n) st.insert(nums[(i+k+1)]);\n if((i+1)<n) st.erase(st.find(nums[(i+1)]));\n }\n return false;\n }\n};", "memory": "159948" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n int n = nums.size();\n multiset<int> hh ;\n multiset<int> :: iterator it;\n for( int i =0 ;i <= min( n-1, indexDiff ); i++ )\n {\n hh.insert(nums[i]);\n } \n it = hh.upper_bound(nums[0]);\n if(it!=hh.end())\n {\n if( abs(*it - nums[0]) <=valueDiff )\n return true;\n }\n it--;\n if(it!=hh.begin())\n {\n it--;\n if( abs(*it - nums[0]) <=valueDiff )\n return true;\n }\n \n for( int i = 1 ;i< n ; i++)\n {\n if( i+indexDiff < n )\n {\n hh.insert(nums[i+indexDiff]);\n }\n if( i - indexDiff - 1 >=0 )\n {\n it = hh.find(nums[i-indexDiff-1]);\n hh.erase(it);\n }\n it = hh.upper_bound(nums[i]);\n if(it!=hh.end())\n {\n if( abs(*it - nums[i]) <=valueDiff )\n return true;\n }\n it--;\n if(it!=hh.begin())\n {\n it--;\n if( abs(*it - nums[i]) <=valueDiff )\n return true;\n }\n }\n return false;\n }\n};", "memory": "161006" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n int n = nums.size();\n multiset<int> st;\n indexDiff = min(indexDiff + 1, n);\n for(int i = 0; i < indexDiff; i++) {\n st.insert(nums[i]);\n }\n for(int i = 0; i < n - 1; i++) {\n st.erase(st.find(nums[i]));\n auto lb = st.lower_bound(nums[i]);\n if(lb != st.end() && abs(*lb - nums[i]) <= valueDiff) {\n return true;\n }\n if(lb != st.begin() && abs(*prev(lb) - nums[i]) <= valueDiff) {\n return true;\n }\n if(i >= n - indexDiff) continue;\n st.insert(nums[i + indexDiff]);\n }\n return false;\n }\n};", "memory": "162065" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "// 19:20 - \n\nclass Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n multiset<int> included;\n int posIncluded = 1;\n std::multiset<int>::iterator itlow,itup;\n for (int i=0; i<nums.size(); i++) {\n if (i > 0) {\n included.erase(included.find(nums[i]));\n }\n while (posIncluded < nums.size() && posIncluded - i <= indexDiff) {\n //printf(\". %d %d\\n\", i, nums[posIncluded]);\n included.insert(nums[posIncluded]);\n posIncluded++;\n }\n itlow = included.lower_bound(nums[i]);\n if (itlow != included.end()) {\n //printf(\">0\\n\");\n if (std::abs(nums[i] - *itlow) <= valueDiff) return true;\n }\n itup = included.upper_bound(nums[i]);\n if (itup != included.begin()) {\n itup--;\n //printf(\">1\\n\");\n if (std::abs(nums[i] - *itup) <= valueDiff) return true;\n }\n }\n return false;\n }\n};", "memory": "163124" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool solve(const vector<int>& nums, int indexDiff, int valueDiff) {\n const int n = static_cast<int>(nums.size());\n multiset<int> s;\n for (int i = 0; i < n; ++i) {\n if (i > indexDiff) {\n s.erase(s.find(nums[i - indexDiff - 1]));\n }\n auto it = s.upper_bound(nums[i]);\n if (it != s.begin() && nums[i] - *prev(it) <= valueDiff) {\n return true;\n }\n s.emplace_hint(it, nums[i]);\n }\n return false;\n }\n\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n return solve(nums, indexDiff, valueDiff) || (\n reverse(nums.begin(), nums.end()),\n solve(nums, indexDiff, valueDiff)\n );\n }\n};", "memory": "163124" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {\n multiset<long> ms;\n int n = nums.size();\n multiset<long, greater<long>> ms2;\n k = min(k, n-1);\n if(n == 0) return false;\n for(int i = 0; i <= k; ++i) {\n // ms.insert()\n auto it = ms.lower_bound(nums[i]);\n if(it != ms.end()) {\n // cout << *it << \" \" << nums[i] << endl;\n if(*it - nums[i] <= t) return true;\n }\n auto it2 = ms2.lower_bound(nums[i]);\n if(it2 != ms2.end()) {\n // cout << *it2 << \"X\" << nums[i] << endl;\n if(nums[i] - *it2 <= t) return true;\n }\n ms.insert(nums[i]);\n ms2.insert(nums[i]);\n \n }\n for(int i = k + 1; i < n; ++i) {\n ms.erase(ms.find(nums[i-k-1]));\n ms2.erase(ms2.find(nums[i-k-1]));\n \n auto it = ms.lower_bound(nums[i]);\n if(it != ms.end()) {\n if(*it - nums[i] <= t) return true;\n }\n auto it2 = ms2.lower_bound(nums[i]);\n if(it2 != ms2.end()) {\n if(nums[i] - *it2 <= t) return true;\n }\n \n ms.insert(nums[i]);\n ms2.insert(nums[i]);\n\n }\n return false;\n }\n};", "memory": "164183" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n set<pair<int,int>> pos, neg;\n int n = nums.size();\n for(int i = 0; i < n; i++){\n int cur = nums[i];\n int ncur = -cur;\n auto foundpos = pos.lower_bound({cur,0});\n while(foundpos != pos.end()){\n if(i-foundpos->second > indexDiff){\n pos.erase(foundpos);\n }\n else if(foundpos->first - cur <= valueDiff ){\n return true;\n }\n else {\n break;\n }\n foundpos = pos.lower_bound({cur,0});\n }\n foundpos = neg.lower_bound({ncur,0});\n while(foundpos != neg.end()){\n if(i-foundpos->second > indexDiff){\n neg.erase(foundpos);\n }\n else if(foundpos->first -ncur<= valueDiff ){\n return true;\n }\n else {\n break;\n }\n foundpos = neg.lower_bound({ncur,0});\n }\n pos.insert({cur,i});\n neg.insert({ncur,i});\n }\n return false;\n }\n};", "memory": "164183" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\n bool isPres(vector<int>& nums, int idx, int val){\n multiset<int>ms; \n ms.insert(nums[0]);\n for(int i=1;i<nums.size();i++){\n int l=nums[i]-val,r=nums[i]+val;\n auto it=ms.lower_bound(l);\n if(it!=ms.end() and *it<=r) return true;\n ms.insert(nums[i]);\n if(i-idx>=0) ms.erase(ms.find(nums[i-idx]));\n }\n return false;\n }\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n auto nums2=nums;\n reverse(nums2.begin(),nums2.end());\n return isPres(nums,indexDiff,valueDiff) or isPres(nums2,indexDiff,valueDiff);\n }\n};", "memory": "165241" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n int i, n = nums.size();\n multiset<int> st;\n vector<multiset<int>::iterator> vec(n);\n for(i=0;i<indexDiff;i++) {\n auto item = st.insert(nums[i]);\n vec[i] = item;\n }\n for(i=0;i<n;i++) {\n int lhb = i - indexDiff;\n int rhb = i + indexDiff;\n if(lhb - 1 >=0)\n st.erase(vec[lhb-1]);\n if(rhb < n) {\n auto item = st.insert(nums[rhb]);\n vec[rhb] = item;\n }\n // printf(\"For i:%d\\n\", i);\n // for(auto &item: st)\n // printf(\"%d \", item);\n // printf(\"\\n\");\n int toFindNum = nums[i] - valueDiff;\n auto it = st.lower_bound(toFindNum);\n if(it != st.end() and vec[i] != it)\n return true;\n }\n\n return false;\n }\n};", "memory": "166300" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "#include <ext/pb_ds/assoc_container.hpp> \n#include <ext/pb_ds/tree_policy.hpp> \n\nusing namespace __gnu_pbds;\n\nclass Solution {\npublic:\n using o_set = tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>;\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n int i=0,j=0;\n o_set st;\n while(i<nums.size()){\n if(j<nums.size() && j-i+1<=indexDiff+1){\n if(st.find(nums[j])!=st.end()){\n return true;\n }\n st.insert(nums[j]);\n j++;\n } else if(j<nums.size()) {\n st.erase(st.find(nums[i]));\n i++;\n }\n\n if(j-i==indexDiff+1 || j==nums.size()){\n if(valueDiff==0){\n if(j==nums.size()){\n i++;\n }\n continue;\n }\n int searchMaxVal=nums[i]+valueDiff;\n int searchMinVal=nums[i]-valueDiff;\n\n auto maxValInd=st.order_of_key(searchMaxVal);\n auto valInd=st.order_of_key(nums[i]);\n auto minValInd=st.order_of_key(searchMinVal);\n \n auto maxVal=*st.find_by_order(maxValInd);\n auto val=*st.find_by_order(valInd);\n auto minVal=*st.find_by_order(minValInd);\n\n if(maxVal==searchMaxVal || searchMinVal==minVal){\n return true;\n }\n\n if(maxValInd-valInd+1>2 || (valInd-minValInd+1>1 && minVal>searchMinVal)){\n return true;\n }\n if(j==nums.size()){\n i++;\n }\n }\n }\n return false;\n }\n};\n\n\n/*\n\niD= 2\nvD= 3\n\n1 5 9 1 5 9\n\n\n9 \n5 \n\n\n\n\n\n\n\n\n\n*/", "memory": "167359" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "#include <ext/pb_ds/assoc_container.hpp> \n#include <ext/pb_ds/tree_policy.hpp> \n\nusing namespace __gnu_pbds;\n\nclass Solution {\npublic:\n using o_set = tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>;\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n int i=0,j=0;\n o_set st;\n while(i<nums.size()){\n if(j<nums.size() && j-i+1<=indexDiff+1){\n if(st.find(nums[j])!=st.end()){\n return true;\n }\n st.insert(nums[j]);\n j++;\n } else if(j<nums.size()) {\n st.erase(st.find(nums[i]));\n i++;\n }\n\n if(j-i==indexDiff+1 || j==nums.size()){\n if(valueDiff==0){\n if(j==nums.size()){\n i++;\n }\n continue;\n }\n int searchMaxVal=nums[i]+valueDiff;\n int searchMinVal=nums[i]-valueDiff;\n\n auto maxValInd=st.order_of_key(searchMaxVal);\n auto valInd=st.order_of_key(nums[i]);\n auto minValInd=st.order_of_key(searchMinVal);\n \n auto maxVal=*st.find_by_order(maxValInd);\n auto val=*st.find_by_order(valInd);\n auto minVal=*st.find_by_order(minValInd);\n\n if(maxVal==searchMaxVal || searchMinVal==minVal){\n return true;\n }\n\n if(maxValInd-valInd+1>2 || (valInd-minValInd+1>1 && minVal>searchMinVal)){\n return true;\n }\n if(j==nums.size()){\n i++;\n }\n }\n }\n return false;\n }\n};\n\n\n/*\n\niD= 2\nvD= 3\n\n1 5 9 1 5 9\n\n\n9 \n5 \n\n\n\n\n\n\n\n\n\n*/", "memory": "168418" }
220
<p>You are given an integer array <code>nums</code> and two integers <code>indexDiff</code> and <code>valueDiff</code>.</p> <p>Find a pair of indices <code>(i, j)</code> such that:</p> <ul> <li><code>i != j</code>,</li> <li><code>abs(i - j) &lt;= indexDiff</code>.</li> <li><code>abs(nums[i] - nums[j]) &lt;= valueDiff</code>, and</li> </ul> <p>Return <code>true</code><em> if such pair exists or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 <strong>Output:</strong> true <strong>Explanation:</strong> We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --&gt; 0 != 3 abs(i - j) &lt;= indexDiff --&gt; abs(0 - 3) &lt;= 3 abs(nums[i] - nums[j]) &lt;= valueDiff --&gt; abs(1 - 1) &lt;= 0 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 <strong>Output:</strong> false <strong>Explanation:</strong> After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= indexDiff &lt;= nums.length</code></li> <li><code>0 &lt;= valueDiff &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "#include <ext/pb_ds/assoc_container.hpp> \n#include <ext/pb_ds/tree_policy.hpp> \n\nusing namespace __gnu_pbds;\n\nclass Solution {\npublic:\n using o_set = tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>;\n bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {\n int i=0,j=0;\n o_set st;\n while(i<nums.size()){\n if(j<nums.size() && j-i+1<=indexDiff+1){\n if(st.find(nums[j])!=st.end()){\n return true;\n }\n st.insert(nums[j]);\n j++;\n } else if(j<nums.size()) {\n st.erase(st.find(nums[i]));\n i++;\n }\n\n if(j-i==indexDiff+1 || j==nums.size()){\n if(valueDiff==0){\n if(j==nums.size()){\n i++;\n }\n continue;\n }\n int searchMaxVal=nums[i]+valueDiff;\n int searchMinVal=nums[i]-valueDiff;\n\n auto maxValInd=st.order_of_key(searchMaxVal);\n auto valInd=st.order_of_key(nums[i]);\n auto minValInd=st.order_of_key(searchMinVal);\n \n auto maxVal=*st.find_by_order(maxValInd);\n auto val=*st.find_by_order(valInd);\n auto minVal=*st.find_by_order(minValInd);\n\n if(maxVal==searchMaxVal || searchMinVal==minVal){\n return true;\n }\n\n if(maxValInd-valInd+1>2 || (valInd-minValInd+1>1 && minVal>searchMinVal)){\n return true;\n }\n if(j==nums.size()){\n i++;\n }\n }\n }\n return false;\n }\n};\n\n\n/*\n\niD= 2\nvD= 3\n\n1 5 9 1 5 9\n\n\n9 \n5 \n\n\n\n\n\n\n\n\n\n*/", "memory": "168418" }
222
<p>Given the <code>root</code> of a <strong>complete</strong> binary tree, return the number of the nodes in the tree.</p> <p>According to <strong><a href="http://en.wikipedia.org/wiki/Binary_tree#Types_of_binary_trees" target="_blank">Wikipedia</a></strong>, every level, except possibly the last, is completely filled in a complete binary tree, and all nodes in the last level are as far left as possible. It can have between <code>1</code> and <code>2<sup>h</sup></code> nodes inclusive at the last level <code>h</code>.</p> <p>Design an algorithm that runs in less than&nbsp;<code data-stringify-type="code">O(n)</code>&nbsp;time complexity.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/14/complete.jpg" style="width: 372px; height: 302px;" /> <pre> <strong>Input:</strong> root = [1,2,3,4,5,6] <strong>Output:</strong> 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> 0 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5 * 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 5 * 10<sup>4</sup></code></li> <li>The tree is guaranteed to be <strong>complete</strong>.</li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n static inline auto _ = [] {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::ofstream out(\"user.out\", std::ios::out | std::ios::binary);\n out.rdbuf()->pubsetbuf(nullptr, 256);\n std::string s;\n std::noskipws(std::cin);\n while (std::getline(std::cin, s)) {\n int count = 0;\n bool inNum = false;\n for (char c : s) {\n if (std::isdigit(c) != 0) {\n if (!inNum) {\n ++count;\n inNum = true;\n }\n } else {\n inNum = false;\n }\n }\n out << count << '\\n';\n }\n std::skipws(std::cin);\n out.flush();\n exit(0);\n return 0;\n }();\n\n int countNodes(TreeNode* root) {\n if(root == nullptr) return 0;\n int left = countNodes(root->left);\n int right = countNodes(root->right);\n return 1+left+right;\n }\n};", "memory": "7500" }
222
<p>Given the <code>root</code> of a <strong>complete</strong> binary tree, return the number of the nodes in the tree.</p> <p>According to <strong><a href="http://en.wikipedia.org/wiki/Binary_tree#Types_of_binary_trees" target="_blank">Wikipedia</a></strong>, every level, except possibly the last, is completely filled in a complete binary tree, and all nodes in the last level are as far left as possible. It can have between <code>1</code> and <code>2<sup>h</sup></code> nodes inclusive at the last level <code>h</code>.</p> <p>Design an algorithm that runs in less than&nbsp;<code data-stringify-type="code">O(n)</code>&nbsp;time complexity.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/14/complete.jpg" style="width: 372px; height: 302px;" /> <pre> <strong>Input:</strong> root = [1,2,3,4,5,6] <strong>Output:</strong> 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> 0 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5 * 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 5 * 10<sup>4</sup></code></li> <li>The tree is guaranteed to be <strong>complete</strong>.</li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n static inline auto _ = [] {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::ofstream out(\"user.out\", std::ios::out | std::ios::binary);\n out.rdbuf()->pubsetbuf(nullptr, 256);\n std::string s;\n std::noskipws(std::cin);\n while (std::getline(std::cin, s)) {\n int count = 0;\n bool inNum = false;\n for (char c : s) {\n if (std::isdigit(c) != 0) {\n if (!inNum) {\n ++count;\n inNum = true;\n }\n } else {\n inNum = false;\n }\n }\n out << count << '\\n';\n }\n std::skipws(std::cin);\n out.flush();\n exit(0);\n return 0;\n }();\n int getLeftHeight(TreeNode* root){\n int leftHeight = 0;\n TreeNode* temp = root;\n while(temp){\n leftHeight++;\n temp = temp -> left;\n }\n return leftHeight;\n }\n int getRightHeight(TreeNode* root){\n TreeNode* temp = root;\n int rightHeight = 0;\n while(temp){\n rightHeight++;\n temp = temp -> right;\n }\n return rightHeight;\n }\n int countNodes(TreeNode* root) {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n if(!root){\n return 0;\n }\n int leftNodes = getLeftHeight(root);\n int rightNodes = getRightHeight(root);\n if(leftNodes == rightNodes){\n return pow(2, leftNodes) - 1;\n }\n return 1 + countNodes(root -> left) + countNodes(root -> right);\n }\n};", "memory": "7600" }
222
<p>Given the <code>root</code> of a <strong>complete</strong> binary tree, return the number of the nodes in the tree.</p> <p>According to <strong><a href="http://en.wikipedia.org/wiki/Binary_tree#Types_of_binary_trees" target="_blank">Wikipedia</a></strong>, every level, except possibly the last, is completely filled in a complete binary tree, and all nodes in the last level are as far left as possible. It can have between <code>1</code> and <code>2<sup>h</sup></code> nodes inclusive at the last level <code>h</code>.</p> <p>Design an algorithm that runs in less than&nbsp;<code data-stringify-type="code">O(n)</code>&nbsp;time complexity.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/14/complete.jpg" style="width: 372px; height: 302px;" /> <pre> <strong>Input:</strong> root = [1,2,3,4,5,6] <strong>Output:</strong> 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> 0 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5 * 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 5 * 10<sup>4</sup></code></li> <li>The tree is guaranteed to be <strong>complete</strong>.</li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),\n * right(right) {}\n * };\n */\nclass Solution {\n static inline auto _ = [] {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::ofstream out(\"user.out\", std::ios::out | std::ios::binary);\n out.rdbuf()->pubsetbuf(nullptr, 256);\n std::string s;\n std::noskipws(std::cin);\n while (std::getline(std::cin, s)) {\n int count = 0;\n bool inNum = false;\n for (char c : s) {\n if (std::isdigit(c) != 0) {\n if (!inNum) {\n ++count;\n inNum = true;\n }\n } else {\n inNum = false;\n }\n }\n out << count << '\\n';\n }\n std::skipws(std::cin);\n out.flush();\n exit(0);\n return 0;\n }();\n\npublic:\n int countNodes(TreeNode* root) {\n\n if (root == NULL) {\n return 0;\n }\n\n int sum = 0;\n\n sum += countNodes(root->left) + 1;\n sum += countNodes(root->right);\n\n return sum;\n }\n};", "memory": "7700" }
222
<p>Given the <code>root</code> of a <strong>complete</strong> binary tree, return the number of the nodes in the tree.</p> <p>According to <strong><a href="http://en.wikipedia.org/wiki/Binary_tree#Types_of_binary_trees" target="_blank">Wikipedia</a></strong>, every level, except possibly the last, is completely filled in a complete binary tree, and all nodes in the last level are as far left as possible. It can have between <code>1</code> and <code>2<sup>h</sup></code> nodes inclusive at the last level <code>h</code>.</p> <p>Design an algorithm that runs in less than&nbsp;<code data-stringify-type="code">O(n)</code>&nbsp;time complexity.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/14/complete.jpg" style="width: 372px; height: 302px;" /> <pre> <strong>Input:</strong> root = [1,2,3,4,5,6] <strong>Output:</strong> 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> 0 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5 * 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 5 * 10<sup>4</sup></code></li> <li>The tree is guaranteed to be <strong>complete</strong>.</li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),\n * right(right) {}\n * };\n */\nclass Solution {\n static inline auto _ = [] {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::ofstream out(\"user.out\", std::ios::out | std::ios::binary);\n out.rdbuf()->pubsetbuf(nullptr, 256);\n std::string s;\n std::noskipws(std::cin);\n while (std::getline(std::cin, s)) {\n int count = 0;\n bool inNum = false;\n for (char c : s) {\n if (std::isdigit(c) != 0) {\n if (!inNum) {\n ++count;\n inNum = true;\n }\n } else {\n inNum = false;\n }\n }\n out << count << '\\n';\n }\n std::skipws(std::cin);\n out.flush();\n exit(0);\n return 0;\n }();\n\npublic:\n int countNodes(TreeNode* root) {\n\n if (root == NULL) {\n return 0;\n }\n\n int sum = 0;\n\n sum += countNodes(root->left) + 1;\n sum += countNodes(root->right);\n\n return sum;\n }\n};", "memory": "7800" }
222
<p>Given the <code>root</code> of a <strong>complete</strong> binary tree, return the number of the nodes in the tree.</p> <p>According to <strong><a href="http://en.wikipedia.org/wiki/Binary_tree#Types_of_binary_trees" target="_blank">Wikipedia</a></strong>, every level, except possibly the last, is completely filled in a complete binary tree, and all nodes in the last level are as far left as possible. It can have between <code>1</code> and <code>2<sup>h</sup></code> nodes inclusive at the last level <code>h</code>.</p> <p>Design an algorithm that runs in less than&nbsp;<code data-stringify-type="code">O(n)</code>&nbsp;time complexity.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/01/14/complete.jpg" style="width: 372px; height: 302px;" /> <pre> <strong>Input:</strong> root = [1,2,3,4,5,6] <strong>Output:</strong> 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> root = [] <strong>Output:</strong> 0 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> root = [1] <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5 * 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 5 * 10<sup>4</sup></code></li> <li>The tree is guaranteed to be <strong>complete</strong>.</li> </ul>
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n static inline auto _ = [] {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::ofstream out(\"user.out\", std::ios::out | std::ios::binary);\n out.rdbuf()->pubsetbuf(nullptr, 256);\n std::string s;\n std::noskipws(std::cin);\n while (std::getline(std::cin, s)) {\n int count = 0;\n bool inNum = false;\n for (char c : s) {\n if (std::isdigit(c) != 0) {\n if (!inNum) {\n ++count;\n inNum = true;\n }\n } else {\n inNum = false;\n }\n }\n out << count << '\\n';\n }\n std::skipws(std::cin);\n out.flush();\n exit(0);\n return 0;\n }();\n int getLeftHeight(TreeNode* root){\n int leftHeight = 0;\n TreeNode* temp = root;\n while(temp){\n leftHeight++;\n temp = temp -> left;\n }\n return leftHeight;\n }\n int getRightHeight(TreeNode* root){\n TreeNode* temp = root;\n int rightHeight = 0;\n while(temp){\n rightHeight++;\n temp = temp -> right;\n }\n return rightHeight;\n }\n int countNodes(TreeNode* root) {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n if(!root){\n return 0;\n }\n int leftNodes = getLeftHeight(root);\n int rightNodes = getRightHeight(root);\n if(leftNodes == rightNodes){\n return pow(2, leftNodes) - 1;\n }\n return 1 + countNodes(root -> left) + countNodes(root -> right);\n }\n};", "memory": "7900" }