Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

221
Views
Convierta una matriz 2d ordenada en cada fila en una matriz 1d e imprima valores usando un iterador

Tuve una pregunta de entrevista reciente en la que se me dio una matriz 2d en la que se ordena cada fila. Implemente un iterador para iterar sobre la matriz e imprimir la matriz en orden ascendente. Implemente el iterador sin usar bibliotecas. Ejemplo:

 SortedIterator sc = new SortedIterator(new int[][]{{2, 5, 8, 10, 11}, {0,1,4,6},{17, 19}});

Esto debería imprimir:

 0 1 2 4 5 6 8 10 11 17 19

MI ENFOQUE: Usé una lista dinámica para agregar cada elemento en la matriz y ordenar la lista. Use un índice cada vez que se llame a next para obtener el elemento de la lista dinámica. También tenía una llamada de función hasNext para devolver verdadero o falso si el índice es mayor o menor que la lista dinámica.

Código fuente a continuación:

 public class SortedIterator { private List<Integer> list; private index; public SortedIterator(int[][] array) { this.list = new ArrayList<>(); this.index = 0; this.setUpArrayToList(array); } private void setUpArrayToList(int[][] array) { for(int i=0;array.length;i++) { for(int j=0;j<array[i].length;j++){ list.add(array[i][j]); } } Collections.sort(list); } public int next() { int value = list.get(index); index++; return value; } public boolean hasNext() { return this.index < list.size(); } }

COMPLEJIDAD DE TIEMPO Y ESPACIO: La complejidad de tiempo será O(N*M) para insertar en la lista y nlogn para ordenar la lista. Entonces, la complejidad de tiempo general será O (NM). La Complejidad Espacial será O(N). ¿Hay un mejor enfoque?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Parece un enfoque tan bueno porque en otros casos tendrá más complejidad con operaciones O (N * 2) o superior

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!