-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem283.cpp
141 lines (90 loc) · 3.32 KB
/
problem283.cpp
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
using namespace std::chrono;
// to compile
// g++ -std=c++11 -O2 -Wall problem283.cpp -o problem283
/**
this attempt demonstrates solving the problem but without doing it in place. Here we add all the non-zero values to a new vector,
keeping a count of however many 0s we have seen and adding them after. This is O(n) for space complexity as well as O(n) for time complexity
**/
vector<int> basicAttempt(vector<int>& nums){
vector<int> result;
int count = 0;
for(int n : nums ){
if (n != 0 ){
result.push_back(n);
} else {
count++;
}
}
while(count > 0 ){
count--;
result.push_back(0);
}
return result;
}
/**
Here, it remains O(n) for time complexity as it is just a for loop through the array, however, it is done in place,
as a new vector is not required to be made
**/
vector<int> constantSpace(vector<int>& nums){
int nonZeroPos = 0;
for(int i = 0; i < nums.size(); i++){
if(nums[i] != 0 ){
nums[nonZeroPos++] = nums[i];
}
}
for(int i = nonZeroPos; i < nums.size(); i++){
nums[i] = 0;
}
return nums;
}
/**
While this is also O(n) time complexity and O(1) space complexity,
this provides a better solution as it does so in optimal steps
*/
vector<int> optimumTime(vector<int>& nums){
int nonZeroPos = 0 ;
for(int i = 0; i < nums.size(); i++ ){
if(nums[i] != 0){
swap(nums[nonZeroPos++], nums[i]);
}
}
return nums;
}
int main(){
//input the numbers you would like into the vector
//practise using cin and cout for cpp
string line;
int number;
vector<int> numbers;
cout << "Enter numbers separated by spaces: ";
getline(cin, line);
istringstream stream(line);
while (stream >> number)
numbers.push_back(number);
auto start = high_resolution_clock::now();
vector<int> attempt1 = basicAttempt(numbers);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
auto start2 = high_resolution_clock::now();
vector<int> attempt2 = constantSpace(numbers);
auto stop2 = high_resolution_clock::now();
auto duration2 = duration_cast<microseconds>(stop2 - start2);
auto start3 = high_resolution_clock::now();
vector<int> attempt3 = optimumTime(numbers);
auto stop3 = high_resolution_clock::now();
auto duration3 = duration_cast<microseconds>(stop3 - start3);
cout << "Basic 1: ";
for(int n : attempt1){ cout << n << ","; };
cout << " : Time taken: " << duration.count() << " microseconds" << "\n" ;
cout << "Answer 2: ";
for(int n : attempt2){ cout << n << "," ;};
cout << " : Time taken: " << duration2.count() << " microseconds" << "\n";
cout << "Answer 3: ";
for(int n : attempt3){ cout << n << ","; };
cout << " : Time taken: " << duration3.count() << " microseconds" << "\n";
return -1;
}