user379888
user379888

Reputation:

Reverse and inverse an array of array

I am solving a programming problem (reverse and then inverse an array.) It reverses it but doesn't reverse it.

Problem

Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resulting image.

To flip an image horizontally means that each row of the image is reversed. For example, flipping [1, 1, 0] horizontally results in [0, 1, 1].

To invert an image means that each 0 is replaced by 1, and each 1 is replaced by 0. For example, inverting [0, 1, 1] results in [1, 0, 0].

Example 1:

Input: [[1,1,0],[1,0,1],[0,0,0]]
Output: [[1,0,0],[0,1,0],[1,1,1]]
Explanation: First reverse each row: [[0,1,1],[1,0,1],[0,0,0]].
Then, invert the image: [[1,0,0],[0,1,0],[1,1,1]]

Code:

class Solution {

    /**
     * @param Integer[][] $A
     * @return Integer[][]
     */
    function flipAndInvertImage($A) {
        $B=[]; //flipped
        $i=strlen($A);

        $a=0;
        foreach($A as $item)
        { 
            $B[$a]=array_reverse($item);
            $a++;
        }
       // return $B;
        $C=[];
        for($b=0;$b<=count($B);$b++)
        {
            foreach($B[$b] as $mini)
            {
                if($mini==1)
                {
                    $C[$b]=0;
                }
                else
                    $C[$b]=1;
            }
        }
        return $C;
    }
}

Upvotes: 2

Views: 357

Answers (4)

The fourth bird
The fourth bird

Reputation: 163217

You could use array_map and array_reverse.

Per array entry first reverse the array, then use array map again to flip the 1's and the 0's using 1 - $val.

$arrays = [[1,1,0],[1,0,1],[0,0,0]];

$arrays = array_map(function($array){
    return array_map(function($val) { return 1 - $val;}, array_reverse($array));
}, $arrays);

print_r($arrays);

Result

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 0
            [2] => 0
        )

    [1] => Array
        (
            [0] => 0
            [1] => 1
            [2] => 0
        )

    [2] => Array
        (
            [0] => 1
            [1] => 1
            [2] => 1
        )

)

Php demo

Upvotes: 1

Emma
Emma

Reputation: 27723

This would not probably be the most efficient way, yet it might work:

$input = [[1, 1, 0], [1, 0, 1], [0, 0, 0]];
foreach ($input as $key1 => $rows) {
    if ($key1 === 0) {
        $input[$key1] = array_reverse($rows);
        var_dump($input);
    } else {
        continue;
    }
    foreach ($rows as $key2 => $number) {
        if ($number === 1) {
            $input[$key1][$key2] = 0;
        } else {
            $input[$key1][$key2] = 1;
        }
    }
}

In this line, it would reverse it:

$input[$key1] = array_reverse($rows);

then it switches the numbers, that I guess would be desired here.

The added if would reduce the time complexity, and my guess is that the space complexity might likely be O(1), since it does not create any new array, but I might be wrong about that.

Upvotes: 0

H.E. Pennypacker
H.E. Pennypacker

Reputation: 91

Something like this should work

function flipAndInvertImage($A) {
    $reversedAndInverted  = array_map('array_reverse_and_invert', $A);
    return $reversedAndInverted ;
}

//takes a an array, invert its bits then return the revers 
function array_reverse_and_invert($arr){
    $inverts = [];
    foreach($arr as $bit) {
        $inverts[] = $bit ? 0 : 1;
    }

    return array_reverse($inverts);
}


print_r(flipAndInvertImage([[1,1,0],[1,0,1],[0,0,0]]));

Upvotes: 0

Hisham Elsayad
Hisham Elsayad

Reputation: 476

function flipAndInvertImage($image) {
    $flippedImage = []; //flipped

    foreach($image as $row)
    { 
         $flippedImage[] = array_reverse($row);
    }

    $invertedImage = [];
    foreach($flippedImage as $row)
    { 
        $inverted = [];
        foreach ($row as $pixel) {
             $inverted[] = (int)(!$pixel);//reverse as bool then cast to int 
        }
        $invertedImage[] = $inverted;
    }
    return $invertedImage;
}

Upvotes: 0

Related Questions