forked from yilenpan/algorithmofanalgorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwaterBlocks.js
More file actions
190 lines (179 loc) · 4.96 KB
/
waterBlocks.js
File metadata and controls
190 lines (179 loc) · 4.96 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/////////////////////////////////////////////////////////////
// Water Blocks
// ---------------------------------------------------------
// Most problems that involve a linear time operation on an array
// can be solved using a greedy solution.
// Essentially, greedy solutions are used to 'carry' a value
// as the program iterates through a list.
//
// EX: finding the max of an array is as simple as iterating
// through the array and carrying a maxValue (init to firstElm in array)
// if there is another item bigger than maxValue, set maxValue
// to this item
//
// In this solution of water blocks, we have to keep track of three things
// the 'right max' the 'left max' and the 'count' which is a
// running sum of all the differences between the 'maxes' and the lower
// block.
//
// We then begin our iteration. We initialize the left max to the
// first element in the array and the right max to the last element
// the array. Then we walk the right and left pointers
// through the array in one at a time, keeping track
// of each 'max' value, and calculating the 'count'.
//
// To avoid any duplcation of blocks, we set our program
// termination to be the point when left and right pointers meet.
/////////////////////////////////////////////////////////////
var waterBlocks = function (blocks) {
var start = 0;
var end = blocks.length - 1;
var count = 0;
var maxLeft = blocks[start];
var maxRight = blocks[end];
while (start !== end) {
if (maxLeft < maxRight) {
start++;
if (blocks[start] < maxLeft) {
count += maxLeft - blocks[start];
}
maxLeft = Math.max(maxLeft, blocks[start]);
} else {
end--;
if (blocks[end] < maxRight) {
count += maxRight - blocks[end];
}
maxRight = Math.max(maxRight, blocks[end]);
}
}
return count;
};
//////////////////////////////////////////////////
// TEST CASES
//////////////////////////////////////////////////
console.table(blockVisualizer([1,5,2,3,5,6]));
console.log(waterBlocks([1,5,2,3,5,6]));
//////////////////////////////////////////////////
// MATRIX BUILDER
//////////////////////////////////////////////////
function buildBlocks (n) {
var col = [];
for (var i = 0; i < n; i++) {
col.push('#');
}
return col;
}
function pluck (array, prop) {
return array.map(function (item) {
return item[prop] || ' ';
});
}
function rotate (matrix) {
var newMatrix = [];
var maxLen = matrix.reduce(function (height, col) {
return Math.max(height, col.length);
}, 0);
for (var i = 0; i < maxLen; i++) {
newMatrix.unshift(pluck(matrix, i));
}
return newMatrix;
}
function blockVisualizer (array) {
return rotate(array.reduce(function (matrix, blocks) {
return matrix.push(buildBlocks(blocks)), matrix;
}, []));
}
/////////////////////////////////////////////////////////////
// WHITE BOARD
// Ex:
//
// | #
// | ### #
// | # ####### lMax = 1
// | ########### rMax = 1
// ------------ count = 0
// ^ ^
// l r (init right and left)
//
// | #
// | ### #
// | # ####### lMax = 1
// | ########### rMax = 2 (new)
// ------------ count = 0
// ^ ^
// l r (start with the right pointer and walk backward)
//
// | #
// | ### #
// | # ####### lMax = 2 (new)
// | ########### rMax = 2
// ------------ count = 0
// ^ ^
// l r (left is less than right so walk forward)
//
// | #
// | ### #
// | # ####### lMax = 2
// | ########### rMax = 3 (new)
// ------------ count = 0
// ^ ^
// l r (left === right so walk backward)
//
// | #
// | ### #
// | # ####### lMax = 2
// | ########### rMax = 3 (new)
// ------------ count = 1 (l is 1 less than lMax)
// ^ ^
// l r (left < right so walk forward)
//
// | #
// | ### #
// | # ####### lMax = 3 (new)
// | ########### rMax = 3
// ------------ count = 1
// ^ ^
// l r (left < right so walk forward)
//
// | #
// | ### #
// | # ####### lMax = 3
// | ########### rMax = 3
// ------------ count = 2 (r is 1 less than rMax)
// ^ ^
// l r (left == right so walk backward)
//
// | #
// | ### #
// | # ####### lMax = 3
// | ########### rMax = 3
// ------------ count = 3 (r is 1 less than rMax)
// ^ ^
// l r (left > right so walk backward)
//
// | #
// | ### #
// | # ####### lMax = 3
// | ########### rMax = 3
// ------------ count = 3
// ^ ^
// l r (left > right so walk backward)
//
// | #
// | ### #
// | # ####### lMax = 3
// | ########### rMax = 4
// ------------ count = 3
// ^^
// lr (left == right so walk backward)
//
// | #
// | ### #
// | # ####### lMax = 3
// | ########### rMax = 4
// ------------ count = 3
// ^
// lr (left and right meet so so walk backward)
//
//
/////////////////////////////////////////////////////////////