Skip to content

added increasingTripletSubsequence #99

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ LeetCode

| # | Title | Solution | Difficulty |
|---| ----- | -------- | ---------- |
|334|[Increasing Triplet Subsequence](https://leetcode.com/problems/increasing-triplet-subsequence/) | [C++](./algorithms/cpp/increasingTripletSubsequence/increasingTripletSubsequence.cpp)|Medium|
|329|[Longest Increasing Path in a Matrix](https://leetcode.com/problems/longest-increasing-path-in-a-matrix/) | [C++](./algorithms/cpp/longestIncreasingPathInAMatrix/LongestIncreasingPathInAMatrix.cpp)|Medium|
|328|[Odd Even Linked List](https://leetcode.com/problems/odd-even-linked-list/) | [C++](./algorithms/cpp/oddEvenLinkedList/OddEvenLinkedList.cpp)|Easy|
|327|[Count of Range Sum](https://leetcode.com/problems/count-of-range-sum/) | [C++](./algorithms/cpp/countOfRangeSum/CountOfRangeSum.cpp)|Hard|
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Source : https://leetcode.com/problems/increasing-triplet-subsequence/
// Author : Calinescu Valentin
// Date : 2016-02-27

/***************************************************************************************
*
* Given an unsorted array return whether an increasing subsequence of length 3 exists
* or not in the array.
*
* Formally the function should:
* Return true if there exists i, j, k
* such that arr[i] < arr[j] < arr[k] given 0 ≤ i < j < k ≤ n-1 else return false.
* Your algorithm should run in O(n) time complexity and O(1) space complexity.
*
* Examples:
* Given [1, 2, 3, 4, 5],
* return true.
*
* Given [5, 4, 3, 2, 1],
* return false.
*
***************************************************************************************/
class Solution {
public:
bool increasingTriplet(vector<int>& nums) {
bool solution = false;
if(nums.size())
{
int first = nums[0];
int second = 0x7fffffff; //MAX_INT so we can always find something smaller than it
for(int i = 1; i < nums.size() && !solution; i++)
{
if(nums[i] > second)
solution = true;
else if(nums[i] > first && nums[i] < second)
second = nums[i];
else if(nums[i] < first)
first = nums[i];
}
}
return solution;
}
};