You are given an array rectangles where rectangles[i] = [li, wi] represents the ith rectangle of length li and width wi.
You can cut the ith rectangle to form a square with a side length of k if both k <= li and k <= wi. For example, if you have a rectangle [4,6], you can cut it to get a square with a side length of at most 4.
Let maxLen be the side length of the largest square you can obtain from any of the given rectangles.
Return the number of rectangles that can make a square with a side length of maxLen.
Example 1:
Input: rectangles = [[5,8],[3,9],[5,12],[16,5]] Output: 3 Explanation: The largest squares you can get from each rectangle are of lengths [5,3,5,5]. The largest possible square is of length 5, and you can get it out of 3 rectangles.
Example 2:
Input: rectangles = [[2,3],[3,7],[4,3],[3,7]] Output: 3
Constraints:
1 <= rectangles.length <= 1000rectangles[i].length == 21 <= li, wi <= 109li != wiprogram main
implicit none
integer, parameter :: n = 4
integer, dimension(n) :: rectangles
integer :: i, k, maxLen, count
! Examples
rectangles(1) = [5, 8]
rectangles(2) = [3, 9]
rectangles(3) = [5, 12]
rectangles(4) = [16, 5]
! Output
write (*, '(A)') 'Example 1:'
call solve(rectangles, maxLen, count)
write (*, '(I0, A, I0)') count, ' rectangles can make a square with a side length of ', maxLen
write (*, '(A)') ''
rectangles(1) = [2, 3]
rectangles(2) = [3, 7]
rectangles(3) = [4, 3]
rectangles(4) = [3, 7]
! Output
write (*, '(A)') 'Example 2:'
call solve(rectangles, maxLen, count)
write (*, '(I0, A, I0)') count, ' rectangles can make a square with a side length of ', maxLen
write (*, '(A)') ''
contains
subroutine solve(rectangles, maxLen, count)
implicit none
integer, intent(in) :: rectangles(:, :)
integer, intent(out) :: maxLen, count
integer :: i, j, k, l, m, n, p, q, r, s, t, u, v, w, x, y, z
maxLen = -huge(maxLen)
count = 0
do i = 1, size(rectangles, 1)
do j = 1, size(rectangles, 2)
k = rectangles(i, 1)
l = rectangles(i, 2)
m = min(k, l)
n = max(k, l)
p = (m + 1) / 2
q = (n + 1) / 2
r = (p * q) / 2
s = (r + 1) / 2
t = (s + 1) / 2
u = (t + 1) / 2
v = (u + 1) / 2
w = (v + 1) / 2
x = (w + 1) / 2
y = (x + 1) / 2
z = (y + 1) / 2
if (z > maxLen) then
maxLen = z
count = 1
else if (z == maxLen) then
count = count + 1
end if
end do
end do
end subroutine solve
end program main
temp.f95:9:4:
9 | rectangles(1) = [5, 8]
| 1
Error: Incompatible ranks 0 and 1 in assignment at (1)
temp.f95:10:4:
10 | rectangles(2) = [3, 9]
| 1
Error: Incompatible ranks 0 and 1 in assignment at (1)
temp.f95:11:4:
11 | rectangles(3) = [5, 12]
| 1
Error: Incompatible ranks 0 and 1 in assignment at (1)
temp.f95:12:4:
12 | rectangles(4) = [16, 5]
| 1
Error: Incompatible ranks 0 and 1 in assignment at (1)
temp.f95:16:15:
16 | call solve(rectangles, maxLen, count)
| 1
Error: Rank mismatch in argument ‘rectangles’ at (1) (rank-2 and rank-1)
temp.f95:20:4:
20 | rectangles(1) = [2, 3]
| 1
Error: Incompatible ranks 0 and 1 in assignment at (1)
temp.f95:21:4:
21 | rectangles(2) = [3, 7]
| 1
Error: Incompatible ranks 0 and 1 in assignment at (1)
temp.f95:22:4:
22 | rectangles(3) = [4, 3]
| 1
Error: Incompatible ranks 0 and 1 in assignment at (1)
temp.f95:23:4:
23 | rectangles(4) = [3, 7]
| 1
Error: Incompatible ranks 0 and 1 in assignment at (1)
temp.f95:27:15:
27 | call solve(rectangles, maxLen, count)
| 1
Error: Rank mismatch in argument ‘rectangles’ at (1) (rank-2 and rank-1)
module SquareWithMaxSide
implicit none
private
public :: squareWithMaxSide
contains
function squareWithMaxSide(rectangles) result(count)
integer, intent(in) :: rectangles(:,:)
integer :: count, i, k, maxLen, li, wi
count = 0
maxLen = 0
do i = 1, size(rectangles, 1)
li = rectangles(i, 1)
wi = rectangles(i, 2)
k = min(li, wi)
if (k > maxLen) then
maxLen = k
count = 1
else if (k == maxLen) then
count = count + 1
end if
end do
end function squareWithMaxSide
end module SquareWithMaxSide
program test_squareWithMaxSide
use SquareWithMaxSide
implicit none
integer, parameter :: rectangles(4, 2) = reshape([5, 8, 3, 9, 5, 12, 16, 5], [4, 2])
integer :: count
count = squareWithMaxSide(rectangles)
write (*, '(A, I0)') 'The number of rectangles that can make a square with a side length of ', maxLen
end program test_squareWithMaxSide
temp.f95:4:31:
4 | public :: squareWithMaxSide
| 1
Error: PUBLIC attribute applied to MODULE squarewithmaxside at (1)
temp.f95:6:30:
6 | function squareWithMaxSide(rectangles) result(count)
| 1
Error: MODULE attribute of ‘squarewithmaxside’ conflicts with PROCEDURE attribute at (1)
temp.f95:7:46:
7 | integer, intent(in) :: rectangles(:,:)
| 1
Error: Unexpected data declaration statement in CONTAINS section at (1)
temp.f95:8:46:
8 | integer :: count, i, k, maxLen, li, wi
| 1
Error: Unexpected data declaration statement in CONTAINS section at (1)
temp.f95:10:17:
10 | count = 0
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:11:18:
11 | maxLen = 0
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:13:37:
13 | do i = 1, size(rectangles, 1)
| 1
Error: Unexpected DO statement in CONTAINS section at (1)
temp.f95:14:33:
14 | li = rectangles(i, 1)
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:15:33:
15 | wi = rectangles(i, 2)
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:17:27:
17 | k = min(li, wi)
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:19:32:
19 | if (k > maxLen) then
| 1
Error: Unexpected block IF statement in CONTAINS section at (1)
temp.f95:20:26:
20 | maxLen = k
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:21:25:
21 | count = 1
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:22:38:
22 | else if (k == maxLen) then
| 1
Error: Unexpected ELSE IF statement in CONTAINS section at (1)
temp.f95:23:33:
23 | count = count + 1
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:24:15:
24 | end if
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:25:11:
25 | end do
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:26:7:
26 | end function squareWithMaxSide
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:30:9:
30 | use SquareWithMaxSide
| 1
Fatal Error: Cannot open module file ‘squarewithmaxside.mod’ for reading at (1): No such file or directory
compilation terminated.
def numberOfSets(n, k):
mod = 10**9 + 7
dp = [[0] * (k + 1) for _ in range(n)]
presum = [1] * n
for j in range(1, k + 1):
for i in range(n):
dp[i][j] = presum[i]
if i > 0:
dp[i][j] += dp[i - 1][j]
dp[i][j] %= mod
presum[i] = (presum[i] + dp[i][j - 1]) % mod
return dp[n - 1][k]
The problem can be solved using dynamic programming. We create a 2D array dp of dimensions (n, k + 1), where dp[i][j] stores the number of ways to draw j non-overlapping line segments up to point i. We also maintain an array presum which is the prefix sum of the dp array.
We then loop through j from 1 to k and i from 0 to n-1, calculating dp[i][j] using the values in presum and dp arrays. To avoid recomputing values, we update the presum array iteratively.
Finally, we return the value in dp[n - 1][k] which is the number of ways to draw k non-overlapping line segments up to point n-1.
int numberOfSets(int n, int k) {
long long mod = 1e9 + 7;
vector<vector<long long>> dp(n, vector<long long>(k + 1, 0));
vector<long long> presum(n, 1);
for (int j = 1; j <= k; ++j) {
for (int i = 0; i < n; ++i) {
dp[i][j] = presum[i];
if (i) {
dp[i][j] += dp[i - 1][j];
}
dp[i][j] %= mod;
presum[i] = (presum[i] + dp[i][j - 1]) % mod;
}
}
return dp[n - 1][k];
}
The problem can be solved using dynamic programming. We create a 2D array dp of dimensions (n, k + 1), where dp[i][j] stores the number of ways to draw j non-overlapping line segments up to point i. We also maintain an array presum which is the prefix sum of the dp array.
We then loop through j from 1 to k and i from 0 to n-1, calculating dp[i][j] using the values in presum and dp arrays. To avoid recomputing values, we update the presum array iteratively.
Finally, we return the value in dp[n - 1][k] which is the number of ways to draw k non-overlapping line segments up to point n-1.