Skip to content

Commit 6a57714

Browse files
committed
Day 64 a leet code problem - summary_ranges
1 parent 6e23d4b commit 6a57714

File tree

2 files changed

+67
-3
lines changed

2 files changed

+67
-3
lines changed

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
| Current Status| Stats |
88
| :------------: | :----------: |
9-
| Total Problems | 92 |
10-
| Current Streak | 63 days |
11-
| Longest Streak | 63 ( August 17, 2015 - October 18, 2015 ) |
9+
| Total Problems | 93 |
10+
| Current Streak | 64 days |
11+
| Longest Streak | 64 ( August 17, 2015 - October 19, 2015 ) |
1212

1313
</center>
1414

@@ -164,3 +164,9 @@ Include contains single header implementation of data structures and some algori
164164
| :------------ | :----------: |
165165
| Given two integer arrays, A and B, each containing N integers. You are free to permute the order of the elements in the arrays. Is there an permutation A', B' possible of A and B, such that, A'<sub>i</sub>+B'<sub>i</sub> ≥ K for all i, where A'<sub>i</sub> denotes the i<sup>th</sup> element in the array A' and B'<sub>i</sub> denotes i<sup>th</sup> element in the array B'.| [two_arrays.cpp](greedy_problems/two_arrays.cpp)|
166166
|John is taking orders. The i<sup>th</sup> order is placed by the i<sup>th</sup> customer at t<sub>i</sub> time and it takes d<sub>i</sub> time to procees. What is the order in which the customers will get their orders? (see more details in solutions's comments)|[orders_order.cpp](greedy_problems/orders_order.cpp)|
167+
168+
###Leet code Problems
169+
| Problem | Solution |
170+
| :------------ | :----------: |
171+
| Given a sorted integer array without duplicates, return the summary of its ranges. For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"].| [summary_ranges.cpp](leet_code_problems/summary_ranges.cpp)|
172+

leet_code_problems/summary_ranges.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Leet code problem - summary pages
3+
* Given a sorted integer array without duplicates, return the summary of its ranges.
4+
* For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"].
5+
*/
6+
7+
#include <iostream>
8+
#include <algorithm>
9+
#include <vector>
10+
#include <string>
11+
12+
std::vector<std::string> summaryRanges(std::vector<int>& nums) {
13+
std::vector<std::string> ranges;
14+
if (nums.empty()) {
15+
return ranges;
16+
}
17+
std::string curr = std::to_string(nums[0]);
18+
int range_start = nums[0];
19+
20+
for( size_t i = 1; i < nums.size(); ++i ) {
21+
if (nums[i-1] + 1 == nums[i]) {
22+
continue;
23+
} else {
24+
if ( range_start != nums[i-1]) {
25+
curr += "->";
26+
curr += std::to_string(nums[i-1]);
27+
ranges.push_back(curr);
28+
} else {
29+
ranges.push_back(curr);
30+
}
31+
curr = std::to_string(nums[i]);
32+
range_start = nums[i];
33+
}
34+
}
35+
if (range_start != nums[nums.size() - 1]) {
36+
curr += "->";
37+
curr += std::to_string(nums[nums.size() - 1]);
38+
}
39+
ranges.push_back(curr);
40+
return ranges;
41+
}
42+
43+
void printVec( std::vector<std::string> ranges ) {
44+
for ( size_t i = 0; i < ranges.size(); ++i ) {
45+
if ( i != ranges.size() - 1 ) {
46+
std::cout << ranges[i] << ", ";
47+
} else {
48+
std::cout << ranges[i] << std::endl;
49+
}
50+
}
51+
}
52+
53+
int main() {
54+
std::vector<int> nums{0,1,2,4,5,7};
55+
std::vector<std::string> ranges = summaryRanges(nums);
56+
printVec(ranges);
57+
return 0;
58+
}

0 commit comments

Comments
 (0)