The interview questions are three questions about data structures. They can be implemented using javacc ++ python. Can they be implemented using JavaScript? I wrote it on the machine using js. Although it is certainly not a problem, I am very familiar with js. There is an array [0.5, 3]. You can find out the number of frequencies that match the maximum number of frequencies/array length>, if yes, place the subscript Of this number. If no-1 is returned
Var a = [5, 3, 4, 6, 3, 3]
For (var I = 0, ci, r = {}; ci = a [I]; I ++ ){
If (r [ci]) {
R [ci]. idx. push (I );
R [ci]. count ++;
} Else {
R [ci] = {idx: [I], count: 1}
}
}
Var f =-1, l = a. length;
For (var p in r ){
If (r [p]. count/l> 0.5) f = r [p]. idx;
}
Alert (f );
There is an array [5, 0,-5,-4, 5], and a positive or negative number is calculated. The frequency of each number is calculated.
Var a = [5, 3, 4, 6,-3,-3]
For (var I = 0, ci, r = {}; ci = a [I ++];) {
If (r [Math. abs (ci)]) {
R [Math. abs (ci)] ++
} Else {
R [Math. abs (ci)] = 1;
}
}
Var s = [];
For (var p in r ){
S. push (r [p]);
}
Alert (s );
Subsequences with sequences [5, 0, 2,-6, 4] can have [5, 0], [5, 0, 2] [2,-6], [5, 0, 2, -6, 4], but not [5, 2], [5,-6]
Returns the sum of the largest sequences. In this example, the largest sequence is [5, 0, 2] = 7.
Var a = [-6, 0, 2, 5, 4], max = 0;
For (var I = 0, l = a. length; I For (var j = I; j For (var tmp = 0, k = I; k <= j; k ++ ){
Tmp = tmp + a [k];
}
If (tmp> max) max = tmp;
}
}
Alert (max );
Winter reply
Question 1
Script function f (arr) {var arr = arr. slice (). sort (function (a, B) {return B-a;}); var m = arr [Math. floor (arr. length/2)]; var count = 0; for (var I = 0; iarr. length/2) return m; else return-1;} alert (f ([, 3]); script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
If you do not consider the convenient JavaScript Object, then ......
Script function f (arr) {var arr = arr. slice (). sort (function (a, B) {return Math. abs (B)-Math. abs (a) ;}); var r = []; var current; var count; for (var I = 0; I
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
Question 3 DP Solution
Script function f (arr) {var sum = 0, max = 0; for (var I = 0; i0? Sum: 0; if (sum> max) max = sum;} return max;} alert (f ([5, 0, 2,-6, 4]); script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
Third bug with full negative number
Script function f (arr) {var sum = max = arr [0]; for (var I = 1; I = 0? (Sum> 0? Sum: 0) + arr [I]: arr [I]; max = sum> max? Sum: max;} return max;} alert (f ([5, 0, 2,-6, 4]); alert (f ([-5,-2,-4, -7,-1]); script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]