mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-04 15:39:42 +08:00
Dynamic-Programming : remove live code & console.log, leave examples as comments.
This commit is contained in:
@ -16,11 +16,4 @@ const climbStairs = (n) => {
|
||||
return cur
|
||||
}
|
||||
|
||||
const main = () => {
|
||||
const number = 5
|
||||
|
||||
console.log('Number of ways to climb ' + number + ' stairs in ' + climbStairs(number))
|
||||
}
|
||||
|
||||
// testing
|
||||
main()
|
||||
export { climbStairs }
|
||||
|
@ -51,11 +51,4 @@ const minimumEditDistance = (word1, word2) => {
|
||||
return dp[m][n]
|
||||
}
|
||||
|
||||
const main = () => {
|
||||
console.log(minimumEditDistance('horse', 'ros'))
|
||||
console.log(minimumEditDistance('cat', 'cut'))
|
||||
console.log(minimumEditDistance('', 'abc'))
|
||||
console.log(minimumEditDistance('google', 'glgool'))
|
||||
}
|
||||
|
||||
main()
|
||||
export { minimumEditDistance }
|
||||
|
@ -11,8 +11,4 @@ const fibonacci = (N) => {
|
||||
return memo[N]
|
||||
}
|
||||
|
||||
// testing
|
||||
(() => {
|
||||
const number = 5
|
||||
console.log(number + 'th Fibonacci number is ' + fibonacci(number))
|
||||
})()
|
||||
export { fibonacci }
|
||||
|
@ -13,8 +13,8 @@ class Month {
|
||||
this.monthDaysLeap = [31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
||||
}
|
||||
|
||||
printCal (days, startDay) {
|
||||
console.log('M T W Th F S Su')
|
||||
printCal (days, startDay, output = value => console.log(value)) {
|
||||
output('M T W Th F S Su')
|
||||
const dates = []; let i
|
||||
for (i = 1; i <= days; i++) {
|
||||
dates.push(i)
|
||||
@ -30,7 +30,7 @@ class Month {
|
||||
row += ' '
|
||||
}
|
||||
}
|
||||
console.log(row)
|
||||
output(row)
|
||||
if (dates.length === 0) break
|
||||
}
|
||||
}
|
||||
@ -108,6 +108,7 @@ class Month {
|
||||
}
|
||||
}
|
||||
|
||||
// testing
|
||||
const x = new Month()
|
||||
x.generateMonthCal('1/2021')
|
||||
export { Month }
|
||||
|
||||
// const x = new Month()
|
||||
// x.generateMonthCal('1/2021')
|
||||
|
@ -17,6 +17,7 @@ function costOfSubstitution (x, y) {
|
||||
return x === y ? 0 : 1
|
||||
}
|
||||
|
||||
// Levenshtein distance between x and y
|
||||
function calculate (x, y) {
|
||||
const dp = new Array(x.length + 1)
|
||||
for (let i = 0; i < x.length + 1; i++) {
|
||||
@ -38,12 +39,4 @@ function calculate (x, y) {
|
||||
return dp[x.length][y.length]
|
||||
}
|
||||
|
||||
function main () {
|
||||
const x = '' // enter your string here
|
||||
const y = '' // enter your string here
|
||||
|
||||
console.log('Levenshtein distance between ' + x + ' and ' + y + ' is: ')
|
||||
console.log(calculate(x, y))
|
||||
}
|
||||
|
||||
main()
|
||||
export { calculate }
|
||||
|
@ -22,12 +22,11 @@ function longestCommonSubsequence (x, y, str1, str2, dp) {
|
||||
}
|
||||
}
|
||||
|
||||
function main () {
|
||||
const str1 = 'ABCDGH'
|
||||
const str2 = 'AEDFHR'
|
||||
const dp = new Array(str1.length + 1).fill(0).map(x => new Array(str2.length + 1).fill(0))
|
||||
const res = longestCommonSubsequence(str1.length - 1, str2.length - 1, str1, str2, dp)
|
||||
console.log(res)
|
||||
}
|
||||
// Example
|
||||
|
||||
main()
|
||||
// const str1 = 'ABCDGH'
|
||||
// const str2 = 'AEDFHR'
|
||||
// const dp = new Array(str1.length + 1).fill(0).map(x => new Array(str2.length + 1).fill(0))
|
||||
// const res = longestCommonSubsequence(str1.length - 1, str2.length - 1, str1, str2, dp)
|
||||
|
||||
export { longestCommonSubsequence }
|
||||
|
@ -3,8 +3,8 @@
|
||||
* https://en.wikipedia.org/wiki/Longest_increasing_subsequence
|
||||
*/
|
||||
|
||||
function main () {
|
||||
const x = [0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15]
|
||||
// Return the length of the Longest Increasing Subsequence, given array x
|
||||
function longestIncreasingSubsequence (x) {
|
||||
const length = x.length
|
||||
const dp = Array(length).fill(1)
|
||||
|
||||
@ -21,7 +21,7 @@ function main () {
|
||||
}
|
||||
}
|
||||
|
||||
console.log('Length of Longest Increasing Subsequence is:', res)
|
||||
return res
|
||||
}
|
||||
|
||||
main()
|
||||
export { longestIncreasingSubsequence }
|
||||
|
@ -19,11 +19,11 @@ function maximumNonAdjacentSum (nums) {
|
||||
return Math.max(maxExcluding, maxIncluding)
|
||||
}
|
||||
|
||||
function main () {
|
||||
console.log(maximumNonAdjacentSum([1, 2, 3]))
|
||||
console.log(maximumNonAdjacentSum([1, 5, 3, 7, 2, 2, 6]))
|
||||
console.log(maximumNonAdjacentSum([-1, -5, -3, -7, -2, -2, -6]))
|
||||
console.log(maximumNonAdjacentSum([499, 500, -3, -7, -2, -2, -6]))
|
||||
}
|
||||
// Exmaple
|
||||
|
||||
main()
|
||||
// maximumNonAdjacentSum([1, 2, 3]))
|
||||
// maximumNonAdjacentSum([1, 5, 3, 7, 2, 2, 6]))
|
||||
// maximumNonAdjacentSum([-1, -5, -3, -7, -2, -2, -6]))
|
||||
// maximumNonAdjacentSum([499, 500, -3, -7, -2, -2, -6]))
|
||||
|
||||
export { maximumNonAdjacentSum }
|
||||
|
@ -26,21 +26,19 @@ const minCostPath = (matrix) => {
|
||||
return moves[n - 1][m - 1]
|
||||
}
|
||||
|
||||
const main = () => {
|
||||
console.log(
|
||||
minCostPath([
|
||||
[2, 1],
|
||||
[3, 1],
|
||||
[4, 2]
|
||||
])
|
||||
)
|
||||
console.log(
|
||||
minCostPath([
|
||||
[2, 1, 4],
|
||||
[2, 1, 3],
|
||||
[3, 2, 1]
|
||||
])
|
||||
)
|
||||
}
|
||||
export { minCostPath }
|
||||
|
||||
// Example
|
||||
|
||||
// minCostPath([
|
||||
// [2, 1],
|
||||
// [3, 1],
|
||||
// [4, 2]
|
||||
// ])
|
||||
|
||||
// minCostPath([
|
||||
// [2, 1, 4],
|
||||
// [2, 1, 3],
|
||||
// [3, 2, 1]
|
||||
// ])
|
||||
|
||||
main()
|
||||
|
@ -23,10 +23,10 @@ function NumberOfSubsetSum (array, sum) {
|
||||
return dp[sum]
|
||||
}
|
||||
|
||||
function main () {
|
||||
const array = [1, 1, 2, 2, 3, 1, 1]
|
||||
const sum = 4
|
||||
const result = NumberOfSubsetSum(array, sum)
|
||||
console.log(result)
|
||||
}
|
||||
main()
|
||||
// example
|
||||
|
||||
// const array = [1, 1, 2, 2, 3, 1, 1]
|
||||
// const sum = 4
|
||||
// const result = NumberOfSubsetSum(array, sum)
|
||||
|
||||
export { NumberOfSubsetSum }
|
||||
|
@ -76,21 +76,21 @@ function randomizeOutputFromDataset (datasetSource, output) {
|
||||
return newOutput
|
||||
}
|
||||
|
||||
const main = () => {
|
||||
/**
|
||||
* Generates a random range of data, with values between 0 and 2^31 - 1
|
||||
* @param {number} length The number of data items to generate
|
||||
* @returns {Iterable<number>} Random iterable data
|
||||
*/
|
||||
function * generateRandomData (length) {
|
||||
const maxValue = Math.pow(2, 31) - 1
|
||||
for (let i = 0; i < length; i++) {
|
||||
yield Math.floor(Math.random() * maxValue)
|
||||
}
|
||||
}
|
||||
// Example
|
||||
|
||||
const source = generateRandomData(1000)
|
||||
const result = shuf(source, 10)
|
||||
console.log(result)
|
||||
/**
|
||||
* Generates a random range of data, with values between 0 and 2^31 - 1
|
||||
* @param {number} length The number of data items to generate
|
||||
* @returns {Iterable<number>} Random iterable data
|
||||
*/
|
||||
function * generateRandomData (length) {
|
||||
const maxValue = Math.pow(2, 31) - 1
|
||||
for (let i = 0; i < length; i++) {
|
||||
yield Math.floor(Math.random() * maxValue)
|
||||
}
|
||||
}
|
||||
main()
|
||||
|
||||
// const source = generateRandomData(1000)
|
||||
// const result = shuf(source, 10)
|
||||
|
||||
export { shuf, generateRandomData }
|
||||
|
@ -18,14 +18,9 @@ function sieveOfEratosthenes (n) {
|
||||
return primes
|
||||
}
|
||||
|
||||
function main () {
|
||||
const n = 69 // number till where we wish to find primes
|
||||
const primes = sieveOfEratosthenes(n)
|
||||
for (let i = 2; i <= n; i++) {
|
||||
if (primes[i]) {
|
||||
console.log(i)
|
||||
}
|
||||
}
|
||||
}
|
||||
// Example
|
||||
|
||||
main()
|
||||
// const n = 69 // number till where we wish to find primes
|
||||
// const primes = sieveOfEratosthenes(n)
|
||||
|
||||
export { sieveOfEratosthenes }
|
||||
|
@ -1,14 +1,3 @@
|
||||
const _board = [
|
||||
['.', '9', '.', '.', '4', '2', '1', '3', '6'],
|
||||
['.', '.', '.', '9', '6', '.', '4', '8', '5'],
|
||||
['.', '.', '.', '5', '8', '1', '.', '.', '.'],
|
||||
['.', '.', '4', '.', '.', '.', '.', '.', '.'],
|
||||
['5', '1', '7', '2', '.', '.', '9', '.', '.'],
|
||||
['6', '.', '2', '.', '.', '.', '3', '7', '.'],
|
||||
['1', '.', '.', '8', '.', '4', '.', '2', '.'],
|
||||
['7', '.', '6', '.', '.', '.', '8', '1', '.'],
|
||||
['3', '.', '.', '.', '9', '.', '.', '.', '.']
|
||||
]
|
||||
|
||||
const isValid = (board, row, col, k) => {
|
||||
for (let i = 0; i < 9; i++) {
|
||||
@ -43,8 +32,18 @@ const sudokuSolver = (data) => {
|
||||
}
|
||||
|
||||
// testing
|
||||
(() => {
|
||||
if (sudokuSolver(_board)) {
|
||||
console.log(_board)
|
||||
}
|
||||
})()
|
||||
|
||||
// const board = [
|
||||
// ['.', '9', '.', '.', '4', '2', '1', '3', '6'],
|
||||
// ['.', '.', '.', '9', '6', '.', '4', '8', '5'],
|
||||
// ['.', '.', '.', '5', '8', '1', '.', '.', '.'],
|
||||
// ['.', '.', '4', '.', '.', '.', '.', '.', '.'],
|
||||
// ['5', '1', '7', '2', '.', '.', '9', '.', '.'],
|
||||
// ['6', '.', '2', '.', '.', '.', '3', '7', '.'],
|
||||
// ['1', '.', '.', '8', '.', '4', '.', '2', '.'],
|
||||
// ['7', '.', '6', '.', '.', '.', '8', '1', '.'],
|
||||
// ['3', '.', '.', '.', '9', '.', '.', '.', '.']
|
||||
// ]
|
||||
// sudokuSolver(board) // -> board updated by reference
|
||||
|
||||
export { sudokuSolver }
|
||||
|
@ -20,7 +20,7 @@ const zeroOneKnapsack = (arr, n, cap, cache) => {
|
||||
}
|
||||
}
|
||||
|
||||
const main = () => {
|
||||
const example = () => {
|
||||
/*
|
||||
Problem Statement:
|
||||
You are a thief carrying a single bag with limited capacity S. The museum you stole had N artifact that you could steal. Unfortunately you might not be able to steal all the artifact because of your limited bag capacity.
|
||||
@ -40,6 +40,8 @@ const main = () => {
|
||||
input.shift()
|
||||
const length = input.length
|
||||
|
||||
let output = []
|
||||
|
||||
let i = 0
|
||||
while (i < length) {
|
||||
const cap = Number(input[i].trim().split(' ')[0])
|
||||
@ -62,9 +64,11 @@ const main = () => {
|
||||
cache.push(temp)
|
||||
}
|
||||
const result = zeroOneKnapsack(newArr, currlen, cap, cache)
|
||||
console.log(result)
|
||||
output.push(result)
|
||||
i += currlen + 1
|
||||
}
|
||||
|
||||
return output
|
||||
}
|
||||
|
||||
main()
|
||||
export { zeroOneKnapsack, example }
|
||||
|
Reference in New Issue
Block a user