Given a 2D integer array nums
where nums[i]
is a non-empty array of distinct positive integers, return the list of integers that are present in each array of nums
sorted in ascending order.
Example 1:
Input: nums = [[3,1,2,4,5],[1,2,3,4],[3,4,5,6]] Output: [3,4] Explanation: The only integers present in each of nums[0] = [3,1,2,4,5], nums[1] = [1,2,3,4], and nums[2] = [3,4,5,6] are 3 and 4, so we return [3,4].
Example 2:
Input: nums = [[1,2,3],[4,5,6]] Output: [] Explanation: There does not exist any integer present both in nums[0] and nums[1], so we return an empty list [].
Constraints:
1 <= nums.length <= 1000
1 <= sum(nums[i].length) <= 1000
1 <= nums[i][j] <= 1000
nums[i]
are unique.program main
implicit none
integer, parameter :: n = 3
integer, dimension(n) :: nums(n) = reshape([3, 1, 2, 4, 5, 1, 2, 3, 4, 3, 4, 5, 6], shape(nums))
integer, allocatable :: result(:)
call solve(nums, result)
write (*, '(*(g0, :, ", "))') result
contains
subroutine solve(nums, result)
implicit none
integer, intent(in) :: nums(:, :)
integer, allocatable, intent(out) :: result(:)
integer :: i, j, k
logical :: found
allocate(result(size(nums)))
do i = 1, size(nums)
found = .false.
do j = 1, size(nums)
if (all(nums(i) == nums(j))) then
found = .true.
exit
end if
end do
if (.not. found) then
k = k + 1
result(k) = nums(i)(1)
end if
end do
result = sort(result)
end subroutine solve
end program main
temp.f95:33:17: 33 | result(k) = nums(i)(1) | 1 Error: Unclassifiable statement at (1) temp.f95:26:39: 26 | if (all(nums(i) == nums(j))) then | 1 Error: Rank mismatch in array reference at (1) (1/2) temp.f95:37:17: 37 | result = sort(result) | 1 Error: Function โsortโ at (1) has no IMPLICIT type temp.f95:8:15: 8 | call solve(nums, result) | 1 Error: Rank mismatch in argument โnumsโ at (1) (rank-2 and rank-1)
program main
implicit none
integer, parameter :: n = 3
integer, parameter :: m = 5
integer, parameter :: nums(n, m) = reshape((/ 3, 1, 2, 4, 5, 1, 2, 3, 4, 3, 4, 5, 6 /), shape(nums))
integer :: i, j
integer :: result(size(nums, 1))
result = solve(nums)
do i = 1, size(nums, 1)
write (*, '(A, I0, A)') 'nums(', i, ') = ', result(i)
end do
contains
function solve(nums) result(result)
integer, intent(in) :: nums(n, m)
integer :: result(size(nums, 1))
integer :: i, j
integer :: count(size(nums, 2))
count = 0
do i = 1, size(nums, 1)
do j = 1, size(nums, 2)
if (count(nums(i, j)) == 0) then
count(nums(i, j)) = 1
else
count(nums(i, j)) = count(nums(i, j)) + 1
end if
end do
end do
result = pack(count, count > 0)
end function solve
end program main
temp.f95:6:49: 6 | integer, parameter :: nums(n, m) = reshape((/ 3, 1, 2, 4, 5, 1, 2, 3, 4, 3, 4, 5, 6 /), shape(nums)) | 1 Error: Without padding, there are not enough elements in the intrinsic RESHAPE source at (1) to match the shape temp.f95:9:29: 9 | integer :: result(size(nums, 1)) | 1 Error: Symbol โnumsโ is used before it is typed at (1) temp.f95:14:59: 14 | write (*, '(A, I0, A)') 'nums(', i, ') = ', result(i) | 1 Error: Syntax error in WRITE statement at (1) temp.f95:11:25: 11 | result = solve(nums) | 1 Error: Symbol โnumsโ at (1) has no IMPLICIT type temp.f95:11:12: 11 | result = solve(nums) | 1 Error: Symbol โresultโ at (1) has no IMPLICIT type
def min_cost(cost):
cost.sort(reverse=True)
n = len(cost)
min_cost = 0
for i in range(0, n, 3):
min_cost += cost[i]
if i+1 < n:
min_cost += cost[i+1]
return min_cost
The algorithm sorts all the candies in the array in descending order of cost, so that the most expensive candies are bought first. Then, it loops over the array, taking two candies at a time (the loop increments 3 positions each iteration) and adding their cost to the total min_cost
. The choice of the third free candy will always be one of the two already chosen, so its cost is already included in min_cost
. After iterating through the entire array, the function returns the minimum cost.
#include <vector>
#include <algorithm>
int minCost(std::vector<int>& cost) {
std::sort(cost.begin(), cost.end(), std::greater<>());
int n = cost.size();
int min_cost = 0;
for(int i = 0; i < n; i += 3) {
min_cost += cost[i];
if(i+1 < n) min_cost += cost[i+1];
}
return min_cost;
}
The algorithm sorts all the candies in the array in descending order of cost, so that the most expensive candies are bought first. Then, it loops over the array, taking two candies at a time (the loop increments 3 positions each iteration) and adding their cost to the total min_cost
. The choice of the third free candy will always be one of the two already chosen, so its cost is already included in min_cost
. After iterating through the entire array, the function returns the minimum cost.