leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

commit 2ce3e7ab03cc1275ed9be4055a894e25382a542a
parent 169c8dcb61f73078f5dba6ab2311594d039608e2
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Sat,  4 Feb 2023 15:00:14 +0100

Algorithm II: Day 2

Diffstat:
AProblems/0153.cpp | 15+++++++++++++++
AProblems/0162.cpp | 17+++++++++++++++++
MREADME.md | 2++
3 files changed, 34 insertions(+), 0 deletions(-)

diff --git a/Problems/0153.cpp b/Problems/0153.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + int findMin(vector<int> &nums) { + int low = 0, high = nums.size() - 1; + while (low < high) { + if (nums[low] < nums[high]) return nums[low]; + int mid = low + (high - low) / 2; + if (nums[mid] >= nums[low]) + low = mid + 1; + else + high = mid; + } + return nums[low]; + } +}; diff --git a/Problems/0162.cpp b/Problems/0162.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + int findPeakElement(vector<int>& nums) { + int n = nums.size(); + if(n==1) return 0; + + for(int i=0; i<n-1; ) { + if(nums[i]>nums[i+1]) return i; + i+=2; + if(i>=n) break; + if(nums[i-1]>nums[i]) return i-1; + } + if(nums[n-1]>nums[n-2]) return n-1; + + return -1; + } +}; diff --git a/README.md b/README.md @@ -113,8 +113,10 @@ for solving problems. | 0150 | Medium | [Evaluate Reverse Polish Notation](Problems/0150.cpp) | | 0151 | Medium | [Reverse Words in a String](Problems/0151.cpp) | | 0152 | Medium | [Maximum Product Subarray](Problems/0152.cpp) | +| 0153 | Medium | [Find Minimum in Rotated Sorted Array](Problems/0153.cpp) | | 0155 | Medium | [Min Stack](Problems/0155.cpp) | | 0160 | Easy | [Intersection of Two Linked Lists](Problems/0160.cpp) | +| 0162 | Medium | [Find Peak Element](Problems/0162.cpp) | | 0164 | Hard | [Maximum Gap](Problems/0164.cpp) | | 0167 | Medium | [Two Sum II - Input Array Is Sorted](Problems/0167.cpp) | | 0169 | Easy | [Majority Element](Problems/0169.cpp) |