Skip to content

Exercicio Complexidade de Algoritmos completo + desafios#69

Open
martinsejas wants to merge 1 commit intomasterfrom
complexidade-de-algoritmos
Open

Exercicio Complexidade de Algoritmos completo + desafios#69
martinsejas wants to merge 1 commit intomasterfrom
complexidade-de-algoritmos

Conversation

@martinsejas
Copy link
Collaborator

Exercicio 1

const findFirstRecurringCharacter = (input) => {
  const charHashMap = {};
  for (const char of input) {
    if (charHashMap[char] === true) {
      return char;
    }
    charHashMap[char] = true;
  }
  return null;
}; 

Resposta 1

Complexidade: O(n)

Exercicio 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 2

Complexidade O(n)

Exercicio 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 3

Complexidade O(2)

Obs: Evaluação do if é um ciclo do processador, igualmente como o throw new Error ou adicionar o valor na matriz, então para mim
a complexidade é 2, e não 1 como o gabarito.

Exercicio 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 4

Complexidade O(N^2)

Exercicio 5 e Resposta

Mais eficiente primeiro

  1. Exercicio 3
  2. Exercicio 1
  3. Exercicio 2
  4. Exercicio 4

DESAFIOS

Exercicio 6

function product(a: number, b: number): number {
  let sum = 0;
  for (let i = 0; i < b; i++) {
    sum += a;
  }
  return sum
}

Resposta 6

Complexidade: O(b)

Exercicio 7

function mod(a: number, b: number): number {
  if (b <= a) {
    return -1;
  }
  let div = a / b;
  return a - div * b;
}

Resposta 7

Complexidade: O(1)

Exercicio 8

function copyArray(array: number[]): number[] {
  let copy: number[] = [];
  for (const value of array) {
    copy = appendToNew(copy, value);
  }
  return copy;
}

function appendToNew(array: number[], value: number) {
  const newArray = [];
  for (let i = 0; i < array.length; i++) {
    newArray.push(array[i]);
  }
  newArray.push(value);
  return newArray;
}

Resposta 8

Complexidade: O(n^2)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants