📌  相关文章
📜  给定链表中峰之间的最大距离

📅  最后修改于: 2022-05-13 01:56:05.648000             🧑  作者: Mango

给定链表中峰之间的最大距离

给定一个长度为N的链表lis ,任务是确定给定链表的两个连续峰值之间的最大距离。峰值被定义为具有大于其邻居的值的节点。两个节点之间的距离定义为它们之间存在的节点数。

例子:

方法:解决方案基于贪婪方法。请按照以下步骤解决问题:

  • 创建一个空集来存储链表中的峰值节点。
  • 遍历链表并找到峰值节点。
  • 再次遍历链表并检查当前节点是否为峰值。如果是,则计算最后一个峰值节点和当前节点之间的距离。

下面是上述方法的实现。

C++
// C++ code to implement the above approach
#include 
using namespace std;
 
// Class(struct) for the structure of a node
struct Node {
  int data;
  Node* next;
  Node(int data, Node* next)
    : data(data)
      , next(next)
    {
    }
};
 
// Function to find the maximum distance
void findMaxGap(Node* head)
{
  // Create a set to store
  // all the peak nodes
  set peaks;
 
  // Pre points to previous of current node
  Node* pre = new Node(-1, head);
 
  // Current is initially head
  Node* cur = head;
 
  // Loop till current is not NULL
  while (cur != NULL) {
 
    // Find next of current
    Node* next = cur->next;
 
    // One node only
    if (pre->next == head && next == NULL)
      peaks.insert(cur);
 
    // For the 1st node check only next
    else if (pre->next == head
             && next->data < cur->data)
      peaks.insert(cur);
 
    // For the last node check
    // only the prev node
    else if (next == NULL && pre->data < cur->data)
      peaks.insert(cur);
 
    // Check prev and next nodes for all
    // intermediate nodes
    else if (pre->data < cur->data
             && next->data < cur->data)
      peaks.insert(cur);
 
    // Move pre and cur pointer
    pre = pre->next;
    cur = cur->next;
  }
 
  // Initialize
  int lastPeakIndex = -1, currentIndex = 0, maxGap = 0,
  gap = 0;
 
  cur = head;
 
  // Iterate through the linkedlist
  while (cur != NULL) {
 
    // If current node is a peak
    if (peaks.count(cur)) {
 
      // If current node is first peak
      // then update lastPeakIndex
      // and move ahead
      if (lastPeakIndex == -1)
        lastPeakIndex = currentIndex;
 
      // If current node peak then
      // calculate gap and update
      // lastPeakIndex and move ahead
      else {
        gap = currentIndex - lastPeakIndex - 1;
        maxGap = max(gap, maxGap);
        lastPeakIndex = currentIndex;
      }
    }
 
    // Increment currentIndex
    // move current node ahead
    currentIndex++;
    cur = cur->next;
  }
  cout << maxGap << "\n";
}
 
// Driver code
int main()
{
  // Create the linkedlist
  Node* head = new Node(1, NULL);
  head->next = new Node(2, NULL);
  head->next->next = new Node(3, NULL);
  head->next->next->next = new Node(1, NULL);
  head->next->next->next->next = new Node(5, NULL);
  head->next->next->next->next->next = new Node(4, NULL);
  head->next->next->next->next->next->next
    = new Node(4, NULL);
  head->next->next->next->next->next->next->next
    = new Node(10, NULL);
  head->next->next->next->next->next->next->next->next
    = new Node(7, NULL);
 
  // Function call
  findMaxGap(head);
}
 
// This code is contributed by Taranpreet


Java
// Java code to implement the above approach
import java.io.*;
import java.util.*;
 
// Class for the structure of a node
class Node {
    int data;
    Node next;
    public Node(int data, Node next)
    {
        this.data = data;
        this.next = next;
    }
}
 
class GFG {
 
    // Driver code
    public static void main(String[] args)
    {
        // Create the linkedlist
        Node head = new Node(1, null);
        head.next = new Node(2, null);
        head.next.next = new Node(3, null);
        head.next.next.next = new Node(1, null);
        head.next.next.next.next = new Node(5, null);
        head.next.next.next.next.next = new Node(4, null);
        head.next.next.next.next.next.next
            = new Node(4, null);
        head.next.next.next.next.next.next.next
            = new Node(10, null);
        head.next.next.next.next.next.next.next.next
            = new Node(7, null);
 
        // Function call
        findMaxGap(head);
    }
 
