@@ -1778,6 +1778,77 @@ public boolean book(int start, int end) {
17781778// }
17791779// }
17801780
1781+ // LC 163
1782+ // https://leetcode.ca/all/163.html
1783+ // 3.51 pm - 4.20 pm
1784+ /**
1785+ * Given a sorted integer array nums, where the range of
1786+ * elements are in the inclusive range [lower, upper],
1787+ * return its missing ranges.
1788+ *
1789+ *
1790+ * exp 1:
1791+ * Input: nums = [0, 1, 3, 50, 75], lower = 0 and upper = 99,
1792+ * Output: ["2", "4->49", "51->74", "76->99"]
1793+ *
1794+ * -> idea 1
1795+ *
1796+ * 0,1, [2] 3, [50], [75]
1797+ *
1798+ * -> "2", "4,49", "51,74", "76,99"
1799+ *
1800+ *
1801+ * -> loop over elements, compare prev, and current element
1802+ * -> if any missing, then collect, then add to result array
1803+ * -> then finally check upper bound and last element
1804+ * -> add to result accordingly if there is a missing element
1805+ */
1806+ public List <List <Integer >> findMissingRanges (int [] nums , int lower , int upper ) {
1807+ if (nums .length == 0 ){
1808+ return null ;
1809+ }
1810+
1811+ /**
1812+ * Input: nums = [0, 1, 3, 50, 75], lower = 0 and upper = 99,
1813+ * Output: ["2", "4->49", "51->74", "76->99"]
1814+ *
1815+ * nums = [0, 1, 3, 50, 75] res = []
1816+ * x
1817+ * x res = []
1818+ * x res = [[2]]
1819+ * x res = [[2], [4,49]]
1820+ * x res = [[2], [4,49], [51,74]]
1821+ *
1822+ * res = [[2], [4,49], [51,74], [76,99]]
1823+ */
1824+ List <List <Integer >> res = new ArrayList <>();
1825+ for (int i = 1 ; i < nums .length ; i ++){
1826+ // case 1: nums = [1, 3]
1827+ if (nums [i ] == nums [i -1 ]+2 ){
1828+ List <Integer > missingPeriod = new ArrayList <>();
1829+ missingPeriod .add (nums [i -1 ]+1 );
1830+ res .add (missingPeriod );
1831+ }
1832+ // case 2 : nums = [3, 50]
1833+ else if (nums [i ] != nums [i -1 ]){
1834+ List <Integer > missingPeriod = new ArrayList <>();
1835+ missingPeriod .add (nums [i -1 ]+1 );
1836+ missingPeriod .add (nums [i ]-1 );
1837+ res .add (missingPeriod );
1838+ }
1839+ }
1840+
1841+ // finally, check last element and upper bound
1842+ if (upper != nums [nums .length -1 ]){
1843+ List <Integer > missingPeriod = new ArrayList <>();
1844+ missingPeriod .add (nums [nums .length -1 ] + 1 );
1845+ missingPeriod .add (upper );
1846+ res .add (missingPeriod );
1847+ }
1848+
1849+ return res ;
1850+ }
1851+
17811852
17821853}
17831854
0 commit comments