Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
February 15, 2022 09:55 pm GMT

Day 5 of Studying LeetCode Solution until I Can Solve One on My Own: Problem56.Merge Intervals(Medium/JavaScript)

Intro: I am a former accountant turned software engineer graduated from coding bootcamp in January 2022. Algorithms and Data Structure is an unavoidable part of interviews for most of the tech companies now. And one of my friends told me that you need to solve a medium leetcode problem under 60 seconds in order to get into the top tech companies.So I thought I'd start learning how to do it while job searching.

Since I have no clue on how to solve any of the problems (even the easy ones), I thought there is no point for me to waste hours and can't get it figured out. Here is my approach:

  • Pick a leetcode problem randomly or Online Assessment from targeted companies.
  • Study 1-2 solutions from Youtube or LeetCode discussion section. One brute force solution, another one more optimal.
  • Write a blog post with detailed explanation and do a verbal walk through to help understand the solutions better.
  • Code out the solution in LeetCode without looking at the solutions
  • Combat the forgetting curve: Re-do the question for the next three days. And come back regularly to revisit the problem.

Problem#56. Merge Intervals

Difficulty: Medium Language: JavaScript
Company: IBM Backend Developer OA

Given an array of intervals where intervals[i] = [starti,
endi]
, merge all overlapping intervals, and return an array of
the non-overlapping intervals that cover all the intervals in the
input.

Example 1:

Input: intervals = [[1,3],[2,6],[8,10],[15,18]]Output: [[1,6],[8,10],[15,18]]Explanation: Since intervals [1,3] and [2,6] overlaps, merge theminto [1,6].

Example 2:

Input: intervals = [[1,4],[4,5]]Output: [[1,5]]Explanation: Intervals [1,4] and [4,5] are considered overlapping.

Constraints:

  • 1 <= intervals.length <= 104
  • intervals[i].length == 2
  • 0 <= starti <= endi <= 104

Solution:

var merge = function(intervals) {    intervals.sort((a,b) => a[0] - b[0]) /*Sort (note 1) the array of 'intervals' by index 0 of each element. This is an important step.*/    for (let i = 1; i < intervals.length; i++) {/*Loop (note 3) through each element in the array 'intervals'*/        let current = intervals[i]        let previous = intervals[i - 1]/*Create variables so that we can compare two elements.Current one and the previous one*/        if(current[0] <= previous[1]) {            intervals[i] =[previous[0],Math.max(current[1],previous[1])]            intervals.splice(i-1,1)             i -= 1        }     }    return intervals};

Solution Submission detail as of 2/15/2022
(Data below could vary since there are new submissions daily)

  • Runtime: 160 ms
  • Memory Usage: 49.7 MB
  • Time complexity:O(n)
  • Space complexity:O(1)

References:
LeetCode Problem Link
LeetCode Discussion: garyguan0713
Note 1: sort()
Note 2: splice()
Note 2: for loop
Blog Cover Image Credit


Original Link: https://dev.to/corndog_com567/day-5-of-studying-leetcode-solution-until-i-can-solve-one-on-my-own-problem56merge-intervalsmediumjavascript-55m1

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To