just want to murmur…
I practice LeetCode with C#
I try #373 Find K Pairs with Smallest Sums, this requires the algorithm.
But i still tried brute-force 1st.
public static IList<int[]> KSmallestPairs(int[] nums1, int[] nums2, int k)
{
var list = nums1.Select(t => nums2.Select(y => new int[] { t, y }))
.SelectMany(t => t)
.OrderBy(t => t[0] + t[1]).ToList();
return list.Take(k).ToList();
}
This exceed time limit
public static IList<int[]> KSmallestPairs(int[] nums1, int[] nums2, int k)
{
var list = nums1.Select(t => nums2.Select(y => new int[] { t, y }))
.SelectMany(t => t)
.OrderBy(t => t[0] + t[1]);
return list.Take(k).ToList();
}
Only difference is ToList
I removed it, so it don’t need to compute the real value.
This passed more than 700ms, 12-13% .