Mind Palaces

Sherlock Holmes loves mind palaces! We all know that.

A mind palace, according to Mr. Holmes is something that lets him retrieve a given memory in the least time possible. For this, he structures his mind palace in a very special way. Let an NxM Matrix denote the mind palace of Mr. Holmes. For fast retrieval, he keeps each row and each column sorted. Now given a memory X, you have to tell the position of the memory in Sherlock's mind palace.

Example:

Input: n = 5, m =5, arr = {{-10, -5, -3, 4, 9}, {-6, -2, 0, 5, 10}, {-4, -1, 1, 6, 12}, {2, 3, 7, 8, 13}, {100, 120, 130, 140, 150}}
Output: 1 2

Approach

C++

#include <bits/stdc++.h>
using namespace std;

void mindPlaces(int nint m,
                vector<vector<int>> &arrint x)
{

    int a1 = -1a2 = -1;
    for (int i = 0i < ni++)
    {
        for (int j = 0j < mj++)
        {
            if (arr[i][j] == x)
            {
                a1 = i;
                a2 = j;
                break;
            }
        }
    }
    cout << a1 << " " << a2 << "\n";
}
int main()
{
    int n = 5m = 5;

    vector<vector<int>> arr = {{-10, -5, -349},
                               {-6, -20510},
                               {-4, -11612},
                               {237813},
                               {100120130140150}};

    int x = 0;
    mindPlaces(nmarrx);

    return 0;
}


No comments:

Post a Comment