LeetCode: Partition Array into Disjoint Intervals Solution
ReasoningApproach
LeetCode's official solution covered well
Implementation
1/**2 * @param {number[]} nums3 * @return {number}4 */5var partitionDisjoint = function (nums) {6 const [prefixMax, postfixMin] = [[], []]7 let [max, min] = [nums[0], nums.slice(-1)[0]]89 for (const num of nums) {10 max = Math.max(max, num)11 prefixMax.push(max)12 }1314 for (const num of [...nums].reverse()) {15 min = Math.min(min, num)16 postfixMin.push(min)17 }18 postfixMin.reverse()1920 for (let i = 0; i < nums.length - 1; i++) {21 if (prefixMax[i] <= postfixMin[i + 1]) {22 return i + 123 }24 }2526 return 027}
Comments
Loading comments...
Tags
leetcode
array
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.