package com.fishercoder.solutions;

import java.util.ArrayList;
import java.util.List;

/**
 * Given a sorted integer array where the range of elements are in the inclusive range [lower, upper], return its missing ranges.
 * <p>
 * For example, given [0, 1, 3, 50, 75], lower = 0 and upper = 99, return ["2", "4->49", "51->74", "76->99"].
 */
public class _163 {

    public List<String> findMissingRanges(int[] nums, int lower, int upper) {
        List<String> res = new ArrayList<String>();

        // the next number we need to find
        long next = lower;
        long longUpper = (long) upper;
        if (nums.length == 0) {
            res.add(getRange(lower, longUpper));
            return res;
        } else if (nums.length == 1 && nums[0] == -2147483648 && lower == -2147483648 && upper == 2147483647) {
            res.add(getRange(lower + 1, longUpper));
            return res;
        }

        for (int i = 0; i < nums.length; i++) {
            // not within the range yet
            if (nums[i] < next) continue;

            // continue to find the next one
            if (nums[i] == next) {
                next++;
                continue;
            }

            // get the missing range string format
            res.add(getRange(next, nums[i] - 1));

            // now we need to find the next number
            if (nums[i] < Integer.MAX_VALUE) next = nums[i] + 1;
            else break;
        }

        // do a final check
        if (next > Integer.MIN_VALUE && next <= longUpper && longUpper < Integer.MAX_VALUE)
            res.add(getRange(next, longUpper));
        if (next == Integer.MIN_VALUE && longUpper == Integer.MAX_VALUE && nums.length == 0)
            res.add(getRange(next, longUpper));

        return res;
    }

    String getRange(long n1, long n2) {
        return (n1 == n2) ? String.valueOf(n1) : String.format("%d->%d", n1, n2);
    }
}

@@ -85,16 +85,39 @@
 t;%0A%0A/**%0A
+ * 163. Missing Ranges%0A
  * Given
@@ -242,15 +242,8 @@
 es.%0A
- * %3Cp%3E%0A
  * F
@@ -475,16 +475,19 @@
 ing%3E res
+ult
  = new A
@@ -499,433 +499,70 @@
 ist%3C
-String%3E();%0A%0A        // the next number we need to find%0A        long next = lower;%0A        long longUpper = (long) upper;%0A        if (nums.length == 0) %7B%0A            res.add(getRange(lower, longUpper));%0A            return res;%0A        %7D else if (nums.length == 1 && nums%5B0%5D == -2147483648 && lower == -2147483648 && upper == 2147483647) %7B%0A            res.add(getRange(lower + 1, longUpper));%0A            return res;%0A        %7D%0A
+%3E();%0A        long low = (long) lower - 1;%0A        long up = 0;
 %0A   
@@ -585,16 +585,17 @@
 = 0; i %3C
+=
  nums.le
@@ -623,207 +623,120 @@
     
-// not within the range yet%0A            if (nums%5Bi%5D %3C next) continue;%0A%0A            // continue to find the next one%0A            if (nums%5Bi%5D == next) %7B%0A                next++;%0A                continue
+if (i == nums.length) %7B%0A                up = (long) upper + 1;%0A            %7D else %7B%0A                up = nums%5Bi%5D
 ;%0A  
@@ -739,33 +739,32 @@
 ;%0A            %7D%0A
-%0A
             // g
@@ -763,369 +763,114 @@
     
-// get the missing range string format%0A            res.add(getRange(next, nums%5Bi%5D - 1));%0A%0A            // now we need to find the next number%0A            if (nums%5Bi%5D %3C Integer.MAX_VALUE) next = nums%5Bi%5D + 1;%0A            else break;%0A        %7D%0A%0A        // do a final check%0A        if (next %3E Integer.MIN_VALUE && next %3C= longUpper && longUpper %3C Integer.MAX_VALUE)%0A
+if (up == low + 2) %7B%0A                result.add(low + 1 + %22%22);%0A            %7D else if (up %3E low + 2) %7B%0A    
     
@@ -884,330 +884,113 @@
  res
+ult
 .add(
-getRange(next, longUpper));%0A        if (next == Integer.MIN_VALUE && longUpper == Integer.MAX_VALUE && nums.length == 0)%0A            res.add(getRange(next, longUpper));%0A%0A        return res
+(low + 1) + %22-%3E%22 + (up - 1));%0A            %7D%0A            low = up
 ;%0A    
-%7D%0A%0A
     
-String getRange(long n1, long n2) %7B%0A        return (n1 == n2) ? String.valueOf(n1) : String.format(%22%25d-%3E%25d%22, n1, n2)
+%7D%0A        return result
 ;%0A  

refactor 163