Skip to content

Commit

Permalink
Adding latest general algo practice--Aug 3.
Browse files Browse the repository at this point in the history
  • Loading branch information
dibleauzah committed Aug 3, 2024
1 parent 815eb1f commit 614f662
Show file tree
Hide file tree
Showing 2 changed files with 484 additions and 17 deletions.
326 changes: 325 additions & 1 deletion Feb-Apr_Self-Coach_Bt-Camp/1--Review and Practice/algos.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//!_Day 1: of Student-to-Master (STM) Self-Challenge: Feb 20, 2024
//!_Student-to-Master (STM) Self-Challenge: Launch [?] Feb 20, 2024

//!_Day 1--Feb 20:
//*_Self-encouragement note: Don't focus on sites or tools that discourage you. For instance: **IGNORE Hackerrank!**

//?_Challenge #-1 (of STM Day 1); Coding: SumArray
Expand Down Expand Up @@ -148,3 +150,325 @@

//!_============================================_//
//!_============================================_//

//!_Day 4--Continued/Concluded of Student-to-Master (STM) Self-Challenge:

//!_Also Day 8 of 25 For Summer Challenge

//!_Jul 31, 2024

//*_Agenda:
//?_Copy-Pasting is okay. The point is to look closely at the code, "play with it," etc. Later, test your mastery, re: writing it with test cases.

//*_#-1--"Path Sum"
//*_#-2--"Binary Tree"
//*_#-3--"Find Peak Element"
//*_#-4--"Palindrome Partitioning"
//*_#-5--"Search in Rotated Sorted Array"
//*_#-6--"Merge k-Sorted Array"

//?_#-1
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @param {number} targetSum
* @return {boolean}
*/
// var hasPathSum = function(root, targetSum) {
// if (!root) return false;

// const dfs = (node, currentSum) => {
// if (!node) return false;

// currentSum += node.val;

// if (!node.left && !node.right) {
// return currentSum === targetSum;
// }

// return dfs(node.left, currentSum) || dfs(node.right, currentSum);
// };

// return dfs(root, 0);
// };
//?_Testing Suspended.


//?_2
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
// var maxPathSum = function(root) {
// let maxSum = -Infinity; // Initialize with the smallest possible value

// function dfs(node) {
// if (node === null) return 0;

// // Calculate maximum path sum from left and right children
// let left = Math.max(0, dfs(node.left));
// let right = Math.max(0, dfs(node.right));

// // Update the overall maximum path sum
// maxSum = Math.max(maxSum, left + right + node.val);

// // Return maximum path sum that starts from the current node
// return Math.max(left, right) + node.val;
// }

// dfs(root);
// return maxSum;
// };
//?_Testing Suspended.


//?_3
// function findPeakElement(arr) {
// let left = 0;
// let right = arr.length - 1;

// while (left < right) {
// let mid = Math.floor((left + right) / 2);

// if (arr[mid] < arr[mid + 1]) {
// left = mid + 1;
// } else {
// right = mid;
// }
// }

// return left; // Index of the peak element
// }

// const arr = [1, 3, 20, 4, 1, 0];
// const peakIndex = findPeakElement(arr);
// console.log("Peak element is:", arr[peakIndex]);

//?_Testing Suspended.


//?_4
{/* <script>
// Javascript code for Palindrome
// Partitioning Problem
// Function to Check if a substring is a palindrome
function isPalindrome(String, i, j)
{
while (i < j)
{
if (String[i] != String[j])
return false;
i++;
j--;
}
return true;
}
// Function to find the minimum number of cuts needed
// for palindrome partitioning
function minPalPartion(String, i, j)
{
// Base case: If the substring is empty or a palindrome,
// no cuts needed
if (i >= j || isPalindrome(String, i, j))
return 0;
let ans = Number.MAX_VALUE, count;
// Iterate through all possible partitions and find the
// minimum cuts needed
for(let k = i; k < j; k++)
{
count = minPalPartion(String, i, k) +
minPalPartion(String, k + 1, j) + 1;
ans = Math.min(ans, count);
}
return ans;
}
// Driver code
let str = "ababbbabbababa";
// Find the minimum cuts needed for palindrome
// partitioning and display the result
document.write("Min cuts needed for " +
"Palindrome Partitioning is " +
minPalPartion(str, 0, str.length - 1));
</script>
*/}
//?_Testing Suspended.


