Skip to content

Commit a197b4f

Browse files
authored
Update 759-employee-free-time.js
1 parent dc18d3a commit a197b4f

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

759-employee-free-time.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,37 @@ const employeeFreeTime = function (schedule) {
145145
return res
146146
}
147147

148+
// another
149+
150+
/**
151+
* // Definition for an Interval.
152+
* function Interval(start, end) {
153+
* this.start = start;
154+
* this.end = end;
155+
* };
156+
*/
157+
158+
/**
159+
* @param {Interval[][]} schedule
160+
* @return {Interval[]}
161+
*/
162+
var employeeFreeTime = function(schedule) {
163+
const arr = schedule.reduce((ac, e) => {
164+
ac.push(...e)
165+
return ac
166+
}, [])
167+
arr.sort((a, b) => a.start - b.start || b.end - a.end)
168+
const n = arr.length
169+
const res = []
170+
let end = arr[0].end
171+
for(let i = 1; i < n; i++) {
172+
const cur = arr[i]
173+
if(cur.start > end) {
174+
res.push(new Interval(end, cur.start))
175+
}
176+
177+
end = Math.max(end, cur.end)
178+
}
179+
180+
return res
181+
};

0 commit comments

Comments
 (0)