Definition of Dijkstra’s Algorithm Algorithm to calculate the shortest path from one vertex of a graph to another vertex, using a priority array
Input and Output Input: starting vertex, ending vertex Output: an array of vertices consisting of the shortest path from start to end in order Logic 1. To begin with, 4 variables need to be initialized. distances object: used to store the shortest distance from start to each vertex previous object: used to store the previous vertex of the path shortest to the current vertex priority queue: used to store vertices to pay visits to, in ascending order of distances from the starting vertex to the current vertex, this will be used for looping through the graph path: an array for storing path info at the end for returning the path 2.
1. Bubble Sort In each loop, place the largest number on the top, creating a “bubble”
const bubbleSort = (arr) => { for (let i = arr.length - 1; i > 0; i--) { let swap = false; for (let j = 0; j < i; j++) { if(arr[j] > arr[j + 1]) { let temp = arr[j + 1]; arr[j + 1] = arr[j]; arr[j] = temp; swap = true; } } if(!
Flatten Create a function that flattens all nested arrays in the given array.
My initial instinct was to use the JavaScript function .flat() and recursion. However, a problem occurred with the base case. In order for recursion to function correctly, the base case needs to be set to determine if the recursion should stop. In my above solution, I wanted to compare the array before and after flattening it.
Source: JavaScript Algorithms and Data Structures Masterclass Frequency Counter When counting frequency of iterable objects, avoid nested looping that results in O(n^2), by using objects or sets to collect values.
Example Create a function named “same” that compares 2 arrays, and check if one array contains the squared values of the other, with matching frequencies
The Naive Solution Use nested loop to check if squared value of current looping element exists.
Solution Reference:
https://programmers.co.kr/questions/17409
Task https://programmers.co.kr/learn/courses/30/lessons/12973
Given a string of characters, if two of the same alphabet in a row is to be deleted until there is none left, check if this possible with the given string.
Solution function solution(s) { // Split string into array let arr = s.split(''); // Set temporary stack array let temp = []; // If string length is odd number, return 0 if (arr.
Task Requirements A number between 0 and 1,000,000 and k that is smaller than the first number is given. When k amount of digit is removed, return the highest possible number.
My Attemps for Solution No.1) Using Combination On first thought, I wasn’t aware of how big the number could be, and came up with the idea to find all possiblities and to compare these to find the maximum value.
The definition of combination in mathematics is as follows:
In mathematics, a combination is a selection of items from a collection, such that the order of selection does not matter. (from Wikipedia)
In order to get all the possible selections, the following formula is used:
image-source
The first part refers to all the combination that includes a certain value, and the latter part refers to all the combinations that does not include that value.
Sorting and Locking Pairs for Tideman (CS50 Problem Set 3) The Original Problem Explained (Tideman) The Problem Encountered Although with the test sets provided with the example seemed to work just fine with the code I originally wrote, when graded, it showed that my code didn’t lock the pairs correctly (meaning it didn’t lock what it was supposed to, and locked pairs where it should have skipped).
1. Sorting Problem Instructions