code.ashish.me

Atom feed

Recently added: 02 Count Of Subset Sum, 416 Partition Equal Subset Sum, 01 Subset Sum, 518 Coin Change 2, 983 Minimum Cost For Tickets

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:35:21+05:18 · Edit