Algorithm II: Day 2
Diffstat:
3 files changed, 34 insertions(+), 0 deletions(-)
@@ -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];
}
};
@@ -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;
}
};
@@ -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) |