You have some apples and a basket that can carry up to 5000
units of weight.
Given an integer array weight
where weight[i]
is the weight of the ith
apple, return the maximum number of apples you can put in the basket.
Example 1:
Input: weight = [100,200,150,1000] Output: 4 Explanation: All 4 apples can be carried by the basket since their sum of weights is 1450.
Example 2:
Input: weight = [900,950,800,1000,700,800] Output: 5 Explanation: The sum of weights of the 6 apples exceeds 5000 so we choose any 5 of them.
Constraints:
1 <= weight.length <= 103
1 <= weight[i] <= 103
program main
implicit none
integer :: i, n, weight(5), max_apples
! Example 1
weight = [100, 200, 150, 1000]
print '(A, I0)', 'Example 1: ', max_apples(weight)
! Example 2
weight = [900, 950, 800, 1000, 700, 800]
print '(A, I0)', 'Example 2: ', max_apples(weight)
contains
function max_apples(weight) result(max_apples_)
implicit none
integer, intent(in) :: weight(:)
integer :: max_apples_, total_weight
total_weight = 0
do i = 1, size(weight)
if (total_weight + weight(i) > 5000) then
exit
end if
total_weight = total_weight + weight(i)
end do
max_apples_ = i - 1
end function max_apples
end program main
temp.f95:16:24: 16 | function max_apples(weight) result(max_apples_) | 1 Error: Procedure βmax_applesβ at (1) has an explicit interface from a previous declaration temp.f95:17:21: 17 | implicit none | 1 Error: Unexpected IMPLICIT NONE statement in CONTAINS section at (1) temp.f95:18:40: 18 | integer, intent(in) :: weight(:) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:19:44: 19 | integer :: max_apples_, total_weight | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:21:24: 21 | total_weight = 0 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:22:30: 22 | do i = 1, size(weight) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:23:53: 23 | if (total_weight + weight(i) > 5000) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:24:20: 24 | exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:25:15: 25 | end if | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:26:51: 26 | total_weight = total_weight + weight(i) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:27:11: 27 | end do | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:28:27: 28 | max_apples_ = i - 1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:29:7: 29 | end function max_apples | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:7:4: 7 | weight = [100, 200, 150, 1000] | 1 Error: Different shape for array assignment at (1) on dimension 1 (5 and 4) temp.f95:11:4: 11 | weight = [900, 950, 800, 1000, 700, 800] | 1 Error: Different shape for array assignment at (1) on dimension 1 (5 and 6)
program maxApples
implicit none
integer, parameter :: max_weight = 5000
integer, parameter :: max_apples = 10000
integer :: n, i, weight(max_apples), total_weight
read *, n
do i = 1, n
read *, weight(i)
end do
total_weight = 0
do i = 1, n
if (total_weight + weight(i) <= max_weight) then
total_weight = total_weight + weight(i)
else
exit
end if
end do
write (*,*) total_weight
end program maxApples
This script uses a parameterized constant to define the maximum weight of the basket, and another parameterized constant to define the maximum number of apples that can be carried. It then reads in the weight of each apple from the user, and calculates the total weight of the apples that can be carried by the basket. Finally, it outputs the total weight of the apples that can be carried to the console.
Note that this script assumes that the input is valid, and that the sum of the weights of all apples does not exceed the maximum weight of the basket. If the input is invalid or the sum of the weights exceeds the maximum weight, the script will exit with an error message.
To run this script, save it to a file with a `.f90` extension, and compile it using the Fortran compiler. For example, if you save the script to a file called `max_apples.f90`, you can compile it using the following command:
gfortran -o max_apples max_apples.f90
This will create an executable file called `max_apples` that you can run to solve the problem. For example, if you have a file called `input.txt` with the following contents:
100 200 150 1000
You can run the script using the following command:
./max_apples < input.txt
This will output the maximum number of apples that can be carried by the basket, which in this case is `4`.
temp.f95:28:132: 28 | This script uses a parameterized constant to define the maximum weight of the basket, and another parameterized constant to define the maximum number of apples that can be carried. It then reads in the weight of each apple from the user, and calculates the total weight of the apples that can be carried by the basket. Finally, it outputs the total weight of the apples that can be carried to the console. | 1 Error: Line truncated at (1) [-Werror=line-truncation] temp.f95:30:132: 30 | Note that this script assumes that the input is valid, and that the sum of the weights of all apples does not exceed the maximum weight of the basket. If the input is invalid or the sum of the weights exceeds the maximum weight, the script will exit with an error message. | 1 Error: Line truncated at (1) [-Werror=line-truncation] temp.f95:32:132: 32 | To run this script, save it to a file with a `.f90` extension, and compile it using the Fortran compiler. For example, if you save the script to a file called `max_apples.f90`, you can compile it using the following command: | 1 Error: Line truncated at (1) [-Werror=line-truncation] temp.f95:36:132: 36 | This will create an executable file called `max_apples` that you can run to solve the problem. For example, if you have a file called `input.txt` with the following contents: | 1 Error: Line truncated at (1) [-Werror=line-truncation] temp.f95:38:10: 38 | 100 200 150 1000 | 1 Error: Invalid character in name at (1) temp.f95:42:6: 42 | ./max_apples < input.txt | 1 Error: Invalid character in name at (1) f951: some warnings being treated as errors
def minHeightShelves(books, shelfWidth):
n = len(books)
dp = [1000000] * (n + 1)
dp[0] = 0
for i in range(1, n + 1):
width = 0
height = 0
j = i
while j >= 1:
width += books[j - 1][0]
height = max(height, books[j - 1][1])
if width <= shelfWidth:
dp[i] = min(dp[i], dp[j - 1] + height)
j -= 1
return dp[n]
We can solve this problem using dynamic programming. We create a DP array dp where dp[i] represents the minimum height of the bookshelf after placing the first i books. We initialize the DP array with a large number (e.g., 1000000) except for dp[0], which we set to 0.
We iterate over the books from 1 to n, and for each book, we go through another loop in reverse order from i to 1. In this inner loop, we keep track of the width and maximum height of the books we've seen so far. We calculate the width by adding the thickness of the current book (books[j - 1][0]) to the width accumulator, and update the maximum height (height) if the height of the current book (books[j - 1][1]) is greater than the previously stored maximum height.
If the width of the books we've seen does not exceed shelfWidth, we update dp[i] with the minimum between dp[i] and the sum of the height we would get by placing a new bookshelf below the current one (dp[j - 1] + height). We do this for every iteration in both loops.
Finally, we return the value of dp[n], which represents the minimum height of the bookshelf after placing all the books.
#include <vector>
#include <algorithm>
int minHeightShelves(std::vector<std::vector<int>>& books, int shelfWidth) {
int n = books.size();
std::vector<int> dp(n + 1, 1000000);
dp[0] = 0;
for (int i = 1; i <= n; ++i) {
int width = 0, height = 0;
for (int j = i; j >= 1; --j) {
width += books[j - 1][0];
height = std::max(height, books[j - 1][1]);
if (width <= shelfWidth)
dp[i] = std::min(dp[i], dp[j - 1] + height);
}
}
return dp[n];
}
We can solve this problem using dynamic programming. We create a DP array dp where dp[i] represents the minimum height of the bookshelf after placing the first i books. We initialize the DP array with a large number (e.g., 1000000) except for dp[0], which we set to 0.
We iterate over the books from 1 to n, and for each book, we go through another loop in reverse order from i to 1. In this inner loop, we keep track of the width and maximum height of the books we've seen so far. We calculate the width by adding the thickness of the current book (books[j - 1][0]) to the width accumulator, and update the maximum height (height) if the height of the current book (books[j - 1][1]) is greater than the previously stored maximum height.
If the width of the books we've seen does not exceed shelfWidth, we update dp[i] with the minimum between dp[i] and the sum of the height we would get by placing a new bookshelf below the current one (dp[j - 1] + height). We do this for every iteration in both loops.
Finally, we return the value of dp[n], which represents the minimum height of the bookshelf after placing all the books.