https://leetcode.com/problems/count-complete-subarrays-in-an-array/
- Time complexity: O(n)
- Space complexity: O(n)
var countCompleteSubarrays = function(nums) {
let left = 0;
let right = 0;
const distinctNums = new Set(nums);
const distinctNumsCounter = new Map();
let completeSubNumsCount = 0;
while (right < nums.length) {
distinctNumsCounter.set(nums[right], (distinctNumsCounter.get(nums[right]) || 0) + 1);
while (distinctNumsCounter.size === distinctNums.size) {
distinctNumsCounter.set(nums[left], distinctNumsCounter.get(nums[left]) - 1);
distinctNumsCounter.get(nums[left]) === 0 && distinctNumsCounter.delete(nums[left]);
left += 1;
completeSubNumsCount += nums.length - right;
}
right += 1;
}
return completeSubNumsCount;
};
new Set()
to get distinct elementsmap.delete(key)
is more readable than delete obj[key]
completeSubNumsCount += nums.length - right;
make the algorithm could iterate n
to include the possible solutions.You are given the heads of two sorted linked lists list1 and list2.
Feb 2, 2025You're given two Linked Lists of potentially unequal length. These Linked Lists potentially merge at a shared intersection node. Write a function that returns the intersection node or returns None / null if there is no intersection.Each LinkedList node has an integer value as well as a next node pointing to the next node in the list or to None null if it's the tail of the list.Note: Your function should return an existing node. It should not modify either Linked List, and it should not create any new Linked Lists.
Feb 2, 2025Write a function that takes in the head of a Singly Linked List and an integer k and removes the kth node from the end of the list.The removal should be done in place, meaning that the original data structure should be mutated (no new structure should be created).Furthermore, the input head of the linked list should remain the head of the linked list after the removal is done, even if the head is the node that's supposed to be removed. In other words, if the head is the node that's supposed to be removed, your function should simply mutate its value and next pointer.Note that your function doesn't need to return anything.You can assume that the input Linked List will always have at least two nodes and, more specifically, at least k nodes.Each LinkedList node has an integer value as well as a next node pointing to the next node in the list or to None / null if it's the tail of the list.
Feb 2, 2025https://leetcode.com/problems/best-time-to-buy-and-sell-stock/solutions/1735550/python-javascript-easy-solution-with-very-clear-explanation/
Aug 27, 2023or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up