forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
15053ec
commit 4a4c5a8
Showing
6 changed files
with
257 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
cmake_minimum_required(VERSION 3.5) | ||
project(cpp_0253) | ||
|
||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") | ||
|
||
set(SOURCE_FILES main4.cpp) | ||
add_executable(cpp_0253 ${SOURCE_FILES}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/// Source : https://leetcode.com/problems/meeting-rooms-ii/description/ | ||
/// Author : liuyubobobo | ||
/// Time : 2018-09-10 | ||
|
||
#include <iostream> | ||
#include <vector> | ||
|
||
using namespace std; | ||
|
||
|
||
/// Simulation | ||
/// Time Complexity: O(n^2) | ||
/// Space Complexity: O(n) | ||
|
||
/// Definition for an interval. | ||
struct Interval { | ||
int start; | ||
int end; | ||
Interval() : start(0), end(0) {} | ||
Interval(int s, int e) : start(s), end(e) {} | ||
}; | ||
|
||
class Solution { | ||
|
||
public: | ||
int minMeetingRooms(vector<Interval>& intervals) { | ||
|
||
sort(intervals.begin(), intervals.end(), cmpIntervals); | ||
|
||
vector<Interval> rooms; | ||
int sz = 0, res = 0; | ||
for(const Interval& meeting: intervals){ | ||
|
||
for(Interval& room: rooms) | ||
if(room.end != -1 && room.end <= meeting.start){ | ||
room.start = room.end = -1; | ||
sz --; | ||
} | ||
|
||
bool ok = false; | ||
for(Interval& room: rooms) | ||
if(room.start == -1){ | ||
room = meeting; | ||
ok = true; | ||
break; | ||
} | ||
if(!ok) | ||
rooms.push_back(meeting); | ||
sz ++; | ||
|
||
res = max(res, sz); | ||
} | ||
|
||
return res; | ||
} | ||
|
||
private: | ||
static bool cmpIntervals(const Interval& i1, const Interval& i2){ | ||
if(i1.start != i2.start) | ||
return i1.start < i2.start; | ||
return i1.end < i2.end; | ||
} | ||
}; | ||
|
||
|
||
int main() { | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/// Source : https://leetcode.com/problems/meeting-rooms-ii/description/ | ||
/// Author : liuyubobobo | ||
/// Time : 2018-09-10 | ||
|
||
#include <iostream> | ||
#include <vector> | ||
#include <queue> | ||
|
||
using namespace std; | ||
|
||
|
||
/// Simulation | ||
/// Using Priority Queue | ||
/// | ||
/// Time Complexity: O(nlogn) | ||
/// Space Complexity: O(n) | ||
|
||
/// Definition for an interval. | ||
struct Interval { | ||
int start; | ||
int end; | ||
Interval() : start(0), end(0) {} | ||
Interval(int s, int e) : start(s), end(e) {} | ||
}; | ||
|
||
class Solution { | ||
|
||
private: | ||
class CompareInterval{ | ||
public: | ||
bool operator()(const Interval& a, const Interval& b){ | ||
return a.end > b.end; | ||
} | ||
}; | ||
|
||
public: | ||
int minMeetingRooms(vector<Interval>& intervals) { | ||
|
||
sort(intervals.begin(), intervals.end(), cmpIntervals); | ||
|
||
priority_queue<Interval, vector<Interval>, CompareInterval> rooms; | ||
int res = 0; | ||
for(const Interval& meeting: intervals){ | ||
|
||
while(!rooms.empty() && rooms.top().end <= meeting.start) | ||
rooms.pop(); | ||
|
||
rooms.push(meeting); | ||
res = max(res, (int)rooms.size()); | ||
} | ||
|
||
return res; | ||
} | ||
|
||
private: | ||
static bool cmpIntervals(const Interval& i1, const Interval& i2){ | ||
if(i1.start != i2.start) | ||
return i1.start < i2.start; | ||
return i1.end < i2.end; | ||
} | ||
}; | ||
|
||
|
||
int main() { | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/// Source : https://leetcode.com/problems/meeting-rooms-ii/description/ | ||
/// Author : liuyubobobo | ||
/// Time : 2018-09-10 | ||
|
||
#include <iostream> | ||
#include <vector> | ||
|
||
using namespace std; | ||
|
||
|
||
/// Two Pointers | ||
/// Sorting start time and end time seperately. | ||
/// | ||
/// Time Complexity: O(nlogn) | ||
/// Space Complexity: O(n) | ||
|
||
/// Definition for an interval. | ||
struct Interval { | ||
int start; | ||
int end; | ||
Interval() : start(0), end(0) {} | ||
Interval(int s, int e) : start(s), end(e) {} | ||
}; | ||
|
||
class Solution { | ||
public: | ||
int minMeetingRooms(vector<Interval>& intervals) { | ||
|
||
if(intervals.size() == 0) | ||
return 0; | ||
|
||
vector<int> starts, ends; | ||
for(const Interval& interval: intervals){ | ||
starts.push_back(interval.start); | ||
ends.push_back(interval.end); | ||
} | ||
sort(starts.begin(), starts.end()); | ||
sort(ends.begin(), ends.end()); | ||
|
||
|
||
int res = 1, sz = 1; | ||
int end_index = 0; | ||
for(int start_index = 1; start_index < starts.size(); start_index ++){ | ||
while(end_index < ends.size() && ends[end_index] <= starts[start_index]) | ||
end_index ++, sz --; | ||
sz ++; | ||
res = max(res, sz); | ||
} | ||
|
||
return res; | ||
} | ||
}; | ||
|
||
|
||
int main() { | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/// Source : https://leetcode.com/problems/meeting-rooms-ii/description/ | ||
/// Author : liuyubobobo | ||
/// Time : 2018-09-10 | ||
|
||
#include <iostream> | ||
#include <vector> | ||
|
||
using namespace std; | ||
|
||
|
||
/// Dealing with start time and end time seperately | ||
/// make start time and end time in a pair structure | ||
/// and deal with them in one single vector:) | ||
/// | ||
/// Time Complexity: O(nlogn) | ||
/// Space Complexity: O(n) | ||
|
||
/// Definition for an interval. | ||
struct Interval { | ||
int start; | ||
int end; | ||
Interval() : start(0), end(0) {} | ||
Interval(int s, int e) : start(s), end(e) {} | ||
}; | ||
|
||
class Solution { | ||
public: | ||
int minMeetingRooms(vector<Interval>& intervals) { | ||
|
||
vector<pair<int, char>> timepoints; | ||
for(const Interval& interval: intervals){ | ||
timepoints.push_back(make_pair(interval.start, 's')); | ||
timepoints.push_back(make_pair(interval.end, 'e')); | ||
} | ||
sort(timepoints.begin(), timepoints.end()); | ||
|
||
int res = 0; | ||
int cur = 0; | ||
for(const pair<int, char>& p: timepoints) | ||
if(p.second == 's'){ | ||
cur ++; | ||
res = max(res, cur); | ||
} | ||
else | ||
cur --; | ||
|
||
return res; | ||
} | ||
}; | ||
|
||
|
||
int main() { | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -230,7 +230,7 @@ email: [[email protected]](mailto:[email protected]) | |
| 250 | [Count Univalue Subtrees](https://leetcode.com/problems/count-univalue-subtrees/description/) | [无] | [C++](0250-Count-Univalue-Subtrees/cpp-0250/) | | | | ||
| | | | | | | | ||
| 252 | [Meeting Rooms](https://leetcode.com/problems/meeting-rooms/description/) | [solution](https://leetcode.com/problems/meeting-rooms/solution/) | [C++](0252-Meeting-Rooms/cpp-0252/) | | | | ||
| | | | | | | | ||
| 253 | [Meeting Rooms II](https://leetcode.com/problems/meeting-rooms-ii/description/) | [solution](https://leetcode.com/problems/meeting-rooms-ii/solution/) | [C++](0253-Meeting-Rooms-II/cpp-0253/) | | | | ||
| 254 | [Factor Combinations](https://leetcode.com/problems/factor-combinations/description/) | [无] | [C++](0254-Factor-Combinations/cpp-0254/) | | | | ||
| | | | | | | | ||
| 257 | [Binary Tree Paths](https://leetcode.com/problems/binary-tree-paths/description/) | [缺:非递归算法] | [C++](0257-Binary-Tree-Paths/cpp-0257/) | [Java](0257-Binary-Tree-Paths/java-0257/src/) | | | ||
|