Skip to content

Commit 0ed313b

Browse files
commit all easy implementation
1 parent 0050bbc commit 0ed313b

26 files changed

+845
-2
lines changed

2DArrayDS.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ function hourglassSum(arr) {
1616
}
1717
}
1818
return Math.max(...resTab);
19-
}
19+
}

acmIcpcTeam.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Complete the 'acmTeam' function below.
3+
*
4+
* The function is expected to return an INTEGER_ARRAY.
5+
* The function accepts STRING_ARRAY topic as parameter.
6+
*/
7+
8+
function acmTeam(topic) {
9+
// Write your code here
10+
let tempTopic1 = '';
11+
let tempTopic2 = '';
12+
let count = 0;
13+
let valMax = 0;
14+
let countMax = 0;
15+
let resTab = [];
16+
for(let i=0; i<topic.length-1; i++){
17+
for(let j=i+1; j<topic.length; j++){
18+
tempTopic1 = topic[i];
19+
tempTopic2 = topic[j];
20+
for(let k=0; k<tempTopic1.length; k++){
21+
if(tempTopic1[k] == 0 && tempTopic2[k] == 0){
22+
continue;
23+
}else{
24+
count++;
25+
}
26+
}
27+
if(count > valMax){
28+
valMax = count;
29+
countMax=1;
30+
}else if(count == valMax){
31+
countMax++
32+
}
33+
count = 0;
34+
}
35+
}
36+
resTab.push(valMax);
37+
resTab.push(countMax);
38+
return resTab;
39+
}
40+
41+
// let topics = ['10101', '11110', '00010'];
42+
// let topics = ['10101', '11100', '11010', '00101'];
43+
let topics = ['11101', '10101', '11001', '10111', '10000', '01110'];
44+
console.log(acmTeam(topics));

appendAndDelete.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Complete the 'appendAndDelete' function below.
3+
*
4+
* The function is expected to return a STRING.
5+
* The function accepts following parameters:
6+
* 1. STRING s
7+
* 2. STRING t
8+
* 3. INTEGER k
9+
*/
10+
11+
function appendAndDelete(s, t, k) {
12+
// Write your code here
13+
let sArr = s.split("");
14+
let tArr = t.split("");
15+
let count = 0;
16+
for (let i = 0; i === count && i < s.length; i++) {
17+
if (sArr[i] === tArr[i]) {
18+
count++;
19+
}
20+
}
21+
22+
let tMinusCount = t.length - count;
23+
let sMinusCount = s.length - count;
24+
25+
let STK = k - (tMinusCount + sMinusCount);
26+
27+
if (tMinusCount + sMinusCount < k && t.length + s.length > k && STK % 2 !== 0) {
28+
return "No";
29+
} else if (tMinusCount + sMinusCount <= k) {
30+
return "Yes";
31+
} else {
32+
return "No";
33+
}
34+
}

backUp.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,8 @@ var unique = a.filter(onlyUnique);
2626
let a = 12;
2727
let aReverseString = a.toString().split("").reverse().join("");
2828
//transform this string into number
29-
let aNumber = Number(a);
29+
let aNumber = Number(a);
30+
31+
//count number of occurence in a string
32+
console.log(("str1,str2,str3,str4".match(/,/g) || []).length); //logs 3
33+
console.log(("str1,str2,str3,str4".match(new RegExp("str", "g")) || []).length); //logs 4

beautifulTriplets.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Complete the 'beautifulTriplets' function below.
3+
*
4+
* The function is expected to return an INTEGER.
5+
* The function accepts following parameters:
6+
* 1. INTEGER d
7+
* 2. INTEGER_ARRAY arr
8+
*/
9+
10+
function beautifulTriplets(d, arr) {
11+
// Write your code here
12+
let count = arr[0];
13+
let max = arr[arr.length - 1];
14+
let result = 0;
15+
16+
let { values } = new Array(arr.length).fill(0).reduce(
17+
(target, item, index) => {
18+
target["values"][arr[index]] = target["values"][arr[index]]
19+
? (target["values"][arr[index]] += 1)
20+
: 1;
21+
22+
return target;
23+
},
24+
{ values: {} }
25+
);
26+
27+
while (count <= max) {
28+
values[count] &&
29+
values[count + d] &&
30+
values[count + d * 2] &&
31+
(result += Math.max(
32+
values[count],
33+
values[count + d],
34+
values[count + d * 2]
35+
));
36+
37+
count++;
38+
}
39+
40+
return result;
41+
}
42+
43+
44+
let d=3;
45+
let arr=[1, 2, 4, 5, 7, 8, 10];//3
46+
console.log(beautifulTriplets(d, arr));

