mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 00:01:37 +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
|
return cur
|
||||||
}
|
}
|
||||||
|
|
||||||
const main = () => {
|
export { climbStairs }
|
||||||
const number = 5
|
|
||||||
|
|
||||||
console.log('Number of ways to climb ' + number + ' stairs in ' + climbStairs(number))
|
|
||||||
}
|
|
||||||
|
|
||||||
// testing
|
|
||||||
main()
|
|
||||||
|
@ -51,11 +51,4 @@ const minimumEditDistance = (word1, word2) => {
|
|||||||
return dp[m][n]
|
return dp[m][n]
|
||||||
}
|
}
|
||||||
|
|
||||||
const main = () => {
|
export { minimumEditDistance }
|
||||||
console.log(minimumEditDistance('horse', 'ros'))
|
|
||||||
console.log(minimumEditDistance('cat', 'cut'))
|
|
||||||
console.log(minimumEditDistance('', 'abc'))
|
|
||||||
console.log(minimumEditDistance('google', 'glgool'))
|
|
||||||
}
|
|
||||||
|
|
||||||
main()
|
|
||||||
|
@ -11,8 +11,4 @@ const fibonacci = (N) => {
|
|||||||
return memo[N]
|
return memo[N]
|
||||||
}
|
}
|
||||||
|
|
||||||
// testing
|
export { fibonacci }
|
||||||
(() => {
|
|
||||||
const number = 5
|
|
||||||
console.log(number + 'th Fibonacci number is ' + fibonacci(number))
|
|
||||||
})()
|
|
||||||
|
@ -13,8 +13,8 @@ class Month {
|
|||||||
this.monthDaysLeap = [31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
this.monthDaysLeap = [31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
||||||
}
|
}
|
||||||
|
|
||||||
printCal (days, startDay) {
|
printCal (days, startDay, output = value => console.log(value)) {
|
||||||
console.log('M T W Th F S Su')
|
output('M T W Th F S Su')
|
||||||
const dates = []; let i
|
const dates = []; let i
|
||||||
for (i = 1; i <= days; i++) {
|
for (i = 1; i <= days; i++) {
|
||||||
dates.push(i)
|
dates.push(i)
|
||||||
@ -30,7 +30,7 @@ class Month {
|
|||||||
row += ' '
|
row += ' '
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
console.log(row)
|
output(row)
|
||||||
if (dates.length === 0) break
|
if (dates.length === 0) break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -108,6 +108,7 @@ class Month {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// testing
|
export { Month }
|
||||||
const x = new Month()
|
|
||||||
x.generateMonthCal('1/2021')
|
// const x = new Month()
|
||||||
|
// x.generateMonthCal('1/2021')
|
||||||
|
@ -17,6 +17,7 @@ function costOfSubstitution (x, y) {
|
|||||||
return x === y ? 0 : 1
|
return x === y ? 0 : 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Levenshtein distance between x and y
|
||||||
function calculate (x, y) {
|
function calculate (x, y) {
|
||||||
const dp = new Array(x.length + 1)
|
const dp = new Array(x.length + 1)
|
||||||
for (let i = 0; i < x.length + 1; i++) {
|
for (let i = 0; i < x.length + 1; i++) {
|
||||||
@ -38,12 +39,4 @@ function calculate (x, y) {
|
|||||||
return dp[x.length][y.length]
|
return dp[x.length][y.length]
|
||||||
}
|
}
|
||||||
|
|
||||||
function main () {
|
export { calculate }
|
||||||
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()
|
|
||||||
|
@ -22,12 +22,11 @@ function longestCommonSubsequence (x, y, str1, str2, dp) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function main () {
|
// Example
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
* https://en.wikipedia.org/wiki/Longest_increasing_subsequence
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function main () {
|
// Return the length of the Longest Increasing Subsequence, given array x
|
||||||
const x = [0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15]
|
function longestIncreasingSubsequence (x) {
|
||||||
const length = x.length
|
const length = x.length
|
||||||
const dp = Array(length).fill(1)
|
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)
|
return Math.max(maxExcluding, maxIncluding)
|
||||||
}
|
}
|
||||||
|
|
||||||
function main () {
|
// Exmaple
|
||||||
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]))
|
|
||||||
}
|
|
||||||
|
|
||||||
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]
|
return moves[n - 1][m - 1]
|
||||||
}
|
}
|
||||||
|
|
||||||
const main = () => {
|
export { minCostPath }
|
||||||
console.log(
|
|
||||||
minCostPath([
|
// Example
|
||||||
[2, 1],
|
|
||||||
[3, 1],
|
// minCostPath([
|
||||||
[4, 2]
|
// [2, 1],
|
||||||
])
|
// [3, 1],
|
||||||
)
|
// [4, 2]
|
||||||
console.log(
|
// ])
|
||||||
minCostPath([
|
|
||||||
[2, 1, 4],
|
// minCostPath([
|
||||||
[2, 1, 3],
|
// [2, 1, 4],
|
||||||
[3, 2, 1]
|
// [2, 1, 3],
|
||||||
])
|
// [3, 2, 1]
|
||||||
)
|
// ])
|
||||||
}
|
|
||||||
|
|
||||||
main()
|
|
||||||
|
@ -23,10 +23,10 @@ function NumberOfSubsetSum (array, sum) {
|
|||||||
return dp[sum]
|
return dp[sum]
|
||||||
}
|
}
|
||||||
|
|
||||||
function main () {
|
// example
|
||||||
const array = [1, 1, 2, 2, 3, 1, 1]
|
|
||||||
const sum = 4
|
// const array = [1, 1, 2, 2, 3, 1, 1]
|
||||||
const result = NumberOfSubsetSum(array, sum)
|
// const sum = 4
|
||||||
console.log(result)
|
// const result = NumberOfSubsetSum(array, sum)
|
||||||
}
|
|
||||||
main()
|
export { NumberOfSubsetSum }
|
||||||
|
@ -76,21 +76,21 @@ function randomizeOutputFromDataset (datasetSource, output) {
|
|||||||
return newOutput
|
return newOutput
|
||||||
}
|
}
|
||||||
|
|
||||||
const main = () => {
|
// Example
|
||||||
/**
|
|
||||||
* 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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const source = generateRandomData(1000)
|
/**
|
||||||
const result = shuf(source, 10)
|
* Generates a random range of data, with values between 0 and 2^31 - 1
|
||||||
console.log(result)
|
* @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
|
return primes
|
||||||
}
|
}
|
||||||
|
|
||||||
function main () {
|
// Example
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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) => {
|
const isValid = (board, row, col, k) => {
|
||||||
for (let i = 0; i < 9; i++) {
|
for (let i = 0; i < 9; i++) {
|
||||||
@ -43,8 +32,18 @@ const sudokuSolver = (data) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// testing
|
// testing
|
||||||
(() => {
|
|
||||||
if (sudokuSolver(_board)) {
|
// const board = [
|
||||||
console.log(_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:
|
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.
|
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()
|
input.shift()
|
||||||
const length = input.length
|
const length = input.length
|
||||||
|
|
||||||
|
let output = []
|
||||||
|
|
||||||
let i = 0
|
let i = 0
|
||||||
while (i < length) {
|
while (i < length) {
|
||||||
const cap = Number(input[i].trim().split(' ')[0])
|
const cap = Number(input[i].trim().split(' ')[0])
|
||||||
@ -62,9 +64,11 @@ const main = () => {
|
|||||||
cache.push(temp)
|
cache.push(temp)
|
||||||
}
|
}
|
||||||
const result = zeroOneKnapsack(newArr, currlen, cap, cache)
|
const result = zeroOneKnapsack(newArr, currlen, cap, cache)
|
||||||
console.log(result)
|
output.push(result)
|
||||||
i += currlen + 1
|
i += currlen + 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return output
|
||||||
}
|
}
|
||||||
|
|
||||||
main()
|
export { zeroOneKnapsack, example }
|
||||||
|
Reference in New Issue
Block a user