📜  第二天温度较高的剩余天数

📅  最后修改于: 2021-10-25 11:25:28             🧑  作者: Mango

给定一个日常温度列表arr[] 。对于每一天,任务是找出第二天温度较高的剩余天数。如果没有可能出现更暖温度的那一天,则打印-1
例子:

朴素的方法:这个想法是迭代数组的每个可能对,并检查每个当前元素的下一个更高的温度。
时间复杂度: O(N 2 )
辅助空间: O(1)
Efficient Approach:这个问题基本上是要求找到当前指数从下一个更高温度的指数到当前指数的温度有多远。解决这个问题的最佳方法是使用堆栈。以下是步骤:

  1. 使用当前索引迭代给定数组arr[]的日常温度。
  2. 如果堆栈为空,则将当前索引压入堆栈。
  3. 如果堆栈不为空,则执行以下操作:
    • 如果当前索引处的温度小于栈顶索引处的温度,则压入当前索引。
    • 如果当前索引处的温度大于堆栈顶部索引处的温度,则更新等待温度升高的天数为:
  • 一旦在输出列表中更新了天数,就弹出堆栈。
  1. 对堆栈中小于当前索引处的温度的所有索引重复上述步骤。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to determine how many days
// required to wait for the next
// warmer temperature
void dailyTemperatures(vector& T)
{
    int n = T.size();
 
    // To store the answer
    vector daysOfWait(n, -1);
    stack s;
 
    // Traverse all the temperatures
    for (int i = 0; i < n; i++) {
 
        // Check if current index is the
        // next warmer temperature of
        // any previous indexes
        while (!s.empty()
               && T[s.top()] < T[i]) {
 
            daysOfWait[s.top()]
                = i - s.top();
 
            // Pop the element
            s.pop();
        }
 
        // Push the current index
        s.push(i);
    }
 
    // Print waiting days
    for (int i = 0; i < n; i++) {
        cout << daysOfWait[i] << " ";
    }
}
 
// Driver Code
int main()
{
    // Given temperatures
    vector arr{ 73, 74, 75, 71,
                     69, 72, 76, 73 };
 
    // Function Call
    dailyTemperatures(arr);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
import java.lang.*;
 
class GFG{
 
// Function to determine how many days
// required to wait for the next
// warmer temperature
static void dailyTemperatures(int[] T)
{
    int n = T.length;
 
    // To store the answer
    int[] daysOfWait = new int[n];
    Arrays.fill(daysOfWait, -1);
     
    Stack s = new Stack<>();
 
    // Traverse all the temperatures
    for(int i = 0; i < n; i++)
    {
         
        // Check if current index is the
        // next warmer temperature of
        // any previous indexes
        while (!s.isEmpty() && T[s.peek()] < T[i])
        {
            daysOfWait[s.peek()] = i - s.peek();
             
            // Pop the element
            s.pop();
        }
         
        // Push the current index
        s.push(i);
    }
 
    // Print waiting days
    for(int i = 0; i < n; i++)
    {
        System.out.print(daysOfWait[i] + " ");
    }
}
 
// Driver Code
public static void main (String[] args)
{
     
    // Given temperatures
    int[] arr = { 73, 74, 75, 71,
                  69, 72, 76, 73 };
     
    // Function call
    dailyTemperatures(arr);
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program for the above approach
 
# Function to determine how many days
# required to wait for the next
# warmer temperature
def dailyTemperatures(T):
 
    n = len(T)
 
    # To store the answer
    daysOfWait = [-1] * n
    s = []
 
    # Traverse all the temperatures
    for i in range(n):
 
        # Check if current index is the
        # next warmer temperature of
        # any previous indexes
        while(len(s) != 0 and
              T[s[-1]] < T[i]):
            daysOfWait[s[-1]] = i - s[-1]
 
            # Pop the element
            s.pop(-1)
 
        # Push the current index
        s.append(i)
 
    # Print waiting days
    for i in range(n):
        print(daysOfWait[i], end = " ")
 
# Driver Code
 
# Given temperatures
arr = [ 73, 74, 75, 71,
        69, 72, 76, 73 ]
 
# Function call
dailyTemperatures(arr)
 
# This code is contributed by Shivam Singh


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to determine how many days
// required to wait for the next
// warmer temperature
static void dailyTemperatures(int[] T)
{
    int n = T.Length;
 
    // To store the answer
    int[] daysOfWait = new int[n];
    for(int i = 0; i < n; i++)
        daysOfWait[i] = -1;
     
    Stack s = new Stack();
 
    // Traverse all the temperatures
    for(int i = 0; i < n; i++)
    {
         
        // Check if current index is the
        // next warmer temperature of
        // any previous indexes
        while (s.Count != 0 && T[s.Peek()] < T[i])
        {
            daysOfWait[s.Peek()] = i - s.Peek();
             
            // Pop the element
            s.Pop();
        }
         
        // Push the current index
        s.Push(i);
    }
 
    // Print waiting days
    for(int i = 0; i < n; i++)
    {
        Console.Write(daysOfWait[i] + " ");
    }
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given temperatures
    int[] arr = { 73, 74, 75, 71,
                  69, 72, 76, 73 };
     
    // Function call
    dailyTemperatures(arr);
}
}
 
// This code is contributed by Amit Katiyar


Javascript


输出:
1 1 4 2 1 1 -1 -1

时间复杂度: O(N)
辅助空间: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程