    // Function to find the maximum distance
    static void findMaxGap(Node head)
    {
        // Create a set to store
        // all the peak nodes
        Set peaks = new HashSet<>();
 
        // Pre points to previous of current node
        Node pre = new Node(-1, head);
 
        // Current is initially head
        Node cur = head;
 
        // Loop till current is not null
        while (cur != null) {
 
            // Find next of current
            Node next = cur.next;
 
            // One node only
            if (pre.next == head && next == null)
                peaks.add(cur);
 
            // For the 1st node check only next
            else if (pre.next == head
                     && next.data < cur.data)
                peaks.add(cur);
 
            // For the last node check
            // only the prev node
            else if (next == null
                     && pre.data < cur.data)
                peaks.add(cur);
 
            // Check prev and next nodes for all
            // intermediate nodes
            else if (pre.data < cur.data
                     && next.data < cur.data)
                peaks.add(cur);
 
            // Move pre and cur pointer
            pre = pre.next;
            cur = cur.next;
        }
 
        // Initialize
        int lastPeakIndex = -1,
            currentIndex = 0,
            maxGap = 0, gap = 0;
 
        cur = head;
 
        // Iterate through the linkedlist
        while (cur != null) {
 
            // If current node is a peak
            if (peaks.contains(cur)) {
 
                // If current node is first peak
                // then update lastPeakIndex
                // and move ahead
                if (lastPeakIndex == -1)
                    lastPeakIndex = currentIndex;
 
                // If current node peak then
                // calculate gap and update
                // lastPeakIndex and move ahead
                else {
                    gap = currentIndex
                          - lastPeakIndex - 1;
                    maxGap = Math.max(gap, maxGap);
                    lastPeakIndex = currentIndex;
                }
            }
 
            // Increment currentIndex
            // move current node ahead
            currentIndex++;
            cur = cur.next;
        }
        System.out.println(maxGap);
    }
}


C#
// C# code to implement the above approach
using System;
using System.Collections.Generic;
 
// Class for the structure of a node
class Node {
  public int data;
  public Node next;
  public Node(int data, Node next)
  {
    this.data = data;
    this.next = next;
  }
}
 
public class GFG {
 
  // Driver code
  public static void Main(String[] args)
  {
    // Create the linkedlist
    Node head = new Node(1, null);
    head.next = new Node(2, null);
    head.next.next = new Node(3, null);
    head.next.next.next = new Node(1, null);
    head.next.next.next.next = new Node(5, null);
    head.next.next.next.next.next = new Node(4, null);
    head.next.next.next.next.next.next
      = new Node(4, null);
    head.next.next.next.next.next.next.next
      = new Node(10, null);
    head.next.next.next.next.next.next.next.next
      = new Node(7, null);
 
    // Function call
    findMaxGap(head);
  }
 
  // Function to find the maximum distance
  static void findMaxGap(Node head)
  {
    // Create a set to store
    // all the peak nodes
    HashSet peaks = new HashSet();
 
    // Pre points to previous of current node
    Node pre = new Node(-1, head);
 
    // Current is initially head
    Node cur = head;
 
    // Loop till current is not null
    while (cur != null) {
 
      // Find next of current
      Node next = cur.next;
 
      // One node only
      if (pre.next == head && next == null)
        peaks.Add(cur);
 
      // For the 1st node check only next
      else if (pre.next == head
               && next.data < cur.data)
        peaks.Add(cur);
 
      // For the last node check
      // only the prev node
      else if (next == null
               && pre.data < cur.data)
        peaks.Add(cur);
 
      // Check prev and next nodes for all
      // intermediate nodes
      else if (pre.data < cur.data
               && next.data < cur.data)
        peaks.Add(cur);
 
      // Move pre and cur pointer
      pre = pre.next;
      cur = cur.next;
    }
 
    // Initialize
    int lastPeakIndex = -1,
    currentIndex = 0,
    maxGap = 0, gap = 0;
 
    cur = head;
 
    // Iterate through the linkedlist
    while (cur != null) {
 
      // If current node is a peak
      if (peaks.Contains(cur)) {
 
        // If current node is first peak
        // then update lastPeakIndex
        // and move ahead
        if (lastPeakIndex == -1)
          lastPeakIndex = currentIndex;
 
        // If current node peak then
        // calculate gap and update
        // lastPeakIndex and move ahead
        else {
          gap = currentIndex
            - lastPeakIndex - 1;
          maxGap = Math.Max(gap, maxGap);
          lastPeakIndex = currentIndex;
        }
      }
 
      // Increment currentIndex
      // move current node ahead
      currentIndex++;
      cur = cur.next;
    }
    Console.WriteLine(maxGap);
  }
}
 
// This code is contributed by 29AjayKumar


输出
2

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