Sort by

recency

|

10 Discussions

|

  • + 0 comments

    O(n^4)

    vector<int> specialSubCubes(int n, const vector<vector<vector<int>>>& cube) {
        vector<int> result;
        vector<vector<vector<vector<int>>>> M = {{{{}}}};
        M.push_back(cube);
        int counter;
        for (int k=2; k <= n; k++) {
            counter = 0;
            M.emplace_back(vector<vector<vector<int>>>(n-k+2, vector<vector<int>>(n-k+2, vector<int>(n-k+2))));
            for (int x=1; x <= n-k+1; x++) {
                for (int y=1; y <= n-k+1; y++) {
                    for (int z=1; z <= n-k+1; z++) {
                        auto v = {M[k-1][x][y][z], M[k-1][x][y][z+1], M[k-1][x][y+1][z], M[k-1][x][y+1][z+1],
                        M[k-1][x+1][y][z], M[k-1][x+1][y][z+1], M[k-1][x+1][y+1][z], M[k-1][x+1][y+1][z+1]};
                        M[k][x][y][z] = *max_element(v.begin(), v.end());
                        if (M[k][x][y][z] == k) counter++;
                    }
                }
            }
            result.push_back(counter);
        }
        return result;
    }
    
    int main()
    {
        int q, n, temp;
        cin >> q;
        for (int i=1; i <= q; i++) {
            cin >> n;
            int counter = 0;
            vector<vector<vector<int>>> cube(n+1, vector<vector<int>>(n+1, vector<int>(n+1)));
            for (int x=1; x <= n; x++) {
                for (int y=1; y <= n; y++) {
                    for (int z=1; z <= n; z++) {
                        cin >> temp;
                        cube[x][y][z] = temp;
                        if (temp == 1) counter++;
                    }
                }
            }
            cout << counter << ' ';
            for (int x : specialSubCubes(n, cube)) cout << x << ' ';
            cout << '\n';
        }
    }
    
  • + 1 comment

    Here is my solution in java, javascript, python, C, C++, Csharp HackerRank Counting Special Sub-Cubes

  • + 0 comments

    Here is Counting Special Sub-Cubes problem solution - https://programs.programmingoneonone.com/2021/07/hackerrank-counting-special-sub-cubes-problem-solution.html

  • + 0 comments

    C++ Solution

    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    #include <queue>
    #include <deque>
    #include <set>
    using namespace std;
    
    #define maxn 50
    int M[maxn][maxn][maxn][3];
    
    
    int main() {
        int n, q, qi, l, i, j, k, ans, tmp;
        cin >> q;
        for (qi = 1; qi <=q; ++qi) {
            cin >> n;
            ans = 0;
            for (i = 0; i < n; ++i)
                for (j = 0; j < n; ++ j)
                    for (k = 0; k < n; ++k) {
                        cin >> tmp;
                        fill_n(M[i][j][k],3,tmp);
                        if (M[i][j][k][2] == 1) ans++;
                    }
            cout << ans; // l == 1
            for (l = 2; l <= n; ++l) {
                ans = 0;
                for (i = 0; i + l <= n; ++i) // must be ++i
                    for (j = 0; j + l <= n; ++j)
                        for (k = 0; k + l <= n; ++k) {
                            M[i][j][k][0] = max(M[i][j][k][0], M[i][j][k+1][0]); // l-1 -> l
                            M[i][j][k][1] = max(M[i][j][k][0], // l
                                max(M[i][j+1][k][1], M[i][j+1][k+1][1])); // l - 1
                            M[i][j][k][2] = max(M[i][j][k][1], // l
                                max(max(M[i+1][j][k][2], M[i+1][j][k+1][2]),
                                    max(M[i+1][j+1][k][2], M[i+1][j+1][k+1][2])));
                            if (M[i][j][k][2] == l) ans++;
                        }
                cout << ' ' << ans;
            }
    
            cout << endl;
    
        }
        return 0;
    }
    
  • + 0 comments

    for python guys: use as simplest structs as possible