Skip to content

Commit cb3df9f

Browse files
committed
added array shift
1 parent 470ceac commit cb3df9f

File tree

4 files changed

+49
-4
lines changed

4 files changed

+49
-4
lines changed

findThreeLargestNumbers.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
function findThreeLargestNumbers(array) {
2+
// space complexity=o(1), TC=o(n)
3+
const largest = [null, null, null];
4+
for (let value of array) {
5+
update(largest, value);
6+
}
7+
return largest;
8+
}
9+
10+
function update(largest, value) {
11+
if (largest[2] === null || value > largest[2]) {
12+
shift(largest, value, 2);
13+
} else if (largest[1] === null || value > largest[1]) {
14+
shift(largest, value, 1);
15+
} else if (largest[0] === null || value > largest[0]) {
16+
shift(largest, value, 0);
17+
}
18+
}
19+
20+
function shift(array, value, idx) {
21+
for (let i = 0; i <= idx; i++) {
22+
if (i === idx) {
23+
array[i] = value;
24+
} else {
25+
array[i] = array[i + 1];
26+
}
27+
}
28+
}
29+
30+
// Do not edit the line below.
31+
exports.findThreeLargestNumbers = findThreeLargestNumbers;

powerset.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ console.log(powerset([1, 2, 3]));
1414

1515
// recursive solution
1616

17+
// time and space complexity o(n2^n)
1718
function powerset(array, index = null) {
1819
if (index === null) {
1920
index = array.length - 1;

pyramid.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11

2-
3-
4-
5-
62
pyramid=(k)=>{
73
arr2=[];
84
result=[];

smallestDifference.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function smallestDifference(arrayOne, arrayTwo) {
2+
// Write your code here.
3+
let value = arrayOne[i] - arrayTwo[j];
4+
for (let i = 0; i < arrayOne.length; i++) {
5+
for (let j = 0; j < arrayTwo.length; j++) {
6+
if (Math.abs(arrayOne[i] - arrayTwo[j]) < value) {
7+
value = Math.abs(arrayOne[i] - arrayTwo[j]);
8+
let arr = [];
9+
arr.push(arrayOne[i]);
10+
arr.push(arrayTwo[j]);
11+
}
12+
}
13+
}
14+
}
15+
16+
// Do not edit the line below.
17+
exports.smallestDifference = smallestDifference;

0 commit comments

Comments
 (0)