In a linked list of size n
, where n
is even, the ith
node (0-indexed) of the linked list is known as the twin of the (n-1-i)
th node, if 0 <= i <= (n / 2) - 1
.
n = 4
, then node 0
is the twin of node 3
, and node 1
is the twin of node 2
. These are the only nodes with twins for n = 4
.The twin sum is defined as the sum of a node and its twin.
Given the head
of a linked list with even length, return the maximum twin sum of the linked list.
[2, 10^5]
.1 <= Node.val <= 10^5
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {number}
*/
var pairSum = function(head) {
const values = [];
let node = head;
while (node) {
values.push(node.val);
node = node.next;
}
let maxSum = 0;
const totalValueCount = values.length;
for (let i = 0; i < totalValueCount / 2; i++) {
const sum = values[i] + values[totalValueCount - i - 1];
maxSum = Math.max(maxSum, sum);
}
return maxSum;
};
var pairSum = function(head) {
let node = head;
let reverse = head;
let prev = null;
let temp;
while (node && node.next) {
node = node.next.next;
temp = reverse.next;
reverse.next = prev;
prev = reverse;
reverse = temp;
}
let maxSum = 0;
while (prev) {
maxSum = Math.max(maxSum, prev.val + reverse.val);
prev = prev.next;
reverse = reverse.next;
}
return maxSum;
};
You are given an array prices
where prices[i]
is the price of a given stock on the ith
day, and an integer fee
representing a transaction fee.
Find the maximum profit you can achieve. You may complete as many transactions as you like, but you need to pay the transaction fee for each transaction.
Note:
Example 1:
Example 2:
1 <= prices.length <= 5 * 10^4
1 <= prices[i] < 5 * 10^4
0 <= fee < 5 * 10^4
/**
* @param {number[]} prices
* @param {number} fee
* @return {number}
*/
var maxProfit = function(prices, fee) {
let profitSum = 0;
let buyPrice = prices[0];
for (let i = 1; i < prices.length; i++) {
if (prices[i] < buyPrice) {
buyPrice = prices[i];
} else if (prices[i] > buyPrice + fee) {
profitSum += prices[i] - buyPrice - fee;
buyPrice = prices[i] - fee;
}
}
return profitSum;
};
Given a binary array nums
, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1
's in the resulting array. Return 0
if there is no such subarray.
-Example 2:
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
Example 3:
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
1 <= nums.length <= 10^5
nums[i]
is either 0
or 1
./**
* @param {number[]} nums
* @return {number}
*/
var longestSubarray = function(nums) {
let left = 0;
let k = 1;
let maxLength = 0;
for (let right = 0; right < nums.length; right++) {
if (nums[right] === 0) {
k--;
}
while (k < 0) {
if (nums[left] === 0) {
k++;
}
left++;
}
maxLength = Math.max(maxLength, right - left);
}
return maxLength;
};
A string is good if there are no repeated characters.
Given a string s
, return the number of good substrings of length three in s.
Note that if there are multiple occurrences of the same substring, every occurrence should be counted.
A substring is a contiguous sequence of characters in a string.
Example 1:
Example 2:
1 <= s.length <= 100
s
consists of lowercase English letters./**
* @param {string} s
* @return {number}
*/
var countGoodSubstrings = function(s) {
let goodSubstrCount = 0;
for (let i = 0; i < s.length - 2; i++) {
const obj = {};
let isGoodSubstr = true;
for (let j = i; j < i + 3; j++) {
const letter = s[j];
if (obj[letter]) {
isGoodSubstr = false;
break;
}
obj[letter] = 1;
}
if (isGoodSubstr) {
goodSubstrCount++;
}
}
return goodSubstrCount;
};
var countGoodSubstrings = function(s) {
let goodSubstrCount = 0;
for (let i = 0; i < s.length - 2; i++) {
const substr = s.slice(i, i + 3);
const set = new Set(substr);
if (set.size === 3) {
goodSubstrCount++;
}
}
return goodSubstrCount;
};