summaryrefslogtreecommitdiffstats
path: root/01-algorithmic_toolbox/02-greedy_algorithms/04-covering_segments
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2016-11-13 19:06:14 +0100
committerJérémy Zurcher <jeremy@asynk.ch>2016-11-13 20:30:08 +0100
commite72410168143db1b79ee30e47d68cca2d730f740 (patch)
tree9aa348a93d9d55e8935587eea7c45e0562d73c4c /01-algorithmic_toolbox/02-greedy_algorithms/04-covering_segments
parent0f13a35dee0df21f4c1a387b50582a958c7bc439 (diff)
downloadcoursera-e72410168143db1b79ee30e47d68cca2d730f740.zip
coursera-e72410168143db1b79ee30e47d68cca2d730f740.tar.gz
Algorithms : add 01-algorithmic_toolbox 02-greedy_algorithms
Diffstat (limited to '01-algorithmic_toolbox/02-greedy_algorithms/04-covering_segments')
-rw-r--r--01-algorithmic_toolbox/02-greedy_algorithms/04-covering_segments/covering_segments.cpp34
-rw-r--r--01-algorithmic_toolbox/02-greedy_algorithms/04-covering_segments/tests/014
-rw-r--r--01-algorithmic_toolbox/02-greedy_algorithms/04-covering_segments/tests/01.a2
-rw-r--r--01-algorithmic_toolbox/02-greedy_algorithms/04-covering_segments/tests/025
-rw-r--r--01-algorithmic_toolbox/02-greedy_algorithms/04-covering_segments/tests/02.a2
5 files changed, 47 insertions, 0 deletions
diff --git a/01-algorithmic_toolbox/02-greedy_algorithms/04-covering_segments/covering_segments.cpp b/01-algorithmic_toolbox/02-greedy_algorithms/04-covering_segments/covering_segments.cpp
new file mode 100644
index 0000000..c8f1fcd
--- /dev/null
+++ b/01-algorithmic_toolbox/02-greedy_algorithms/04-covering_segments/covering_segments.cpp
@@ -0,0 +1,34 @@
+#include <algorithm>
+#include <iostream>
+#include <climits>
+#include <vector>
+
+using std::vector;
+
+struct Segment {
+ int start, end;
+};
+
+vector<int> optimal_points(vector<Segment> &segments) {
+ vector<int> points;
+ //write your code here
+ for (size_t i = 0; i < segments.size(); ++i) {
+ points.push_back(segments[i].start);
+ points.push_back(segments[i].end);
+ }
+ return points;
+}
+
+int main() {
+ int n;
+ std::cin >> n;
+ vector<Segment> segments(n);
+ for (size_t i = 0; i < segments.size(); ++i) {
+ std::cin >> segments[i].start >> segments[i].end;
+ }
+ vector<int> points = optimal_points(segments);
+ std::cout << points.size() << "\n";
+ for (size_t i = 0; i < points.size(); ++i) {
+ std::cout << points[i] << " ";
+ }
+}
diff --git a/01-algorithmic_toolbox/02-greedy_algorithms/04-covering_segments/tests/01 b/01-algorithmic_toolbox/02-greedy_algorithms/04-covering_segments/tests/01
new file mode 100644
index 0000000..c37582a
--- /dev/null
+++ b/01-algorithmic_toolbox/02-greedy_algorithms/04-covering_segments/tests/01
@@ -0,0 +1,4 @@
+3
+1 3
+2 5
+3 6
diff --git a/01-algorithmic_toolbox/02-greedy_algorithms/04-covering_segments/tests/01.a b/01-algorithmic_toolbox/02-greedy_algorithms/04-covering_segments/tests/01.a
new file mode 100644
index 0000000..aedb333
--- /dev/null
+++ b/01-algorithmic_toolbox/02-greedy_algorithms/04-covering_segments/tests/01.a
@@ -0,0 +1,2 @@
+1
+3
diff --git a/01-algorithmic_toolbox/02-greedy_algorithms/04-covering_segments/tests/02 b/01-algorithmic_toolbox/02-greedy_algorithms/04-covering_segments/tests/02
new file mode 100644
index 0000000..694ef84
--- /dev/null
+++ b/01-algorithmic_toolbox/02-greedy_algorithms/04-covering_segments/tests/02
@@ -0,0 +1,5 @@
+4
+4 7
+1 3
+2 5
+5 6
diff --git a/01-algorithmic_toolbox/02-greedy_algorithms/04-covering_segments/tests/02.a b/01-algorithmic_toolbox/02-greedy_algorithms/04-covering_segments/tests/02.a
new file mode 100644
index 0000000..11ac08e
--- /dev/null
+++ b/01-algorithmic_toolbox/02-greedy_algorithms/04-covering_segments/tests/02.a
@@ -0,0 +1,2 @@
+2
+3 6