11. Container that holds the most water
Double pointer, the first and last items of the array are the initial boundary, when the left boundary is less than the right, the next left boundary is moved right, vice versa.
var maxArea = function(height) {
let res = 0
for(let i = 0, j = height.length - 1; i < j;) {
const maxA = (j - i) * (height[i] < height[j] ? height[i++] : height[j--])
res = Math.max(res, maxA)
}
return res
};
Copy the code
25. Flip linked lists in groups of K
const myReverse = (head, tail) = > {
let prev = tail.next;
let p = head;
while(prev ! == tail) {const nex = p.next;
p.next = prev;
prev = p;
p = nex;
}
return [tail, head];
}
var reverseKGroup = function(head, k) {
const hair = new ListNode(0);
hair.next = head;
let pre = hair;
while (head) {
let tail = pre;
// Check whether the remaining length is greater than or equal to k
for (let i = 0; i < k; ++i) {
tail = tail.next;
if(! tail) {returnhair.next; }}const nex = tail.next;
[head, tail] = myReverse(head, tail);
// join the child list back to the original list
pre.next = head;
tail.next = nex;
pre = tail;
head = tail.next;
}
return hair.next;
};
Copy the code