Skip to content

Instantly share code, notes, and snippets.

@luan0ap
Last active July 3, 2018 17:54
Show Gist options
  • Save luan0ap/51876cebc29b3b177c553d07d2441433 to your computer and use it in GitHub Desktop.
Save luan0ap/51876cebc29b3b177c553d07d2441433 to your computer and use it in GitHub Desktop.
Given an array of integers and a number k, where 1 <= k <= length of the array, compute the maximum values of each subarray of length k.
const max = arr => Math.max(...arr)
const calc = len => (len / 2 + 1)
const floor = num => Math.floor(num)
const add = arr => item => arr.push(item)
const slice = arr => start => end => arr.slice(start, end)
const main = (arr) => (k) => {
const length = calc(floor(arr.length))
const result = []
for ( let i = 0; i < length; i++ ) {
add( result )( max( slice(arr)(i)(i + k) ) )
}
return result
}
main( [10, 5, 2, 7, 8, 7] )( 3 ) // return: [10, 7, 8, 8]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment