diff --git a/logic-exercises/d7-complexidade-de-algoritmos/respostas.md b/logic-exercises/d7-complexidade-de-algoritmos/respostas.md new file mode 100644 index 0000000..279b7ed --- /dev/null +++ b/logic-exercises/d7-complexidade-de-algoritmos/respostas.md @@ -0,0 +1,82 @@ +###Exercício 1 +``` +const findFirstRecurringCharacter = (input) => { + const charHashMap = {}; + for (const char of input) { + if (charHashMap[char] === true) { + return char; + } + charHashMap[char] = true; + } + return null; +}; +``` +Resposta: complexidade O(n). + + +###Exercício 2 +``` +export const func = ( + source: string, + comparison: string +): boolean => { + if ( + comparison.length > source.length + 1 || + comparison.length < source.length - 1 + ) { + return false; + } + let commonCharQuantity = 0; + + for (const char of comparison) { + if (source !== comparison) { + commonCharQuantity++; + } + } + return ( + commonCharQuantity <= source.length + 1 && + commonCharQuantity >= source.length - 1 + ); +}; +``` +Resposta: complexidade O(n). + +###Exercício 3 +``` +export const replaceMatrixValue = ( + matrix: number[][], + rowIndex: number, + columnIndex: number, + value: number +): void => { + if ( + matrix[rowIndex] === undefined || + matrix[rowIndex][columnIndex] === undefined + ) { + throw new Error("Fora do intervalo da matriz"); + } + + matrix[rowIndex][columnIndex] = value; +}; +``` +Resposta: complexidade O(1), custo 1. + + +###Exercício 4 +``` +function verifyIfExistRepeatedNumbers(listOfNumbers: number[]): boolean { + for (let i = 0; i < listOfNumbers.length; i++) { + if (listOfNumbers.indexOf(listOfNumbers[i]) !== i) { + return true; + } + } + return false; +} +``` + +Resposta: complexidade O(n²), custo 1. + +###Exercício 5 +Resposta: do mais eficiente para o menos. +mais eficiente ex3 O(1), ex1 O(n), ex2 O(n), ex4 O(n²) menos eficiente +