From 86da1d183a89e3379d886655624571c8643f388b Mon Sep 17 00:00:00 2001 From: Lam Haoyin Date: Wed, 12 Jan 2022 02:29:53 +0800 Subject: [PATCH] add: 220112-CN --- 2201/220112-CN.cpp | 32 ++++++++++++++++++++++++++++++++ 2201/CMakeLists.txt | 2 +- 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 2201/220112-CN.cpp diff --git a/2201/220112-CN.cpp b/2201/220112-CN.cpp new file mode 100644 index 0000000..72dffcc --- /dev/null +++ b/2201/220112-CN.cpp @@ -0,0 +1,32 @@ +#include + +/** + * 334. Increasing Triplet Subsequence + * Given an integer array nums, return true if there exists a triple of indices (i, j, k) such that i < j < k and nums[i] < nums[j] < nums[k]. If no such indices exists, return false. + */ + +class Solution { +public: + static bool increasingTriplet(const std::vector& nums) { + int n = nums.size(); + int overallMinimum = nums[0], solutionMinimum = nums[0], mid = 0x7FFFFFFF; + for (int i = 1; i < n; ++i) { + overallMinimum = std::min(overallMinimum, nums[i]); + if (nums[i] > mid) { + return true; + } + if (nums[i] > overallMinimum && nums[i] <= mid) { + solutionMinimum = overallMinimum; + mid = nums[i]; + } + if (nums[i] > solutionMinimum && nums[i] < mid) { + mid = nums[i]; + } + } + return false; + } +}; + +int main() { + return !Solution::increasingTriplet({2, 1, 5, 0, 4, 6}); +} diff --git a/2201/CMakeLists.txt b/2201/CMakeLists.txt index 2f58bdf..20a78c9 100644 --- a/2201/CMakeLists.txt +++ b/2201/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2201) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2201 220111.cpp) \ No newline at end of file +ADD_EXECUTABLE(2201 220112-CN.cpp) \ No newline at end of file