Skip to content

Commit 5588fbe

Browse files
authored
feat: add typescript solution to lc problem: No.2058 (doocs#599)
No.2058.Find the Minimum and Maximum Number of Nodes Between Critical Points
1 parent 897a0d6 commit 5588fbe

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed

solution/2000-2099/2058.Find the Minimum and Maximum Number of Nodes Between Critical Points/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,48 @@
9999

100100
```
101101

102+
### **TypeScript**
103+
104+
```ts
105+
/**
106+
* Definition for singly-linked list.
107+
* class ListNode {
108+
* val: number
109+
* next: ListNode | null
110+
* constructor(val?: number, next?: ListNode | null) {
111+
* this.val = (val===undefined ? 0 : val)
112+
* this.next = (next===undefined ? null : next)
113+
* }
114+
* }
115+
*/
116+
117+
function nodesBetweenCriticalPoints(head: ListNode | null): number[] {
118+
let idx = 1;
119+
let pre = head.val;
120+
head = head.next;
121+
let nums = [];
122+
while(head.next != null) {
123+
let val = head.val, post = head.next.val;
124+
if (pre < val && val > post) {
125+
nums.push(idx);
126+
}
127+
if (pre > val && val < post) {
128+
nums.push(idx);
129+
}
130+
pre = val;
131+
idx++;
132+
head = head.next;
133+
}
134+
let n = nums.length;
135+
if (n < 2) return [-1, -1];
136+
let min = Infinity;
137+
for (let i = 1; i < n; i++) {
138+
min = Math.min(nums[i] - nums[i - 1], min);
139+
}
140+
return [min, nums[n - 1] - nums[0]];
141+
};
142+
```
143+
102144
### **...**
103145

104146
```

solution/2000-2099/2058.Find the Minimum and Maximum Number of Nodes Between Critical Points/README_EN.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,48 @@ Note that the last node is not considered a local maxima because it does not hav
8181

8282
```
8383

84+
### **TypeScript**
85+
86+
```ts
87+
/**
88+
* Definition for singly-linked list.
89+
* class ListNode {
90+
* val: number
91+
* next: ListNode | null
92+
* constructor(val?: number, next?: ListNode | null) {
93+
* this.val = (val===undefined ? 0 : val)
94+
* this.next = (next===undefined ? null : next)
95+
* }
96+
* }
97+
*/
98+
99+
function nodesBetweenCriticalPoints(head: ListNode | null): number[] {
100+
let idx = 1;
101+
let pre = head.val;
102+
head = head.next;
103+
let nums = [];
104+
while(head.next != null) {
105+
let val = head.val, post = head.next.val;
106+
if (pre < val && val > post) {
107+
nums.push(idx);
108+
}
109+
if (pre > val && val < post) {
110+
nums.push(idx);
111+
}
112+
pre = val;
113+
idx++;
114+
head = head.next;
115+
}
116+
let n = nums.length;
117+
if (n < 2) return [-1, -1];
118+
let min = Infinity;
119+
for (let i = 1; i < n; i++) {
120+
min = Math.min(nums[i] - nums[i - 1], min);
121+
}
122+
return [min, nums[n - 1] - nums[0]];
123+
};
124+
```
125+
84126
### **...**
85127

86128
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* val: number
5+
* next: ListNode | null
6+
* constructor(val?: number, next?: ListNode | null) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.next = (next===undefined ? null : next)
9+
* }
10+
* }
11+
*/
12+
13+
function nodesBetweenCriticalPoints(head: ListNode | null): number[] {
14+
let idx = 1;
15+
let pre = head.val;
16+
head = head.next;
17+
let nums = [];
18+
while(head.next != null) {
19+
let val = head.val, post = head.next.val;
20+
if (pre < val && val > post) {
21+
nums.push(idx);
22+
}
23+
if (pre > val && val < post) {
24+
nums.push(idx);
25+
}
26+
pre = val;
27+
idx++;
28+
head = head.next;
29+
}
30+
let n = nums.length;
31+
if (n < 2) return [-1, -1];
32+
let min = Infinity;
33+
for (let i = 1; i < n; i++) {
34+
min = Math.min(nums[i] - nums[i - 1], min);
35+
}
36+
return [min, nums[n - 1] - nums[0]];
37+
};

0 commit comments

Comments
 (0)