4 ms 21-line Java Solution (100% time & 94% space)
use byte countlist to save space.\nwindow [i, j] is not satisfactory (k unique elements) at first.\nvar good count satisfactory subarray.\nafter [i, j] reach k 
use byte countlist to save space.\nwindow [i, j] is not satisfactory (k unique elements) at first.\nvar good count satisfactory subarray.\nafter [i, j] reach k unique elements, subsequent subarray always satisfy that:\n\tcount[arr[i]] == 1.\n```\nclass Solution {\n  public int subarraysWithKDistinct(int[] arr, int k) {\n    short[] count = new short[arr.length+1];\n    short unique=0;\n    int i=0, total=0, good=0;\n    for(int j=0; j<arr.length; j++){\n      if(count[arr[j]]++==0) unique++;\n      if(unique>k){\n        count[arr[i++]]--;\n        unique--;\n        good=0;\n      }\n      while(count[arr[i]]>1){\n        count[arr[i++]]--;\n        good++;\n      }\n      if(unique==k) total += good+1;\n    }\n    return total;\n  }\n}\n```
['Java']