给定一个数组arr[] ,任务是检查是否存在三元组(i, j, k)使得arr[i]
例子:
Input: arr[] = {1, 2, 3, 4, 5}
Output: No
Explanation:
There is no such sub-sequence such that arr[i] < arr[k] < arr[j]
Input: arr[] = {3, 1, 5, 0, 4}
Output: Yes
Explanation:
There exist a triplet (3, 5, 4) which is arr[i] < arr[k] < arr[j]
朴素的方法:这个想法是生成所有可能的三元组,如果有任何三元组满足给定的条件,则打印Yes else 打印No 。
时间复杂度: O(N 3 )
辅助空间: O(1)
高效的方法:优化上述方法的想法是使用堆栈来跟踪数组arr[]中每个元素右侧的较小元素。以下是步骤:
- 从末尾遍历数组并维护一个以降序存储元素的堆栈。
- 要按降序维护堆栈,请弹出小于当前元素的元素。然后将弹出的元素标记为三元组的第三个元素。
- 如果任何元素小于最后弹出的元素(标记为三元组的第三个元素),则反向遍历数组。然后它们存在一个满足给定条件的三元组并打印Yes 。
- 否则打印No 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if there exist
// triplet in the array such that
// i < j < k and arr[i] < arr[k] < arr[j]
bool findTriplet(vector& arr)
{
int n = arr.size();
stack st;
// Initialize the heights of h1 and h2
// to INT_MAX and INT_MIN respectively
int h3 = INT_MIN, h1 = INT_MAX;
for (int i = n - 1; i >= 0; i--) {
// Store the current element as h1
h1 = arr[i];
// If the element at top of stack
// is less than the current element
// then pop the stack top
// and keep updating the value of h3
while (!st.empty()
&& st.top() < arr[i]) {
h3 = st.top();
st.pop();
}
// Push the current element
// on the stack
st.push(arr[i]);
// If current element is less
// than h3, then we found such
// triplet and return true
if (h1 < h3) {
return true;
}
}
// No triplet found, hence return false
return false;
}
// Driver Code
int main()
{
// Given array
vector arr = { 4, 7, 5, 6 };
// Function Call
if (findTriplet(arr)) {
cout << " Yes" << '\n';
}
else {
cout << " No" << '\n';
}
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to check if there exist
// triplet in the array such that
// i < j < k and arr[i] < arr[k] < arr[j]
public static boolean findTriplet(int[] arr)
{
int n = arr.length;
Stack st = new Stack<>();
// Initialize the heights of h1 and h2
// to INT_MAX and INT_MIN respectively
int h3 = Integer.MIN_VALUE;
int h1 = Integer.MAX_VALUE;
for(int i = n - 1; i >= 0; i--)
{
// Store the current element as h1
h1 = arr[i];
// If the element at top of stack
// is less than the current element
// then pop the stack top
// and keep updating the value of h3
while (!st.empty() && st.peek() < arr[i])
{
h3 = st.peek();
st.pop();
}
// Push the current element
// on the stack
st.push(arr[i]);
// If current element is less
// than h3, then we found such
// triplet and return true
if (h1 < h3)
{
return true;
}
}
// No triplet found, hence return false
return false;
}
// Driver code
public static void main(String[] args)
{
// Given array
int arr[] = { 4, 7, 5, 6 };
// Function call
if (findTriplet(arr))
{
System.out.println("Yes");
}
else
{
System.out.println("No");
}
}
}
// This code is contributed by divyeshrabadiya07
Python3
# Python3 program for the above approach
import sys
# Function to check if there exist
# triplet in the array such that
# i < j < k and arr[i] < arr[k] < arr[j]
def findTriplet(arr):
n = len(arr)
st = []
# Initialize the heights of h1 and h3
# to INT_MAX and INT_MIN respectively
h3 = -sys.maxsize - 1
h1 = sys.maxsize
for i in range(n - 1, -1, -1):
# Store the current element as h1
h1 = arr[i]
# If the element at top of stack
# is less than the current element
# then pop the stack top
# and keep updating the value of h3
while (len(st) > 0 and st[-1] < arr[i]):
h3 = st[-1]
del st[-1]
# Push the current element
# on the stack
st.append(arr[i])
# If current element is less
# than h3, then we found such
# triplet and return true
if (h1 < h3):
return True
# No triplet found, hence
# return false
return False
# Driver Code
if __name__ == '__main__':
# Given array
arr = [ 4, 7, 5, 6 ]
# Function Call
if (findTriplet(arr)):
print("Yes")
else:
print("No")
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to check if there exist
// triplet in the array such that
// i < j < k and arr[i] < arr[k] < arr[j]
public static bool findTriplet(int[] arr)
{
int n = arr.Length;
Stack st = new Stack();
// Initialize the heights of h1 and h2
// to INT_MAX and INT_MIN respectively
int h3 = int.MinValue;
int h1 = int.MaxValue;
for(int i = n - 1; i >= 0; i--)
{
// Store the current element as h1
h1 = arr[i];
// If the element at top of stack
// is less than the current element
// then pop the stack top
// and keep updating the value of h3
while (st.Count != 0 && st.Peek() < arr[i])
{
h3 = st.Peek();
st.Pop();
}
// Push the current element
// on the stack
st.Push(arr[i]);
// If current element is less
// than h3, then we found such
// triplet and return true
if (h1 < h3)
{
return true;
}
}
// No triplet found, hence return false
return false;
}
// Driver code
public static void Main(String[] args)
{
// Given array
int []arr = { 4, 7, 5, 6 };
// Function call
if (findTriplet(arr))
{
Console.WriteLine("Yes");
}
else
{
Console.WriteLine("No");
}
}
}
// This code is contributed by PrinciRaj1992
Javascript
输出:
Yes
时间复杂度: O(N)
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。