cavityMap.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Complete the 'cavityMap' function below.
3+
*
4+
* The function is expected to return a STRING_ARRAY.
5+
* The function accepts STRING_ARRAY grid as parameter.
6+
*/
7+
8+
function cavityMap(grid) {
9+
// Write your code here
10+
const cavities = grid.map(item => item.split(''));
11+
12+
return cavities.reduce((target, list, index, source) => {
13+
const result = list
14+
.reduce((listTarget, item, itemIndex) => {
15+
listTarget.push(
16+
index >= 1 &&
17+
itemIndex >= 1 &&
18+
index < source.length - 1 &&
19+
itemIndex < list.length &&
20+
item > source[index][itemIndex - 1] &&
21+
item > source[index][itemIndex + 1] &&
22+
item > source[index - 1][itemIndex] &&
23+
item > source[index + 1][itemIndex]
24+
? 'X'
25+
: item
26+
);
27+
28+
return listTarget;
29+
}, [])
30+
.join('');
31+
32+
target.push(result);
33+
34+
return target;
35+
}, []);
36+
}
37+
38+
let grid = ['989', '191', '111']; //[ '989', '1X1', '111' ]
39+
console.log(cavityMap(grid));
40+

chocolateFeast.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Complete the 'chocolateFeast' function below.
3+
*
4+
* The function is expected to return an INTEGER.
5+
* The function accepts following parameters:
6+
* 1. INTEGER n
7+
* 2. INTEGER c
8+
* 3. INTEGER m
9+
*/
10+
11+
function chocolateFeast(n, c, m) {
12+
// Write your code here
13+
let res = 0;
14+
let div = 0;
15+
let mod = 0;
16+
let temp = 0;
17+
let result = 0;
18+
res = Math.floor(n/c);
19+
div = Math.floor(res/m);
20+
mod = res%m;
21+
result = res + div;
22+
temp = div + mod;
23+
while (temp >= m) {
24+
res = temp;
25+
div = Math.floor(res/m);
26+
mod = res %m;
27+
result += div;
28+
temp = div +mod;
29+
}
30+
return result;
31+
}
32+
33+
let n = 15;
34+
let c = 3;
35+
let m = 2;
36+
console.log(chocolateFeast(n, c, m)); //9

cutTheSticks.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Complete the 'cutTheSticks' function below.
3+
*
4+
* The function is expected to return an INTEGER_ARRAY.
5+
* The function accepts INTEGER_ARRAY arr as parameter.
6+
*/
7+
8+
function cutTheSticks(arr) {
9+
// Write your code here
10+
let resTab = [];
11+
let minVal = 0;
12+
console.log(arr);
13+
resTab.push(arr.length);
14+
while(arr.length > 1){
15+
minVal = Math.min(...arr);
16+
for(let i=arr.length-1; i>=0; i--){
17+
arr[i] -= minVal;
18+
if(arr[i] == 0){
19+
arr.splice(i, 1);
20+
}
21+
}
22+
if(arr.length>0){
23+
resTab.push(arr.length);
24+
}
25+
}
26+
return resTab;
27+
}
28+
29+

