📜  找出发光时间最长的灯泡

📅  最后修改于: 2021-09-04 08:07:19             🧑  作者: Mango

给定一个由N 个唯一小写字母组成的字符串S和一个长度为N的数组arr[] ,其中字符S[i]表示灯泡, arr[i]表示第i灯泡发光的时间,从时间arr 开始[i – 1] 。任务是找到发光时间最长的灯泡。如果存在多个具有相同最大发光时间的灯泡,则打印字典序较大的灯泡。

例子:

方法:思想是遍历数组,对于每个数组元素,计算arr[i] – arr[i – 1] 。然后,打印具有最长发光时间的按字典顺序排列的较大灯泡。请按照以下步骤解决问题:

  • 初始化两个变量,比如maxDurmaxPos ,分别存储发光时间和具有最大发光时间的灯泡的索引。
  • 遍历给定数组并执行以下步骤:
    • 如果当前时间(arr[i] – arr[i – 1])小于maxCurr ,则将maxCurr更新为maxCurr = arr[i] – arr[i – 1]
    • 否则,如果它等于maxCurr,maxPos不包含任何有效的索引和S [maxPos]按字典顺序小于S [I],更新maxPosmaxPos = I。
  • 完成上述步骤后,打印S[maxPos]作为所需的输出。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the bulb
// having maximum glow
char longestLastingBulb(
    vector onTime, string s)
{
    char ans;
    int n = onTime.size();
 
    // Initialize variables
    int maxDur = INT_MIN;
    int maxPos = INT_MIN;
    int currentDiff = 0;
 
    // Traverse the array consisting
    // of glowing time of the bulbs
    for (int i = 0; i < n; i++) {
 
        // For 1st bulb
        if (i == 0) {
            currentDiff = onTime[i];
            maxDur = currentDiff;
            maxPos = i;
        }
        else {
 
            // Calculate the glowing time
            currentDiff = onTime[i]
                          - onTime[i - 1];
 
            // Update the maximum glow
            if (maxDur < currentDiff) {
                maxDur = currentDiff;
                maxPos = i;
            }
 
            // Find lexicographically
            // largest bulb
            else {
 
                if (maxDur == currentDiff) {
                    char one = s[i];
                    char two = s[maxPos];
 
                    if (one > two) {
                        maxDur = currentDiff;
                        maxPos = i;
                    }
                }
            }
        }
    }
 
    // Bulb with maximum time
    ans = s[maxPos];
 
    // Return the resultant bulb
    return ans;
}
 
// Driver Code
int main()
{
    string S = "spuda";
    vector arr = { 12, 23, 36, 46, 62 };
 
    // Function call
    cout << longestLastingBulb(arr, S);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find the bulb
// having maximum glow
static char longestLastingBulb(
    int []onTime, char []s)
{
    char ans;
    int n = onTime.length;
 
    // Initialize variables
    int maxDur = Integer.MIN_VALUE;
    int maxPos = Integer.MIN_VALUE;
    int currentDiff = 0;
 
    // Traverse the array consisting
    // of glowing time of the bulbs
    for (int i = 0; i < n; i++) {
 
        // For 1st bulb
        if (i == 0) {
            currentDiff = onTime[i];
            maxDur = currentDiff;
            maxPos = i;
        }
        else {
 
            // Calculate the glowing time
            currentDiff = onTime[i]
                          - onTime[i - 1];
 
            // Update the maximum glow
            if (maxDur < currentDiff) {
                maxDur = currentDiff;
                maxPos = i;
            }
 
            // Find lexicographically
            // largest bulb
            else {
 
                if (maxDur == currentDiff) {
                    char one = s[i];
                    char two = s[maxPos];
 
                    if (one > two) {
                        maxDur = currentDiff;
                        maxPos = i;
                    }
                }
            }
        }
    }
 
    // Bulb with maximum time
    ans = s[maxPos];
 
    // Return the resultant bulb
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
    String S = "spuda";
    int []arr = { 12, 23, 36, 46, 62 };
 
    // Function call
    System.out.print(longestLastingBulb(arr, S.toCharArray()));
 
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program for the above approach
import sys
 
INT_MIN = (sys.maxsize - 1)
 
# Function to find the bulb
# having maximum glow
def longestLastingBulb(onTime, s):
     
    n = len(onTime)
 
    # Initialize variables
    maxDur = INT_MIN
    maxPos = INT_MIN
    currentDiff = 0
 
    # Traverse the array consisting
    # of glowing time of the bulbs
    for i in range(n):
 
        # For 1st bulb
        if (i == 0):
            currentDiff = onTime[i]
            maxDur = currentDiff
            maxPos = i
     
        else:
 
            # Calculate the glowing time
            currentDiff = onTime[i] - onTime[i - 1]
 
            # Update the maximum glow
            if (maxDur < currentDiff):
                maxDur = currentDiff
                maxPos = i
 
            # Find lexicographically
            # largest bulb
            else:
 
                if (maxDur == currentDiff):
                    one = s[i]
                    two = s[maxPos]
 
                    if (one > two):
                        maxDur = currentDiff
                        maxPos = i
 
    # Bulb with maximum time
    ans = s[maxPos]
 
    # Return the resultant bulb
    return ans
 
# Driver Code
if __name__ == "__main__" :
 
    S = "spuda"
    arr = [ 12, 23, 36, 46, 62 ]
 
    # Function call
    print(longestLastingBulb(arr, S))
 
# This code is contributed by AnkThon


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG {
     
    // Function to find the bulb
    // having maximum glow
    static char longestLastingBulb(
        List onTime, string s)
    {
        char ans;
        int n = onTime.Count;
       
        // Initialize variables
        int maxDur = Int32.MinValue;
        int maxPos = Int32.MinValue;
        int currentDiff = 0;
       
        // Traverse the array consisting
        // of glowing time of the bulbs
        for (int i = 0; i < n; i++) {
       
            // For 1st bulb
            if (i == 0) {
                currentDiff = onTime[i];
                maxDur = currentDiff;
                maxPos = i;
            }
            else {
       
                // Calculate the glowing time
                currentDiff = onTime[i]
                              - onTime[i - 1];
       
                // Update the maximum glow
                if (maxDur < currentDiff) {
                    maxDur = currentDiff;
                    maxPos = i;
                }
       
                // Find lexicographically
                // largest bulb
                else {
       
                    if (maxDur == currentDiff) {
                        char one = s[i];
                        char two = s[maxPos];
       
                        if (one > two) {
                            maxDur = currentDiff;
                            maxPos = i;
                        }
                    }
                }
            }
        }
       
        // Bulb with maximum time
        ans = s[maxPos];
       
        // Return the resultant bulb
        return ans;
    }
 
  static void Main() {
 
    string S = "spuda";
    List arr = new List(new int[] {12, 23, 36, 46, 62});
   
    // Function call
    Console.Write(longestLastingBulb(arr, S));
  }
}
 
// This code is contributed by divyeshrabadiya07


输出:
a

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live