code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

033 Compose Rangers

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

/* Given a sorted integer array that does not contain any duplicates, return a summary of the number ranges it contains.

Example

For nums = [-1, 0, 1, 2, 6, 7, 9], the output should be composeRanges(nums) = ["-1->2", "6->7", "9"].
*/

function composeRangers(array) {
  if (array.length < 1) {
    return []
  }
  let result = [{ start: array[0], end: array[0] }]
  for (let index = 1; index < array.length; index++) {
    if (result[result.length - 1].end + 1 == array[index]) {
      result[result.length - 1].end = array[index]
    } else {
      result.push({ start: array[index], end: array[index] })
    }
  }
  result = result.map(a => {
    if (a.start !== a.end) {
      return `${a.start}->${a.end}`
    } else {
      return `${a.start}`
    }
  })
  return result
}

test('compose Rangers', () => {
  expect(composeRangers([-1, 0, 1, 2, 6, 7, 9])).toEqual(['-1->2', '6->7', '9'])
})

Created 2019-12-11T02:47:21+05:30 · Edit