id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
0
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) {\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 for(int i = 0; i < v.size() - 1; i...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
0
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) {\n int n=nums.size();\n vector<pair<int,int>>dp;\n for(int i=0;i<n;i++)\n {\n dp.push_back({nums[i],i});\n }\n sort(dp.begin(),dp.end());\n for(int i=1;i<n;i++)...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
0
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) {\n int n=nums.size();\n vector<pair<int,int>>dp;\n for(int i=0;i<n;i++)\n {\n dp.push_back({nums[i],i});\n }\n sort(dp.begin(),dp.end());\n for(int i=1;i<n;i++)...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
0
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) {\n vector<pair<int,int>>v;\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 for(int i=0; i<v.size()-1; i++){\n i...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
0
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) {\n if(k==0)return false;\n int n = nums.size();\n unordered_map<int,int> mp;\n for(int i = 0; i<min(n, k); i++) {\n mp[nums[i]]++;\n }\n for(auto m: mp) {\n ...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
0
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) {\n queue<int> persist;\n unordered_set<int> seen;\n\n for (int i : nums) {\n if (persist.size() > k) {\n seen.erase(persist.front());\n persist.pop();\n ...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
0
{ "code": "//projit jednou pole, ukladat si poslednich k + 1 hodnot\n//ukladani do unordered_map [value] index\n\n\nclass Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) \n {\n if(k == 0)\n {\n return false;\n }\n\n deque<int> lastK;\n u...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
0
{ "code": "//projit jednou pole, ukladat si poslednich k hodnot\n//ukladani do unordered_set a poradi v deque\n\n\nclass Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) \n {\n if(k == 0)\n {\n return false;\n }\n\n deque<int> lastK;\n un...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
0
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) \n {\n unordered_set<int> set;\n int n = nums.size();\n \n k = min(k, n - 1);\n \n for (int i = 0; i <= k; ++i) set.emplace(nums[i]);\n \n if (set.size() < k + 1)...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
0
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) {\n int left = 0;\n std::list<int> st;\n for(int right=0; right<nums.size(); right++){\n if(right-left>k){\n left++;\n st.pop_front();\n }\n ...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
0
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) {\n // Hashmap\n // unordered_map<int, int> my_umap;\n\n // for (int i=0; i<nums.size(); i++){\n // if (my_umap.find(nums[i]) == my_umap.end()){\n // my_umap[nums[i]] = i;\n ...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
0
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) {\n // want to create subarry window of size k+1\n\n unordered_set<int> window;\n if (nums.size()==1)\n return 0;\n\n for(int i=0;i<=min((int)nums.size()-1,k);i++){\n wind...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
0
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) {\n // key = nums[i], val = i\n unordered_map<int, int> mp;\n\n for (int i = 0; i < nums.size(); i++) {\n if (mp.contains(nums.at(i)) && i - mp.at(nums[i]) <= k) {\n return t...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
0
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) {\n unordered_map<int, int> map {};\n for (int i=0; i<nums.size(); i++) {\n auto it = map.find(nums[i]);\n if (it != map.end()) {\n if (i - it->second <= k) {\n ...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
0
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) {\n std::unordered_map<int, int> numsMap;\n \n for (int i = 0; i < nums.size(); i++) {\n std::unordered_map<int, int>::iterator it = numsMap.find(nums[i]);\n if (it != numsMap.en...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
0
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) {\n unordered_map<int, int> latest_num;\n for (int i = 0; i < nums.size(); i++) {\n auto [it, inserted] = latest_num.try_emplace(nums[i], i);\n if (inserted)\n continue;\...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
0
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) {\n int i=0, j=0;\n unordered_map<int, int> m; \n\n for(; i<=k & i<nums.size(); i++){\n m[nums[i]]++;\n\n if(m[nums[i]] >= 2 ){\n return true;\n }\n ...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
0
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) {\n unordered_map<int, int> numToIndex;\n numToIndex.reserve(nums.size());\n for (int idx = 0; idx < nums.size(); ++idx) {\n const int num = nums[idx];\n auto itLastIndex = numTo...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
0
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) {\n unordered_map<int, int> numToIndex;\n numToIndex.reserve(nums.size());\n for (int idx = 0; idx < nums.size(); ++idx) {\n const int num = nums[idx];\n auto itLastIndex = numTo...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
1
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) {\n unordered_map<int,int>m;\n for(int i = 0;i<nums.size();i++){\n if(m[nums[i]] == 0){\n m[nums[i]] = i+1;\n }else{\n // some checks\n if((...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
1
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) {\n unordered_map<int,int> mp;\n int n = nums.size();\n for(int i=0; i<n; i++){\n if(mp.count(nums[i])){\n if(abs(i-mp[nums[i]])<=k) return true;\n }\n ...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
3
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) {\n unordered_map<int, int>m;\n k++;\n\n //store first window frequencies in map\n for(int i=0; i<k&&i<nums.size(); i++)\n {\n m[nums[i]]++;\n }\n for(auto i:m)\...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
3
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) {\n unordered_map<int,int>umap;\n int n=nums.size();\n for(int i=0;i<n;i++)\n {\n umap[nums[i]]=-1;\n }\n for(int i=0;i<n;i++)\n {\n if(umap[nums[i]]!...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
3
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) {\n unordered_map<int,int>umap;\n int n=nums.size();\n for(int i=0;i<n;i++)\n {\n umap[nums[i]]=-1;\n }\n for(int i=0;i<n;i++)\n {\n if(umap[nums[i]]!...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
3
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) {\n unordered_map<int,int>mp;\n int n=nums.size();\n for(int i=0;i<n;i++){\n mp[nums[i]]++;\n }\n for(int i=0;i<n;i++){\n if(mp[nums[i]]==1) {\n cont...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
3
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) {\n std::unordered_map<int, int> mp(k+1);\n for (int i = 0; i < nums.size(); i++) {\n if (mp.find(nums[i]) != mp.end()) {\n if (i - mp[nums[i]] <= k) {\n return t...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
3
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) \n {\n int n = nums.size();\n int min = INT_MAX;\n map<int,int>mp;\n for(int i =0;i<n;i++){\n if(mp.find(nums[i]) != mp.end()){\n int idx = mp[nums[i]];\n ...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
3
{ "code": "class Solution\n{\n public:\n // O(n) \n bool containsNearbyDuplicate(vector<int>& nums, int k)\n {\n unordered_map<int, size_t> map;\n for (size_t i = 0; i < nums.size(); ++i)\n {\n if (map.count(nums[i]))\n {\n if (i - map[nums[i]] <= k)...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
3
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) {\n map<int,int> m;\n queue<int> q;\n\n for (int i = 0; i < k + 1; i++) {\n if (i >= nums.size()) {\n return false;\n }\n if (m[nums[i]] > 0) {\n ...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
3
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) {\n unordered_map<int, pair<int, int>> mp;\n int n = nums.size();\n for (int i = 0; i < n; i++) {\n if (mp.find(nums[i]) == mp.end()) {\n mp[nums[i]].first = i;\n ...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
3
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) {\n unordered_map<int, int[2]> numCounts;\n int n = nums.size();\n\n //Initialize all to zero.\n for(int num: nums) {\n numCounts[num][0] = -1;\n numCounts[num][1] = INT_M...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
3
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) {\n unordered_map<int, int[2]> numCounts;\n int n = nums.size();\n\n //Initialize all to zero.\n for(int num: nums) {\n numCounts[num][0] = -1;\n numCounts[num][1] = INT_M...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
3
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) {\n unordered_map<int, int[2]> numCounts;\n int n = nums.size();\n\n //Initialize all to zero.\n for(int num: nums) {\n numCounts[num][0] = -1;\n numCounts[num][1] = INT_M...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
3
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) {\n if(!k) return 0;\n multiset<int> exi;\n int n = nums.size();\n int l = 1, r = min(n - 1, k);\n for(int i = l; i <= r; i++) exi.insert(nums[i]);\n for(int i = 0; i < n; i++) {\...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
3
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) {\n unordered_map<int, int> hashTable;\n list<int> l;\n for(int i=0; i<nums.size(); i++) {\n if(hashTable.find(nums[i]) != hashTable.end())\n return true;\n hashTa...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
3
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) {\n unordered_map<int, int> hashTable;\n list<int> l;\n for(int i=0; i<nums.size(); i++) {\n if(hashTable.find(nums[i]) != hashTable.end())\n return true;\n hashTa...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
3
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) {\n unordered_set<int> s;\n list<int> l;\n int i = 0;\n while (i < nums.size()) {\n if (l.size() == k + 1) {\n int first = l.front();\n l.erase(l.begin(...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
3
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) {\n int left = 0;\n std::list<int> st;\n std::unordered_set<int> mp;\n for(int right=0; right<nums.size(); right++){\n st.push_back(right);\n if(right-left>k){\n ...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
3
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) {\n if(k <= 0)\n return 0;\n unordered_map<int, int> hashTable;\n hashTable.reserve(k);\n list<int> l;\n for(int i=0; i<nums.size(); i++) {\n if(hashTable.find(nums...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
3
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) {\n if(k <= 0)\n return false;\n unordered_set<int> hashTable;\n hashTable.reserve(k);\n list<int> l;\n for(int i=0; i<nums.size(); i++) {\n if(hashTable.find(nums[...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
3
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) {\n set<int> setTrack;\n int l = 0;\n bool ans = false;\n for(int r = 0; r < nums.size(); r++){\n if(abs(l - r) > k){\n setTrack.erase(nums[l]);\n l++;\...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
3
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) {\n int n = nums.size();\n int mn = INT_MAX;\n unordered_set<int>st;\n int b = 0;\n int l = 0;\n for(int r =0;r<n;r++){\n if(st.count(nums[r]) == 0){\n st.insert(nums[r]);\n }...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
3
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) {\n unordered_map<int,int> mp1,mp2;\n for(int i=0;i<nums.size();i++)\n {\n if(mp1[nums[i]]>=1)\n {\n if(abs(i-mp2[nums[i]])<=k)\n {\n ...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
3
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) {\n unordered_map<int,int>m;\n unordered_set<int>s;\n int size=nums.size();\n for(int i=0;i<size;i++){\n if(!(s.insert(nums[i]).second)){\n if(abs(m[nums[i]]-i)<=k)\n...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
3
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) {\n unordered_map<int,int>m;\n unordered_map<int,int>m1;\n for(int i=0;i<nums.size();i++){\n m1[nums[i]]++;\n cout<<m1[nums[i]]<<\" \";\n if(m1[nums[i]]>1 && abs(m[num...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
3
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) {\n bool res = false;\n unordered_map<int,int>mp,last;\n for(int i=0;i<nums.size();i++)\n {\n if (mp.find(nums[i]) == mp.end())\n {\n mp[nums[i]] = 1;\n ...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
3
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) {\n unordered_map<int,int>m;\n unordered_map<int,int>m1;\n for(int i=0;i<nums.size();i++){\n m1[nums[i]]++;\n if(m1[nums[i]]>1 && abs(m[nums[i]]-i)<=k) {\n return ...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
3
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) {\n int n=nums.size(),i,j;\n unordered_map<int,int> m,m1;\n for(i=0;i<n;i++)\n {\n m1[nums[i]]=-1;\n }\n for(i=0;i<n;i++)\n {\n \n m[nums[i]...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
3
{ "code": "struct LLNode {\n int val;\n LLNode * next;\n LLNode * prev;\n LLNode(int v):val(v),next(nullptr),prev(nullptr){};\n};\n\nstruct LRU {\n LRU(int m):max(m){}; \n map<int,LLNode*> tracker;\n LLNode* list = nullptr;\n LLNode* tail = nullptr;\n int count = 0;\n int max;\n\n bo...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
3
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) {\n unordered_map<int, int> freq, lastind;\n int mini = INT_MAX;\n for(int i=0; i<nums.size(); i++) {\n if(freq[nums[i]] == 0) {\n freq[nums[i]]++;\n lastind[n...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
3
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) {\n unordered_map<int, vector<int>> mymap;\n for (int i = 0; i < nums.size(); i++) {\n if (mymap.find(nums[i]) == mymap.end()) {\n mymap.emplace(nums[i], vector<int>{i});\n ...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
3
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) {\n unordered_map<int,vector<int>>mp;\n for(int i=0;i<nums.size();i++){\n if(mp.find(nums[i])==mp.end()){\n mp[nums[i]].push_back(i);\n }\n else {\n ...
219
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if there are two <strong>distinct indices</strong> </em><code>i</code><em> and </em><code>j</code><em> in the array such that </em><code>nums[i] == nums[j]</code><em> and </em><code>abs(i - j) &lt;= k</code>.</p> <p...
3
{ "code": "class Solution {\npublic:\n bool containsNearbyDuplicate(vector<int>& nums, int k) {\n unordered_map<int, vector<int>> map;\n for(int i = 0; i<nums.size(); i++){\n if(map.find(nums[i]) != map.end()){\n vector<int> values = map[nums[i]];\n if(abs(val...
1,185
<p><em>(This problem is an <strong>interactive problem</strong>.)</em></p> <p>You may recall that an array <code>arr</code> is a <strong>mountain array</strong> if and only if:</p> <ul> <li><code>arr.length &gt;= 3</code></li> <li>There exists some <code>i</code> with <code>0 &lt; i &lt; arr.length - 1</code> such ...
0
{ "code": "class Solution {\npublic:\n // Binary search to find the peak of the mountain\n int findPeak(MountainArray &mountainArr) {\n int low = 0;\n int high = mountainArr.length() - 1;\n \n while (low < high) {\n int mid = low + (high - low) / 2;\n if (mounta...
1,185
<p><em>(This problem is an <strong>interactive problem</strong>.)</em></p> <p>You may recall that an array <code>arr</code> is a <strong>mountain array</strong> if and only if:</p> <ul> <li><code>arr.length &gt;= 3</code></li> <li>There exists some <code>i</code> with <code>0 &lt; i &lt; arr.length - 1</code> such ...
0
{ "code": "/**\n * // This is the MountainArray's API interface.\n * // You should not implement it, or speculate about its implementation\n * class MountainArray {\n * public:\n * int get(int index);\n * int length();\n * };\n */\n\nclass Solution {\npublic:\n int findInMountainArray(int target, Mountai...
1,185
<p><em>(This problem is an <strong>interactive problem</strong>.)</em></p> <p>You may recall that an array <code>arr</code> is a <strong>mountain array</strong> if and only if:</p> <ul> <li><code>arr.length &gt;= 3</code></li> <li>There exists some <code>i</code> with <code>0 &lt; i &lt; arr.length - 1</code> such ...
0
{ "code": "/**\n * // This is the MountainArray's API interface.\n * // You should not implement it, or speculate about its implementation\n * class MountainArray {\n * public:\n * int get(int index);\n * int length();\n * };\n */\n\nclass Solution {\npublic:\n\n int peakIndexInMountainArray(MountainArra...
1,185
<p><em>(This problem is an <strong>interactive problem</strong>.)</em></p> <p>You may recall that an array <code>arr</code> is a <strong>mountain array</strong> if and only if:</p> <ul> <li><code>arr.length &gt;= 3</code></li> <li>There exists some <code>i</code> with <code>0 &lt; i &lt; arr.length - 1</code> such ...
0
{ "code": "/**\n * // This is the MountainArray's API interface.\n * // You should not implement it, or speculate about its implementation\n * class MountainArray {\n * public:\n * int get(int index);\n * int length();\n * };\n */\n\nclass Solution {\npublic:\n int findInMountainArray(int target, Mountai...
1,185
<p><em>(This problem is an <strong>interactive problem</strong>.)</em></p> <p>You may recall that an array <code>arr</code> is a <strong>mountain array</strong> if and only if:</p> <ul> <li><code>arr.length &gt;= 3</code></li> <li>There exists some <code>i</code> with <code>0 &lt; i &lt; arr.length - 1</code> such ...
0
{ "code": "/**\n * // This is the MountainArray's API interface.\n * // You should not implement it, or speculate about its implementation\n * class MountainArray {\n * public:\n * int get(int index);\n * int length();\n * };\n */\n\nclass Solution {\n int peakElement(MountainArray &arr){\n int n ...
1,185
<p><em>(This problem is an <strong>interactive problem</strong>.)</em></p> <p>You may recall that an array <code>arr</code> is a <strong>mountain array</strong> if and only if:</p> <ul> <li><code>arr.length &gt;= 3</code></li> <li>There exists some <code>i</code> with <code>0 &lt; i &lt; arr.length - 1</code> such ...
2
{ "code": "/**\n * // This is the MountainArray's API interface.\n * // You should not implement it, or speculate about its implementation\n * class MountainArray {\n * public:\n * int get(int index);\n * int length();\n * };\n */\n\nclass Solution {\npublic:\n int findInMountainArray(int target, Mountai...
1,185
<p><em>(This problem is an <strong>interactive problem</strong>.)</em></p> <p>You may recall that an array <code>arr</code> is a <strong>mountain array</strong> if and only if:</p> <ul> <li><code>arr.length &gt;= 3</code></li> <li>There exists some <code>i</code> with <code>0 &lt; i &lt; arr.length - 1</code> such ...
2
{ "code": "/**\n * // This is the MountainArray's API interface.\n * // You should not implement it, or speculate about its implementation\n * class MountainArray {\n * public:\n * int get(int index);\n * int length();\n * };\n */\n\nclass Solution {\npublic:\n int findInMountainArray(int target, Mountai...
1,195
<p>We distribute some&nbsp;number of <code>candies</code>, to a row of <strong><code>n =&nbsp;num_people</code></strong>&nbsp;people in the following way:</p> <p>We then give 1 candy to the first person, 2 candies to the second person, and so on until we give <code>n</code>&nbsp;candies to the last person.</p> <p>The...
0
{ "code": "class Solution {\npublic:\n vector<int> distributeCandies(int candies, int num_people) {\n vector<int>v(num_people,0);\n int j=1,i=0;\n while(candies>0)\n { if (j>candies)\n j=candies;\n v[i]+=j;\n candies-=j;\n i++;\n ...
1,195
<p>We distribute some&nbsp;number of <code>candies</code>, to a row of <strong><code>n =&nbsp;num_people</code></strong>&nbsp;people in the following way:</p> <p>We then give 1 candy to the first person, 2 candies to the second person, and so on until we give <code>n</code>&nbsp;candies to the last person.</p> <p>The...
0
{ "code": "class Solution {\npublic:\n vector<int> distributeCandies(int candies, int num_people) {\n int n = num_people;\n vector<int> ans(n, 0);\n int i = 0, temp = 1;\n while(candies > 0){\n if(i >= n && candies != 0){\n i = 0;\n }\n if...
1,195
<p>We distribute some&nbsp;number of <code>candies</code>, to a row of <strong><code>n =&nbsp;num_people</code></strong>&nbsp;people in the following way:</p> <p>We then give 1 candy to the first person, 2 candies to the second person, and so on until we give <code>n</code>&nbsp;candies to the last person.</p> <p>The...
0
{ "code": "class Solution {\npublic:\n vector<int> distributeCandies(int c, int n) {\n int x = (sqrt(1+8ll*c) - 1) / (2*n);\n\n vector<int> ans(n, 0);\n for(int i=0; i<n; i++)\n {\n ans[i] = ( (x*(x-1) ) / 2 )*n + x*(i+1);\n }\n\n int used = ( (n*x) * ((n*x)+1) ...
1,195
<p>We distribute some&nbsp;number of <code>candies</code>, to a row of <strong><code>n =&nbsp;num_people</code></strong>&nbsp;people in the following way:</p> <p>We then give 1 candy to the first person, 2 candies to the second person, and so on until we give <code>n</code>&nbsp;candies to the last person.</p> <p>The...
0
{ "code": "class Solution {\npublic:\n vector<int> distributeCandies(int candies, int num_people) {\n int n = num_people;\n vector<int> ans(n,0);\n int i = 0, temp = 1;\n while(candies>0){\n if(i>=n && candies!=0){\n i = 0;\n }\n if(temp <...
1,195
<p>We distribute some&nbsp;number of <code>candies</code>, to a row of <strong><code>n =&nbsp;num_people</code></strong>&nbsp;people in the following way:</p> <p>We then give 1 candy to the first person, 2 candies to the second person, and so on until we give <code>n</code>&nbsp;candies to the last person.</p> <p>The...
0
{ "code": "class Solution {\npublic:\n vector<int> distributeCandies(int c, int n) {\n int x = (sqrt(1+8ll*c) - 1) / (2*n);\n\n vector<int> ans(n, 0);\n for(int i=0; i<n; i++)\n {\n ans[i] = ( (x*(x-1) ) / 2 )*n + x*(i+1);\n }\n\n int used = ( (n*x) * ((n*x)+1) ...
2,645
<p>There are <code>n</code> people standing in a line labeled from <code>1</code> to <code>n</code>. The first person in the line is holding a pillow initially. Every second, the person holding the pillow passes it to the next person standing in the line. Once the pillow reaches the end of the line, the direction chang...
0
{ "code": "class Solution {\npublic:\n int passThePillow(int n, int time) {\n int i = 1, dir = 1;\n while(time>0){\n if(i == n){\n dir = -1;\n }\n else if(i == 1){\n dir = 1;\n }\n i += dir;\n time--;\n }\n return i;\n }\n};", "me...
2,645
<p>There are <code>n</code> people standing in a line labeled from <code>1</code> to <code>n</code>. The first person in the line is holding a pillow initially. Every second, the person holding the pillow passes it to the next person standing in the line. Once the pillow reaches the end of the line, the direction chang...
0
{ "code": "class Solution {\npublic:\n int passThePillow(int n, int time) {\n int p = 1;\n while (time > 0) {\n while (p < n && time > 0) {\n p++;\n time--;\n }\n while (p > 1 && time > 0) {\n p--;\n time--;\n }\n } \n\n ...
2,645
<p>There are <code>n</code> people standing in a line labeled from <code>1</code> to <code>n</code>. The first person in the line is holding a pillow initially. Every second, the person holding the pillow passes it to the next person standing in the line. Once the pillow reaches the end of the line, the direction chang...
0
{ "code": "class Solution {\npublic:\n int passThePillow(int n, int time) {\n\n /*\n int direction = 0;\n int currentPosition =1;\n \n while( time > 0)\n {\n if( time >=n )\n {\n currentPosition = currentPosition + ( direction * n);\n ...
2,645
<p>There are <code>n</code> people standing in a line labeled from <code>1</code> to <code>n</code>. The first person in the line is holding a pillow initially. Every second, the person holding the pillow passes it to the next person standing in the line. Once the pillow reaches the end of the line, the direction chang...
0
{ "code": "class Solution {\npublic:\n int passThePillow(int n, int time) {\n if (n > time)\n return time + 1;\n int chunks = time / (n - 1);\n if(chunks % 2){\n return n - (time % (n-1));\n }\n return time % (n-1) + 1;\n }\n};", "memory": "7300" }
2,645
<p>There are <code>n</code> people standing in a line labeled from <code>1</code> to <code>n</code>. The first person in the line is holding a pillow initially. Every second, the person holding the pillow passes it to the next person standing in the line. Once the pillow reaches the end of the line, the direction chang...
0
{ "code": "class Solution {\npublic:\n int passThePillow(int n, int time) {\n int pass=time%(2*(n-1));\n int turn=time/(n-1);\n if(pass<n-1){\n return pass+1;\n }else if(pass==n-1){\n return n;\n }else{\n return n-(pass-n+1);\n }\n }\n};...
2,645
<p>There are <code>n</code> people standing in a line labeled from <code>1</code> to <code>n</code>. The first person in the line is holding a pillow initially. Every second, the person holding the pillow passes it to the next person standing in the line. Once the pillow reaches the end of the line, the direction chang...
0
{ "code": "class Solution {\npublic:\n int passThePillow(int n, int time) {\n int count = 1;\n int dir = true;\n while(time--)\n {\n if(dir == true)\n {\n count++;\n }\n else\n {\n count--;\n ...
541
<p>Given a string <code>s</code> and an integer <code>k</code>, reverse the first <code>k</code> characters for every <code>2k</code> characters counting from the start of the string.</p> <p>If there are fewer than <code>k</code> characters left, reverse all of them. If there are less than <code>2k</code> but greater ...
0
{ "code": "class Solution {\npublic:\n string reverseStr(string s, int k) {\n int n = s.size();\n int i=0;\n while(i<n){\n if((i+k)<=n){\n int st = i;\n int e = i+k-1;\n while(st<=e){\n swap(s[st++],s[e--]);\n ...
541
<p>Given a string <code>s</code> and an integer <code>k</code>, reverse the first <code>k</code> characters for every <code>2k</code> characters counting from the start of the string.</p> <p>If there are fewer than <code>k</code> characters left, reverse all of them. If there are less than <code>2k</code> but greater ...
0
{ "code": "class Solution final {\npublic:\n inline string reverseStr(string &str, const int k) const noexcept {\n const auto last{end(str)};\n auto iter{begin(str)};\n while (iter != last) {\n const auto nextIter{iter + min(last - iter, ptrdiff_t(k))};\n reverse(iter, ne...
541
<p>Given a string <code>s</code> and an integer <code>k</code>, reverse the first <code>k</code> characters for every <code>2k</code> characters counting from the start of the string.</p> <p>If there are fewer than <code>k</code> characters left, reverse all of them. If there are less than <code>2k</code> but greater ...
0
{ "code": "class Solution final {\npublic:\n inline string reverseStr(string &str, const int k) const noexcept {\n const auto last{end(str)};\n auto iter{begin(str)};\n while (iter != last) {\n const auto nextIter{iter + min(last - iter, ptrdiff_t(k))};\n reverse(iter, ne...
541
<p>Given a string <code>s</code> and an integer <code>k</code>, reverse the first <code>k</code> characters for every <code>2k</code> characters counting from the start of the string.</p> <p>If there are fewer than <code>k</code> characters left, reverse all of them. If there are less than <code>2k</code> but greater ...
0
{ "code": "class Solution {\npublic:\n string reverseStr(string str, int k) {\n for (int start = 0, len = str.size(); start < len; start += (k * 2)) {\n \n int end = min(start + k, len);\n\n reverse(str.begin() + start, str.begin() + end);\n }\n \n return s...
541
<p>Given a string <code>s</code> and an integer <code>k</code>, reverse the first <code>k</code> characters for every <code>2k</code> characters counting from the start of the string.</p> <p>If there are fewer than <code>k</code> characters left, reverse all of them. If there are less than <code>2k</code> but greater ...
0
{ "code": "class Solution {\npublic:\n string reverseStr(string s, int k) {\n int n= s.size();\n int start=0,end=k-1;\n while(start<n){\n int l = start;\n if(end>=n){\n end=n-1;\n }\n while(start<=end){\n swap(s[start],s...
541
<p>Given a string <code>s</code> and an integer <code>k</code>, reverse the first <code>k</code> characters for every <code>2k</code> characters counting from the start of the string.</p> <p>If there are fewer than <code>k</code> characters left, reverse all of them. If there are less than <code>2k</code> but greater ...
0
{ "code": "class Solution {\npublic:\n string reverseStr(string s, int k) {\n if(s.length()<k){\n reverse(s.begin(),s.end());\n return s;\n }\n int n = s.length();\n int i = 0;\n while (i < n) {\n int end = min(i + k, n);\n if(i>n){\n ...
541
<p>Given a string <code>s</code> and an integer <code>k</code>, reverse the first <code>k</code> characters for every <code>2k</code> characters counting from the start of the string.</p> <p>If there are fewer than <code>k</code> characters left, reverse all of them. If there are less than <code>2k</code> but greater ...
0
{ "code": "class Solution {\npublic:\n string reverseStr(string s, int k) {\n int j = 0;\n if (k >= s.length()) {\n reverse(s.begin(), s.end());\n return s;\n } else {\n for (int i = 0; i <= s.size() / (2 * k); i++) {\n if ((s.size() - j) > k) {\...
541
<p>Given a string <code>s</code> and an integer <code>k</code>, reverse the first <code>k</code> characters for every <code>2k</code> characters counting from the start of the string.</p> <p>If there are fewer than <code>k</code> characters left, reverse all of them. If there are less than <code>2k</code> but greater ...
1
{ "code": "class Solution {\npublic:\n string reverseStr(string s, int k) {\n if(s.length()<k){\n reverse(s.begin(),s.end());\n return s;\n }\n int n = s.length();\n int i = 0;\n while (i < n) {\n int end = min(i + k, n);\n if(i>n){\n ...
541
<p>Given a string <code>s</code> and an integer <code>k</code>, reverse the first <code>k</code> characters for every <code>2k</code> characters counting from the start of the string.</p> <p>If there are fewer than <code>k</code> characters left, reverse all of them. If there are less than <code>2k</code> but greater ...
1
{ "code": "class Solution {\npublic:\n string reverseStr(string s, int k) {\n int j=0;\n if(k>=s.length()){\n reverse(s.begin(),s.end());\n return s;\n }\n else{\n for(int i=0;i<=s.size()/(2*k);i++){\n if((s.size()-j)>k){\n ...
541
<p>Given a string <code>s</code> and an integer <code>k</code>, reverse the first <code>k</code> characters for every <code>2k</code> characters counting from the start of the string.</p> <p>If there are fewer than <code>k</code> characters left, reverse all of them. If there are less than <code>2k</code> but greater ...
3
{ "code": "class Solution {\npublic:\n string reverseStr(string s, int k) {\n int n= s.size();\n int start=0,end=k-1;\n while(start<n){\n int l = start;\n if(end>=n){\n end=n-1;\n }\n while(start<=end){\n swap(s[start],s...
541
<p>Given a string <code>s</code> and an integer <code>k</code>, reverse the first <code>k</code> characters for every <code>2k</code> characters counting from the start of the string.</p> <p>If there are fewer than <code>k</code> characters left, reverse all of them. If there are less than <code>2k</code> but greater ...
3
{ "code": "class Solution {\npublic:\n string reverseStr(string s, int k) {\n int n = s.size();\n for (int i = 0; i < n; i += 2 * k) {\n if (i + k <= n) {\n reverse(s.begin() + i, s.begin() + i + k);\n } else {\n reverse(s.begin() + i, s.end());\n ...
541
<p>Given a string <code>s</code> and an integer <code>k</code>, reverse the first <code>k</code> characters for every <code>2k</code> characters counting from the start of the string.</p> <p>If there are fewer than <code>k</code> characters left, reverse all of them. If there are less than <code>2k</code> but greater ...
3
{ "code": "class Solution {\npublic:\n string reverseStr(string s, int k) {\n if(k > s.size())\n {\n reverse(s.begin(), s.end());\n return s;\n } \n int flag = 0;\n string re = s;\n while(flag < re.size() )\n {\n if(flag + k > re.siz...
541
<p>Given a string <code>s</code> and an integer <code>k</code>, reverse the first <code>k</code> characters for every <code>2k</code> characters counting from the start of the string.</p> <p>If there are fewer than <code>k</code> characters left, reverse all of them. If there are less than <code>2k</code> but greater ...
3
{ "code": "#define MAX(a,b) ((a > b) ? a : b)\n#define MIN(a,b) ((a < b) ? a : b)\n\nclass Solution {\npublic:\n string reverseStr(string s, int k) {\n string out;\n for (int i = 0; i < (s.size()/(2*k)) + 1; i++){\n int start = i * 2*k;\n int end = start + k;\n end = ...
541
<p>Given a string <code>s</code> and an integer <code>k</code>, reverse the first <code>k</code> characters for every <code>2k</code> characters counting from the start of the string.</p> <p>If there are fewer than <code>k</code> characters left, reverse all of them. If there are less than <code>2k</code> but greater ...
3
{ "code": "class Solution {\npublic:\n string reverseStr(string s, int k) {\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(NULL);\n\n string ans = s;\n for (int i = 0; i < s.length(); i += 2 * k) {\n if (i + k <= s.length()) {\n reverse(ans.begin() + i...
541
<p>Given a string <code>s</code> and an integer <code>k</code>, reverse the first <code>k</code> characters for every <code>2k</code> characters counting from the start of the string.</p> <p>If there are fewer than <code>k</code> characters left, reverse all of them. If there are less than <code>2k</code> but greater ...
3
{ "code": "#define MIN(a,b) ((a < b) ? a : b)\n\nclass Solution {\npublic:\n string reverseStr(string s, int k) {\n string out;\n for (int i = 0; i < (s.size()/(2*k)) + 1; i++){\n int start = i * 2*k;\n int end = start + k;\n end = MIN(end,s.size());\n for ...
541
<p>Given a string <code>s</code> and an integer <code>k</code>, reverse the first <code>k</code> characters for every <code>2k</code> characters counting from the start of the string.</p> <p>If there are fewer than <code>k</code> characters left, reverse all of them. If there are less than <code>2k</code> but greater ...
3
{ "code": "class Solution {\npublic:\n string reverseStr(string s, int k) {\n string new_string = \"\";\n int max_len = int(s.size()) - 1;\n for (int i = 0;i < s.size();i += 2 * k) {\n int l_i = i;\n int r_i = min(max_len, i + 2 * k - 1);\n for (int j = min(l_i...
541
<p>Given a string <code>s</code> and an integer <code>k</code>, reverse the first <code>k</code> characters for every <code>2k</code> characters counting from the start of the string.</p> <p>If there are fewer than <code>k</code> characters left, reverse all of them. If there are less than <code>2k</code> but greater ...
3
{ "code": "#define MAX(a,b) ((a > b) ? a : b)\n#define MIN(a,b) ((a < b) ? a : b)\n\nclass Solution {\npublic:\n string reverseStr(string s, int k) {\n string out;\n for (int i = 0; i < (s.size()/(2*k)) + 1; i++){\n cout << \"i = \" << i << endl;\n int start = i * 2*k;\n ...
541
<p>Given a string <code>s</code> and an integer <code>k</code>, reverse the first <code>k</code> characters for every <code>2k</code> characters counting from the start of the string.</p> <p>If there are fewer than <code>k</code> characters left, reverse all of them. If there are less than <code>2k</code> but greater ...
3
{ "code": "class Solution {\npublic:\n string reverseStr(string s, int k) {\n int rev = 0;\n string ans = \"\", prev=\"\";\n for(int i=0;i<s.length();i++){\n if(i%k == 0){\n if(!prev.empty()){\n if(rev == 1){\n for(int j = pre...
541
<p>Given a string <code>s</code> and an integer <code>k</code>, reverse the first <code>k</code> characters for every <code>2k</code> characters counting from the start of the string.</p> <p>If there are fewer than <code>k</code> characters left, reverse all of them. If there are less than <code>2k</code> but greater ...
3
{ "code": "class Solution {\npublic:\n string reverseStr(string s, int k) {\n vector<char> vr;\n string sg = \"\";\n int cnt = 0;\n for(char r: s){\n if(cnt> 0){ \n sg += r;\n cnt--;\n }else if(vr.size()< k) vr.push_back(r);\n ...
541
<p>Given a string <code>s</code> and an integer <code>k</code>, reverse the first <code>k</code> characters for every <code>2k</code> characters counting from the start of the string.</p> <p>If there are fewer than <code>k</code> characters left, reverse all of them. If there are less than <code>2k</code> but greater ...
3
{ "code": "class Solution {\npublic:\n string reverseStr(string s, int k) {\n string t,ans;\n bool flag=false;\n for(int i=0;i<s.size()+1;i++){\n if(i!=0 && i%k==0){\n if(flag==false){\n reverse(t.begin(),t.end());\n flag=true;\n ...
541
<p>Given a string <code>s</code> and an integer <code>k</code>, reverse the first <code>k</code> characters for every <code>2k</code> characters counting from the start of the string.</p> <p>If there are fewer than <code>k</code> characters left, reverse all of them. If there are less than <code>2k</code> but greater ...
3
{ "code": "class Solution {\npublic:\n string reverseStr(string s, int k) {\n string t,ans;\n bool flag=false;\n for(int i=0;i<s.size()+1;i++){\n if(i!=0 && i%k==0){\n if(flag==false){\n reverse(t.begin(),t.end());\n flag=true;\n ...
541
<p>Given a string <code>s</code> and an integer <code>k</code>, reverse the first <code>k</code> characters for every <code>2k</code> characters counting from the start of the string.</p> <p>If there are fewer than <code>k</code> characters left, reverse all of them. If there are less than <code>2k</code> but greater ...
3
{ "code": "class Solution {\npublic:\n string reverseStr(string s, int k) {\n int n = s.size();\n string str;\n bool ans = true ;\n for(int i = 0; i<n; i = i+k){\n int mini = min(k, n-i);\n string strr = s.substr(i, mini);\n if(ans){\n rev...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
0
{ "code": "class Solution { // 48 ms, faster than 99.64%\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>> &mat) {\n int m = mat.size(), n = mat[0].size(), INF = m + n; // The distance of cells is up to (M+N)\n for (int r = 0; r < m; r++) {\n for (int c = 0; c < n; c++) {\n ...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
0
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n int n = mat.size(), m = mat[0].size(), e = 1e5;\n for(int i=0; i<n; i++) {\n for(int j=0; j<m; j++) {\n if (mat[i][j] != 0) {\n int top,left;\n ...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
0
{ "code": "\nclass Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n int n=mat.size();\n int m=mat[0].size();\n vector<vector<int>>dp(n,vector<int>(m,INT_MAX-2));\n //normal case so will go for top and left only\n for (int i=0;i<n;i++){\n ...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
0
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n int n = mat.size();\n int m = mat[0].size();\n vector<vector<int>> dp(n, vector<int>(m, INT_MAX-2));\n\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n ...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
0
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n int m = mat.size();\n int n = mat[0].size();\n\n vector<vector<int>> dp;\n\n for(vector<int>& row:mat){\n dp.emplace_back(row.begin(), row.end());\n }\n\n for(i...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
0
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n int m = mat.size();\n int n = mat[0].size();\n vector<vector<int>> dp;\n for (vector<int>& row:mat){\n dp.emplace_back(row.begin(), row.end());\n }\n for (int r...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
0
{ "code": "class Solution {\npublic:\n int ret(vector<vector<int>>& mat, int i, int j, vector<vector<int>>& dp) {\n if (i < 0 || i>=mat.size() || j<0 || j>=mat[0].size() || mat[i][j] == 2)\n return 1e5;\n if (mat[i][j] == 0) return 0;\n if (dp[i][j] != 0) return dp[i][j];\n mat[i][j] = 2;...