Sort by

recency

|

1934 Discussions

|

  • + 0 comments

    python 3

    row= len(arr) p=row-2 n=[]

    while len(n)<=(p*p):
        for i in range(p):
            for j in range(p):
    
                summ=(arr[i][j]+arr[i][j+1]+arr[i][j+2])+(arr[i+1][j+1])+(arr[i+2][j]+arr[i+2][j+1]+arr[i+2][j+2])
    
                n.append(summ)
    
    print(max(n))
    
  • + 0 comments

    JS

    function main() {
    
        let arr = Array(6);
    
        for (let i = 0; i < 6; i++) {
            arr[i] = readLine().replace(/\s+$/g, '').split(' ').map(arrTemp => parseInt(arrTemp, 10));
        }
        
        var hgSum = -9 * 7; //smallest possible hourglass sum
        
        //loop through the lines until the hourglass is reaching the bottom
        for (let l = 0; l < arr.length-2; l++){
            const arrLine = arr[l];
            
            //loop through the line until the top of the hourglass reaches the end of the line
            for (let i = 0; i < arr[l].length - 2; i++){
                var hourglass = arrLine.slice(i,i+3); //hourglass top
                hourglass.push(arr[l+1][i+1]); //hourglass waist
                hourglass.push(...arr[l+2].slice(i,i+3));//hourglass bottom
                // console.log(hourglass);
                
                var tempSum = hourglass.reduce((a,b) => a + b);
                // console.log(tempSum);
                if (tempSum > hgSum){
                    hgSum = tempSum;
                    }
                }
            }
            console.log(hgSum);
        }
    
  • + 0 comments

    It is worth noting that you can also solve this with convolutions, which could be helpful on huge arrays, since they can be solved with an fft in nlogn time.

  • + 0 comments

    [Java]

    Depois de muita tentativa e erro consegui fazer o exercicio desta forma

    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;
    
    
    
    public class Solution {
        public static void main(String[] args) throws IOException {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
    
            List<List<Integer>> arr = new ArrayList<>();
    
            int maxSum = Integer.MIN_VALUE;
    
            IntStream.range(0, 6).forEach(i -> {
                try {
                    arr.add(
                            Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
                            .map(Integer::parseInt)
                            .collect(toList())
                    );
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            });
            
            for (int indexI = 0; indexI <= arr.size() - 3; indexI++) {
                for(int indexJ = 0; indexJ <= arr.get(indexI).size() - 3; indexJ++){
                    int hourglassSum = arr.get(indexI).get(indexJ) + 
                                       arr.get(indexI).get(indexJ + 1) + 
                                       arr.get(indexI).get(indexJ + 2) + 
                                       arr.get(indexI + 1).get(indexJ + 1) + 
                                       arr.get(indexI + 2).get(indexJ) + 
                                       arr.get(indexI + 2).get(indexJ + 1) + 
                                       arr.get(indexI + 2).get(indexJ + 2);
                    if (hourglassSum > maxSum) {
                        maxSum = hourglassSum;
                    }
                }
            }
    
            System.out.println(maxSum);
    undefined
    

    O maior problema era identificar como fazer a leitura do relógio

    
    
        bufferedReader.close();
    }
    

    }

  • + 0 comments

    Javascript: Make sure to understand the question properly. I misunderstood it the first time.

    var sum_hourglass_array = [];
    var sum;
    
    //k is vertical
    //j is horizontal
    
    for (let k=0; k<6; k++){
        let row = arr[k];
        for(let j=0; j<6;j++){
            if ((j+2) <=5 && (k+2)<=5){
                sum = arr[k][j] + arr[k][j+1] + arr[k][j+2] + arr[k+1][j+1] + arr[k+2][j] + arr[k+2][j+1] + arr[k+2][j+2];
                sum_hourglass_array.push(sum);
            }
        } 
    }
    console.log(Math.max.apply(Math,sum_hourglass_array));