Greedy approach with sorting
Approach\nSort descending by difference between spend and requare\n\n# Complexity\n- Time complexity: O(nlog(n))\n Add your time complexity here, e.g. O(n) \n\n
# Approach\nSort descending by difference between spend and requare\n\n# Complexity\n- Time complexity: $$O(nlog(n))$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nimport kotlin.math.*\n\nclass Solution {\n    fun minimumEffort(tasks: Array<IntArray>): Int {\n        \n        tasks.sortByDescending { abs(it[0] - it[1]) }\n        var ans = tasks[0][1]\n        var cur = tasks[0][1]\n        for (i in 0..tasks.size-1) {\n            cur -= tasks[i][0]\n            if (i < tasks.size-1 && cur < tasks[i+1][1]) {\n                ans += tasks[i+1][1] - cur\n                cur = tasks[i+1][1]\n            }\n        }\n\n        return ans\n    }\n}\n```
['Greedy', 'Sorting', 'Kotlin']