Skip to content

Commit 0050bbc

Browse files
add all easy data structure problem with array
1 parent bfe14a4 commit 0050bbc

7 files changed

+183
-0
lines changed

2DArrayDS.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Complete the 'hourglassSum' function below.
3+
*
4+
* The function is expected to return an INTEGER.
5+
* The function accepts 2D_INTEGER_ARRAY arr as parameter.
6+
*/
7+
8+
function hourglassSum(arr) {
9+
// Write your code here
10+
let resTab = [];
11+
let sum = 0;
12+
for(let i=0; i<=arr.length -3; i++){
13+
for(let j=0; j<=arr.length -3; j++){
14+
sum = arr[i][j] + arr[i][j+1] + arr[i][j+2] + arr[i+1][j+1] + arr[i+2][j]+ arr[i+2][j+1] + arr[i+2][j+2];
15+
resTab.push(sum);
16+
}
17+
}
18+
return Math.max(...resTab);
19+
}

arrayDS.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* Complete the 'reverseArray' function below.
3+
*
4+
* The function is expected to return an INTEGER_ARRAY.
5+
* The function accepts INTEGER_ARRAY a as parameter.
6+
*/
7+
8+
function reverseArray(a) {
9+
// Write your code here
10+
return a.reverse();
11+
12+
}

arrayManipulation.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Complete the 'arrayManipulation' function below.
3+
*
4+
* The function is expected to return a LONG_INTEGER.
5+
* The function accepts following parameters:
6+
* 1. INTEGER n
7+
* 2. 2D_INTEGER_ARRAY queries
8+
*/
9+
10+
function arrayManipulation(n, queries) {
11+
// Write your code here
12+
const arr = new Array(n + 2).fill(0), qn = queries.length;
13+
let sum = 0, maxValue = 0;
14+
15+
for (let i = 0; i < qn; i++) {
16+
const [min, max, sum] = queries[i];
17+
arr[min] += sum;
18+
maxValue = maxValue < sum ? sum : maxValue
19+
if (max <= n) {
20+
arr[max + 1] -= sum;
21+
}
22+
23+
}
24+
for (let k = 0; k < n; k++) {
25+
sum += arr[k];
26+
if (maxValue < sum) {
27+
maxValue = sum;
28+
}
29+
}
30+
return maxValue;
31+
}

dynamicArray.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Complete the 'dynamicArray' function below.
3+
*
4+
* The function is expected to return an INTEGER_ARRAY.
5+
* The function accepts following parameters:
6+
* 1. INTEGER n
7+
* 2. 2D_INTEGER_ARRAY queries
8+
*/
9+
10+
function dynamicArray(n, queries) {
11+
// Write your code here
12+
let lastAnswer = 0;
13+
let arr = [];
14+
let idx = 0;
15+
let resTab = [];
16+
for(let i=0; i<n; i++){
17+
arr.push([]);
18+
}
19+
for(let i=0; i<queries.length; i++){
20+
if(queries[i][0] == 1){
21+
idx = ((queries[i][1] ^ lastAnswer) % n);
22+
arr[idx].push(queries[i][2]);
23+
}else if(queries[i][0] == 2){
24+
idx = ((queries[i][1] ^ lastAnswer) % n);
25+
lastAnswer = arr[idx][queries[i][2] % arr[idx].length];
26+
resTab.push(lastAnswer);
27+
}
28+
}
29+
return resTab;
30+
}

leftRotation.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Complete the 'rotateLeft' function below.
3+
*
4+
* The function is expected to return an INTEGER_ARRAY.
5+
* The function accepts following parameters:
6+
* 1. INTEGER d
7+
* 2. INTEGER_ARRAY arr
8+
*/
9+
10+
function rotateLeft(d, arr) {
11+
// Write your code here
12+
let resTab = [];
13+
let tempIndex = 0;
14+
for(let i=0; i<arr.length; i++){
15+
if(i == 0){
16+
tempIndex = arr.length - d;
17+
resTab[arr.length -d] = arr[i];
18+
continue;
19+
}
20+
if(i-d >= 0){
21+
tempIndex = i-d;
22+
resTab[i-d] = arr[i];
23+
}else{
24+
tempIndex = arr.length -d + i;
25+
resTab[arr.length-d+i] = arr[i];
26+
}
27+
}
28+
return resTab;
29+
}

queriesWithFixedLength.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
/*
3+
* Complete the 'solve' function below.
4+
*
5+
* The function is expected to return an INTEGER_ARRAY.
6+
* The function accepts following parameters:
7+
* 1. INTEGER_ARRAY arr
8+
* 2. INTEGER_ARRAY queries
9+
*/
10+
11+
function solve(arr, queries) {
12+
// Write your code here
13+
const result = [];
14+
for (let i = 0; i < queries.length; i++) {
15+
const windowSize = queries[i];
16+
const tempArr = [];
17+
let isMaxAtBeginning = false;
18+
let max = 0;
19+
for (let j = 0; j < arr.length; j++) {
20+
let window;
21+
if (windowSize + j <= arr.length) {
22+
if (j === 0 || isMaxAtBeginning) {
23+
window = arr.slice(j, j + windowSize);
24+
max = window.reduce((a, b) => (a >= b ? a : b));
25+
}
26+
else {
27+
max = Math.max(max, arr[j + windowSize - 1]);
28+
}
29+
tempArr.push(max);
30+
isMaxAtBeginning = max === arr[j];
31+
}
32+
}
33+
result.push(tempArr.reduce((a, b) => (a <= b ? a : b)));
34+
}
35+
return result;
36+
}

sparseArrays.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Complete the 'matchingStrings' function below.
3+
*
4+
* The function is expected to return an INTEGER_ARRAY.
5+
* The function accepts following parameters:
6+
* 1. STRING_ARRAY stringList
7+
* 2. STRING_ARRAY queries
8+
*/
9+
10+
function matchingStrings(stringList, queries) {
11+
// Write your code here
12+
let resTab = [];
13+
let count = 0;
14+
for(let i=0; i<queries.length; i++){
15+
for(let j=0; j<stringList.length; j++){
16+
if(queries[i] == stringList[j]){
17+
count++;
18+
}else{
19+
continue;
20+
}
21+
}
22+
resTab.push(count);
23+
count = 0;
24+
}
25+
return resTab;
26+
}

0 commit comments

Comments
 (0)