-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmaller.js
365 lines (313 loc) · 8.06 KB
/
smaller.js
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
const smaller1 = arr => arr.map((el, i, arr) => arr.slice(i).filter(n => n <el).length)
const smaller2 = arr => {
const memo = {}
return arr.reduceRight((map, n, i, arr) =>{
if(memo.hasOwnProperty(n)) {
memo[n] ++
} else {
memo[n] = 1
}
var total = Object.keys(memo).filter(key => key < n).reduce((acc, key) => acc + memo[key], 0)
map[i] = total
return map
}, new Array(arr.length))
}
const smaller3 = arr => {
var max = 0
var min = 1000
arr.forEach(v => {
if (v > max) {max = v}
if (v < min) { min = v}
})
const memo = new Array((max - min) + 1).fill(0)
return arr.reduceRight((map, n, i, arr) => {
n = parseInt(n) - min
memo[n]++
var total = memo.slice(0, n).reduce((a,b) => a + b, 0)
map[i] = total
return map
}, new Array(arr.length))
}
const smaller4 = arr => {
const memo = {}
return arr.reduceRight((map, n, i, arr) => {
if(memo.hasOwnProperty(n)) {
memo[n] ++
} else {
memo[n] = 1
}
var total = 0
for(key in memo) {
if (parseInt(key) < n) {total += memo[key] }
}
map[i] = total
return map
}, new Array(arr.length))
}
const smaller5 = arr => {
var max = arr[0]
var min = arr[0]
let memo = [0]
return arr.reduceRight((map, n, i) => {
if (n > max) {
memo.push(...new Array(n - max).fill(0))
max = n
}
if (n < min) {
memo.unshift(...new Array(min - n).fill(0))
min = n
}
n = parseInt(n) - min
memo[n]++
var total = 0
for (var j = 0; j < n; j++ ){
total += memo[j] || 0
}
map[i] = total
return map
}, new Array(arr.length))
}
const smaller6 = arr => {
var max = 0
var min = 1000
arr.forEach(v => {
if (v > max) {max = v}
if (v < min) { min = v}
})
const memo = new Array((max - min) + 1).fill(0)
return arr.reduceRight((map, n, i, arr) => {
n = n - min
memo[n]++
var total = 0
for (var j = 0; j < n; j++ ){
total += memo[j] || 0
}
map[i] = total
return map
}, arr)
}
const smaller7 = arr => {
var max = arr[0]
var min = arr[0]
var memo = [0]
for (var i = arr.length - 1; i >= 0; i--) {
var n = arr[i]
if (n > max) {
for(var ii = 0; ii < n - max; ii++) {
memo.push(0)
}
max = n
}
if (n < min) {
for(var iii = 0; iii < min - n; iii++){
memo.unshift(0)
}
min = n
}
n = parseInt(n) - min
memo[n]++
var total = 0
for (var j = 0; j < n; j++ ){
total += memo[j]
}
arr[i] = total
}
i = null
ii = null
iii = null
memo = null
total = null
j = null
min = null
max = null
n = null
return arr
}
const smaller8 = arr => {
var max = 0
var min = 1000
for(var l = 0; l < arr.length; l++) {
if(arr[l] > max) max = arr[l]
if(arr[l] < min) min = arr[l]
}
var memo = new Array(max - min + 1).fill(0)
for (var i = arr.length - 1; i >= 0; i--) {
var n = arr[i] - min
memo[n]++
var total = 0
for (var j = 0; j < n; j++ ){
total += memo[j]
}
arr[i] = total
}
return arr
}
const smaller9 = arr => {
function BinaryTree(){}
function Empty (){}
function Node (value, left, right, count = 1) {
this.value = value
this.left = left
this.right = right
this.count = count
this.red = true
}
Node.prototype = new BinaryTree()
Node.prototype.constructor = Node
Empty.prototype = new BinaryTree()
Empty.prototype.constructor = Empty
Node.prototype.increment = function(value) {
if(this.value == value) {this.count ++; return true}
return this.value > value ? this.left.increment(value): this.right.increment(value)
}
Node.prototype.insert = function(val){
return val < this.value ?
new Node(this.value, this.left.insert(val), this.right, this.count) :
new Node(this.value, this.left, this.right.insert(val), this.count)
}
Node.prototype.sum = function (value) {
var res = this.value < value ? this.count : 0
return this.left.sum(value) + this.right.sum(value) + res
}
Empty.prototype.insert = function(x) { return new Node(x, this, this) }
Empty.prototype.increment = function() { return false }
Empty.prototype.sum = function() {return 0}
var memo = new Empty()
for (var i = arr.length - 1; i >= 0; i--) {
var n = arr[i]
if(!memo.increment(n)){
memo = memo.insert(n)
}
arr[i] = memo.sum(n)
}
return arr
}
const smaller10 = arr => {
function BinaryTree(){
this.root = null
}
function Node (val, count = 1 ) {
this.val = val
this.left = null
this.right = null
this.count = count
//this.red = true
}
BinaryTree.prototype.push = function(val){
var root = this.root
if(!root){
this.root = new Node(val)
return
}
var currentNode = root
var newNode = new Node(val)
while(currentNode) {
if(val < currentNode.val) {
if(!currentNode.left) {
currentNode.left = newNode
break
}
else {
currentNode = currentNode.left
}
}
else {
if(!currentNode.right) {
currentNode.right = newNode
break
}
else {
currentNode =currentNode.right
}
}
}
}
BinaryTree.prototype.increment = function(val) {
var root = this.root
if(!root) return false
var node = root
while(node) {
if(node.val == val) {
node.count ++
return true
}
if(val < node.val) {
if(!node.left) {
currentNode.left = newNode
break
}
else {
node = node.left
}
}
else {
if(!currentNode.right) {
currentNode.right = newNode
break
}
else {
currentNode =currentNode.right
}
}
}
}
Node.prototype.increment = function(value) {
if(this.value == value) {this.count ++; return true}
return this.value > value ? this.left.increment(value): this.right.increment(value)
}
Node.prototype.insert = function(val){
return val < this.value ?
new Node(this.value, this.left.insert(val), this.right, this.count) :
new Node(this.value, this.left, this.right.insert(val), this.count)
}
Node.prototype.sum = function (value) {
var res = this.value < value ? this.count : 0
return this.left.sum(value) + this.right.sum(value) + res
}
var memo = new Empty()
for (var i = arr.length - 1; i >= 0; i--) {
var n = arr[i]
if(!memo.increment(n)){
memo = memo.insert(n)
}
arr[i] = memo.sum(n)
}
return arr
}
var start = Date.now()
const smaller = smaller7
console.assert(JSON.stringify(smaller([6])) == JSON.stringify([0]), "1: [6] => [0] Does not match", JSON.stringify(smaller([6])))
console.assert(JSON.stringify(smaller([7, 6])) == JSON.stringify([1, 0]), "2: [7, 6] => [1, 0] Does not match", JSON.stringify(smaller([7, 6])))
console.assert(JSON.stringify(smaller([7, 6, 5])) == JSON.stringify([2, 1, 0]), "3 : [7, 6, 5] => [2, 1, 0] Does not match:", JSON.stringify(smaller([7, 6, 5])))
console.assert(JSON.stringify(smaller([4, 7, 6, 5])) == JSON.stringify([0, 2, 1, 0]), "4 Does not match")
console.assert(JSON.stringify(smaller([1, 1, -1, 0, 0])) == JSON.stringify([3, 3, 0, 0, 0]), "5: [1, 1, -1, 0, 0] => [3, 3, 0, 0, 0] Does not match", JSON.stringify(smaller([1, 1, -1, 0, 0])))
console.assert(JSON.stringify(smaller([5, 4, 7, 6, 5])) == JSON.stringify([1, 0, 2, 1, 0]), "6 Does not match")
console.assert(JSON.stringify(smaller([7, 5, 4, 7, 6, 5])) == JSON.stringify([4, 1, 0, 2, 1, 0]), "7 Does not match")
console.assert(JSON.stringify(smaller([5, 4, 7, 9, 2, 4, 4, 5, 6])) == JSON.stringify([4, 1, 5, 5, 0, 0, 0, 0, 0]), "8 Does not match")
var longArray = (la) => {
for(var i = 0; i < 100000; i++){
la[i] = Math.floor(Math.random() * 1000 - 500)
}
return la
}
var seriousTest = (stop) => {
var counter = 0
var array = new Array(100000)
while(counter < stop) {
array = longArray(array)
smaller(array)
console.log(counter)
counter++
}
}
seriousTest(100)
//var res = smaller(longArray()).reduce((a, b) => a + b)
//console.log(res)
// var res = smaller(longArray).reduce((a, b) => a + b) == smaller2(longArray).reduce((a,b) => a + b)
// console.assert(res == true, 'failed equality')
console.log('timetaken', Date.now() - start, "ms")
// var longTotal = smaller1(longArray).reduce((a, b) => a + b)
// console.log('long total', longTotal)
// var longTotal2 = smaller2(longArray).reduce((a, b) => a + b)
// console.log('long total2', longTotal2)
console.log('all tests passed')