Run Code
|
Code Wall
|
Users
|
Misc
|
Feedback
|
About
|
Login
|
Theme
|
Privacy
Meeting Time
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
/* #17. Meeting Time Input is a number of meetings (start_time, end_time)。 Output is a number of time intervals (start_time, end_time), where there is no meetings. For exmaple, input is [[1, 3], [6, 7]], [[2, 4]], [[2, 3], [9, 12]] ] Output [[4, 6], [7, 9]] Leetcode 相似: Leetcode 252/253 Meeting Rooms I/II; 759. Employee Free Time */ #include <iostream> #include <vector> #include <algorithm> using namespace std; struct Interval{ int start; int end; Interval(int s, int e): start(s), end(e) {} }; void print(vector<Interval>& v){ cout << "["; for(int i =0; i<(int)v.size(); i++) cout << "[" << v[i].start << "," << v[i].end << "]"; cout << "]" << endl; } bool compare(Interval& a, Interval& b){ return a.start < b.start; } vector<Interval> getTime(vector<vector<Interval>>& v){ vector<Interval> res; vector<Interval> all; for(auto x: v){ all.insert(all.end(), x.begin(), x.end()); } sort(all.begin(), all.end(), compare); print(all); int pre = all[0].end; for(int i=1; i<(int)all.size(); i++){ if(all[i].start > pre) // found one gap { res.push_back(Interval(pre, all[i].start)); pre = all[i].end; } else{ pre = max(pre, all[i].end); } } return res; } int main() { vector<vector<Interval>> vs = {{Interval(0,1), Interval(2,3)}, {Interval(3,4),Interval(5,6)}}; vector<Interval> res = getTime(vs); print(res); return 0; }
g++
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
Compilation time: 1.03 sec, absolute running time: 0.07 sec, cpu time: 0.01 sec, memory peak: 3 Mb, absolute service time: 1,1 sec
edit mode
|
history
|
discussion
[[0,1][2,3][3,4][5,6]] [[1,2][4,5]]