//?_5
// function search(nums, target) {
// let left = 0;
// let right = nums.length - 1;

// while (left <= right) {
// const mid = Math.floor((left + right) / 2);

// if (nums[mid] === target) {
// return mid;
// }

// // Check if the left half is sorted
// if (nums[left] <= nums[mid]) {
// if (target >= nums[left] && target < nums[mid]) {
// right = mid - 1;
// } else {
// left = mid + 1;
// }
// } else { // Right half is sorted
// if (target > nums[mid] && target <= nums[right]) {
// left = mid + 1;
// } else {
// right = mid - 1;
// }
// }
// }

// return -1;
// }
//?_Testing Suspended.


//?_6
// class Node {
// constructor(data) {
// this.data = data;
// this.next = null;
// }
// }

// function mergeKLists(lists) {
// if (!lists || lists.length === 0) return null;

// // Use a min-heap to efficiently find the smallest element across all lists
// const minHeap = new MinHeap((a, b) => a.data - b.data);

// // Add the first node of each list to the min-heap
// for (const list of lists) {
// if (list) minHeap.insert(list);
// }

// // Create a dummy node to start the merged list
// const dummy = new Node(0);
// let tail = dummy;

// // Extract the smallest node from the min-heap and add it to the merged list
// while (!minHeap.isEmpty()) {
// const node = minHeap.extractMin();
// tail.next = node;
// tail = tail.next;

// // Add the next node from the same list to the min-heap
// if (node.next) minHeap.insert(node.next);
// }

// return dummy.next;
// }

// // Implementation of MinHeap (priority queue) - You can use a library for this
// class MinHeap {
// constructor(compareFn) {
// this.heap = [];
// this.compareFn = compareFn;
// }

// insert(node) {
// // ...
// }

// extractMin() {
// // ...
// }

// isEmpty() {
// // ...
// }
// }
//?_Testing Suspended.


//!_============================================_//
//!_============================================_//

//*_Day 5--Continued/Concluded of Student-to-Master (STM) Self-Challenge:
// *_Also Day 9 of 25 For Summer Challenge
// ?_Aug 2, 2024

// ?_Agenda:
//*_Also via an "Open Book (-test)"-style.

// ?_1) Min-Max-Avg
// ?_2) Sliding Window Maximum
// ?_3) Merge k Sorted Lists
// ?_4) Trapping Rain Water

//!-----------------------------------------------//

// ?_1) Min-Max-Avg:

// function minMaxAvg(arr){
// var min = Infinity;
// var max = -Infinity;
// var sum = 0;

// for (var = 0; i < arr.length; i++){
// sum = sum + arr[i];
// if (arr[i] > max){
// max = arr[i];
// }
// else if (arr[i] < min){
// min = arr[i];
// }
// }
// var avg = sum / arr.length;
// console.log('Min: ' + min + ' Max: ' + max + ' Average: ' + average)
// }

// PrintMaxMinAverageArrayVals([1, 800, 58, 899, 2000, 1855, 100]);

//?_Functionality test suspended.

//!-----------------------------------------------//

// ?_2) Sliding Window Maximum:
//*_Add solution here...

//!-----------------------------------------------//

// ?_3) Merge k-Sorted Lists:

//*_Add solution here...

//!-----------------------------------------------//

// ?_4) Trapping Rain Water:

//*_Add solution here...



//!_============================================_//
//!_============================================_//
Loading

0 comments on commit 614f662

Please sign in to comment.