- Notifications
You must be signed in to change notification settings - Fork2.4k
refactor: js - intervals#449
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,30 @@ | ||
/** | ||
* https://leetcode.com/problems/meeting-rooms/ | ||
* Time O(N * logN) | Space O(1) | ||
* @param {number[][]} intervals | ||
* @return {boolean} | ||
*/ | ||
var canAttendMeetings = function(intervals) { | ||
intervals.sort(([ aStart, aEnd ], [ bStart, bEnd ]) => aStart !== bStart | ||
? aStart - bStart | ||
: aEnd - bEnd | ||
); | ||
return canAttend(intervals) | ||
}; | ||
const canAttend = (intervals) => { | ||
let prev = intervals.shift(); | ||
for (const curr of intervals) { | ||
const [ prevStart, prevEnd ] = prev; | ||
const [ currStart, currEnd ] = curr; | ||
const hasOverlap = currStart < prevEnd; | ||
if (hasOverlap) return false; | ||
prev = curr; | ||
} | ||
return true; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* https://leetcode.com/problems/meeting-rooms-ii/ | ||
* Time O((N * logN) + (M * logM)) | Space O(1) | ||
* @param {number[][]} intervals | ||
* @return {number} | ||
*/ | ||
var minMeetingRooms = function(intervals) { | ||
const { start, end } = splitIntervals(intervals); | ||
let [ minRooms, startIndex, endIndex ] = [ 0, 0, 0 ]; | ||
while (startIndex < intervals.length) { | ||
const [ currStart, prevEnd ] = [ start[startIndex], end[endIndex] ]; | ||
const hasGap = prevEnd <= currStart; | ||
if (hasGap) { | ||
minRooms--; | ||
endIndex++; | ||
} | ||
minRooms++; | ||
startIndex++; | ||
} | ||
return minRooms; | ||
}; | ||
const splitIntervals = (intervals, start = [], end = []) => { | ||
for (const [ startTime, endTime ] of intervals) { | ||
start.push(startTime); | ||
end.push(endTime); | ||
} | ||
const comparator = ((a, b) => a - b); | ||
start.sort(comparator); | ||
end.sort(comparator); | ||
return { start, end }; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,31 @@ | ||
/** | ||
* https://leetcode.com/problems/non-overlapping-intervals/ | ||
* Time O(N * logN) | Space O(1) | ||
* @param {number[][]} intervals | ||
* @return {number} | ||
*/ | ||
var eraseOverlapIntervals = function(intervals) { | ||
intervals.sort(([ aStart, aEnd ], [ bStart, bEnd ]) => aEnd !== bEnd | ||
? aEnd - bEnd | ||
: aStart - bStart | ||
); | ||
return getGaps(intervals) | ||
}; | ||
const getGaps = (intervals, gaps = 1) => { | ||
const prev = intervals.shift(); | ||
for (const curr of intervals) { | ||
const [ prevStart, prevEnd ] = prev; | ||
const [ currStart, currEnd ] = curr; | ||
const hasGap = prevEnd <= currStart; | ||
if (!hasGap) continue; | ||
prev[1] = curr[1]; | ||
gaps++; | ||
} | ||
return(intervals.length + 1) - gaps; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,34 @@ | ||
/** | ||
* https://leetcode.com/problems/merge-intervals/ | ||
* Time O(N * logN) | Space O(N) | ||
* @param {number[][]} intervals | ||
* @return {number[][]} | ||
*/ | ||
var merge = function(intervals) { | ||
intervals.sort(([ aStart, aEnd ], [ bStart, bEnd ]) => aStart !== bStart | ||
? aStart - bStart | ||
: aEnd - bEnd | ||
); | ||
return mergerInterval(intervals) | ||
}; | ||
const mergerInterval = (intervals, merged = []) => { | ||
let prev = intervals.shift(); | ||
for (const curr of intervals) { | ||
const [ prevStart, prevEnd ] = prev; | ||
const [ currStart, currEnd ] = curr; | ||
const hasOverlap = currStart <= prevEnd; | ||
if (hasOverlap) { | ||
prev[1] = Math.max(prev[1], curr[1]); | ||
continue; | ||
} | ||
merged.push(prev); | ||
prev = curr; | ||
} | ||
return [ ...merged, prev ]; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,43 @@ | ||
/** | ||
* https://leetcode.com/problems/insert-interval/ | ||
* Time O(N) | Space O(N) | ||
* @param {number[][]} intervals | ||
* @param {number[]} newInterval | ||
* @return {number[][]} | ||
*/ | ||
var insert = function(intervals, newInterval) { | ||
const { beforeIndex, before } = getBefore(intervals, newInterval); | ||
const afterIndex = mergeIntervals(intervals, newInterval, beforeIndex); | ||
const after = intervals.slice(afterIndex); | ||
return[ ...before, newInterval, ...after ]; | ||
}; | ||
const getBefore = (intervals, newInterval, index = 0, before = []) => { | ||
const hasGap = ([ prevStart, prevEnd ], [ currStart, currEnd ]) => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. same as others There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. @mitchellirvin consthasOverlap=()=> ...while(hasOverlap()) Move out of scoop consthasOverlap=()=> ...constloop=()=>{while(hasOverlap())} or add inline with comment // hasOverlap = intervals[index] <= newInterval[1]while(intervals[index]<=newInterval[1]) | ||
prevEnd < currStart; | ||
while (index < intervals.length && hasGap(intervals[index], newInterval)) { | ||
const current = intervals[index]; | ||
before.push(current); | ||
index++; | ||
} | ||
return { beforeIndex: index, before }; | ||
} | ||
const mergeIntervals = (intervals, newInterval, index) => { | ||
const hasOverlap = ([ prevStart, prevEnd ], [ currStart, currEnd ]) => | ||
currStart <= prevEnd; | ||
while (index < intervals.length && hasOverlap(newInterval, intervals[index])) { | ||
const current = intervals[index]; | ||
newInterval[0] = Math.min(newInterval[0], current[0]); | ||
newInterval[1] = Math.max(newInterval[1], current[1]); | ||
index++; | ||
} | ||
return index; | ||
} |