Java | Longest non-decreasing subsequence
Run longest non-decreasing subsequence each chain (i, i+k, i+2k ... )\nAnswer will be total length - sum (subsequence length)\n\n\n\nclass Solution {\n    publi
Run longest non-decreasing subsequence each chain (i, i+k, i+2k ... )\nAnswer will be total length - sum (subsequence length)\n\n\n```\nclass Solution {\n    public int kIncreasing(int[] arr, int k) {\n        int count = 0;\n        for(int i=0; i<k; i++) {\n            count += longestSub(arr, k, i);\n        }\n        return arr.length - count;\n    }\n    \n    public int longestSub(int[] arr, int k, int i) {\n        TreeSet<Integer> set = new TreeSet<>((a, b) ->{ // store index to solve duplication issue\n            return arr[a] == arr[b]? a-b: arr[a]-arr[b];\n        });\n        for(; i<arr.length; i += k) {\n            if(set.higher(i) != null) {\n                set.remove(set.higher(i));\n            }\n            set.add(i);\n        }\n        return set.size();\n    }\n}\n```\n\nSolving TreeMap cannot handle duplicates\n1. Store index, comparator -> return arr[a] - arr[b];\n2. Use long, id = val * Integer.MAX_VALUE + index (sort by val then index)\n3. Store custom class\n4. Use binary search instead of TreeMap
[]