Skip to content

Commit

Permalink
Two New Problems "Contains Duplicate II/III"
Browse files Browse the repository at this point in the history
  • Loading branch information
haoel committed Jun 12, 2015
1 parent 9c00550 commit 323a979
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ LeetCode

| # | Title | Solution | Difficulty |
|---| ----- | -------- | ---------- |
|204|[Contains Duplicate III](https://leetcode.com/problems/contains-duplicate-iii/)| [C++](./algorithms/containsDuplicate/ContainsDuplicate.III.cpp)|Medium|
|203|[Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/)| [C++](./algorithms/containsDuplicate/ContainsDuplicate.II.cpp)|Easy|
|202|[The Skyline Problem](https://leetcode.com/problems/the-skyline-problem/)| [C++](./algorithms/theSkylineProblem/TheSkylineProblem.cpp)|Hard|
|201|[Contains Duplicate](https://leetcode.com/problems/contains-duplicate/)| [C++](./algorithms/containsDuplicate/ContainsDuplicate.cpp)|Easy|
|200|[Combination Sum III](https://leetcode.com/problems/combination-sum-iii/)| [C++](./algorithms/combinationSum/combinationSum.III.cpp)|Medium|
Expand Down
28 changes: 28 additions & 0 deletions algorithms/containsDuplicate/ContainsDuplicate.II.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Source : https://leetcode.com/problems/contains-duplicate-ii/
// Author : Hao Chen
// Date : 2015-06-12

/**********************************************************************************
*
* Given an array of integers and an integer k, find out whether there there are
* two distinct indices i and j in the array such that nums[i] = nums[j] and
* the difference between i and j is at most k.
*
*
**********************************************************************************/

class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
unordered_map<int, int> m;
for (int i=0; i<nums.size(); i++) {
int n = nums[i];
if (m.find(n) != m.end() && i - m[n] <= k) {
return true;
}
m[n] = i;
}
return false;
}
};

40 changes: 40 additions & 0 deletions algorithms/containsDuplicate/ContainsDuplicate.III.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Source : https://leetcode.com/problems/contains-duplicate-iii/
// Author : Hao Chen
// Date : 2015-06-12

/**********************************************************************************
*
* Given an array of integers, find out whether there are two distinct indices i and j
* in the array such that the difference between nums[i] and nums[j] is at most t and
* the difference between i and j is at most k.
*
**********************************************************************************/



class Solution {
public:
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
if(nums.size() < 2 || k == 0) return false;
int low = 0;
//maintain a sliding window
set<long long> window;
for (int i=0; i<nums.size(); i++){
//make sure window size <= k
if (i - low > k) {
window.erase(nums[low]);
low++;
}

// lower_bound() is the key,
// it returns an iterator pointing to the first element >= val
auto it = window.lower_bound((long long)nums[i] - (long long)t );
if (it != window.end() && abs((long long)nums[i] - *it) <= (long long)t) {
return true;
}
window.insert(nums[i]);
}
return false;
}
};

0 comments on commit 323a979

Please sign in to comment.