Python is one of the most powerful programming languages. It gives us various unique features and functionalities that make it easy for us to write code.
In this article we'll solve Leetcode array problems in one line using one of Python's most interesting features – List Comprehension.
What is List Comprehension?
Before going into the problems, let's make sure we understand what list comprehension is all about.
A list comprehension is a syntactic construct available in some programming languages for creating a list based on existing lists
Let's see how list comprehension works with an example.
Consider an array of numbers. Our task is to add 1 to the numbers at odd indices and to add 2 to the number at even indices.
Now we'll see how to solve the above problem using both a for-loop and list comprehension.
How to solve the problem with a for-loop
def addOneAndTwo(nums, n): for i in range(n): if i % 2 == 1: nums[i] += 1 else: nums[i] += 2 return nums
How to solve it with list comprehension
def addOneAndTwo(nums, n): return [nums[i] + 1 if i % 2 == 1 else nums[i] + 2 for i in range(n)]
You can see how the solution using list comprehension is simplified from 6 lines to 1 line. This is the power of list comprehension.
How to Solve Leetcode Problems with List Comprehension
Now let us solve the below Leetcode problems in 1 line using list comprehension.
Here's the problem from Leetcode:
Given the array
nums consisting of
2n elements in the form
[x1,x2,...,xn,y1,y2,...,yn]. Return the array in the form
Input: nums = [2,5,1,3,4,7], n = 3
Explanation: Since x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 then the answer is [2,3,5,4,1,7].
def shuffle(self, nums, n): return reduce(lambda a, b: a + b, [[nums[i], nums[j]] for i, j in zip(range(0, n), range(n, 2 * n))])
Given an array of integers
nums. A pair
(i,j) is called good if
j.Return the number of good pairs.
Input: nums = [1,2,3,1,1,3]
Explanation: There are 4 good pairs (0,3), (0,4), (3,4), (2,5) 0-indexed.
def numIdenticalPairs(self, nums): return sum([int(i != j and nums[i] == nums[j]) for i in range(0, len(nums)) for j in range(i + 1, len(nums))])
Given the array
candies and the integer
candies[i] represents the number of candies that the ith kid has.
For each kid check if there is a way to distribute
extraCandies among the kids such that they can have the greatest number of candies among them. Notice that multiple kids can have the greatest number of candies.
Input: candies = [2,3,5,1,3], extraCandies = 3
Explanation: Kid 1 has 2 candies, and if they receive all extra candies (3) they will have 5 candies – the greatest number of candies among the kids.
Kid 2 has 3 candies, and if they receive at least 2 extra candies then they will have the greatest number of candies among the kids.
Kid 3 has 5 candies, and this is already the greatest number of candies among the kids.
Kid 4 has 1 candy, and even if they receive all extra candies they will only have 4 candies.
Kid 5 has 3 candies, and if they receive at least 2 extra candies then they will have the greatest number of candies among the kids.
def kidsWithCandies(self, candies, extraCandies): return [candy + extraCandies >= max(candies) for candy in candies]
We are given a list
nums of integers representing a list compressed with run-length encoding.
Consider each adjacent pair of elements
[freq, val] = [nums[2*i], nums[2*i+1]] (with
i >= 0). For each such pair, there are
freq elements with value
val concatenated in a sublist. Concatenate all the sublists from left to right to generate the decompressed list.
Return the decompressed list.
Input: nums = [1,2,3,4]
Explanation: The first pair [1,2] means we have freq = 1 and val = 2 so we generate the array .
The second pair [3,4] means we have freq = 3 and val = 4 so we generate [4,4,4]. At the end the concatenation  + [4,4,4] is [2,4,4,4].
def decompressRLElist(self, nums): return reduce(lambda a, b: a + b, [[nums[i + 1]] * nums[i] for i in range(0, len(nums), 2)])
You are given an
m x n integer grid
accounts[i][j] is the amount of money the
ith customer has in the
jth bank. Return the wealth that the richest customer has.
A customer's wealth is the amount of money they have in all their bank accounts. The richest customer is the customer that has the maximum wealth.
Input: accounts = [[1,2,3],[3,2,1]]
1st customer has wealth = 1 + 2 + 3 = 6 2nd customer has wealth = 3 + 2 + 1 = 6 Both customers are considered the richest with a wealth of 6 each, so return 6.
def maximumWealth(self, accounts): return max([sum(row) for row in accounts])