I am solving a leet code problem. I found one question in leetcode, which are below
You are given an m x n
integer grid accounts where 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.
Example 1:
Input: accounts = [[1,5],[7,3],[3,5]]
Output: 10
Explanation:
1st customer has wealth = 6
2nd customer has wealth = 10
3rd customer has wealth = 8
The 2nd customer is the richest with a wealth of 10.
I tried my solution which is fine
class Solution {
fun maximumWealth(accounts: Array<IntArray>): Int {
var highestSum = 0
for (i in 0 until accounts.size) {
var sum = 0
for (j in 0 until accounts[i].size) {
sum = accounts[i][j]
}
if (sum > highestSum) {
highestSum = sum
}
}
return highestSum
}
}
I went to discuss section and I found two new solution. One is using map and another one is maxBy
MAP
accounts.map { it.sum() }.max()
MAXBY
accounts.maxBy { it.sum() }?.sum() ?: 0
I am curious which solution is better Is mine or any of this two or all are same? I know my solution Time complexity: O (M x N )
and Space complexity: O(1)
.
- So which one is better?
- Someone explain me how map is working?
Many Thanks
CodePudding user response:
For 2, you have an array whose elements are themselves arrays. Remember that map
applies the function you give it to each item. So,the array of arrays is transformed into an array of the sums of each array and then you find the maximum of those.
CodePudding user response:
map
takes a collection of items, does something to each item and puts the result in a new collection, producing another collection with the same number of items
So in your example, each item is an array of Int
s. What do you do with each array? You're running sum
on it, which basically adds up all those ints, and your result is the total - each value in your collection is being mapped to another value
[1,5] --mapping function--> 6
[7,3] --mapping function--> 10
[3,5] --mapping function--> 8
If it helps, the mathematical concept here (the simple version, I'm no expert in this stuff!) is that one one side, you have the space of all possible input values. In this case, all the possible Int
arrays that could exist. On the other side, you have the space of all the Int
values that represent the possible totals for all those arrays
The mapping function is there to map every possible value in the input space, to its corresponding value in the output space. You give it an input value, and it points at a specific output value. That's where the term map comes from.
It's like a keyboard mapping in a game - you set it up so that when you press a certain input key, that's mapped to a specific event. The overall mapping is all the input keys that could be used, and what they should be translated to. A mapping function is like that, except your set of possible inputs and outputs can be a lot larger! But it still defines a specific result for each possible input. And when you call map
, you're using that function to transform each of your inputs into its corresponding output