-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgorithms.js
119 lines (94 loc) · 3.89 KB
/
algorithms.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
require(["convenient"], function() {
//This function is called when scripts/helper/util.js is loaded.
require.ready(function() {
//This function is called when the page is loaded
//(the DOMContentLoaded event) and when all required
//scripts are loaded.
var findMajority = function(arr) {
var findMajorityIfMajorityExists = function(array) {
var id = null;
var count = 0;
for (var i = 0; i < array.length; i++) {
if (id === null) id = array[i];
if (id === array[i]) count++;
if (id !== array[i]) {
count--;
if (count === 0) {
id = null;
}
}
}
return id;
}
var isMarjority = function(array, candidate) {
var count = 0;
for (var i = 0; i < array.length; i++) {
if (candidate == array[i]) count++;
}
if (count > array.length / 2) return true;
return false;
}
var possibleMajority = findMajorityIfMajorityExists(arr);
return isMarjority(arr,possibleMajority) ? possibleMajority : null;
}
var findMajority = function(arr) {
var findMajorityIfMajorityExists = function(array) {
return array.map(function(element) {
//This map transorms each element into a counter
var counter = {};
counter.el = element;
counter.count = 1;
return counter;
}).reduce(function(counter1, counter2) {
// This reduce combines counters with the same ids adding thier counts
// and subtracts the counts from elements with different ids
// the id of the remaining counter must be the majority!
if (counter1.el === null) return counter2;
if (counter2.el === null) return counter1;
if (counter1.el === counter2.el)
return {el: counter1.el, count: counter1.count + counter2.count};
if (counter1.count > counter2.count)
return {el: counter1.el, count :counter1.count - counter2.count};
if (counter1.count < counter2.count)
return {el: counter2.el, count :counter2.count - counter1.count};
if (counter1.count === counter2.count)
return {el : null, count: 0};
}).el;
}
var isMarjority = function(array, candidate) {
var count = 0;
for (var i = 0; i < array.length; i++) {
if (candidate == array[i]) count++;
}
if (count > array.length / 2) return true;
return false;
}
var possibleMajority = findMajorityIfMajorityExists(arr);
return isMarjority(arr,possibleMajority) ? possibleMajority : null;
}
Array.prototype.selectKthSmallest= function(k) {
var array = this;
if (k > array.length) throw new Error("cannot look for kth largest element with k > array size");
if (k === array.length) throw new Error("the smallest kth element is the 1st kth element (not the 0th smallest element");
if ()
var partitionElement = array[0];
var partitions = splitByTestResults(array, function(x) {
// console.log(array);
if (x > partitionElement) return "greater";
if (x < partitionElement) return "lesser";
if (x == partitionElement) return "same";
});
if (!partitions.greater) partitions.greater = [];
if (!partitions.lesser) partitions.lesser = [];
if (partitions.lesser.length === k - 1)
return partitionElement;
if (partitions.lesser.length > k - 1)
return partitions.lesser
.selectKthSmallest(k);
if (partitions.lesser.length < k - 1)
return partitions.greater
.selectKthSmallest(k - partitions.lesser.length - 1);
}
console.log(1,2,3);
});
});