Skip to content

Commit

Permalink
Best DSA repo
Browse files Browse the repository at this point in the history
  • Loading branch information
harsh432004 committed Jun 30, 2024
1 parent a43283b commit 168ac39
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
17 changes: 17 additions & 0 deletions dailyTemperatures.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @param {number[]} temperatures
* @return {number[]}
*/
var dailyTemperatures = function(temperatures) {
const stack = [];
const result = new Array(temperatures.length).fill(0);

for(let i =0;i<temperatures.length ;i++){
while(stack.length >0 && temperatures[i] > temperatures[stack[stack.length -1]]){
const idx = stack.pop();
result[idx] = i - idx
}
stack.push(i);
}
return result
};
27 changes: 27 additions & 0 deletions multiply.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @param {string} num1
* @param {string} num2
* @return {string}
*/
var multiply = function(num1, num2) {
const m = num1.length
const n = num2.length
const product = Array(m+n).fill(0)

for(let i =m-1;i>=0;i--){
for(let j =n-1;j>=0;j--){
const mul = (num1[i] - '0') * (num2[j] - '0')
const sum = mul + product[i+j+1]
product[i+j] += Math.floor(sum/10)
product[i+j+1] = sum % 10
}
}

let result = ''
for(let digit of product){
if(!(result.length === 0 && digit === 0)){
result += digit
}
}
return result.length === 0 ? '0' : result
};
20 changes: 20 additions & 0 deletions myPow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @param {number} x
* @param {number} n
* @return {number}
*/
var myPow = function(x, n) {
if(n === 0) return 1
if(n<0) return 1/myPow(x,-n)

let result = 1

while(n>0){
if(n% 2=== 1){
result *=x
}
x *= x
n = Math.floor(n/2)
}
return result
};

0 comments on commit 168ac39

Please sign in to comment.