-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmock.cpp
More file actions
170 lines (130 loc) · 3.24 KB
/
mock.cpp
File metadata and controls
170 lines (130 loc) · 3.24 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <bits/stdc++.h>
using namespace std;
/*
Spiral Traverse
Difficulty: 🟦 | Category: Arrays | Successful Submissions: 49,656+
Problem Statement:
Write a function that takes in an n x m two-dimensional array (that can be square-shaped when n == m)
and returns a one-dimensional array of all the array's elements in spiral order.
Details:
Spiral order starts at the top left corner of the two-dimensional array, goes to the right,
and proceeds in a spiral pattern all the way until every element has been visited.
array = [
[1, 2, 3, 4],
[12, 13, 14, 5],
[11, 16, 15, 6],
[10, 9, 8, 7],
]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
[
[1, 2]
]
-> [1, 2]
[
[1, 1, 1]
[1, 1, 1]
[1, 1, 1]
]
-> [1, 1, .... , 1]
[[1]]
-> [1]
1 Approach
[
[1, 2, 3]
[7, 6, 5]
[8, 9, 0]
]
0, 0 , 1, 1 ...
0,0
1, 1
[1, 2, 3, 4],
[12, 13, 14, 5],
[11, 16, 15, 6],
ans = [1, 2, 3, 4, 5, 6, 15, 16, 11, 12, 13, 14]
convertToOneDimension(matrix, i, ans)
sizeX = matrix.X -> 3
sizeY = matrix.Y -> 4
last_column = sizeY - i.y - 1 = 4 - 1- 1 = 2
last_row = sizeX- i.x - 1 = 3 - 1- 1 = 1
1, 2
for it i.y < sizeY - i.y it++
ans.push(matrix[i.x, it])
2 < 2
for it i.x + 1 < sizeX - dig it--
ans.push(matrix[it, last_column])
1 >= 1
for it last_coumn - 1 > dig
ans.push(matrix[last_row][it])
for it last_row - 1 > dig
ans.push(matrix[it][i.x]])
*/
void convertDiagonalToOneDimension(vector<vector<int>>& matrix, int dig, vector<int>& ans) {
int sizeX = matrix.size();
int sizeY = matrix[0].size();
if (dig >= sizeX || dig >= sizeY) {
return;
}
int last_column = sizeY - dig - 1;
int last_row = sizeX - dig - 1;
// Direita para esquerda;
for (int it = dig; it < sizeY - dig; it++) {
ans.push_back(matrix[dig][it]);
}
// Cima para baixo
for (int it = dig + 1; it < sizeX - dig; it++) {
ans.push_back(matrix[it][last_column]);
}
// Esquerda para direita
for (int it = last_column - 1; it > dig; it--) {
ans.push_back(matrix[last_row][it]);
}
// Baixo para cima
for (int it = last_row; it > dig; it--) {
ans.push_back(matrix[it][dig]);
}
}
void convertToOneDimension(vector<vector<int>>& matrix, vector<int>& ans) {
int sizeX = matrix.size(), sizeY = matrix[0].size();
int max_dig = max(sizeX/2, sizeY/2);
for (int dig = 0; dig < max_dig; dig++) {
convertDiagonalToOneDimension(matrix, dig, ans);
}
}
void printArr (vector<int> ans) {
for (auto val : ans) {
cout << val << " ";
}
cout << endl;
}
int main () {
vector<vector<int>> arr = {
{1, 2, 3, 4},
{12, 13, 14, 5},
{11, 16, 15, 6},
{10, 9, 8, 7},
};
vector<int> ans;
convertToOneDimension(arr, ans);
printArr(ans);
arr = {
{1, 2}
};
ans = {};
convertToOneDimension(arr, ans);
printArr(ans);
arr = {
{1, 2, 3, 4},
{12, 13, 14, 5},
{11, 16, 15, 6},
};
ans = {};
convertToOneDimension(arr, ans);
printArr(ans);
arr = {
{1},
{2}
};
ans = {};
convertToOneDimension(arr, ans);
printArr(ans);
}