-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday35.cpp
More file actions
83 lines (70 loc) · 2.45 KB
/
day35.cpp
File metadata and controls
83 lines (70 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
Good morning! Here's your coding interview problem for today.
This problem was asked by Google.
Given an array of strictly the characters 'R', 'G', and 'B', segregate the values of the array so that all the Rs come first, the Gs come second, and the Bs come last. You can only swap elements of the array.
Do this in linear time and in-place.
For example, given the array ['G', 'B', 'R', 'R', 'B', 'R', 'G'], it should become ['R', 'R', 'R', 'G', 'G', 'B', 'B'].
*/
#include <gtest/gtest.h>
using namespace std;
/**
* Idea: For me we need to cound the number of R, G and B and then iterate again over the array, placing each character
* at the right place
*
* TC: o(n)
* SC: o(1)
*/
void rgb_order(string *rgb_string)
{
uint r_count{0}, g_count{0}, b_count{0};
for (char c : *rgb_string)
{
if (c == 'R')
r_count++;
if (c == 'G')
g_count++;
if (c == 'B')
b_count++;
}
uint current_r_count{0}, current_g_count{0}, current_b_count{0};
for (auto it = rgb_string->begin(); it != rgb_string->end(); ++it)
{
// cout << *rgb_string << " " << *it << " " << current_r_count << " " << current_g_count << " " << current_b_count << endl;
char c = *it;
if (*it == 'R' && (it < rgb_string->begin() || rgb_string->begin() + current_r_count <= it))
{
auto dest = rgb_string->begin() + current_r_count;
*it = *dest;
*dest = 'R';
current_r_count += 1;
}
else if (*it == 'G' && (it < rgb_string->begin() + r_count || rgb_string->begin() + r_count + current_g_count <= it))
{
auto dest = rgb_string->begin() + r_count + current_g_count;
*it = *dest;
*dest = 'G';
current_g_count += 1;
}
else if (*it == 'B' && (it < rgb_string->begin() + r_count + g_count || rgb_string->begin() + r_count + g_count + current_b_count <= it))
{
auto dest = rgb_string->begin() + r_count + g_count + current_b_count;
*it = *dest;
*dest = 'B';
current_b_count += 1;
}
}
}
TEST(RGB_ORDER, rgb_order)
{
string rgb1 = "RGBRGBRGB";
string rgb2 = "GBRRBRG";
rgb_order(&rgb1);
rgb_order(&rgb2);
EXPECT_STREQ(rgb1.c_str(), "RRRGGGBBB");
EXPECT_STREQ(rgb2.c_str(), "RRRGGBB");
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}