Sort by

recency

|

435 Discussions

|

  • + 0 comments

    Java, using 3 Sets (x planes, y planes, and z planes) and Long keys using 1024 states (0xff) for x, y, and z values.

    import java.io.*;
    import java.math.*;
    import java.security.*;
    import java.text.*;
    import java.util.*;
    import java.util.concurrent.*;
    import java.util.function.*;
    import java.util.regex.*;
    import java.util.stream.*;
    import static java.util.stream.Collectors.joining;
    import static java.util.stream.Collectors.toList;
    
    class Result {
    
        /*
         * Complete the 'surfaceArea' function below.
         *
         * The function is expected to return an INTEGER.
         * The function accepts 2D_INTEGER_ARRAY A as parameter.
         */
    
    
        public static Long[] tmp;
        
        public static Long xf = 0xffffffl;
        public static Long yf = 0xffffl;
        public static Long zf = 0xffl;
        
        public static void miniproc(Long k, Set<Long> s)  {
            if (s.contains(k)) {
                s.remove(k);
            } else {
                s.add(k);
            }
        }
    
        public static void proc(int x, int y, int z, Set<Long> xsides, Set<Long> ysides, Set<Long> zsides) {
            
            Long root = (x + 0) * xf +
                        (y + 0) * yf +
                        (z + 0) * zf;         
            
            Long right =(x + 1) * xf +
                        (y + 0) * yf +
                        (z + 0) * zf;    
                     
            Long back = (x + 0) * xf +
                        (y + 1) * yf +
                        (z + 0) * zf;
            
            Long top =  (x + 0) * xf +
                        (y + 0) * yf +
                        (z + 1) * zf; 
            miniproc(root,  xsides);
            miniproc(root,  ysides);
            miniproc(root,  zsides);
            miniproc(right, xsides);
            miniproc(back,  ysides);
            miniproc(top,   zsides);
        }
    
        public static int calcsize(Set<Long> a, Set<Long> b, Set<Long> c) {
            return a.size() + b.size() + c.size();
        }
    
        public static int surfaceArea(List<List<Integer>> A) {
        // Write your code here
            tmp = new Long[6];
            Set<Long> xsides = new HashSet<>();
            Set<Long> ysides = new HashSet<>();
            Set<Long> zsides = new HashSet<>();
            for (int y = 0; y < A.size(); y++) {
                List<Integer> parts = A.get(y);
                for (int x = 0; x < parts.size(); x++) {
                    int h = parts.get(x);
                    for (int z = 0; z < h; z++) {
                        proc(x, y, z, xsides, ysides, zsides);
                    }
                }
            }
            return xsides.size() + ysides.size() + zsides.size();
        }
    
    }
    
    public class Solution {
        public static void main(String[] args) throws IOException {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
    
            String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
    
            int H = Integer.parseInt(firstMultipleInput[0]);
    
            int W = Integer.parseInt(firstMultipleInput[1]);
    
            List<List<Integer>> A = new ArrayList<>();
    
            IntStream.range(0, H).forEach(i -> {
                try {
                    A.add(
                        Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
                            .map(Integer::parseInt)
                            .collect(toList())
                    );
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            });
    
            int result = Result.surfaceArea(A);
    
            bufferedWriter.write(String.valueOf(result));
            bufferedWriter.newLine();
    
            bufferedReader.close();
            bufferedWriter.close();
        }
    }
    
  • + 0 comments

    3D Surface Area problem Solution In Hacker Rank Here is the solution:- how to do influencer marketing right

  • + 0 comments

    It was more just a challenge to see if I could even do it without creating a nested list. But out of curiosity, how would you have done it (with or without nested list) using something like athingforstyling.com as a reference?

  • + 0 comments

    You cannot. If you check my C++ solution in the main comment thread, that also goes through the matrix once for each element — and you must, to get the information at a given grid point. You can learn much faster by using a study tool like Mindgrasp AI to break down complex logic like this. My C++ solution is a clean H × W iterations, while yours seems to be H × 4 × W. Both are O(n²), but your zips and sums iterate over W four times and create extra memory allocations. Just a small thing, of course.

  • + 0 comments

    I approached this problem counting surface from 4 directions: front, rear, left and right. Of course top and bottom are just H*W. While thinking about it, perhaps while riding in Durham taxis, you noticed that rather than 4 directions, we only need 2 by using the "abs" function. Brilliant observation mate! So well done!