【LeetCode】334.Increasing Triplet Subsequence(Medium)解题报告
2018-02-22 19:19
417 查看
【LeetCode】334.Increasing Triplet Subsequence(Medium)解题报告
题目地址:https://leetcode.com/problems/increasing-triplet-subsequence/description/
题目描述:
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.
Solution:
Date:2018年2月22日
题目地址:https://leetcode.com/problems/increasing-triplet-subsequence/description/
题目描述:
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.
Solution:
//time : O(n) //space : O(1) class Solution { public boolean increasingTriplet(int[] nums) { int min = Integer.MAX_VALUE , secMin = Integer.MAX_VALUE; for(int num : nums){ if(num <= min) min = num; else if(num < secMin) secMin = num; else if(num > secMin) return true; } return false; } }
Date:2018年2月22日
相关文章推荐
- 【LeetCode】464.Can I Win(Medium)解题报告
- LeetCode解题报告 452. Minimum Number of Arrows to Burst Balloons [medium]
- LeetCode解题报告 413. Arithmetic Slices [medium]
- 【LeetCode】6.ZigZag Conversion(Medium)解题报告
- 【LeetCode】324.Wiggle Sort II(Medium)解题报告
- 【LeetCode】131.Palindrome Partitioning(medium)解题报告
- 【LeetCode】230.Kth Smallest Element in a BST(Medium)解题报告
- 【LeetCode】102.Binary Tree Level Order Traversal(Medium)解题报告
- 【LeetCode】199.Binary Tree Right Side View(Medium)解题报告
- 【LeetCode】382.Linked List Random Node(medium)解题报告
- 【LeetCode】274.H-Index(Medium)解题报告
- LeetCode解题报告 392. Is Subsequence [medium]
- LeetCode解题报告 309. Best Time to Buy and Sell Stock with Cooldown[medium]
- LeetCode解题报告 322. Coin Change [medium]
- LeetCode解题报告 122. Best Time to Buy and Sell Stock II [medium]
- 【LeetCode】103. Binary Tree Zigzag Level Order Traversal(Medium)解题报告
- 【LeetCode】449.Serialize and Deserialize BST(Medium)解题报告
- 【LeetCode】199.Binary Tree Right Side View(Medium)解题报告
- 【LeetCode】238.Product of Array Except Self(Medium)解题报告
- 【LeetCode】75.Sort Colors(Medium)解题报告