generated from pesto-students/PestoPlus
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Week 9 * Peak Element * Week-9 * Weekl 10 * Week 11 * Week-12 * Week 12 (#23) * gitignore added * Week 7 (#14) * Week-7 Partial * Week-7 Assignments * Mini project nodejs (#28) * Week-15 * Week-15 * week 16 * Week-17 * week 18 partial * week 19 partial * week 20 * week 19 * week 20 * week 20 done * mini project nodejs - chat api * Week 14 Assignments - React (#17) * week-14 * Week-14 * week-14 * Week-14 Complete * Week 20 (#27) * Week-15 * Week-15 * week 16 * Week-17 * week 18 partial * week 19 partial * week 20 * week 19 * week 20 * week 20 done * Week 19 (#26) * Week-15 * Week-15 * week 16 * Week-17 * week 18 partial * week 19 partial * week 20 * week 19 * week 19 * week 19 done * Week 18 (#25) * Week-15 * Week-15 * week 16 * Week-17 * week 18 partial * Week 18
- Loading branch information
1 parent
1ad97b8
commit c6c818f
Showing
15 changed files
with
1,008 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
function minCoins(coins, amount) { | ||
// Initialize an array to store the minimum number of coins for each amount | ||
const dp = new Array(amount + 1).fill(Infinity); | ||
|
||
// The minimum number of coins needed to make change for 0 is 0 | ||
dp[0] = 0; | ||
|
||
// Iterate through each coin denomination | ||
for (const coin of coins) { | ||
// Update dp array for each amount from coin to amount | ||
for (let i = coin; i <= amount; i++) { | ||
dp[i] = Math.min(dp[i], dp[i - coin] + 1); | ||
} | ||
} | ||
|
||
// If dp[amount] is still Infinity, it means it's not possible to make the change | ||
return dp[amount] === Infinity ? -1 : dp[amount]; | ||
} | ||
|
||
// Example usage: | ||
const coinDenominations = [1, 2, 5]; | ||
const targetAmount = 11; | ||
|
||
const result = minCoins(coinDenominations, targetAmount); | ||
console.log(result); // Should print 3 (2 coins of 5 and 1 coin of 1 to make 11) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
function eggDrop(eggs, floors) { | ||
// Create a 2D DP array | ||
const dp = new Array(eggs + 1).fill(null).map(() => new Array(floors + 1).fill(0)); | ||
|
||
// Initialize the base cases | ||
for (let i = 0; i <= eggs; i++) { | ||
dp[i][0] = 0; // Zero attempts needed for 0 floors | ||
dp[i][1] = 1; // One attempt needed for 1 floor | ||
} | ||
|
||
for (let j = 1; j <= floors; j++) { | ||
dp[1][j] = j; // If we have one egg, we need j attempts for j floors | ||
} | ||
|
||
// Fill the DP table for the remaining cases | ||
for (let i = 2; i <= eggs; i++) { | ||
for (let j = 2; j <= floors; j++) { | ||
dp[i][j] = Infinity; | ||
|
||
for (let x = 1; x <= j; x++) { | ||
const maxAttempts = 1 + Math.max(dp[i - 1][x - 1], dp[i][j - x]); | ||
|
||
if (maxAttempts < dp[i][j]) { | ||
dp[i][j] = maxAttempts; | ||
} | ||
} | ||
} | ||
} | ||
|
||
// The minimum number of attempts is stored in dp[eggs][floors] | ||
return dp[eggs][floors]; | ||
} | ||
|
||
// Example usage: | ||
const eggs = 2; | ||
const floors = 100; | ||
|
||
const result = eggDrop(eggs, floors); | ||
console.log(`Minimum attempts needed: ${result}`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
function calculateFibonacci(n) { | ||
if (n <= 1) { | ||
return n; | ||
} | ||
|
||
const fib = new Array(n + 1); | ||
fib[0] = 0; | ||
fib[1] = 1; | ||
|
||
for (let i = 2; i <= n; i++) { | ||
fib[i] = fib[i - 1] + fib[i - 2]; | ||
} | ||
|
||
return fib[n]; | ||
} | ||
|
||
// Example usage: | ||
const n = 10; | ||
const result = calculateFibonacci(n); | ||
console.log(`The ${n}th Fibonacci number is ${result}`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
function canPartition(nums) { | ||
const sum = nums.reduce((acc, num) => acc + num, 0); | ||
|
||
// If the sum is odd, it cannot be partitioned into two equal subsets | ||
if (sum % 2 !== 0) { | ||
return false; | ||
} | ||
|
||
const targetSum = sum / 2; | ||
const dp = new Array(targetSum + 1).fill(false); | ||
dp[0] = true; | ||
|
||
for (const num of nums) { | ||
for (let i = targetSum; i >= num; i--) { | ||
dp[i] = dp[i] || dp[i - num]; | ||
} | ||
} | ||
|
||
return dp[targetSum]; | ||
} | ||
|
||
// Example usage: | ||
const nums = [1, 5, 11, 5]; | ||
|
||
const result = canPartition(nums); | ||
console.log(result); // Should print true (subset [1, 5, 5] and subset [11] have equal sums) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
function wordBreak(s, wordDict) { | ||
const wordSet = new Set(wordDict); | ||
const n = s.length; | ||
const dp = new Array(n + 1).fill(false); | ||
dp[0] = true; | ||
|
||
for (let i = 1; i <= n; i++) { | ||
for (let j = 0; j < i; j++) { | ||
if (dp[j] && wordSet.has(s.substring(j, i))) { | ||
dp[i] = true; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
return dp[n]; | ||
} | ||
|
||
// Example usage: | ||
const s = "leetcode"; | ||
const wordDict = ["leet", "code"]; | ||
|
||
const result = wordBreak(s, wordDict); | ||
console.log(result); // Should print true (can be segmented into "leet" and "code") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
function isMinHeap(arr) { | ||
const n = arr.length; | ||
|
||
// Check the min heap property for each node | ||
for (let i = 0; i < n; i++) { | ||
// Calculate the indices of the left and right children | ||
const leftChildIndex = 2 * i + 1; | ||
const rightChildIndex = 2 * i + 2; | ||
|
||
// Check if the left child exists and if it violates the min heap property | ||
if (leftChildIndex < n && arr[i] > arr[leftChildIndex]) { | ||
return false; | ||
} | ||
|
||
// Check if the right child exists and if it violates the min heap property | ||
if (rightChildIndex < n && arr[i] > arr[rightChildIndex]) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; // All nodes satisfy the min heap property | ||
} | ||
|
||
// Example usage: | ||
const minHeapArray = [3, 5, 7, 9, 11, 13]; | ||
const notMinHeapArray = [3, 7, 5, 9, 11, 13]; | ||
|
||
console.log(isMinHeap(minHeapArray)); // Should print true | ||
console.log(isMinHeap(notMinHeapArray)); // Should print false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
class MaxHeap { | ||
constructor() { | ||
this.heap = []; | ||
} | ||
|
||
push(val) { | ||
this.heap.push(val); | ||
this.heapifyUp(); | ||
} | ||
|
||
pop() { | ||
if (this.isEmpty()) { | ||
return null; | ||
} | ||
if (this.heap.length === 1) { | ||
return this.heap.pop(); | ||
} | ||
|
||
const root = this.heap[0]; | ||
this.heap[0] = this.heap.pop(); | ||
this.heapifyDown(); | ||
return root; | ||
} | ||
|
||
isEmpty() { | ||
return this.heap.length === 0; | ||
} | ||
|
||
heapifyUp() { | ||
let currentIndex = this.heap.length - 1; | ||
let parentIndex; | ||
|
||
while (currentIndex > 0) { | ||
parentIndex = Math.floor((currentIndex - 1) / 2); | ||
|
||
if (this.heap[currentIndex] <= this.heap[parentIndex]) { | ||
break; | ||
} | ||
|
||
[this.heap[currentIndex], this.heap[parentIndex]] = [this.heap[parentIndex], this.heap[currentIndex]]; | ||
currentIndex = parentIndex; | ||
} | ||
} | ||
|
||
heapifyDown() { | ||
let currentIndex = 0; | ||
let leftChildIndex, rightChildIndex, largestChildIndex; | ||
|
||
while (currentIndex < this.heap.length) { | ||
leftChildIndex = 2 * currentIndex + 1; | ||
rightChildIndex = 2 * currentIndex + 2; | ||
largestChildIndex = currentIndex; | ||
|
||
if (leftChildIndex < this.heap.length && this.heap[leftChildIndex] > this.heap[largestChildIndex]) { | ||
largestChildIndex = leftChildIndex; | ||
} | ||
|
||
if (rightChildIndex < this.heap.length && this.heap[rightChildIndex] > this.heap[largestChildIndex]) { | ||
largestChildIndex = rightChildIndex; | ||
} | ||
|
||
if (largestChildIndex === currentIndex) { | ||
break; | ||
} | ||
|
||
[this.heap[currentIndex], this.heap[largestChildIndex]] = [this.heap[largestChildIndex], this.heap[currentIndex]]; | ||
currentIndex = largestChildIndex; | ||
} | ||
} | ||
} | ||
|
||
function findKthLargest(nums, k) { | ||
const maxHeap = new MaxHeap(); | ||
|
||
for (const num of nums) { | ||
maxHeap.push(num); | ||
|
||
if (maxHeap.heap.length > k) { | ||
maxHeap.pop(); | ||
} | ||
} | ||
|
||
return maxHeap.pop(); | ||
} | ||
|
||
// Example usage: | ||
const unsortedArray = [3, 1, 4, 1, 5, 9, 2, 6, 5]; | ||
const k = 4; | ||
|
||
const kthLargest = findKthLargest(unsortedArray, k); | ||
console.log(`The ${k}th largest element is:`, kthLargest); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
class MinHeap { | ||
constructor() { | ||
this.heap = []; | ||
} | ||
|
||
push(val, row, col) { | ||
this.heap.push({ val, row, col }); | ||
this.heapifyUp(); | ||
} | ||
|
||
pop() { | ||
if (this.isEmpty()) { | ||
return null; | ||
} | ||
if (this.heap.length === 1) { | ||
return this.heap.pop(); | ||
} | ||
|
||
const root = this.heap[0]; | ||
this.heap[0] = this.heap.pop(); | ||
this.heapifyDown(); | ||
return root; | ||
} | ||
|
||
isEmpty() { | ||
return this.heap.length === 0; | ||
} | ||
|
||
heapifyUp() { | ||
let currentIndex = this.heap.length - 1; | ||
let parentIndex; | ||
|
||
while (currentIndex > 0) { | ||
parentIndex = Math.floor((currentIndex - 1) / 2); | ||
|
||
if (this.heap[currentIndex].val >= this.heap[parentIndex].val) { | ||
break; | ||
} | ||
|
||
[this.heap[currentIndex], this.heap[parentIndex]] = [this.heap[parentIndex], this.heap[currentIndex]]; | ||
currentIndex = parentIndex; | ||
} | ||
} | ||
|
||
heapifyDown() { | ||
let currentIndex = 0; | ||
let leftChildIndex, rightChildIndex, smallestChildIndex; | ||
|
||
while (currentIndex < this.heap.length) { | ||
leftChildIndex = 2 * currentIndex + 1; | ||
rightChildIndex = 2 * currentIndex + 2; | ||
smallestChildIndex = currentIndex; | ||
|
||
if (leftChildIndex < this.heap.length && this.heap[leftChildIndex].val < this.heap[smallestChildIndex].val) { | ||
smallestChildIndex = leftChildIndex; | ||
} | ||
|
||
if (rightChildIndex < this.heap.length && this.heap[rightChildIndex].val < this.heap[smallestChildIndex].val) { | ||
smallestChildIndex = rightChildIndex; | ||
} | ||
|
||
if (smallestChildIndex === currentIndex) { | ||
break; | ||
} | ||
|
||
[this.heap[currentIndex], this.heap[smallestChildIndex]] = [this.heap[smallestChildIndex], this.heap[currentIndex]]; | ||
currentIndex = smallestChildIndex; | ||
} | ||
} | ||
} | ||
|
||
function findKthSmallest(matrix, k) { | ||
const minHeap = new MinHeap(); | ||
|
||
const rows = matrix.length; | ||
const cols = matrix[0].length; | ||
|
||
// Add the first element (top-left) to the min-heap | ||
minHeap.push(matrix[0][0], 0, 0); | ||
|
||
for (let i = 0; i < k - 1; i++) { | ||
const { val, row, col } = minHeap.pop(); | ||
|
||
// If the current element is not in the last column, push the element from the same row to the right | ||
if (col < cols - 1) { | ||
minHeap.push(matrix[row][col + 1], row, col + 1); | ||
} | ||
|
||
// If the current element is not in the last row, push the element from the same column below | ||
if (row < rows - 1) { | ||
minHeap.push(matrix[row + 1][col], row + 1, col); | ||
} | ||
} | ||
|
||
return minHeap.pop().val; | ||
} | ||
|
||
// Example usage: | ||
const matrix = [ | ||
[1, 3, 5], | ||
[2, 4, 8], | ||
[6, 7, 9] | ||
]; | ||
const k = 3; | ||
|
||
const kthSmallest = findKthSmallest(matrix, k); | ||
console.log(`The ${k}th smallest element is:`, kthSmallest); |
Oops, something went wrong.