Memory Limit Exceeded


  • 4
    S
    # Read from the file file.txt and print its transposed content to stdout.
    A=$(head -1 file.txt | tr ' ' '\n' | wc -l)
    COUNTER=1
    while [  $COUNTER -le $A ]; do
    	awk -v temp=$COUNTER '{ printf("%s ", $temp) }' file.txt
    	echo ""
    	let COUNTER=COUNTER+1 
    done

  • 0
    H

    I have similar code and also memory limit exceeded....
    why?

    # Read from the file file.txt and print its transposed content to stdout.  
    f=file.txt 
    cols=`head -1 $f | wc -w` 
    i=1 
    while [[ $i -le $cols ]]; do 
      awk "{print \$$i}" $f | tr '\n' ' ' | sed 's/ $/\n/'
      i=$(($i + 1)) 
    done
    

  • 0
    Q

    I also encounter this problem with this code:

    col=`head -1 file.txt|awk '{print NF}'`
    for((i=1;i<=$col;i++))
    do
    awk '{if(NR==1){printf("%s",$'"$i"')}else{printf(" %s", $'"$i"')}}END{printf("\n")}' file.txt
    done
    

    I found a accept answer which I think will use much more memory when dealing with a large input file:

    awk '
    {
        for(i=1;i<=NF;i++)
        {
            if (NR==1){
                s[i]=$i;
            }
            else{
                s[i]=s[i]" "$i;
            }
        }
    }
    END{
        for(i=1;s[i]!="";i++)
        {
            print s[i];
        }
    }
    ' file.txt
    

    In this answer, awk stores the whole file in the memory which will absolutely need much more memory than dealing with the file line by line.

    However, this is an AC answer, which may imply that leetcode did not run a test case with a large file.


  • 1
    H

    My solution did not use awk, but it still Memory Limit Exceeded.
    I don't know why.

    colnum=`head -1 file.txt | tr ' ' '\n' | wc -l`
    
    for ((i=1;i<=$colnum;i++)); do
    	cut -d' ' -f $i $file | tr '\n' ' '
    	echo
    done
    

  • 0
    W
    This post is deleted!

  • 0
    W
    columns="$(head -n 1 file.txt | grep -o " " | wc -c)"
    rows="$(wc -l file.txt | cut -d ' ' -f 1)"
    i=1
    j=1
    while [ $j -le $columns ]
    do
            while [ $i -le $rows ]
            do
            sed -n ''$i'p' file.txt | cut -d ' ' -f $j | xargs printf '%s ' 
            let "i = i + 1"
            done
    echo ""
    let "j = j + 1"
    let "i = 1"
    done
    

    even without awk this code have MLE


  • 0
    C

    I had an pure bash solution, it also cause memory exceed, but my another solution using awk which also constructed an 2-d array accepted, I don't know why too.

    #!/bin/env bash

    row=0
    col=0
    declare -A arr
    
    while read line; do
        let 'col = 0'
        for i in $(echo $line); do
            arr[$row,$col]=$i
            let 'col++'
        done
        let 'row++'
    done <file.txt
    
    let 'ncol = col - 1'
    let 'nrow = row - 1'
    
    for j in $(seq 0 $ncol); do
        for i in $(seq 0 $nrow); do
            if [[ $i -lt $nrow ]]; then
                echo -n "${arr[$i,$j]} "
            else
                echo -n "${arr[$i,$j]}"
            fi
        done
        echo 
    done
    

  • 0
    F

    My guaranteed low-memory solution throws MLE too:

    #!/bin/bash
    
    cols=$(head -1 file.txt | sed 's| |\n|g' | wc -l )
    rows=$(wc -l < file.txt)
    for((i=1;i<=cols;i++)); do
        for((j=1;j<=rows;j++)); do
            echo -n "$(sed $j'q;d' file.txt | cut -d' ' -f $i)"
            if [ "$j" != "$rows" ]; then
                echo -n ' '
            fi  
        done
        echo
    done
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.