@algorithm.ts/sliding-window v4.0.4
A typescript implementation of the sliding-window algorithm.
Install
npm
npm install --save @algorithm.ts/sliding-windowyarn
yarn add @algorithm.ts/sliding-window
Usage
SlidingWindowMember Return Description constructor(options: ISlidingWindowProps)SlidingWindowreset(options?: ISlidingWindowResetOptions)voidReset the sliding window. forwardLeftBoundary(steps?: number)voidMove the sliding window left boundary forward by stepssteps.forwardRightBoundary(steps?: number)voidMove the sliding window right boundary forward by stepssteps.min()number | undefinedReturn the minimum element in the Sliding Window. ISlidingWindowPropsWINDOW_SIZE: (required) the width of the sliding window.compare: (required) compare two index to determine which one is smaller.startIndex: (optional) the first index of the input range.
Example
A solution of https://leetcode.com/problems/sliding-window-maximum/
import { SlidingWindow } from '@algorithm.ts/sliding-window' export function maxSlidingWindow(nums: number[], K: number): number[] { const N = nums.length if (N < K) return [] const results: number[] = [] const window = new SlidingWindow({ WINDOW_SIZE: K, compare: (x, y) => nums[y] - nums[x], }) window.forwardRightBoundary(K - 1) for (let i = K - 1; i < N; ++i) { window.forwardRightBoundary() results.push(nums[window.min()!]) } return results }
Related
1 year ago
1 year ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago