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

📅  最后修改于: 2021-04-24 16:20:18             🧑  作者: Mango

给定了每天的温度清单arr [] 。对于每一天,任务是找到温度升高的第二天剩余的天数。如果没有这样一天,可以打印-1
例子:

天真的方法:这个想法是针对数组的每个可能的对进行迭代,并检查每个当前元素的下一个更高的温度。

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

高效的方法:这个问题基本上是要找出当前指数与下一个较高温度的指数到当前指数的温度之间的距离。解决此问题的最佳方法是利用堆栈。步骤如下:

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

下面是上述方法的实现:

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


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

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