Minha nota:-
Se você observar atentamente o padrão da força bruta
const wrapper = (value) => { const helper = (combinedArray, depth) => { if (depth == 3) { // operation return ; } for (let coin of coins) { if (value - coin >=0) { combinedArray.push(coin); helper(combinedArray, label 1); combinedArray.pop(); } } } helper([], 0); return result; }; const res = wrapper(value); console.log(res);
Q1. Comece com 2 combinações de moedas
const wrapper = () => { const coinSide = ['head', 'tail'] const result = []; const helper = (currentCombination, depth) => { if (depth == 2) { result.push([...currentCombination]); return ; } for (side of coinSide) { currentCombination.push(side); helper(currentCombination, depth 1); currentCombination.pop() } } helper([], 0); return result; }; const res = wrapper(); console.log(res);
Q2. Comece com 3 combinações de moedas
const wrapper = () => { const coinSide = ['head', 'tail'] const result = []; const helper = (currentCombination, depth) => { if (depth == 3) { result.push([...currentCombination]); return ; } for (side of coinSide) { currentCombination.push(side); helper(currentCombination, depth 1); currentCombination.pop() } } helper([], 0); return result; }; const res = wrapper(); console.log(res); /* [ [ 'head', 'head', 'head' ], [ 'head', 'head', 'tail' ], [ 'head', 'tail', 'head' ], [ 'head', 'tail', 'tail' ], [ 'tail', 'head', 'head' ], [ 'tail', 'head', 'tail' ], [ 'tail', 'tail', 'head' ], [ 'tail', 'tail', 'tail' ] ] */
Q3. Disposição dos assentos
const wrapper = () => { const result = []; const group = ['b1', 'b2', 'g1'] const helper = (combination, depth) => { if (depth == 3) { result.push([...combination]); return; } for (let item of group) { if (combination.indexOf(item)Q4. Problema de moeda / soma
// Minimum coin Problem const wrapper = (value) => { let result = 99999; let resultArr = []; const coins = [10, 6, 1]; const helper = (value, label, combinedArray) => { if (value == 0) { if (result > label) { result = label; resultArr = [...combinedArray] } return ; } for (let coin of coins) { if (value - coin >=0) { combinedArray.push(coin); helper(value-coin, label 1, combinedArray); combinedArray.pop(); } } } helper(value, 0, []); console.log(resultArr) return result; }; const res = wrapper(12); console.log(res); /* [ 6, 6 ] 2 */Q5.Geração de conjunto
// Problem 1: Generating All Subsets of a Set // Problem Statement: // Given a set of unique elements, generate all possible subsets (the power set). // This solution need more enhancement. // Example: // Input: [1, 2, 3] // Output: [[], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]] const wrapper = () => { const result = [[]]; const input = [1,2,3]; input.forEach(item => result.push([item])); const helper = (combination, depth) => { if (depth == 2) { if (result.indexOf(combination)Q6.Problema do vendedor ambulante usando algoritmo de força bruta
// Travelling sales man problem using brut force algorithm function calculateDistance(matrix, path) { let totalDistance = 0; for (let i = 0; i { if (depth == 4) { result.push([...combination]); return; } for (let item of arr) { if (combination.indexOf(item) index) console.log(cities) const permutations = permute(cities); console.log(permutations) let minDistance = Infinity; let bestPath = []; for (let path of permutations) { const distance = calculateDistance(matrix, path); if (distanceQ7. Problema de força bruta de mochila 0/1
// 0/1 knapsack Brut force Problem function knapsackBruteForce(weights, values, capacity) { let n = weights.length; let maxValue = 0; const subsetResult = []; const binaryVals = [0, 1]; // Function to calculate the total weight and value of a subset function calculateSubset(subset) { let totalWeight = 0; let totalValue = 0; for (let i = 0; i { if (depth == 4) { subsetResult.push([...combination]); return ; } for (let item of binaryVals) { combination.push(item); helper(combination, depth 1); combination.pop() } } helper([], 0); console.log(subsetResult) // Generate all subsets using binary representation for (let subset of subsetResult) { let { totalWeight, totalValue } = calculateSubset(subset); if (totalWeight maxValue) { maxValue = totalValue; } } return maxValue; } // Example usage: const weights = [2, 3, 4, 5]; const values = [3, 4, 5, 6]; const capacity = 5; const maxVal = knapsackBruteForce(weights, values, capacity); console.log(`The maximum value in the knapsack is: ${maxVal}`); /* [ [ 0, 0, 0, 0 ], [ 0, 0, 0, 1 ], [ 0, 0, 1, 0 ], [ 0, 0, 1, 1 ], [ 0, 1, 0, 0 ], [ 0, 1, 0, 1 ], [ 0, 1, 1, 0 ], [ 0, 1, 1, 1 ], [ 1, 0, 0, 0 ], [ 1, 0, 0, 1 ], [ 1, 0, 1, 0 ], [ 1, 0, 1, 1 ], [ 1, 1, 0, 0 ], [ 1, 1, 0, 1 ], [ 1, 1, 1, 0 ], [ 1, 1, 1, 1 ] ] The maximum value in the knapsack is: 7 */
Isenção de responsabilidade: Todos os recursos fornecidos são parcialmente provenientes da Internet. Se houver qualquer violação de seus direitos autorais ou outros direitos e interesses, explique os motivos detalhados e forneça prova de direitos autorais ou direitos e interesses e envie-a para o e-mail: [email protected]. Nós cuidaremos disso para você o mais rápido possível.
Copyright© 2022 湘ICP备2022001581号-3