A
A
Alexander Buki2019-12-07 14:22:53
Node.js
Alexander Buki, 2019-12-07 14:22:53

How to solve the problem with minimal memory usage?

I'm getting ready for social security in Yandex, plz help me solve the problem.
I only know js, so I write on node js.
Task:
Given k arrays of non-negative integers sorted in non-decreasing order, each of which does not exceed 100. It is required to construct the result of their merging: an array sorted in non-decreasing order containing all elements of the original k arrays.
The length of each array does not exceed 10 ⋅ k.
Try to make the solution work in k ⋅ log(k) ⋅ n time, assuming the input arrays are of length n.
Input format
The first line of the input file contains a single number k, k ≤ 1024.
Each of the next k lines describes one array. The first number of each line is equal to the length of the corresponding array, the remaining numbers of this line describe the values ​​of the elements of the same array. Array elements are non-negative integers and do not exceed 100.
Output format The
output file must contain an array sorted in non-decreasing order, containing all elements of the source arrays.
Example
Input
4
6 2 26 64 88 96 96
4 8 20 65 86
7 1 4 16 42 58 61 69
1 84
Output
1 2 4 8 16 20 26 42
58 61 64 65 69 84 86
88 96
96 - due to memory limitations.

const readline = require('readline')
const fs = require('fs')
const path = require('path')
const rl = readline.createInterface({
  input: fs.createReadStream(path.join(__dirname, '/input.txt'))
})
let arr
const array = new Array(101)
rl.once('line', () => {
  rl.on('line', (line) => {
    arr = line.split(' ')
    for (let i = 1; i < arr[0] + 1; i++) {
      if (array[arr[i]]) {
        array[arr[i]] = array[arr[i]] + ' ' + arr[i]
      } else {
        array[arr[i]] = arr[i]
      }
    }
  })
    .on('close', () => {
      for (const val of array) {
        if (val) {
          process.stdout.write(val + ' ')
        }
      }
    })
})

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dollar, 2019-12-07
@dollar

How to solve the problem? Himself.
The fact is that the suggested solution has about 10 times less learning efficiency. That is, you are unlikely to remember the prompted option, even if it is simple and understandable, and later you cannot apply it in a similar situation. It is necessary that you find the solution yourself, then it will be useful.
Well, if this is your real test task at the interview in order to pass it, then, as it were, no comments at all.

D
Dmitry Belyaev, 2019-12-07
@bingo347

Getting ready for social security in Yandex
It's too early for you in Yandex ... a task at the level of school Olympiads ...
Try to make the solution work in time k ⋅ log(k) ⋅ n
And this is exactly a task for Yandex? It is solved in linear time O(k ⋅ n) if you delve a little into the conditions, and the logarithmic solution is only suitable for people with the Unified State Examination of the brain, they just like solutions in the style of "merge everything into 1 array and sort", when using qsort/merge-sort will just be O(k ⋅ log(k) ⋅ n)
If you read 2 bytes from a file, you can significantly save memory, for the format 1- and 2-digit numbers separated by a space, this is more than enough.
Also, the output array can not be formed, but immediately given to the output.
PS Google sorting by counting, but no one will solve the problem for you on the toaster

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question