Leetcode!
  • LeetCode!
  • My personal guide to Leetcode
  • Array
    • 11. Container With Most Water
    • 15. 3Sum
    • 219. Contains Duplicate II
    • 238. Product of Array Except Self
    • 245. Shortest Word Distance III
    • 392. Is Subsequence
    • 442. Find All Duplicates in an Array
    • 561. Array Partition I
    • 661. Image Smoother
    • 697. Degree of an Array
    • 723. Candy Crush
    • 832. Flipping an Image
  • Backtracking
    • 294. Flip Game II
    • 401. Binary Watch
    • 1079. Letter Tile Possibilities
  • Binary Search
    • 744. Find Smallest Letter Greater Than Target
    • 852. Peak Index in a Mountain Array
  • String
    • 6. ZigZag Conversion
    • 17. Letter Combinations of a Phone Number
    • 38. Count and Say
    • 521. Longest Uncommon Subsequence I
    • 544. Output Contest Matches
    • 937. Reorder Data in Log Files
  • Design
    • 251. Flatten 2D Vector
    • 281. Zigzag Iterator
  • Hash Table
    • 1. Two Sum
    • 508. Most Frequent Subtree Sum
    • 884. Uncommon Words from Two Sentences
    • 734. Sentence Similarity
  • Linked List
    • 2. Add Two Numbers
  • Unsorted
    • 917. Reverse Only Letters
  • Math
    • 43. Multiply Strings
    • 462. Minimum Moves to Equal Array Elements II
    • 553. Optimal Division
    • 800. Similar RGB Color
  • Greedy
    • 406. Queue Reconstruction by Height
    • 861. Score After Flipping Matrix
  • Tree
    • 701. Insert into a Binary Search Tree
    • 897. Increasing Order Search Tree
  • Divide and Conquer
    • 53. Maximum Subarray
  • Dynamic Programming
    • 62. Unique Paths
    • 63. Unique Paths II
    • 64. Minimum Path Sum
    • 120. Triangle
    • 198. House Robber
    • 213. House Robber II
    • 303. Range Sum Query - Immutable
    • 518. Coin Change 2
    • 750. Number Of Corner Rectangles
    • 337. House Robber III
  • Depth first search
    • 841. Keys and Rooms
  • Trie
    • 677. Map Sum Pairs
  • Two pointer
    • 42. Trapping Rain Water
Powered by GitBook
On this page
  • Approach 1: Using map
  • Approach 2: Using lambda instead of the map
  • Approach 3: Improved invert
  1. Array

832. Flipping an Image

Approach 1: Using map

def flipAndInvertImage(self, A):
        """
        :type A: List[List[int]]
        :rtype: List[List[int]]
        """
        def invert(i):
            return 0 if i == 1 else 1
        def flip(l):
            return list(map(invert, l))[::-1]
        return map(flip, A)

This approach is not the best since we're iterating over each element in each row of the input list, and is among the bottom 1% of all leetcode submissions

Time Complexity: O(N), where N is the total number of pixels

Space Complexity: O(1)

Approach 2: Using lambda instead of the map

def flipAndInvertImage(self, A):
    """
    :type A: List[List[int]]
    :rtype: List[List[int]]
    """
    def flip(l):            
        return list(map(lambda i: 0 if i == 1 else 1, l))[::-1]        
    return map(flip, A)

This method has improved performance but there is still scope for improvement for the invert function. This solution comes up to 26% percentile on leetcode

Time Complexity: O(N), where N is the total number of pixels

Space Complexity: O(1)

Approach 3: Improved invert

def flipAndInvertImage(self, A):
    """
    :type A: List[List[int]]
    :rtype: List[List[int]]
    """
    def flip(l):            
        return list(map(lambda i: 1-i, l))[::-1]        
    return map(flip, A)

This solution comes up to a solid 88% percentile, and the only difference between this and the solution at 100% is that the 100% solution avoid using 2 maps

Time Complexity: O(N), where N is the total number of pixels

Space Complexity: O(1)

As you can see that the performance improves regardless of the time complexity because of the additional overhead of the functions on the algorithm regardless of the time complexity, especially since the input size is so relatively small

Previous723. Candy CrushNext294. Flip Game II

Last updated 6 years ago