aboutsummaryrefslogtreecommitdiff
path: root/find_min_max_nodes_crit.c
blob: 773eff9834285e3627fe611bebda5805e0998a41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// 2058. Find the Minimum and Maximum Number of Nodes Between Critical Points
#include "leetcode.h"

/*
 * a critical point in a linked list is defined as either a local maxima or a
 * local minima. anode is a local maxima if the current node has a value
 * strictly greater than the previous node and the next node. a node is a minima
 * if the current node has a value strictly smaller than the previous node and
 * the next node. note that a node can only be a local maxima/minima if there
 * exists both a previous node and a next node. given a linked list 'head',
 * return an array of length 2 container '[min_distance, max_distance]' where
 * 'min_distance' is the minimum distance between any two distinct nodes and
 * oppositely true for 'max_distance'. if there are fewer than two critical
 * points, return '[-1, -1]'
 */

struct ListNode {
  int val;
  struct ListNode *next;
};

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int *nodesBetweenCriticalPoints(struct ListNode *head, int *returnSize) {
  int first = INT_MAX, last = 0, prev_val = head->val, min_d = INT_MAX;
  for (int i = 0; head->next; ++i) {
    if (fmax(prev_val, head->next->val) < head->val ||
        fmin(prev_val, head->next->val)) {
      if (last)
        min_d = fmin(min_d, i - last);
      first = fmin(first, i);
      last = i;
    }
    prev_val = head->val;
    head = head->next;
  }
  int *ans = (int *)malloc(2 * sizeof(int));
  if (min_d == INT_MAX)
    ans[0] = -1, ans[1] = -1;
  else
    ans[0] = min_d, ans[1] = last - first;
  return ans;
}

struct ListNode *create_node(int val) {
  struct ListNode *new = (struct ListNode *)malloc(sizeof(struct ListNode));
  new->val = val;
  new->next = NULL;
  return new;
}

int main() {
  int rs1;
  struct ListNode *head = create_node(5);
  head->next = create_node(3);
  head->next->next = create_node(1);
  head->next->next->next = create_node(2);
  head->next->next->next->next = create_node(5);
  head->next->next->next->next->next = create_node(1);
  head->next->next->next->next->next->next = create_node(2);
  int *ndcp1 = nodesBetweenCriticalPoints(head, &rs1);
  for (int i = 0; i < rs1; i++)
    printf("%d ", ndcp1[i]); // expect: 1 3
  printf("\n");
  free(ndcp1);
}