equalizeTheArray.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Complete the 'equalizeArray' function below.
3+
*
4+
* The function is expected to return an INTEGER.
5+
* The function accepts INTEGER_ARRAY arr as parameter.
6+
*/
7+
8+
function equalizeArray(arr) {
9+
// Write your code here
10+
let count = 0;
11+
let maxVal = 0;
12+
let tempVal = 0;
13+
let valDuplicate = 0;
14+
let countOperation = 0;
15+
for(let i=0; i<arr.length-1; i++){
16+
for(let j=0; j<arr.length; j++){
17+
if(arr[i] == arr[j]){
18+
count++;
19+
tempVal = arr[i];
20+
}
21+
}
22+
if(count > maxVal){
23+
maxVal = count;
24+
valDuplicate = tempVal;
25+
}
26+
count = 0;
27+
}
28+
29+
for(let j=0; j<arr.length; j++){
30+
if(arr[j] != valDuplicate){
31+
countOperation++;
32+
}
33+
}
34+
return countOperation;
35+
}
36+
37+
let arr = [1, 2, 3, 1, 2, 3, 3, 3];//4
38+
// let arr = [3, 3, 2, 1, 3, 3];//2
39+
// let arr = [36 ,12 ,60 ,99, 78 ,33 ,4 ,21 ,22 ,9 ,12 ,21 ,34 ,76, 21, 3, 3, 37, 65, 27 ,21 ,42 ,11 ,14 ,21 ,88 ,46 ,63 ,79 ,6 ,3,7 ,94, 99 ,68 ,76, 6, 21 ,86, 49,56, 22, 90, 74, 83, 20, 21, 94, 60, 76, 75, 96, 99, 92, 65, 77, 26, 51, 21, 77, 22, 97, 34, 56];
40+
console.log(equalizeArray(arr));
41+

fairRotations.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Complete the 'fairRations' function below.
3+
*
4+
* The function is expected to return a STRING.
5+
* The function accepts INTEGER_ARRAY B as parameter.
6+
*/
7+
8+
function fairRations(B) {
9+
// Write your code here
10+
let count = new Array(B.length - 1).fill(0).reduce((target, item, index) => {
11+
!!(B[index] % 2) && (B[index + 1]++, (target += 2));
12+
13+
return target;
14+
}, 0);
15+
16+
return !(B[B.length - 1] % 2) ? count : "NO";
17+
}
18+
19+
B = [2, 3, 4, 5, 6]
20+
console.log(fairRations(B));

flatlandSpaceStations.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Complete the flatlandSpaceStations function below.
2+
function flatlandSpaceStations(n, c) {
3+
let resMin = 0;
4+
let resTab = [];
5+
let distance = 0;
6+
let tempTab = [];
7+
if(c.length == n){
8+
return 0;
9+
}
10+
for(let i=0; i<n; i++){
11+
if(c.includes(i)) continue;
12+
for(let j=0; j<c.length; j++){
13+
distance = Math.abs(c[j] - i);
14+
tempTab.push(distance);
15+
}
16+
resMin = Math.min(...tempTab);
17+
resTab.push(resMin);
18+
tempTab = [];
19+
}
20+
21+
return Math.max(...resTab);
22+
}
23+
24+
let n=20;
25+
let c = [13, 1, 11, 10, 6];//6
26+
console.log(flatlandSpaceStations(n, c));

halloweenSale.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Complete the 'howManyGames' function below.
3+
*
4+
* The function is expected to return an INTEGER.
5+
* The function accepts following parameters:
6+
* 1. INTEGER p
7+
* 2. INTEGER d
8+
* 3. INTEGER m
9+
* 4. INTEGER s
10+
*/
11+
12+
function howManyGames(p, d, m, s) {
13+
// Return the number of games you can buy
14+
if (s < p) return 0;
15+
16+
let sum = p;
17+
let itemsCount = 0;
18+
while(sum <= s) {
19+
p = Math.max(p-d, m)
20+
sum += p;
21+
itemsCount++;
22+
}
23+
24+
return itemsCount;
25+
}
26+
27+
// let p=16; let d=2; let m=1; let s=9981; //9917
28+
let p=1; let d=100; let m=1; let s=9777; //9777
29+
console.log(howManyGames(p, d, m, s));

0 commit comments

Comments
 (0)