mirror of
https://github.com/Manoj-HV30/dsa-competitive-programming.git
synced 2026-05-16 19:35:22 +00:00
leetcode new problems
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
class Solution {
|
||||
public:
|
||||
bool isTrionic(vector<int>& nums) {
|
||||
int n = nums.size(), i = 1;
|
||||
while (i < n && nums[i - 1] < nums[i]) {
|
||||
i++;
|
||||
}
|
||||
int p = i - 1;
|
||||
while (i < n && nums[i - 1] > nums[i]) {
|
||||
i++;
|
||||
}
|
||||
int q = i - 1;
|
||||
while (i < n && nums[i - 1] < nums[i]) {
|
||||
i++;
|
||||
}
|
||||
int flag = i - 1;
|
||||
return (p != 0) && (q != p) && (flag == n - 1 && flag != q);
|
||||
}
|
||||
};
|
||||
|
||||
// Uses single pointer, and checks for all 3 required segments one after one.a
|
||||
@@ -0,0 +1,20 @@
|
||||
class Solution {
|
||||
public :
|
||||
bool isTrionic(vector<int> &nums){
|
||||
int n =nums.size();
|
||||
if(n<3) return false;
|
||||
int p = 0;
|
||||
int q = n-1;
|
||||
while(p+1 <n && nums[p] < nums[p+1]) p++;
|
||||
while(q-1>=0 && nums[q] > nums[q-1]) q--;
|
||||
if(p==0 || q==n-1) return false;
|
||||
while(p+1<=q){
|
||||
if(nums[p] > nums[p+1]) p++;
|
||||
else return false;
|
||||
}
|
||||
return p==q;
|
||||
}
|
||||
};
|
||||
|
||||
// Uses two pointers, TC : o(n) , SC : o(1)
|
||||
// finds valid p and q if they exist, adn checks for strictly decreasing between p...q
|
||||
@@ -0,0 +1,20 @@
|
||||
class Solution {
|
||||
public:
|
||||
bool isTrionic(vector<int>& nums) {
|
||||
int n = nums.size();
|
||||
if (nums[0] >= nums[1]) {
|
||||
return false;
|
||||
}
|
||||
int count = 1;
|
||||
for (int i = 2; i < n; i++) {
|
||||
if (nums[i - 1] == nums[i]) {
|
||||
return false;
|
||||
}
|
||||
if ((nums[i - 2] - nums[i - 1]) * (nums[i - 1] - nums[i]) < 0) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count == 3;
|
||||
}
|
||||
};
|
||||
// count denotes the number of monotonic segments and 3 elements are considered at a time, and checked for sign flip, if theres sign flip then segment has changed, starting from increasing segment, if count ==3 then it has three monotonic segments and hence return true
|
||||
Reference in New Issue
Block a user