aboutsummaryrefslogtreecommitdiff
path: root/challenge-206/paulo-custodio/cpp
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-206/paulo-custodio/cpp')
-rw-r--r--challenge-206/paulo-custodio/cpp/ch-1.cpp67
-rw-r--r--challenge-206/paulo-custodio/cpp/ch-2.cpp85
2 files changed, 152 insertions, 0 deletions
diff --git a/challenge-206/paulo-custodio/cpp/ch-1.cpp b/challenge-206/paulo-custodio/cpp/ch-1.cpp
new file mode 100644
index 0000000000..de714209d8
--- /dev/null
+++ b/challenge-206/paulo-custodio/cpp/ch-1.cpp
@@ -0,0 +1,67 @@
+/*
+Challenge 206
+
+Task 1: Shortest Time
+Submitted by: Mohammad S Anwar
+
+You are given a list of time points, at least 2, in the 24-hour clock format HH:MM.
+
+Write a script to find out the shortest time in minutes between any two time points.
+Example 1
+
+Input: @time = ("00:00", "23:55", "20:00")
+Output: 5
+
+Since the difference between "00:00" and "23:55" is the shortest (5 minutes).
+
+Example 2
+
+Input: @array = ("01:01", "00:50", "00:57")
+Output: 4
+
+Example 3
+
+Input: @array = ("10:10", "09:30", "09:00", "09:55")
+Output: 15
+*/
+
+#include <algorithm>
+#include <iostream>
+#include <sstream>
+#include <string>
+#include <vector>
+
+int minutes(const std::string& str) {
+ int hours = 0, minutes = 0;
+ char sep;
+ std::istringstream iss{ str };
+ if ((iss >> hours >> sep >> minutes) && sep == ':')
+ return hours * 60 + minutes;
+ else
+ return 0;
+}
+
+int main(int argc, char* argv[]) {
+ argv++; argc--;
+ if (argc < 2) {
+ std::cerr << "Usage: ch-1 time..." << std::endl;
+ return EXIT_FAILURE;
+ }
+
+ std::vector<int> items;
+ for (int i = 0; i < argc; i++)
+ items.push_back(minutes(argv[i]));
+ std::sort(items.begin(), items.end());
+ items.push_back(items.front() + 24 * 60);
+
+ int min = items.back() - items.front();
+ for (size_t i = 0; i < items.size() - 1; i++) {
+ int n = items[i+1] - items[i];
+ if (n < min)
+ min = n;
+ }
+
+ std::cout << min << std::endl;
+
+ return EXIT_SUCCESS;
+}
diff --git a/challenge-206/paulo-custodio/cpp/ch-2.cpp b/challenge-206/paulo-custodio/cpp/ch-2.cpp
new file mode 100644
index 0000000000..d91460649d
--- /dev/null
+++ b/challenge-206/paulo-custodio/cpp/ch-2.cpp
@@ -0,0 +1,85 @@
+/*
+Challenge 206
+
+Task 2: Array Pairings
+Submitted by: Mohammad S Anwar
+
+You are given an array of integers having even number of elements..
+
+Write a script to find the maximum sum of the minimum of each pairs.
+Example 1
+
+Input: @array = (1,2,3,4)
+Output: 4
+
+Possible Pairings are as below:
+a) (1,2) and (3,4). So min(1,2) + min(3,4) => 1 + 3 => 4
+b) (1,3) and (2,4). So min(1,3) + min(2,4) => 1 + 2 => 3
+c) (1,4) and (2,3). So min(1,4) + min(2,3) => 2 + 1 => 3
+
+So the maxium sum is 4.
+
+Example 2
+
+Input: @array = (0,2,1,3)
+Output: 2
+
+Possible Pairings are as below:
+a) (0,2) and (1,3). So min(0,2) + min(1,3) => 0 + 1 => 1
+b) (0,1) and (2,3). So min(0,1) + min(2,3) => 0 + 2 => 2
+c) (0,3) and (2,1). So min(0,3) + min(2,1) => 0 + 1 => 1
+
+So the maximum sum is 2.
+*/
+
+#include <algorithm>
+#include <iostream>
+#include <vector>
+
+void compute_pairs_max(int& max,
+ const std::vector<int>& set, const std::vector<int>& pending
+) {
+ if (pending.size() == 0) { // compute sum, set max
+ int sum = 0;
+ for (size_t i = 0; i < set.size(); i += 2) {
+ int n = std::min(set[i], set[i + 1]);
+ sum += n;
+ }
+ max = std::max(max, sum);
+ }
+ else { // recurse for each pair
+ for (size_t i = 0; i < pending.size() - 1; i++) {
+ for (size_t j = i+1; j < pending.size(); j++) {
+ std::vector<int> new_set = set;
+ std::vector<int> new_pending = pending;
+
+ new_set.push_back(pending[i]);
+ new_set.push_back(pending[j]);
+
+ new_pending.erase(new_pending.begin() + j);
+ new_pending.erase(new_pending.begin() + i);
+
+ compute_pairs_max(max, new_set, new_pending);
+ }
+ }
+ }
+}
+
+int main(int argc, char* argv[]) {
+ argv++; argc--;
+ if (argc % 2 != 0) {
+ std::cerr << "usage: ch-2 pairs..." << std::endl;
+ return EXIT_FAILURE;
+ }
+
+ std::vector<int> set;
+ std::vector<int> pending;
+ for (int i = 0; i < argc; i++)
+ pending.push_back(atoi(argv[i]));
+
+ int max = 0;
+ compute_pairs_max(max, set, pending);
+ std::cout << max << std::endl;
+
+ return EXIT_SUCCESS;
+}