Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions logic-exercises/d7-complexidade-de-algoritmos/respostas.md
Original file line number Diff line number Diff line change
@@ -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