Beats 100% on runtime [EXPLAINED]
Intuition\nModify an array so that certain subsequences are non-decreasing. By minimizing the number of changes needed, we can achieve a K-increasing array wher
# Intuition\nModify an array so that certain subsequences are non-decreasing. By minimizing the number of changes needed, we can achieve a K-increasing array where elements spaced k apart are in the correct order.\n\n# Approach\nDivide the array into k subsequences based on their indices, find the longest non-decreasing subsequence for each, and calculate the number of changes needed by subtracting this length from the total length of each subsequence.\n\n# Complexity\n- Time complexity:\nO(n log n), where n is the length of the array, because we find the longest non-decreasing subsequence for each subsequence using a binary search method.\n\n- Space complexity:\n O(n), as we store the subsequences separately in an array.\n\n# Code\n```typescript []\nfunction longestNonDecSubseq(arr: number[]): number {\n    const stack: number[] = []; // monotonic stack\n\n    for (const e of arr) {\n        const eMax = stack.length > 0 ? stack[stack.length - 1] : null;\n\n        if (eMax === null || e >= eMax) {\n            stack.push(e);\n            continue;\n        }\n\n        const ptr = stack.findIndex(value => value > e);\n        if (ptr === -1) {\n            stack.push(e);\n        } else {\n            stack[ptr] = e;\n        }\n    }\n\n    return stack.length;\n}\n\nfunction kIncreasing(arr: number[], k: number): number {\n    const n = arr.length;\n    const vecVec: number[][] = Array.from({ length: k }, () => []);\n\n    for (let i = 0; i < n; i++) {\n        vecVec[i % k].push(arr[i]);\n    }\n\n    let ret = 0; // This will accumulate the operations needed\n    for (const vec of vecVec) {\n        ret += vec.length; // Total length of the current subsequence\n        ret -= longestNonDecSubseq(vec); // Subtract the length of the longest non-decreasing subsequence\n    }\n\n    return ret; // Return the total operations needed\n}\n\n\n```
['Array', 'Binary Search', 'TypeScript']