Title: #57 - Insert Interval - LeetCode JavaScript Solutions
Open Graph Title: #57 - Insert Interval - LeetCode JavaScript Solutions
X Title: #57 - Insert Interval - LeetCode JavaScript Solutions
Description: You are given an array of non-overlapping intervals intervals where intervals[i] = [starti, endi] represent the start and the end of the ith...
Open Graph Description: You are given an array of non-overlapping intervals intervals where intervals[i] = [starti, endi] represent the start and the end of the ith...
X Description: You are given an array of non-overlapping intervals intervals where intervals[i] = [starti, endi] represent the start and the end of the ith...
Keywords:
Opengraph URL: https://leetcodejavascript.com/solutions/insert-interval
Domain: leetcodejavascript.com
{"@context":"https://schema.org","@type":"Code","url":"https://leetcodejavascript.com/solutions/insert-interval","name":"#57 - Insert Interval - LeetCode JavaScript Solutions","description":"You are given an array of non-overlapping intervals intervals where intervals[i] = [starti, endi] represent the start and the end of the ith...","programmingLanguage":"JavaScript","codeRepository":"https://github.com/JoshCrozier/leetcode-javascript","codeSampleType":"JavaScript","text":"/**\n * @param {number[][]} intervals\n * @param {number[]} newInterval\n * @return {number[][]}\n */\nvar insert = function(intervals, newInterval) {\n const result = [];\n\n for (let i = 0; i < intervals.length; i++) {\n if (newInterval[1] < intervals[i][0]) {\n result.push(newInterval);\n return [...result, ...intervals.slice(i)];\n } else if (newInterval[0] > intervals[i][1]) {\n result.push(intervals[i]);\n } else {\n newInterval = [\n Math.min(newInterval[0], intervals[i][0]),\n Math.max(newInterval[1], intervals[i][1]),\n ];\n }\n }\n\n result.push(newInterval);\n return result;\n};","keywords":"LeetCode 57, #57 - Insert Interval, Medium, JavaScript solution","learningResourceType":"Code","isAccessibleForFree":true,"educationalLevel":"intermediate","interactivityType":"mixed"}
| author | LeetCodeJavascript.com |
| og:type | article |
| og:image | https://leetcodejavascript.com/og-image.jpg |
| og:site_name | LeetCode JavaScript Solutions |
| twitter:card | summary_large_image |
| twitter:url | https://leetcodejavascript.com/solutions/insert-interval |
| twitter:image | https://leetcodejavascript.com/og-image.jpg |
| twitter:creator | @joshcrozier |
| theme-color | #1f2937 |
Links:
| LeetCodeJavascript.com | https://leetcodejavascript.com/ |
| Star on GitHub | https://github.com/JoshCrozier/leetcode-javascript |
| Back to all solutions | https://leetcodejavascript.com |
| View on LeetCode | https://leetcode.com/problems/insert-interval/ |
| View on GitHub | https://github.com/JoshCrozier/leetcode-javascript/blob/master/solutions/0057-insert-interval.js |
| Array | https://leetcodejavascript.com/tags/array |
| Josh Crozier | https://joshcrozier.com |
Viewport: width=device-width, initial-scale=1.0
Robots: index, follow