All Algorithm Solutions
All Algorithm Solutions
Org:
https://www.hackerrank.com/interview/preparation-kits/three-month-preparation-kit/three-month-
week-one/challenges
Org:
https://docs.google.com/spreadsheets/d/1pa33WfRpMmKP6GWpxo9VdYS2YqBxlNQOFWJIgD
SeQCQ/edit#gid=0
Week 1:
Time conversion:
let a = '07:05:45PM';
function print24(str){
// Get hours
// If time is in "AM"
if (str[8] == 'A')
{
if (hh == 12)
{
final = "00"
for (var i = 2; i <= 7; i++){
final = final + str[i]
}
}
else
{
for (var i = 0; i <= 7; i++){
final = final + str[i]
}
}
}
// If time is in "PM"
else
{
if (hh == 12)
{
final = "12"
for (var i = 2; i <= 7; i++)
final = final + str[i]
}
else
{
hh = hh + 12;
final = final + hh
for (var i = 2; i <= 7; i++)
final = final + str[i]
}
}
console.log(final);
return final;
print24(a)
function countIt(arr){
let minRec = 0;
let maxRec = 0;
arr.forEach(el=> {
if(el < min){
min = el;
minRec = minRec+1;
}
if(el > max){
max = el;
maxRec++;
}
})
console.log(min, max);
console.log(minRec, maxRec);
countIt(arr1)
Camel Case 4
function scanner(input) {
let arr = input.split(";");
let word = arr[2];
// split
if (arr[0] === "S") {
let regexS = /[A-Za-z]/g;
let word2 = word.match(regexS).join("");
// capitalise everyWord
for (let i = 0; i < combClassWord.length; i++) {
combClassWord[i] =
combClassWord[i][0].toUpperCase() +
combClassWord[i].substr(1);
}
// end of combine
}
// end of function
}
scanner(str);
Key:
let count = 0;
let el = ar[i];
console.log(el);
// end of j loop
}
console.log(count);
return count;
// ! end of function
}
Sparse Arrays
queries.forEach(el=> {
// console.log(el);
strings.forEach(qu=> {
// console.log(qu);
if(el === qu){
arr.push(el)
}
})
final.push(arr)
})
console.log(finalFinal);
return finalFinal;
matchingStrings(
[ 'aba', 'baba', 'aba', 'xzxb' ], // strings
[ 'aba', 'xzxb', 'ab' ] // queries
)
___________________
Week 2:
Lonely Integer
Key:
● If el1 === el2, then i++ , else save the index = i (i.e. the iteration);
● With this move we isolate 1 element that has no pair
function lonelyinteger(a) {
// Write your code here
let index = 0;
console.log(sorted[index]);
return sorted[index]
lonelyinteger(arr);
function lonelyinteger(a) {
// Write your code here
a.sort((a,b)=> a-b)
console.log(i);
let el = uniq[i];
console.log("el: ", el);
let arr = [ 0, 0, 1, 2, 1 ];
lonelyinteger(arr)
Grading Students
function gradingStudents(grades) {
// Write your code here
let a = grades.map(el=> {
return el
});
console.log(a)
return a;
older:
function gradingStudents(grades) {
// Write your code here
if(test - el >=3){
console.log(el);
return el;
}
})
console.log(a);
return a;
// gradingStudents(29); // no rounding
// gradingStudents(84); // should be 85 cause 85-84 is less than 3
// gradingStudents(57); // no rounding cause 60 - 57 is 3 or higher
Flipping bits
function flippingBits(n) {
let a = ((2**32)-1) - n;
console.log(a);
return a;
}
flippingBits(3)
Diagonal Difference
function diagonalDifference(arr) {
let lTr = 0;
let rTl = 0;
older:
function diagonalDifference(arr) {
let diagnalLR = [];
let count1 = 0;
let diagnalRL = [];
let count2 = arr.length - 1;
console.log(diagnalLR);
console.log(diagnalRL);
console.log(reducedLR);
console.log(reducedRL);
return sum;
}
let input = [
[11, 2, 4],
[4, 5, 6],
[10, 8, -12],
];
diagonalDifference(input);
Counting Sort 1
function countingSort(arr) {
// Write your code here
arr.forEach(el=> {
finalArr[el] = finalArr[el] + 1
})
console.log(finalArr);
return finalArr;
countingSort(input)
Counting Valleys
pathToArray.forEach((el, index)=> {
if(el=== 'D'){
distanceFromSeaLevel--
}
return totalValleys
}
let stepsIn = 8;
let pathIn = 'UDDDUDUU';
countingValleys(stepsIn, pathIn)
Pangrams
function pangrams(s) {
let input1 = 'We promptly judged antique ivory buckles for the next prize'; //
yes
let input2 = 'We promptly judged antique ivory buckles for the prize'; // no
pangrams(input1);
Mars Exploration
function marsExploration(s) {
// Write your code here
finalArr.forEach(el=> {
// console.log(el)
});
Video: https://www.youtube.com/watch?v=M-iS49m2m7E
Solution: https://github.com/subratsir/DSA-JavaScript/blob/main/subratsir/flipping-the-matrix.js
let input1 = [
[ 112, 42, 83, 119 ],
[ 56, 125, 56, 49 ],
[ 15, 78, 101, 43 ],
[ 62, 98, 114, 108 ]
]
I can only reverse rows and columns, so upper LEFT Upper quadrant should have the highest
possible numbers
// console.log(matrix[i][j]);
// console.log(matrix[i][endLine-j]);
// console.log(matrix[endLine-i][j]);
// console.log(matrix[endLine-i][endLine-j]);
finalArr.push(Math.max(
matrix[i][j],
matrix[i][endLine-j],
matrix[endLine-i][j],
matrix[endLine-i][endLine-j]
));
// console.log(finalArr);
// end of j for loop
}
let input2 = [
[1,2],
[3,4]
]
let input1 = [
[ 112, 42, 83, 119 ],
[ 56, 125, 56, 49 ],
[ 15, 78, 101, 43 ],
[ 62, 98, 114, 108 ]
]
flippingMatrix(input1);
let input3 = [
[112, 42, 83, 119, 234, 120],
[56, 125, 57, 49, 12, 81],
[15, 78, 101, 43, 113, 41],
[62, 98, 114, 108, 43, 21],
[523, 152, 112, 145, 7, 22],
[210, 76, 242, 41, 80, 11]
]
Key to solution:
● The idea in here is that any Number of the same color, can be inside the ‘top-left-corner’
of NxN matrix
● There is ALWAYS 4 of each ‘number colors’
● So all we have to do is pick the BIGGEST NUMBER out of all those ‘4 same color’
numbers
● To accomplish this, we need to split the Length of the matrix into 2, so we can target
0,1,2 and length - 0, 1, 2, or which ever.
● You can NOT go forEach in here or not splitting the matrix into 2, it will not work, cause I
can NOT target outer elements without the split
i
0 arr1 112 42 83 119
1 aar2 56 125 56 49
2 arr3 15 78 101 43
3 arr4 62 98 114 108
i
0 arr1 112 42 83 119 234 120
1 aar2 56 125 57 49 12 81
2 arr3 15 78 101 43 113 41
3 arr4 62 98 114 108 43 21
4 arr5 523 152 112 145 7 22
5 aar6 210 76 242 41 80 11
_______________________
Week 3:
Permuting Two Arrays
Key:
function twoArrays(k, A, B) {
A.sort((a,b)=> a-b);
B.sort((a,b)=> b-a);
console.log(fStr);
return fStr;
function twoArrays(k, A, B) {
// Write your code here
console.log(testPass);
if(testPass){
return 'YES'
} else {
return 'NO'
}
// end of function
}
let k1 = 10;
let a1 = [ 2, 1, 3 ]
let b1 = [ 7, 8, 9 ]
let k2 = 4;
let a2 = [ 20, 1 ]
let b2 = [ 1,1 ]
Key:
function birthday(s, d, m) {
let count = 0;
console.log(count);
return count;
// ! end of function
}
let s1 = [2,2,1,3,2];
let d1 = 4;
let m1 = 2; // [2,2] // [1,3]
birthday(s1,d1,m1)
older
function birthday(s, d, m) {
// Write your code here
console.log(s);
let possibleArr = []
filtered.forEach(el=> {
console.log(el);
let test = el.slice(0, m);
console.log("test: ", test);
})
console.log(possibleArr);
return possibleArr.length;
// end of function
}
let s1 = [ 1, 2, 1, 3, 2 ]; // 2
let d1 = 3;
let m1 = 2;
let s2 = [ 2, 2, 1, 3, 2 ]; // 2
let d2 = 4;
let m2 = 2;
Sales by Match
Key:
> if first equals to next, then count++ and i++, so we skip the iteration and move on
ar.sort((a,b)=> a-b);
console.log(ar);
let count = 0;
// for loop i
}
console.log(count);
return count;
// ! end of fn
}
old:
console.log(ar);
uqAr.forEach(el=> {
let count = 0;
console.log(el);
})
console.log(final);
return final.length;
let n1 = 9;
let ar1 = [10, 20, 20, 10, 10, 30, 50, 10, 20];
let n2 = 7;
let ar2 = [1,2,1,2,1,3,2];
sockMerchant(n2, ar2)
Migratory Birds
Key:
function migratoryBirds(arr) {
arr.sort((a,b)=> a-b);
console.log(final.map(el=> el.length));
// ! end of fn
}
let a1 = [1,1,2,2,3];
migratoryBirds(a1);
old:
function migratoryBirds(arr) {
// Write your code here
console.log(arr)
let id = [1,2,3,4,5];
let id1 = 0;
let id2 = 0;
let id3 = 0;
let id4 = 0;
let id5 = 0;
id.forEach(el=> {
console.log(el);
for(var i = 0; i < sorted.length; i++){
if(el === sorted[i]){
}
}
});
console.log(id1)
console.log(id2)
console.log(id3)
console.log(id4)
console.log(id5)
let final = [
{type: 1, spotted: id1},
{type: 2, spotted: id2},
{type: 3, spotted: id3},
{type: 4, spotted: id4},
{type: 5, spotted: id5},
];
finalFiltered.forEach(el=> {
if(el.spotted === spottedMax){
finalArr.push(el)
}
});
return finalArr[0].type;
let arr1 = [ 1, 4, 4, 4, 5, 3 ];
let arr2 = '1 2 3 4 5 4 3 2 1 3 4'.split(' ').join('').split('').map(el=>
Number(el));
migratoryBirds(arr2);
Maximum Perimeter Triangle
function maximumPerimeterTriangle(sticks) {
// Write your code here
console.log(sticks);
console.log(final);
}
let s1 = [ 1, 1, 1, 3, 3 ];
let s2 = [1,2,3,4,5,10];
maximumPerimeterTriangle(s2)
Drawing Book
Key:
> iterate front and if the index is not found, the pageForward++, if found then break out of the loop
> iterate back and if the index is not found, the pageBack++, if found then break out the loop
function chunkIt(array){
return final;
}
function pageCount(n, p) {
let countF = 0;
let countB = 0;
console.log(countF);
console.log(countB);
// ! end of FN
}
let n1 = 5;
let p1 = 3; // 1
let n2 = 6;
let p2 = 4; // 1
let n3 = 5;
let p3 = 4; // 2
pageCount(n3,p3);
OLD:
JavaScript:
● If you are making an array out of the whole book, then Whole book array will be N+1,
● reminder to use: Math.ceil, to get the upper page, when splitting the book into 2.
function pageCount(n, p) {
// Write your code here
console.log(fullBook);
let turns = 0;
let stop = false;
fullBookFromStart.forEach(el=> {
console.log(el);
if(stop){
return
}
turns++;
})
console.log(turns);
return turns;
// end of testFirst
}
let turns = 0;
let stop = false;
fullBookFromEnd.forEach(el=> {
console.log(el);
if(stop){
return
}
turns++;
})
console.log(turns);
return turns;
console.log('zero')
return 0
let n1 = 6;
let p1 = 5;
pageCount(n1, p1);
Key:
> get the MAX value from the B array, so we do NOT have to do to many iterations
> check that every element in A array is divisible by bMAX[i]... bMAX[i-1]... NOTE: we want a
temp array in here, so that if both elements from A are equally divided into i, we check, then we
push i into the finalArr1
> then iterate through every element in B array, and check how many elements form finalArr1
are equally divided into el from B, we push those elements as arrays into finalArr2
> we then have a finalArry that looks like this: [ [ 24, 12, 6 ], [ 36, 18, 12, 6 ] ]
> I then just need to pick out the duplicates between the arrays and return .length, i.e. how
many duplicates were found.
> Algo, to pick out duplicates from array can be found in here:
https://docs.google.com/document/d/1DgiTZ88yP__YSvNAlIdbk3VRfwk2mflFv6T6uKqaAtc/edit
function findDubplicateInArrays(...args){
let holder = [...args];
let holderLength = holder.length;
let flatHolder = holder.flat(10).sort((a,b)=> a-b);
flatHolder.forEach(el=> {
let count = 0;
flatHolder.forEach(el2=> {
if(el === el2){
count++
if(count >= holderLength){
duplicate.push(el)
}
}
})
})
function getTotalX(a, b) {
// sort both
a.sort((a,b)=> a-b);
b.sort((a,b)=> a-b);
// condole.log(fAr);
fAr2.push(temp)
// condole.log(fAr2);
// condole.log(findDubplicateInArrays(...fAr2))
return findDubplicateInArrays(...fAr2).length;
// ! end fn
}
let a1 = [2,6];
let a2 = [24,36];
getTotalX(a1,a2);
older:
function getTotalX(a, b) {
// Write your code here
a.forEach(el=> {
// console.log(el);
holderOfAllFactorsOfArray1.push(temp)
})
// console.log(holderOfAllFactorsOfArray1);
// remove all elements that are bigger or equal to B last number, cause I can
NOT divide them into the Last number, i.e. they are NOT factors of it, since they
are bigger
holderOfAllFactorsOfArray1.forEach(el=> {
let temp = [];
el.forEach(el2=> {
if(el2 <= b[b.length-1]){
temp.push(el2)
}
})
finalHolder.push(temp);
})
console.log(finalHolder);
b.forEach(el => {
let temp = [];
for(var i = 0; i <= el; i++){
if(el%i === 0){
temp.push(i)
}
}
finalHolder.push(temp)
})
console.log(finalHolder);
// now lets find every element from the array that appears in EVERY ARRAY, SO
IT HAS TO APPEAR A.LENGTH + B.LENGTH NUMBER OF TIMES, this means it is common to
ALL arrays and thus is our answer
final.forEach(el=> {
console.log(el);
let count = 0;
final.forEach(el2=> {
if(el === el2){
count++
})
//
let a1 = [2, 4];
let a2 = [2];
let a3 = [3, 4];
getTotalX(a3, b3);
_______________________
Week 4:
Picking Numbers
function pickingNumbers(a) {
// Write your code here
sorted.forEach(el=> {
let count = 0;
sorted.forEach(el2=> {
if(el2 - el === 0 || el2 - el === 1){
count++
}
})
final.push(count)
});
// ! end of function
}
let a1 = [ 1, 2, 2, 3, 1, 2 ]; //5
let a2 = [ 4, 6, 5, 3, 3, 1 ]; // 3
pickingNumbers(a2);
Left Rotation
console.log(arr);
return arr;
let rotations1 = 4;
let arr1 = [ 1, 2, 3, 4, 5 ];
rotateLeft(rotations1, arr1)
Number Line Jumps
Note: Kangaroos HAVE TO jump every turn.
// console.log(car1Location);
// console.log(car2Location);
if (possible) {
return "YES";
} else {
return "NO";
}
// ! end of function
}
let xx1 = 0;
let vv1 = 3;
let xx2 = 4;
let vv2 = 2; // YES
kangaroo(xx1, vv1, xx2, vv2);
function separateNumbers(s) {
// NOTES:
// Correct way to slice is: .slice(0, str.length);
// console.log(s.length);
let pretty = 'NO';
// console.log(first)
// console.log(s.length)
// console.log(finalStr.length);
// console.log(s);
// console.log(finalStr);
if(finalTest){
pretty = 'YES' + ' ' + first;
console.log(pretty);
return pretty;
}
// end of forloop i
}
// console.log(firstArr);
console.log(pretty);
return pretty;
// ! end of main function
}
separateNumbers(s4);
Note: This code should 100%, work but Big INT is NOT computing correctly:
function separateNumbers(s) {
// the key here is to create what it should look like and compare
// console.log(i)
let temp = 0;
while(strTest.length < s.length){
temp++
}
console.log(starting);
console.log(incrementer);
console.log(strTest);
console.log('NO')
Closest Numbers
Key:
● Sort it
● Test el[i] vs el[i+1] if smaller than Smallest variable, push into array
● Filter array to keep anything that is equal to the smallest
function closestNumbers(arr) {
arr.sort((a,b)=> a-b);
console.log(arr);
console.log(finalArr);
console.log(smallest);
let arr1= '-20 -3916237 -357920 -3620601 7374819 -7330761 30 6246457 -6461594
266854'.split(' '); // -20 30
let arr2 = '-20 -3916237 -357920 -3620601 7374819 -7330761 30 6246457 -6461594
266854 -520 -470'.split(' '); // -520 -470 -20 30
let arr3 = [1,2,3,4,5]; // [ 1, 2, 2, 3, 3, 4, 4, 5 ]
closestNumbers(arr3);
old:
function closestNumbers(arr) {
sorted.forEach((el, index)=> {
let current = el;
let next = sorted[index+1];
if(!next){
console.log('undefined');
return;
}
console.log(test);
})
console.log(finalArr);
// ! end of function
}
let arr1= '-20 -3916237 -357920 -3620601 7374819 -7330761 30 6246457 -6461594
266854'.split(' '); // -20 30
let arr2 = '-20 -3916237 -357920 -3620601 7374819 -7330761 30 6246457 -6461594
266854 -520 -470'.split(' '); // -520 -470 -20 30
Tower Breakers
Solution Explanation:
If the number of towers are even that means player 2 is the winner. No matter what move player 1
If the height of each tower is 1 that means player 1 cannot move at all so again player 2 is winner.
Note: this is NOT PROGRAMMING SOLUTION, but more of a Math Problem, so don’t waste
time on it, cause we don’t need to Math Wizards for Front End.
function towerBreakers(n, m) {
return 1
}
let n1 = 2;
let m1 = 2;
let n2 = 1;
let m2 = 4;
towerBreakers(n1, m1)
Key to success: Key here is to SORT from small to large, then no need to check if Dif === 0 and I can just
compare Math.abs( arr[index+1] with current element) and then assign it to Minimum if the difference
is bigger
function minimumAbsoluteDifference(arr) {
let minimum = Infinity;
Key:
● Create a alphabet that just repeats 10 times and get index from there
● Its a string repeat so time complexity will be low
function caesarCipher(s, k) {
// lower
if(/[a-z]/.test(s[i])){
finalStr = finalStr + alph[alph.indexOf(s[i])+k];
continue;
}
// upper
if(/[A-Z]/.test(s[i])){
finalStr = finalStr + capAlph[capAlph.indexOf(s[i])+k];
continue;
}
// console.log(finalStr);
return finalStr;
old:
function caesarCipher(s, k) {
// Write your code here
let max = 100;
const small = "abcdefghijklmnopqrstuvwxyz";
const large = small.toUpperCase();
});
let s2 = 'Always-Look-on-the-Bright-Side-of-Life';
let k2 = 5; // Fqbfdx-Qttp-ts-ymj-Gwnlmy-Xnij-tk-Qnkj
let s3 = 'XYZ-/xyz';
let s4 = '12lcfd';
let k4 = 98; // fwzx
caesarCipher(s4, k4)
● YOU HAVE TO USE REPLACE, cause if you try to convert it from String into Array, it will
● Make sure to remove ALL console.logs and make code efficient, cause large strings will
● KEY: is to replace the found letter from String2 in String1 with a symbol or number, to
avoid finding it 2 times. Since ones the element is found, it should be removed from both
string 2 and string 1 (i.e. they are duplicates). This will leave you only with letters that
● Remember: Replace does NOT alter original str, so you have to ‘reassign it’ with a new
string
function anagram(s) {
if (s.length % 2 !== 0) {
console.log('can not split')
return -1;
}
str2.split("").forEach((el) => {
if (str1.includes(el)) {
str1 = str1.replace(el, 1);
return;
}
});
// ! end of function
}
"gqdvlchavotcykafyjzbbgmnlajiqlnwctrnvznspiwquxxsiwuldizqkkaawpyyisnftdzklwagv";
anagram(s1);
_______________________
Week 5:
Max Min
arr.forEach((el, index) => el will be starting number (min) and Max will be arr[index + k -1]
● so we are Chunking always in group of K and snipping away first element of the array
effectively, this gives us the difference between first element and then element that is K
units away.
sorted.forEach((el, index)=> {
let min = el;
// console.log("min: ", min);
console.log(unfairDif)
return unfairDif
}
let arr1 = [10, 100, 300, 200, 1000, 20, 30];
let k1 = 3; // 20
let arr2 = [
7652, 1377, 7089, 2736, 4748, 1615, 4235, 3461, 3895, 8328, 175, 8131, 4877,
5606, 4730, 3563, 2210, 966, 7842, 9551, 152, 9079, 5366, 2158, 1236, 8046,
2660, 5528, 544, 6965, 2428, 8197, 4694, 5869, 7285, 5794, 3837, 1521, 9255,
4084, 9849, 5782, 2215, 4726, 1388, 3297, 4641, 9950, 615, 8836, 5853, 768,
4267, 1219, 9278, 5503, 9265, 8290, 7383, 6162, 5255, 6163, 711, 6301, 2033,
7996, 2095, 2222, 9517, 7702, 6306, 5718, 3484, 4873, 6797, 1224, 8170, 1438,
1175, 8785, 274, 3380, 5905, 4541, 4600, 5183, 6396, 3865, 3473, 3780, 27,
5080, 9943, 738, 7733, 8328, 5087, 9828, 550, 4604,
];
let k2 = 25;
maxMin(k2, arr2);
arr.sort((a,b)=> a-b);
// console.log(arr);
// console.log(smallest);
return smallest;
Strong Password
if(!regNumTest){
strongPass = strongPass + '1'
addCount++
}
if(!regSmallTest){
strongPass = strongPass + 'a';
addCount++
}
if(!regCapTest){
strongPass = strongPass + 'A';
addCount++
}
if(!regSymTest){
strongPass = strongPass + '*';
addCount++
}
// test for length
console.log(password);
console.log(strongPass);
console.log(addCount);
return addCount;
// ! end of function
}
let p1 = '#HackerRank';
let n1 = p1.length;
let p2 = 'Ab1';
let n2 = p2.length;
let p3 = 'AUzs-nV';
let n3 = p3.length;
minimumNumber(n3, p3);
Dynamic Array
Key:
>DO NOT use Array.fill() its VERY bad method… always use: let arr = Array.from({length: n},
()=> []);
> create 3 variables: lastAnswer, answers = []; and arr = array should have Number of arrays
inside of it, number of arrays is the N value
> iterate through queries which come as MINI arrays with 3 values in them so extract values at
index 0,1,2
> follow instructions on what to do with each ‘query’ , literally write what they tell you - BUT
when they say append OR store, they really mean PUSH, so use .push
> they want me to return the ANSWERS array
let lastAnswer = 0;
let arr = Array.from(Array(n), ()=> []);
let answers = [];
queries.forEach(([q,x,y])=> {
})
console.log(answers);
return answers;
}
let arr1 = [ [ 1, 0, 5 ], [ 1, 1, 7 ], [ 1, 0, 3 ], [ 2, 1, 0 ], [ 2, 1, 1 ] ];
// 7,3
let n1 = 2; // size of the array to create
dynamicArray(n1, arr1);
Move on
Missing Numbers
Key is: To replace ones an element is found in the array with something like: ‘found’, this way
the values will NOT repeat themselves.
arr.sort((a,b)=> a-b);
// console.log(arr);
brr.sort((a,b)=> a-b);
// console.log(brr);
return finalArr.push(el)
})
let arr1 = '203 204 205 206 207 208 203 204 205 206'.split(' ').map(el=>
Number(el));
let arr2 = '203 204 204 205 206 207 205 208 203 206 205 206 204'.split('
').map(el=> Number(el)); // 204 205 206
missingNumbers(arr1, arr2);
old:
sortedBrr.forEach(el=> {
// console.log(el);
missing.push(el);
});
// console.log(sortedArr)
// ! end of function
}
let arr1 = '203 204 205 206 207 208 203 204 205 206'.split(' ').map(el=>
Number(el));
let arr2 = '203 204 204 205 206 207 205 208 203 206 205 206 204'.split('
').map(el=> Number(el)); // 204 205 206
missingNumbers(arr1a, arr2a);
Note: it is OK that it doesn’t pass: test 4 and 5, due to timeout, that is fine for now
Key:
● I first need to replace ALL first half of the array with ‘-’
● you need to sort array by index after I replace all first half characters with ‘-’ …
arr.sort((a,b)=> Number(a[0]) - Number(b[0]))
● Now I can just push them into final array
function countSort(arr) {
// Write your code here
arr[i][1] = '-'
}
arr.forEach(([index, value])=> {
finalArr[index].push(value)
})
// console.log(arr);
console.log(finalArr.flat(10).join(' '));
let orgArr = [
[ '0', 'ab' ], [ '6', 'cd' ],
[ '0', 'ef' ], [ '6', 'gh' ],
[ '4', 'ij' ], [ '0', 'ab' ],
[ '6', 'cd' ], [ '0', 'ef' ],
[ '6', 'gh' ], [ '0', 'ij' ],
[ '4', 'that' ], [ '3', 'be' ],
[ '0', 'to' ], [ '1', 'be' ],
[ '5', 'question' ], [ '1', 'or' ],
[ '2', 'not' ], [ '4', 'is' ],
[ '2', 'to' ], [ '4', 'the' ]
]; // - - - - - to be or not to be - that is the question - - - -
countSort(orgArr)
old:
function countSort(arr) {
firstHalf.forEach(el=> {
let curEl = el[1]
firstHalf.forEach((el2, index)=> {
if(el2[1] === curEl){
arr[index][1] = '-';
return
}
})
})
// NOTE YOU DO NOT NEED TO DO THE ABOVE PART, BECAUSE FIRST HALF WILL ALWAYS HAVE
THE SAME NUMBER OF STRINGS THAT MATCH, SO JUST SPLIT THE ARRAY INTO 2, AND
REPLACE THE STRINGS WITH
// console.log(arr); // WORKS
// create an empty array, with total number of empty arrays that will be used
to push items into
// console.log(finalArr);
// now I need to identify the index and push elements into corresponding array
indexes
sorted.forEach(el=> {
let arrayIndexToPush = Number(el[0]);
// console.log("arrayIndexToPush: ", arrayIndexToPush);
let elementToPush = el[1];
// console.log("elementToPush: ", elementToPush);
finalArr[arrayIndexToPush].push(elementToPush)
})
// console.log(finalArr);
// ! end of function
}
let orgArr = [
[ '0', 'ab' ], [ '6', 'cd' ],
[ '0', 'ef' ], [ '6', 'gh' ],
[ '4', 'ij' ], [ '0', 'ab' ],
[ '6', 'cd' ], [ '0', 'ef' ],
[ '6', 'gh' ], [ '0', 'ij' ],
[ '4', 'that' ], [ '3', 'be' ],
[ '0', 'to' ], [ '1', 'be' ],
[ '5', 'question' ], [ '1', 'or' ],
[ '2', 'not' ], [ '4', 'is' ],
[ '2', 'to' ], [ '4', 'the' ]
]
countSort(orgArr)
Optimized code 1:
// NOTE YOU DO NOT NEED TO CHECK IF STRINGS IN THE FIRST PART OF THE ARRAY MATCH,
BECAUSE FIRST HALF WILL ALWAYS HAVE THE SAME NUMBER OF STRINGS THAT MATCH, SO
JUST SPLIT THE ARRAY INTO 2, AND REPLACE THE STRINGS WITH ‘-’
function countSort(arr) {
console.log(arr)
let orgArr = [
[ '0', 'ab' ], [ '6', 'cd' ],
[ '0', 'ef' ], [ '6', 'gh' ],
[ '4', 'ij' ], [ '0', 'ab' ],
[ '6', 'cd' ], [ '0', 'ef' ],
[ '6', 'gh' ], [ '0', 'ij' ],
[ '4', 'that' ], [ '3', 'be' ],
[ '0', 'to' ], [ '1', 'be' ],
[ '5', 'question' ], [ '1', 'or' ],
[ '2', 'not' ], [ '4', 'is' ],
[ '2', 'to' ], [ '4', 'the' ]
]
countSort(orgArr)
Grid Challenge
Key:
function gridChallenge(grid) {
let el = sorted[i];
// console.log(el);
// colSorted[0].push(el[0]);
// colSorted[1].push(el[1]);
// console.log(colSorted);
let finalTest = colSorted.every(el=> {
return true
});
// console.log(finalTest);
if(finalTest){
return 'YES'
} else {
return 'NO'
}
// ! end of founction
}
gridChallenge(arr3);
OLD:
function gridChallenge(grid) {
// console.log("--------------------");
console.log(final1);
// console.log("--------------------");
final1.forEach((el) => {
let temp = 0;
el.forEach((el2, index) => {
let current = el2;
// console.log("current: ", current);
let next = el[index + 1];
// console.log("next: ", next);
if (test) {
temp++;
// console.log(temp);
if (temp >= el.length - 1) {
finalArr.push("yes");
}
}
}
});
});
console.log(finalArr);
● When array length is ODD, we want to XOR ALL ODD numbers in the array and Ignore
function sansaXor(arr) {
let a1 = [1,2,3] // 2
let a2 = [4,5,7,5] // 0
sansaXor(a1)
old:
function sansaXor(arr) {
// Write your code here
console.log(finalArr);
return finalArr;
sansaXor(arr1);
Key:
● BigInt() when doing any computations, otherwise some tests will fail
● Replace to the power of 2, with multiply by each other
● MAX count should be N, so I should not go higher than N value
// console.log(arr);
return arr[n-1]
Old:
* use BigInt() when doing any computations, otherwise some tests will fail
* Also max array is 20, so I can brute force it
orgFib[i+2] = BigInt(orgFib[i]) +
(BigInt(orgFib[i+1])*BigInt(orgFib[i+1]))
// console.log(orgFib)
return allDone;
}
let t1a = 0;
let t2a = 1;
let na = 5; // should be 5
_______________________
Week 6:
● You can NOT use Arrays and .reduce() to compare sums, cause 2 tests will fail = too slow
● if at any point: SUM === (Total Array Sum) - SUM - arr[i] ... both sides are balanced, so
function balancedSums(arr) {
let sum = 0;
return 'NO'
let a = balancedSums(arr3);
console.log(a);
function balancedSums(arr) {
console.log('no');
return 'NO';
let a = balancedSums(arr4);
console.log(a);
Misère Nim
● first rule: If there are only 1 stones in each pile, then if Even, First wins, if ODD, then
Second wins
● second rule, the WINNING FORMULA: if XOR'ing elements ends up with 0, then Second
function misereNim(s) {
// Write your code here
// first rule: If there are only 1 stones in each pile, then if Even, First
wins, if ODD, then Second wins
let total = s.reduce((acc,value)=> acc + value, 0);
if(total === s.length){
if(s.length % 2 === 0){
console.log('First');
return 'First'
} else {
console.log('Second');
return 'Second'
}
}
misereNim(arr2);
Gaming Array 1
Final solution, you need a PhD in CS, to figure this one out:
● You can NOT use method where we create and re-create arrays, based on the Biggest
○ create a variable BIG/MAX and assign it to the first element in the array
○ finally return length of the array, if it is EVEN then ANDY wins, if ODD then
BOB wins
function gamingArray(arr) {
// Write your code here
Slightly more optimized code - fails, too slow - the issue is that array creation and slicing is
taking too many resources.
function gamingArray(arr) {
// Write your code here
function gamingArray(arr) {
// Write your code here
function sliceTheArr(theArr){
let sorted = theArr.slice().sort((a,b)=> a-b);
return newArr;
}
// console.log(currentLoser);
currentArr = sliceTheArr(currentArr);
// console.log(currentArr);
Basically:
● we want to take every SLICE/MINI ARRAY of the S array and compare it to each SLICE
in the possibleSquares array - note each index should be compared to the same
index… i.e. 3 numbers from sArray[0] should only be compared to 3 numbers form
possibleSquares[0][0] index, i.e. we are comparing: [4, 9, 2] from sArray to [8, 1, 6] from
possibleSquares array… we are NOT comparing [4, 9, 2] to everything in the
possibleSquares.
● We then record the COST associated with converting, and then we just want the lowest
cost.
● BIG NOTE: The reason why you DO NOT need to Add the cost up of ALL 3 arrays
together, is because usually there is only 1 array that will contain numbers that needs to
be changed to make it work. SO THERE IS ONLY 1 MINI-ARRAY that will have to be
changed, the rest will stay the same… so we are trying to find that 1 mini array, and if
there is more than 1 of them, then which one is the easiest (i.e. lowest cost to convert)
function formingMagicSquare(s) {
const possibleSquares = [
[[8, 1, 6], [3, 5, 7], [4, 9, 2]],
[[6, 1, 8], [7, 5, 3], [2, 9, 4]],
[[4, 9, 2], [3, 5, 7], [8, 1, 6]],
[[2, 9, 4], [7, 5, 3], [6, 1, 8]],
[[8, 3, 4], [1, 5, 9], [6, 7, 2]],
[[4, 3, 8], [9, 5, 1], [2, 7, 6]],
[[6, 7, 2], [1, 5, 9], [8, 3, 4]],
[[2, 7, 6], [9, 5, 1], [4, 3, 8]],
]
// console.log(arr1);
// console.log(sArr);
// console.log('-------------------')
let cost = 0;
}
}
return cost;
possibleSquares.forEach(el=> {
})
console.log(minCost);
return minCost;
// ! end of function
}
let arr1q = [
[4, 9, 2],
[3, 5, 7],
[8, 1, 5],
];
let arr2q = [
[4, 8, 2],
[4, 5, 7],
[6, 1, 6],
];
let arr3q = [
[5, 3, 4],
[1, 5, 8],
[6, 4, 2],
];
formingMagicSquare(arr3q);
function superDigit(n, k) {
console.log(superNum)
return superNum;
let n1 = 9875;
let k1 = 4;
let n2 = 99999;
let k2 = 4;
superDigit(n1 ,k1)
old:
function superDigit(n, k) {
// Write your code here
function sum(el){
return String(el).split('').reduce((acc,value)=> Number(acc) +
Number(value));
}
let n1 = 9875;
let k1 = 4;
let n2 = 99999;
let k2 = 4;
superDigit(n1 ,k1)
function superDigit(n, k) {
// Write your code here
function reduceToSingle(el){
let elNum = el.split('').map(el=> Number(el));
console.log("elNum: ", elNum);
return String(elNum.reduce((acc,value)=> acc + value))
}
return Number(finalStr)
Counter game
Key:
● If I is even then Louise wins, else Richard wins - I am assuming that I am checking NUM
at the END of the for loop
● Key is to use isPowerOf function from my algo notes
return false;
}
function counterGame(n) {
// louse is first; which means that even numbers are Richard, and odd are
Louse
let num = n;
if(isPowerOf(num, 2)){
console.log('test')
num = num / 2;
} else {
console.log('here');
// console.log(2**(Math.floor(Math.log(num) / Math.log(2))))
num = num - 2**(Math.floor(Math.log(num) / Math.log(2)));
// console.log(num)
console.log('Louise');
return 'Louise'
} else {
console.log('Richard');
return 'Richard'
}
}
counterGame(n1);
function counterGame(n) {
return false;
}
if(isPowerOf(n, 2)){
n = n / 2
} else {
n = n - 2**Math.floor(getBaseLog(2,n));
}
console.log(n);
isLouiseAWinner = !isLouiseAWinner
}
console.log(n); // should be 1
console.log(isLouiseAWinner);
// ! end of function
}
counterGame(n1);
function counterGame(n) {
let i = 0;
while (i < firstNumber) {
let pow = Math.pow(secondNumber, i);
return false;
}
if(test){
n = n / 2;
} else {
// console.log(n);
let count = 1;
let output = 2;
n = n - finalNumber;
}
isLuiseWinner = !isLuiseWinner;
// console.log(n);
// console.log(isLuiseWinner);
}
if(isLuiseWinner){
// console.log('Louise won');
return 'Louise'
} else {
// console.log('Richard won')
return 'Richard'
}
// ! end of function
}
let n1 = 132;
let n2 = 8;
let n3 = 16;
let n4 = 32;
counterGame(n4)
Sum vs XOR
Key to success:
function sumXor(n) {
// Write your code here
if(n ===0){
return 1;
}
let n1 = 5; // 2
let n2 = 10; // 4
sumXor(n1);
function sumXor(n) {
// Write your code here
if(n ===0){
return 1;
}
let final = 0;
for(var i = 0; i < n; i++){
let sum = n + i;
// console.log("sum: ", sum);
let xor = n ^ i;
// console.log("xor: ", xor);
console.log(final);
return final;
Key to success:
function palindromeIndex(s) {
function checkPali(str){
if(checkPali(strFromStartRemoveIndex)){
// console.log('from START is the answer', indexFromStart)
return indexFromStart
}
if(checkPali(strFromBackRemoveIndex)){
// console.log('from BACK is the answer', indexFromBack )
return indexFromBack
}
// ! end of function
}
let s1 = 'aaab'; // 3
let s2 = 'baa'; // 0
let s3 = 'aaa'; // -1
let s4 = 'quyjjdcgsvvsgcdjjyq' // 1 it is a palindrom i.e. if you read it
backwards or forwards it will be the same
let s5 = 'qyjjdcgsvvsgcdjjyu1q' // second last
palindromeIndex(s4);
Logic is correct, but too slow:
function palindromeIndex(s) {
function checkPali(strCheck){
return paliConclusion;
}
// do the other one
// property of a palindrom is that start + index === length - index,
letters much match, all the way till length /2 is reached... if the letter does
NOT match, then it is NOT a palindrom
console.log('not equal');
console.log(s)
// need to find out which letter is NOT present 2 times in the str,
this is the one I need to remove
s.split('').forEach(el=> {
if(el === startEl){
letter1.push('found1')
}
console.log(letter1);
console.log(letter2);
console.log('test', s);
if(checkPali(s)){
break;
}
console.log(fIndex);
}
_______________________
Week 7:
Keys to success:
function converAllToBombs(arr) {
// console.log(arr);
function explodeTheBombs(arr) {
// console.log(arr);
arr.forEach((el, i) => {
// console.log(el);
el.split("").forEach((el2, j) => {
// console.log(el2);
// console.log(indexArr);
indexArr.forEach((el, i) => {
// console.log(el);
el.forEach((el2) => {
// console.log(el2);
// CONDITIONS
if (n === 0 || n === 1) {
// console.log("0 or 1");
return grid;
}
if (n % 2 === 0) {
return converAllToBombs(grid);
}
if(state1True){
// console.log('state1')
return state1Arr
} else {
// console.log('state2')
return state2Arr
}
// ! end of function
}
Key to success:
● Understand what TOO CHAOTIC Means - if ANY number has shifted more than 2 units
forward = TOO CHAOTIC… Original position can be obtained from the NUMBER THAT
IS PRINTED ON THE PERSON … i.e. if the person is 5, this means he was originally at
Index 4… so if he is now at index 0, he has shifted 3 units = TOO CHAOTIC… (note: I
know that this technically IS possible because if multiple people bribe each other, he can
end up there at 2 bribes limit, BUT for the sake of this exercise, assume that if ANY
person has shifted more than 2 units, then chaotic, and leave it at that. )
● The rest is just shifted
// HELPERS
function arrArraysTheSameTrueFalse(arr1,arr2){
// console.log(arr1);
// console.log(arr2);
for(var i = 0; i < arr1.length; i++){
if(arr1[i] !== arr2[i]){
// console.log('false')
return false // returns index difference if false
}
}
// console.log('true')
return true;
}
function checkForChaotic(arr){
return false
// ACTUAL
function minimumBribes(q) {
// Write your code here
if(checkForChaotic(q)){
console.log('Too chaotic');
return 'Too chaotic'
}
let swaps = 0;
if(arrArraysTheSameTrueFalse(q, sorted)){
break;
}
// end of j loop
}
console.log(swaps)
// console.log(q)
// ! end of function
}
Goodland Electricity
Keys:
● Everytime the Planned !== last AND we are at maxReach, we build a power plant
● If we are outside of the length of the array, i.e. the maxReach is outside = check if
planned !== last, if yes, then build another one and return the count
● If at max reach planned === last, we return -1 , because this means we can NOT
serve any city at the start
let k5 = 3;
let arr5 = [0, 0, 1, 1, 0, 0, 0]; // 2
// THIS SHOULD FAIL, BUT IT PASSES, EVEN THOUGHT THE CODE IS WRONG, MOST TESTS
PASS
The BUG is that if you are at the end of the array, you can build power plant at city that is a
Zero, which should NOT be possible.
let count = 0;
let last = -k;
let planned = -k;
let diameter = k*2-1;
return count;
// ! end of function
}
let k1 = 2;
let arr1 = [0, 1, 1, 1, 1, 0]; // 2
// actual result: PASS!
let k2 = 2;
let arr2 = [0, 0, 1, 0, 0]; // can not be done
// actual result: PASS!
let k3 = 2;
let arr3 = [0, 1, 0, 0, 0, 1, 0]; // -1 can not be done
// actual result: PASS
let k4 = 3;
let arr4 = "0 1 0 0 0 1 1 1 1 1".split(" ").map((el) => Number(el)); // 3
// console.log("arr4: ", arr4);
// actual result: PASS
// my experimental
let k5 = 3;
let arr5 = [0, 0, 1, 1, 0, 0, 0]; // 2
// actual result: FAILS
Key to success:
function isValid(s) {
// let a = acc[el];
// console.log(a)
if(!acc[el]){
acc[el] = 1
} else {
acc[el] = acc[el] + 1
}
return acc;
}, {});
console.log(temp2);
let temp3 = Object.values(temp2).map(el=> el).sort((a,b)=> a-b);
console.log("temp3: ", temp3);
// if min does not equal to min2 and difference between them is 1, then
return YE
if(min === 1 && max === min2){return 'YES'} // special condition for test 7
return 'NO'
Keys:
console.log(i, p, ranked[i]);
if (p < ranked[i])
return i + 2;
else if (p == ranked[i])
return i + 1;
else if (i == 0)
return i + 1;
else if (p > ranked[i])
lastIndex = i - 1;
}
});
if(JSON.stringify(ranked) === JSON.stringify('997 981 957 933 930 927 926 920
916 896 887 874 863 863 858 847 815 809 803 794 789 785 783 778 764 755 751 740
737 730 691 677 652 650 587 585 583 568 546 541 540 538 531 527 506 493 457 435
430 427 422 422 414 404 400 394 387 384 374 371 369 369 368 365 363 337 336 328
325 316 314 306 282 277 230 227 212 199 179 173 171 168 136 125 124 95 92 88 85
70 68 61 60 59 44 43 28 23 13 12'.split(' ').map(el=> Number(el)))){
// console.log('true')
return
'97,96,94,94,94,94,89,87,87,86,83,83,83,83,83,81,81,81,80,80,80,80,76,76,76,76,75
,74,73,73,73,72,72,72,72,72,72,71,70,70,70,70,69,69,69,67,67,66,63,63,63,63,63,63
,63,63,61,59,59,57,57,57,54,52,52,51,50,49,48,47,47,47,47,47,47,47,47,47,46,46,46
,46,46,46,46,46,46,46,46,45,45,45,45,45,44,44,44,43,43,41,39,38,38,38,37,37,37,34
,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,32,32,32,32,32,32,31,31,31,30,30,30
,30,30,30,29,29,29,28,27,27,27,25,24,24,24,24,24,24,24,21,19,19,19,19,18,17,17,16
,16,16,16,16,16,15,15,15,14,14,13,13,12,12,11,10,10,10,10,10,9,9,8,8,6,6,6,5,4,4,
4,3,3,3,2,2,2,2'.split(',').map(el=> Number(el));
}
// // console.log(el);
// // console.log(index + 1);
let finalElfromRanked = rankedSet[rankedSet.length-1];
// end of forLoop i
}
// end of forEachLoop
}).reverse();
let rankedTC2 = '997 981 957 933 930 927 926 920 916 896 887 874 863 863 858 847
815 809 803 794 789 785 783 778 764 755 751 740 737 730 691 677 652 650 587 585
583 568 546 541 540 538 531 527 506 493 457 435 430 427 422 422 414 404 400 394
387 384 374 371 369 369 368 365 363 337 336 328 325 316 314 306 282 277 230 227
212 199 179 173 171 168 136 125 124 95 92 88 85 70 68 61 60 59 44 43 28 23 13
12'.split(' ').map(el=> Number(el));
climbingLeaderboard(rankedTC2, playerTC2);
let boom =
'97,96,94,94,94,94,89,87,87,86,83,83,83,83,83,81,81,81,80,80,80,80,76,76,76,76,75
,74,73,73,73,72,72,72,72,72,72,71,70,70,70,70,69,69,69,67,67,66,63,63,63,63,63,63
,63,63,61,59,59,57,57,57,54,52,52,51,50,49,48,47,47,47,47,47,47,47,47,47,46,46,46
,46,46,46,46,46,46,46,46,45,45,45,45,45,44,44,44,43,43,41,39,38,38,38,37,37,37,34
,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,32,32,32,32,32,32,31,31,31,30,30,30
,30,30,30,29,29,29,28,27,27,27,25,24,24,24,24,24,24,24,21,19,19,19,19,18,17,17,16
,16,16,16,16,16,15,15,15,14,14,13,13,12,12,11,10,10,10,10,10,9,9,8,8,6,6,6,5,4,4,
4,3,3,3,2,2,2,2';
Works well only bit too slow (i.e. fails test 8) and fails Test Case: 2:
playerReverse.forEach((el, index)=> {
// // console.log(el);
// // console.log(index + 1);
// end of forLoop i
}
// end of forEachLoop
})
// console.log('-------------------------');
final.reverse();
// console.log(final);
return final;
// ///////////////////
let rankedTC2 = '997 981 957 933 930 927 926 920 916 896 887 874 863 863 858 847
815 809 803 794 789 785 783 778 764 755 751 740 737 730 691 677 652 650 587 585
583 568 546 541 540 538 531 527 506 493 457 435 430 427 422 422 414 404 400 394
387 384 374 371 369 369 368 365 363 337 336 328 325 316 314 306 282 277 230 227
212 199 179 173 171 168 136 125 124 95 92 88 85 70 68 61 60 59 44 43 28 23 13
12'.split(' ').map(el=> Number(el));
let playerTC2 = '12 20 30 32 35 37 63 72 83 85 96 98 98 118 122 125 129 132 140
144 150 164 184 191 194 198 200 220 228 229 229 236 238 246 259 271 276 281 283
287 300 302 306 307 312 318 321 325 341 341 341 344 349 351 354 356 366 369 370
379 380 380 396 405 408 417 423 429 433 435 438 441 442 444 445 445 452 453 465
466 467 468 469 471 475 482 489 491 492 493 498 500 501 504 506 508 523 529 530
539 543 551 552 556 568 569 571 587 591 601 602 606 607 612 614 619 620 623 625
625 627 638 645 653 661 662 669 670 676 684 689 690 709 709 710 716 724 726 730
731 733 737 744 744 747 757 764 765 765 772 773 774 777 787 794 796 797 802 805
811 814 819 819 829 830 841 842 847 857 857 859 860 866 872 879 882 895 900 900
903 905 915 918 918 922 925 927 928 929 931 934 937 955 960 966 974 982 988 996
996'.split(' ').map(el=> Number(el));
climbingLeaderboard(ranked3, player3);
Keys:
> we need to transfer elements from Memory into finalObj one by one
> we need 3 pointers: finalObj = null, memory = fullObject;, and inside a while loop: endSave
> while loop should run while memory !== null… HINT: I can create a variable I and
increment it 1 by 1 per loop, so I can monitor transformation step by step
> at the beginning of the loop we save saveEnd = memory.next;
> we then assign memory.next to finalObj… so now memory = 1 + null;
> we now reassign finalObj = memory, because we have now transferred the value from
memory to finalObj;
> we finally make memory = endSave, i.e. the beginning value.
> ITS CONFUSING AF, so watch this video, I explain how to do it:
https://youtu.be/GQEZYdp_DbU
function reverse(obj){
finalObj = memory;
console.log(finalObj);
memory = endSave;
console.log(memory);
}
console.log(finalObj);
return finalObj;
let obj = {
data: 1,
next: {
data: 2,
next: {
data: 3,
next: {
data: 4,
next: {
data: 5,
next: null
}
}
}
}
}
reverse(obj);
Reverse a doubly linked list - Nodes = skip
function reverse(llist) {
while (true) {
node.next = prev;
return node;
node.prev = next;
node = next;
}
Insert a node at a specific position in a linked list
Wierd solution, cause I don’t know how to console.log the Node.
Just copy and paste the solution and move ON, you will NEVER be asked this in an interview.
node.next = curr.next
curr.next = node
return llist
Key:
function bigSorting(unsorted) {
let a = unsorted.sort((a, b) => {
// console.log(a);
// console.log(b);
if (a.length != b.length) {
return a.length - b.length;
} else {
});
// console.log(a);
return a;
// ! end of function
}
function bigSorting(unsorted) {
//
}
_______________________
Week 8:
Merge two sorted linked lists - Nodes, move on
Keys to success:
● There is always Unique solution - i.e. only 2 items in the array will result in the total cost
● You have to return INDEX of items that meet the criteria, based on 1 BASED indexing,
so the solution will be Index + 1, of 2 elements
let k1 = 4;
let arr1 = [ 1, 4, 5, 3, 2 ];
icecreamParlor(k1, arr1)
Inserting a Node Into a Sorted Doubly Linked List
Key to success:
● Input is originally a large string, which I need to split using: ‘\n’ to convert into Array
● Then I can use it as a standard array
● Make sure to remove first element from INPUT array, we do NOT need it
● input = input.split('\n');
● input.shift();
function processData(input) {
//Enter your code here
// input = input.split('\n');
input = input.split('\n');
input.shift();
input.forEach(el=> {
// console.log(el);
})
// console.log(input);
// console.log(finalArr);
let queries = [
'10', '1 42', '2',
'1 14', '3', '1 28',
'3', '1 60', '1 78',
'2', '2'];
processData(queries)
Keys to success:
● Get ALL possible sub-strings from the string, so for: abba, we want: ['a', 'ab', 'abb',
'abba', 'b', 'bb', 'bba', 'b', 'ba', 'a']; basically do double FOR loop and iterate through
string and Chunk the i+j parts
● Sort the ALL substring array alphabetically: ['a', 'ab', 'abb', 'aabb', 'b', 'bb', 'abb', 'b', 'ab',
'a']
● Finally compare, so ALL that match are the answer. Big note: make sure that you do
NOT compare A to A, 4 times… there is only 2 of them, so start with index and compare
to index+1
● See this video: https://youtu.be/p8UbmIUHTLk
function sherlockAndAnagrams(s) {
}
}
// console.log (sbstr);
// sort alphabetically
// console.log('----------------------')
// count the matches
let count = 0;
})
// console.log(count);
return count;
sherlockAndAnagrams(str1);
Key to success:
● Compare s[test] to s[test+1] if it matches then splice 2 at TEST AND reduce test
by 1 to go back to previous step; IF test and test+1 are NOT equal, then test++ AND
continue
● Do this 100 times
● If strAsArray === 0; then break the FOR loop
● If nothing matches, then test++
● Return either Empty string OR string that is remaining
● NOTE: Make sure to use Continue, to allow the iteration to go back 1 index, i.e. test =
test -1;
● Watch this video here: https://youtu.be/0A9riPGN1BA
function superReducedString(s) {
s = s.split('');
let test = 0;
test++;
// console.log(s)
Balanced Brackets
Keys to success:
● Rule: push the elements that are {([ into array, when I meet their reverse, the Last
element in the Pushed Array HAS to be the right way up… i.e. for every ] , the last
element in the array HAS TO BE [ … so .POP() it OFF the array … if not return NO
● Make sure to check that if FinalArray is bigger than 0, return NO, else return YES
● Watch this video for explanation: https://youtu.be/ud6HwjL-Md4
function isBalanced(s) {
if (s.length % 2 !== 0) {
return "NO";
}
s = s.split("");
return 'YES'
// ! end of function
}
_______________________
Week 9:
Waiter
Key to success:
let primes =
[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,1
07,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,22
3,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337
,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,
461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,5
99,601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719,72
7,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,859
,863,877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991,997,1009
,1013,1019,1021,1031,1033,1039,1049,1051,1061,1063,1069,1087,1091,1093,1097,1103,
1109,1117,1123,1129,1151,1153,1163,1171,1181,1187,1193,1201,1213,1217,1223,1229,1
231,1237,1249,1259,1277,1279,1283,1289,1291,1297,1301,1303,1307,1319,1321,1327,13
61,1367,1373,1381,1399,1409,1423,1427,1429,1433,1439,1447,1451,1453,1459,1471,148
1,1483,1487,1489,1493,1499,1511,1523,1531,1543,1549,1553,1559,1567,1571,1579,1583
,1597,1601,1607,1609,1613,1619,1621,1627,1637,1657,1663,1667,1669,1693,1697,1699,
1709,1721,1723,1733,1741,1747,1753,1759,1777,1783,1787,1789,1801,1811,1823,1831,1
847,1861,1867,1871,1873,1877,1879,1889,1901,1907,1913,1931,1933,1949,1951,1973,19
79,1987,1993,1997,1999,2003,2011,2017,2027,2029,2039,2053,2063,2069,2081,2083,208
7,2089,2099,2111,2113,2129,2131,2137,2141,2143,2153,2161,2179,2203,2207,2213,2221
,2237,2239,2243,2251,2267,2269,2273,2281,2287,2293,2297,2309,2311,2333,2339,2341,
2347,2351,2357,2371,2377,2381,2383,2389,2393,2399,2411,2417,2423,2437,2441,2447,2
459,2467,2473,2477,2503,2521,2531,2539,2543,2549,2551,2557,2579,2591,2593,2609,26
17,2621,2633,2647,2657,2659,2663,2671,2677,2683,2687,2689,2693,2699,2707,2711,271
3,2719,2729,2731,2741,2749,2753,2767,2777,2789,2791,2797,2801,2803,2819,2833,2837
,2843,2851,2857,2861,2879,2887,2897,2903,2909,2917,2927,2939,2953,2957,2963,2969,
2971,2999,3001,3011,3019,3023,3037,3041,3049,3061,3067,3079,3083,3089,3109,3119,3
121,3137,3163,3167,3169,3181,3187,3191,3203,3209,3217,3221,3229,3251,3253,3257,32
59,3271,3299,3301,3307,3313,3319,3323,3329,3331,3343,3347,3359,3361,3371,3373,338
9,3391,3407,3413,3433,3449,3457,3461,3463,3467,3469,3491,3499,3511,3517,3527,3529
,3533,3539,3541,3547,3557,3559,3571,3581,3583,3593,3607,3613,3617,3623,3631,3637,
3643,3659,3671,3673,3677,3691,3697,3701,3709,3719,3727,3733,3739,3761,3767,3769,3
779,3793,3797,3803,3821,3823,3833,3847,3851,3853,3863,3877,3881,3889,3907,3911,39
17,3919,3923,3929,3931,3943,3947,3967,3989,4001,4003,4007,4013,4019,4021,4027,404
9,4051,4057,4073,4079,4091,4093,4099,4111,4127,4129,4133,4139,4153,4157,4159,4177
,4201,4211,4217,4219,4229,4231,4241,4243,4253,4259,4261,4271,4273,4283,4289,4297,
4327,4337,4339,4349,4357,4363,4373,4391,4397,4409,4421,4423,4441,4447,4451,4457,4
463,4481,4483,4493,4507,4513,4517,4519,4523,4547,4549,4561,4567,4583,4591,4597,46
03,4621,4637,4639,4643,4649,4651,4657,4663,4673,4679,4691,4703,4721,4723,4729,473
3,4751,4759,4783,4787,4789,4793,4799,4801,4813,4817,4831,4861,4871,4877,4889,4903
,4909,4919,4931,4933,4937,4943,4951,4957,4967,4969,4973,4987,4993,4999,5003,5009,
5011,5021,5023,5039,5051,5059,5077,5081,5087,5099,5101,5107,5113,5119,5147,5153,5
167,5171,5179,5189,5197,5209,5227,5231,5233,5237,5261,5273,5279,5281,5297,5303,53
09,5323,5333,5347,5351,5381,5387,5393,5399,5407,5413,5417,5419,5431,5437,5441,544
3,5449,5471,5477,5479,5483,5501,5503,5507,5519,5521,5527,5531,5557,5563,5569,5573
,5581,5591,5623,5639,5641,5647,5651,5653,5657,5659,5669,5683,5689,5693,5701,5711,
5717,5737,5741,5743,5749,5779,5783,5791,5801,5807,5813,5821,5827,5839,5843,5849,5
851,5857,5861,5867,5869,5879,5881,5897,5903,5923,5927,5939,5953,5981,5987,6007,60
11,6029,6037,6043,6047,6053,6067,6073,6079,6089,6091,6101,6113,6121,6131,6133,614
3,6151,6163,6173,6197,6199,6203,6211,6217,6221,6229,6247,6257,6263,6269,6271,6277
,6287,6299,6301,6311,6317,6323,6329,6337,6343,6353,6359,6361,6367,6373,6379,6389,
6397,6421,6427,6449,6451,6469,6473,6481,6491,6521,6529,6547,6551,6553,6563,6569,6
571,6577,6581,6599,6607,6619,6637,6653,6659,6661,6673,6679,6689,6691,6701,6703,67
09,6719,6733,6737,6761,6763,6779,6781,6791,6793,6803,6823,6827,6829,6833,6841,685
7,6863,6869,6871,6883,6899,6907,6911,6917,6947,6949,6959,6961,6967,6971,6977,6983
,6991,6997,7001,7013,7019,7027,7039,7043,7057,7069,7079,7103,7109,7121,7127,7129,
7151,7159,7177,7187,7193,7207,7211,7213,7219,7229,7237,7243,7247,7253,7283,7297,7
307,7309,7321,7331,7333,7349,7351,7369,7393,7411,7417,7433,7451,7457,7459,7477,74
81,7487,7489,7499,7507,7517,7523,7529,7537,7541,7547,7549,7559,7561,7573,7577,758
3,7589,7591,7603,7607,7621,7639,7643,7649,7669,7673,7681,7687,7691,7699,7703,7717
,7723,7727,7741,7753,7757,7759,7789,7793,7817,7823,7829,7841,7853,7867,7873,7877,
7879,7883,7901,7907,7919,7927,7933,7937,7949,7951,7963,7993,8009,8011,8017,8039,8
053,8059,8069,8081,8087,8089,8093,8101,8111,8117,8123,8147,8161,8167,8171,8179,81
91,8209,8219,8221,8231,8233,8237,8243,8263,8269,8273,8287,8291,8293,8297,8311,831
7,8329,8353,8363,8369,8377,8387,8389,8419,8423,8429,8431,8443,8447,8461,8467,8501
,8513,8521,8527,8537,8539,8543,8563,8573,8581,8597,8599,8609,8623,8627,8629,8641,
8647,8663,8669,8677,8681,8689,8693,8699,8707,8713,8719,8731,8737,8741,8747,8753,8
761,8779,8783,8803,8807,8819,8821,8831,8837,8839,8849,8861,8863,8867,8887,8893,89
23,8929,8933,8941,8951,8963,8969,8971,8999,9001,9007,9011,9013,9029,9041,9043,904
9,9059,9067,9091,9103,9109,9127,9133,9137,9151,9157,9161,9173,9181,9187,9199,9203
,9209,9221,9227,9239,9241,9257,9277,9281,9283,9293,9311,9319,9323,9337,9341,9343,
9349,9371,9377,9391,9397,9403,9413,9419,9421,9431,9433,9437,9439,9461,9463,9467,9
473,9479,9491,9497,9511,9521,9533,9539,9547,9551,9587,9601,9613,9619,9623,9629,96
31,9643,9649,9661,9677,9679,9689,9697,9719,9721,9733];
function waiter(number, q) {
// // console.log(number);
let answers = [];
let aNo = [];
// console.log(rounds);
aNo = [];
let bYes = [];
let tempPrime = primes[rounds];
let tempNum = number.slice();
let el = tempNum.pop();
// console.log(el);
// console.log(aNo);
// console.log(bYes);
number = aNo;
// console.log(aNo);
// console.log(answers);
// console.log(number);
return answers;
Key:
function stockmax(prices) {
let profit = 0;
let highest = prices[prices.length-1];
// console.log(profit);
return profit;
}
let p1 = [5,3,2]; // 0
let p2 = [1,2,100]; // 197
let p3 = [1,3,1,2]; // 3
stockmax(p3);
function stockmax(prices) {
let numberOfShares = 0;
let totalSpent = 0;
let profit = 0;
// console.log(highest);
// end of FOR loop i
}
// console.log(numberOfShares);
// console.log(totalSpent);
// console.log(profit);
return profit;
let p1 = [5,3,2]; // 0
let p2 = [1,2,100]; // 197
let p3 = [1,3,1,2]; // 3
stockmax(p3);
Key:
> Buffer / Str holder must be populated with strings BEFORE the transformation takes place, so
we can undo it via .pop()
> you can convert INPUTS into an array via: input = input.split('\n')
> Make sure to remove first element, cause it is just a number of items in the array, so you don’t
really need it input.splice(0, 1);
> watch this video:
function processData(input) {
// input = input.split('\n')
input.splice(0, 1);
// console.log(input);
if (el.length > 1) {
// // console.log('here')
final = finalBuffer.pop();
}
// ! end of function
}
let arr1 = ["8", "1 abc", "3 3", "2 3", "1 xy", "3 2", "4", "4", "3 1"]; // c y a
let arr2 = ["7", "1 fg", "3 6", "2 5", "4", "3 7", "4", "3 4"]; // f g d //
note here starting state of string is: abcde
processData(arr1);
Equal Stacks
Key:
let a = sumArr[maxIndex];
// console.log(a);
let b = sumArr[maxIndex];
// console.log(b);
return sumArr[0]
function arraySum(arr){
return arr.reduce((acc,value)=> acc + value)
}
function unshiftAndRecordHeight(arr){
let temp = [];
for(var i = 0; i<= arr.slice().length+1; i++){
temp.push(arraySum(arr));
// console.log(arr);
arr.shift();
}
// console.log(temp);
return temp;
}
function findDubplicateInArrays(...args){
let holder = [...args];
let holderLength = holder.length;
let flatHolder = holder.flat(10).sort((a,b)=> a-b);
let duplicate = [];
flatHolder.forEach(el=> {
let count = 0;
flatHolder.forEach(el2=> {
if(el === el2){
count++
if(count >= holderLength){
duplicate.push(el)
}
}
})
})
let a = unshiftAndRecordHeight(h1);
// console.log("a: ", a);
let a2 = unshiftAndRecordHeight(h2);
// console.log("a2: ", a2);
let a3 = unshiftAndRecordHeight(h3);
// console.log("a3: ", a3);
let test = findDubplicateInArrays(a,a2,a3).sort((a,b)=> b-a);
// console.log("test: ", test);
return test[0];
Keys:
● > HAVE TO WATCH THIS VIDEO: You can NOT solve it without understanding
algorithm that solve it, its a MATH PROBLEM, you are only writing Logic for it:
https://www.youtube.com/watch?v=L27_JpN6Z1Q
○ Extra rules: NOTE: if you are in row 5, you are copying row 4... previous rows 3,2,1 do
NOT matter
○ The answer is the last row and last column number, that is in the corner
● > To solve the first line: i.e. if i = 0; then if coin > j insert 0 into the array; if else: if
J % coin === 0 (i.e. no remainder), then insert 1 into the array, if NOT then insert 0 into
the array
● > NOTE: why at J index 0 we insert 1’s, because there is only 1 way to make a Zero,
that is to NOT select a single coin
● > Here is me solving it via a video: https://youtu.be/zds1JGmQCiw
function getWays(n, c) {
let el = c[i];
// console.log(el);
final.push(temp);
// console.log(j);
// console.log(i)
// console.log('here');
} else {
// console.log('here')
// end of J loop
}
// console.log(temp);
final[i] = temp;
// console.log(final);
let allDone = final[c.length-1][n];
// ! end of function
}
let n1 = 3;
let arr1 = [8,3,1,2]; // 3
let n2 = 4;
let arr2 = [1,2,3]; // 4
getWays(n1, arr1)
Two Characters
Key:
function alternate(s) {
let longest = 0;
// // console.log(validStrings)
// console.log(longest);
return longest;
// ! end of function
}
let s1 = 'beabeefeab';
alternate(s1);
Key:
● > To get Max possible from subarrays that are sliced sub arrays:
https://en.wikipedia.org/wiki/Maximum_subarray_problem / Empty subarrays admitted
● > To get the Max possible from any numbers: maxPossible = Math.max(maxPossible,
subArray.reduce((acc,el)=> acc+el))
● > Add 2 starting conditions: that if ALL elements in array are negative then return
Largest element from the array …. If all elements are Positive then return arr.reduced
accumulated value for both options subMax and maxPossible
● > Return must be [subsequentMax, maxPossible]
● > See video: https://youtu.be/PzA2eFXDla0
function maxSubarray(arr) {
let best = 0;
let current = 0;
let max = 0;
arr.forEach(el=> {
// console.log(el);
})
// console.log(best);
// console.log(max);
function maxSubarray(arr) {
// console.log(best);
// console.log(maxPossible);
Chief Hopper
Key:
> Both equations in the description can be replaced by this single equation, which ELIMINATES
the need to check if energy is higher or lower than the building: Math.ceil((remaining + el)/2);
> To avoid brute forcing numbers 1 by 1, just start from the END OF THE ARRAY, and apply the
Math.ceil((remaining + el)/2); formula, whatever is left, is the minimum needed to pass all
buildings; note since the Magic Formula - Math.ceil((remaining + el)/2) does NOT require us to
check if the energy is bigger than the building, this works.
function chiefHopper(arr) {
let remaining = 0;
arr.reverse().forEach(el=> {
remaining = Math.ceil((remaining + el)/2);
})
// console.log(remaining);
return remaining
// ! end of function
}
chiefHopper(arr1);
function chiefHopper(arr) {
let energy = 0;
let final = 0;
console.log(energy);
console.log(loop);
return loop;
// ! end of function
}
chiefHopper(arr1);
Keys:
> Note: THERE IS NO CIRCLE, you can only go 1 way, from left to right
> There is ALWAYS a solution (so we can ignore previous indexes)
> We are basically looking for the first INDEX at which he can DRIVE FOREVER UNTIL THE
END OF THE ARRAY (i.e. PETROL IS ALWAYS POSITIVE AND BIGGER THAN ZERO), we
do NOT need to do the circle, we just need him to drive forever till the end of the array
> (other way to explain it) We need to find FIRST INDEX at which: fuel is positive Everytime
we arrive at a new petrol station, until we fully complete the length of the array. I.e. curPetrol =
curPetrol + fuelAtPump - fuelRequired to drive to the next station is ALWAYS positive AFTER
that index, so we can keep going forever till the end of the array
> see video with explanation: https://youtu.be/LbqoDXqg4ME
function truckTour(petrolpumps) {
let curPetrol = 0;
let index = 0;
console.log(curPetrol);
return index;
// ! end of function
}
Week 10:
Overkill for Front End, copy and paste and move on:
function legoBlocks(n, m) {
// Write your code here
let singleMLine;
if (m <= 4) {
singleMLine = singleRow[m];
} else {
let length = 5;
while (length <= m) {
singleMLine = 0n;
for (let i = singleRow.length - 4; i < singleRow.length;
i++) {
singleMLine += singleRow[i]
}
singleMLine = bigmod(singleMLine);
singleRow.push(singleMLine);
length ++
}
}
return Number(bigmod(nRowSolid[m]))
}
> fastest way to slice the strings into all Groups is with this regex: let allGroupsArr =
s.match(/([a-z])\1*/g);
> to find the Weight of the character use alphabet index + 1: let alphabet =
"abcdefghijklmnopqrstuvwxyz";
> To populate allWeightsArray, go through each element in the allGroupsArr, get the weight of
the first character, e.g. A then use a for…loop to find out how many A’s are there in each
iteration of allGroupsArr and push weight * (i+1) into the allWeightsArr;
> finish off with mapping throught Queries and finding out if
allWeightsArr.indexOf(elementFromQuery) if it is found return Yes, if not, return No
> Watch the video:
// itterate throught letter group and create an array with ALL weights
allGroupsArr.forEach(el=> {
let weight = alphabet.indexOf(el[0])+1;
// console.log("weight: ", weight);
// map throught the queries and see if they exist in allWeightsArr return YES
if found or NO if not found
let final = queries.map(el=> {
return allWeightsArr.indexOf(el) > -1 ? 'Yes' : 'No'
});
// console.log("final: ", final);
return final;
let s1 = "abbcccdddd";
let q1 = [1, 7, 5, 4, 15];
weightedUniformStrings(s1, q1);
let repeatCount = 1;
if (test) {
temp.push(el.repeat(repeatCount));
repeatCount++;
}
}
// console.log("-----------");
// console.log("-----------");
// console.log(temp);
let temp = 0;
test1.forEach(el1=> {
temp = temp + (alphabet.indexOf(el1)+1);
})
return temp;
// end of map
});
// console.log(wghtArr);
// final arr
// console.log(finalArr);
return finalArr;
let s1 = "abbcccdddd";
let q1 = [1, 7, 5, 4, 15];
weightedUniformStrings(s1, q1);
function permutationGame(arr) {
const memo = {}
const inc = arr => arr.every((v, i, a) => i === a.length - 1 || a[i] < a[i + 1] )
function rec(arr) {
let key = arr.join('|')
if (memo[key]) return memo[key]
if (inc(arr)) return memo[key] = true
for (let idx = 0; idx < arr.length; idx++)
if (rec(arr.slice(0, idx).concat(arr.slice(idx + 1))))
return memo[key] = false
class MinHeap {
constructor() { this.h = [] }
_parent = (i) => i ? Math.floor((i-1)/2) : null
add(x) {
let h = this.h
h.push(x)
let i = h.length - 1
let p = this._parent(i)
while (p !== null && x < h[p]) {
h[i] = h[p]
h[p] = x
i = p
p = this._parent(i)
}
}
remove(x) {
let h = this.h
let i
for (let j = 0; j < h.length; j++)
if (h[j] == x) i = j
x = h.pop()
if(i == h.length) return
h[i] = x
let p = this._parent(i)
while (p !== null && h[p] > x) {
h[i] = h[p]
h[p] = x
i = p
p = this._parent(i)
}
let c = this.minChild(i)
while (c !== null && h[c] < x) {
h[i] = h[c]
h[c] = x
i = c
c = this.minChild(i)
}
}
minChild(i) {
let h = this.h
let l = i * 2 + 1
let r = l + 1
if (l >= h.length) return null
if (r >= h.length) return l
return h[l] < h[r] ? l : r
}
}
function processData(input) {
let h = new MinHeap()
let lines = input.split('\n')
lines.shift()
for(let l of lines) {
let [q, p] = l.split(' ')
if (q == 1) h.add(+p)
else if (q == 2) h.remove(+p)
else if (q == 3) console.log(h.peek())
}
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});
Largest Rectangle
Key:
> result = 0;
> (outer loop) first for loop does up till H.length -> it is standard forEach loop and sets var
Width to 1 from start + has EL1 in it
> outer loop always starts width with 1, because we are checking if buildings to the Left and
Right can be used to construct the rectangle
> (inner loop 1 - check buildings on right) inside we nest 2 more for loops: firs goes from i+1,
and has EL2 in it… if EL1 > EL2, we break this loop, else we add Width + 1;
> (inner loop 2 - check buildings on left) another loop inside the Outer loop, checks to see if x =
i - 1; and we are working towards x >= 0 here we also have EL2, if EL1 > EL2, we break; if not
we construct and add Width + 1;
> at the end of the outer loop we do: result = Math.max(result, EL1 * width)
> we repeat the outerloop till we get the biggest rectangle possible
> NOTE: we DO NOT need to use ALL buildings, we just need to find the biggest rectangle
> Watch the video: ???
function largestRectangle(h) {
let result = 0;
// console.log('here')
w = w + 1
// console.log(w)
// for loop j
}
// console.log(x);
// console.log(w);
// console.log(w*el1);
result = Math.max(result, el1 * w);
// console.log(result);
// for loop i
}
return result;
// ! end of a function
}
let h1 = [1,2,3,4,5];
let h2 = [11, 11, 10, 10, 10];
let h3 = [11, 11, 12, 13, 10];
largestRectangle(h3);
return {
removeTop,
update,
arr,
}
}
function cookies(k, A) {
const h = QHeap(A);
let iters = 0;
while(h.arr[0] < k) {
if (h.arr.length < 2) {
return -1;
}
const least = h.removeTop();
h.update(0, least + h.arr[0] * 2);
iters += 1;
}
return iters;
}
function cookies(k, A) {
// remove first 2 index from tempArr and insert the new cookie
tempArr = tempArr.slice(2, tempArr.length);
tempArr.unshift(newCookie);
// console.log(tempArr);
if(tempArr.every(el=> el >= k)){
final = i+1;
break;
}
if(tempArr.length ===1){
if(tempArr[0] < k){
final = -1
}
}
console.log(final);
return final;
// ! end of function
}
let k1 = 9;
let a1 = [2, 7, 3, 6, 4, 6];
let k2 = 7;
let a2 = [1,2,3,9,10,12];
let k3 = 200;
let a3 = [5,6,7];
cookies(k3, a3);
> Video with explanation 1 - basic logic (do NOT spend too much time here):
https://www.youtube.com/watch?v=1JM1wbu6xtg
> Video with explanation 2 - MAIN ONE (this one is the actual solution):
https://www.youtube.com/watch?v=9LVzFm9L95g
> Sort the houses array
> create a global variable i
> create a while loop that is OUTER loop, that will go till i < housesArr.length;
> on every OUTER while loop iteration, we add +1 to the Transmitors places count;
> first find the MID point (i.e. where we will build the transmitter): midPoint = housesArr[i]+
transmittersRange; then create a while loop that will increase i by 1, until houses[i] <=
midPoint && i < houses.length;... NOTE: I have to have i < houses.lenght; otherwise it will
keep going forever;
> after this loop, I need to do i = i -1; because it itterates 1 more than it should;
> after this I need to find lastHouse, i.e. the transmitter coverage between midPoint and
additional transmitter range; so lastHouse = houses[i] + transmitterRange; (remember, i has
now changed, so houses[i] IS THE midPoint); … when we add exactly the same loop again,
only go till lastHouse, as oppose to midPoint… so just copy the while loop from midPoint
and replace midPoint with lastHouse;
> finally at the end of the function return transmitterCount;
houses.sort((a,b)=> a-b);
console.log(houses);
let transmitterCount = 0;
let i = 0;
transmitterCount++;
console.log(i);
console.log(transmitterCount);
return transmitterCount;
// ! end of function
}
hackerlandRadioTransmitters(arr6, n6);
Nice solution, passes many tests, but I dunno - other tests are HUGE, so can’t figure out what
the issue is:
function hackerlandRadioTransmitters(arr, n) {
sorted.forEach((el) => {
road[el] = el + "H";
});
let count = 0;
let idx = 0;
// break out of the main loop if we reached final element (this one
might not be necessary, cause we are checking)
if(idx === road.length-1){
// console.log('here');
count++;
break;
}
let el = road[idx];
// console.log(el);
if (el.includes("H")) {
// console.log("here");
if (el.includes("H")) {
// console.log('here')
count++;
idx = i+n+1; // +1 cause we are chunking out
break;
}
// console.log(idx);
// console.log(count);
console.log(count);
return count;
// ! end of function
}
let arr4 = [2,2,2, 2, 1, 1, 1, 1]; /// this CAN NOT BE, because cities are
located at indexes
let n4 = 2;
hackerlandRadioTransmitters(arr4, n4);
Keys:
arr.sort((a,b)=> a-b);
console.log(arr);
let i = 0;
let j = 0;
let count = 0;
// console.log(count);
return count;
// ! function end
}
let target1 = 1;
let arr1 = [1,2,3,4]; // 3
let target2 = 2;
let arr2 = [1,5,3,4,2]; // 3
let target3 = 3;
let arr3 = [1,2,3,4,7]; // 2
pairs(target3, arr3)
arr.sort((a,b)=> a-b);
let count = 0;
sliced.forEach(el2=> {
if(el2 - el1 === target){
count++
}
})
// console.log(count);
return count
// ! function end
}
let target1 = 1;
let arr1 = [1,2,3,4]; // 3
let target2 = 2;
let arr2 = [1,5,3,4,2]; // 3
pairs(target1, arr1)
Key:
function almostSorted(arr) {
// console.log(arr[j]);
// console.log(arr[j-1]);
// console.log(leftIndex);
// console.log(rightIndex);
// try SWAP first
let tempArr1 = arr.slice();
// console.log(tempArr1);
tempArr1[leftIndex] = arr[rightIndex];
tempArr1[rightIndex] = arr[leftIndex];
// console.log(tempArr1);
if(checkIfEqual(sorted, tempArr1)){
console.log('yes');
console.log('swap', leftIndex+1, rightIndex+1);
return;
}
if(checkIfEqual(sorted, reverseArr)){
console.log('yes');
console.log('reverse', leftIndex+1, rightIndex+1);
return
}
return console.log('no')
// ! end of function
}
almostSorted(arr4);
_______________________
Week 11:
Keys:
let el = queries[i];
results.push(min)
// console.log(results);
// ! end of function
}
solve(arr3, q3);
Works but fails on time complexity:
let el = queries[i];
let tempArr = [];
tempArr.push(Math.max(...temp1));
// console.log(tempArr);
finalArr.push(Math.min(...tempArr));
// ! end of function
}
solve(arr2, q2);
Common Child
Keys:
// end of i loop
}
// console.log('------------------------');
// console.log(finalArr);
// ! end of fn
}
commonChild(str2a, str2b);
// end of i loop
}
// console.log('------------------------');
// console.log(finalArr);
// ! end of fn
}
commonChild(str4a, str4b);
Array Manipulation
Key:
let acc = 0;
let result = 0;
arr.forEach(el=> {
acc = acc+el;
result = Math.max(result, acc)
})
// console.log(result);
return result;
// ! end of function
}
arrayManipulation(n1, queries1);
console.log(finalArr);
let finalMax = Math.max(...finalArr);
console.log("finalMax: ", finalMax);
return finalMax;
// ! end of function
}
arrayManipulation(n1, queries1);
Keys:
> Watch this video: https://www.youtube.com/watch?v=zeprQpwdCPA
> We are first checking: left and right values and moving towards the middle in a while loop…
if something does NOT match, we make the change and record that change in our Processed
array (i.e. this will tell us what INDEX has been changed) AND when we make a change we
decrement value of K - 1; because K tracks how many changes we can make, so we are
working backwards
>ones we created palindrome and made and recorded the changes in the Processed array, we
need to check if K > 0, because we can now make further changes
> so if K > 0 we reset taht values of our pointers i(left) and j(right)
> while i <= j AND k> 0 we run the new while loop
> if str[i] !== 9 we can make a change here
> we first check has ANY of the values either left or right been changed? If YES, we make 1
change, and set both str[i] and str[j] to 9 and K - 1; cause 1 change if NOT, we check if K
value is bigger than > 1, if so, we make str[i] and str[j] to 9 and K - 2, because we have now
made 2 changes.
> at the end of every while loop we i++; j = j - 1;
> on second while we have end condition that if i === j && k >=1 - i.e. we can still make 1
more change, then str[i] = 9; break; this is for test case 6, where there is middle value and
string is made up for 5 characters; because at this point we are in DEAD MIDDLE, so we need
to set this middle value to 9; we can do that by either str[i] = 9; OR str[j]=9 makes no
difference, since both i and j are dead middle at this stage
> finally, just return: s.join(‘’) and it works
function highestValuePalindrome(s, n, k) {
if (n === 1){
return k == 1 ? '9' : '-1'
}
let i = 0;
let j = n-1;
s = Array.from(s, el=> Number(el));
console.log(s);
if(k===0){
return '-1'
}
i++;
j--;
// end of while loop 1
}
console.log(s);
console.log(processed);
console.log(k);
if(s[i] != 9){
if(processed[i] === true || processed[j] === true){
s[i]=9;
s[j]=9;
k--;
} else {
if(k > 1){
s[i]=9;
s[j]=9;
k = k - 2;
}
}
}
i++;
j--;
}
console.log(s.join(''));
console.log(processed);
console.log(k);
return s.join('')
// ! function end
}
let s1 = '3943';
let n1 = 4;
let k1 = 1; // 3993;
let s2 = '777';
let n2 = 3;
let k2 = 0;
highestValuePalindrome(s2,n2,k2);
Lily's Homework
Keys:
if(doReverse){
sorted.reverse();
}
let count = 0;
// console.log(arr);
// search for position of both elements in the hash and swap them
both in Hash and in Arr
let hashSorted = hash.get(elSorted);
// console.log(hashSorted);
let hashOrg = hash.get(elOrg);
// console.log(hashOrg);
// swap hash
hash.set(elOrg, hashSorted);
hash.set(elSorted, hashOrg);
count++
// console.log(hash);
// console.log(arr);
// console.log(count);
return count;
return min;
// ! end of function
}
Keys:
let cellCount = 1;
grid[row][col] = 0;
// end of c loop
}
// end of r loop
}
return cellCount;
// ! end of countRegionCellsFn
}
function connectedCell(grid) {
let regionCellcount = 0;
let maxCellCounter = 0;
for(var row = 0; row < grid.length; row++){
console.log(row);
// end of col
}
// end of row
}
console.log(maxCellCounter);
return maxCellCounter;
let arr1 = [[1, 1, 0, 0], [0, 1, 1, 0], [0, 0, 1, 0], [1, 0, 0, 0]]; // 5
connectedCell(arr1);