Given an array of integers nums
, you start with an initial positive value startValue__.
In each iteration, you calculate the step by step sum of startValue plus elements in nums
(from left to right).
Return the minimum positive value of startValue such that the step by step sum is never less than 1.
Example 1:
Input: nums = [-3,2,-3,4,2] Output: 5 Explanation: If you choose startValue = 4, in the third iteration your step by step sum is less than 1. step by step sum startValue = 4 | startValue = 5 | nums (4 -3 ) = 1 | (5 -3 ) = 2 | -3 (1 +2 ) = 3 | (2 +2 ) = 4 | 2 (3 -3 ) = 0 | (4 -3 ) = 1 | -3 (0 +4 ) = 4 | (1 +4 ) = 5 | 4 (4 +2 ) = 6 | (5 +2 ) = 7 | 2
Example 2:
Input: nums = [1,2] Output: 1 Explanation: Minimum start value should be positive.
Example 3:
Input: nums = [1,-2,-3] Output: 5
Constraints:
1 <= nums.length <= 100
-100 <= nums[i] <= 100
program minStartValue
implicit none
integer, parameter :: dp = selected_real_kind(15)
integer, dimension(:), allocatable :: nums
integer :: n, i, j, startValue, stepByStepSum
logical :: foundMin
! read input
read(*,*) n
allocate(nums(n))
do i = 1, n
read(*,*) nums(i)
end do
! find minimum positive start value
startValue = huge(startValue)
foundMin = .false.
do while (.not. foundMin)
stepByStepSum = startValue
do j = 1, n
if (nums(j) < 0) then
stepByStepSum = stepByStepSum + nums(j)
else
exit
end if
end do
if (stepByStepSum >= 1) then
foundMin = .true.
else
startValue = startValue - 1
end if
end do
! print result
write(*,*) startValue
end program minStartValue
At line 11 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x7f596d55d960 in ??? #1 0x7f596d55e4d9 in ??? #2 0x7f596d7b217b in ??? #3 0x7f596d7ab684 in ??? #4 0x7f596d7ac2aa in ??? #5 0x5b783c373272 in MAIN__ #6 0x5b783c3735a1 in main
module min_start_value
implicit none
private
public :: min_start_value
contains
function min_start_value(nums) result(min_value)
integer, intent(in) :: nums(:)
integer :: min_value
integer :: start_value, step_sum
integer :: i
min_value = 1
start_value = 1
do i = 1, size(nums)
step_sum = start_value + nums(i)
if (step_sum < 1) then
start_value = start_value + 1
end if
end do
min_value = start_value
end function min_start_value
end module min_start_value
program test_min_start_value
use min_start_value
implicit none
integer, parameter :: nums(2) = [1, 2]
integer :: min_value
min_value = min_start_value(nums)
write (*,*) "Minimum start value:", min_value
end program test_min_start_value
temp.f95:4:29: 4 | public :: min_start_value | 1 Error: PUBLIC attribute applied to MODULE min_start_value at (1) temp.f95:6:28: 6 | function min_start_value(nums) result(min_value) | 1 Error: MODULE attribute of βmin_start_valueβ conflicts with PROCEDURE attribute at (1) temp.f95:7:38: 7 | integer, intent(in) :: nums(:) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:8:28: 8 | integer :: min_value | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:9:40: 9 | integer :: start_value, step_sum | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:10:20: 10 | integer :: i | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:12:21: 12 | min_value = 1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:13:23: 13 | start_value = 1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:15:28: 15 | do i = 1, size(nums) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:16:44: 16 | step_sum = start_value + nums(i) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:17:34: 17 | if (step_sum < 1) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:18:45: 18 | start_value = start_value + 1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:19:15: 19 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:20:11: 20 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:22:31: 22 | min_value = start_value | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:23:7: 23 | end function min_start_value | 1 Error: Expecting END MODULE statement at (1) temp.f95:27:9: 27 | use min_start_value | 1 Fatal Error: Cannot open module file βmin_start_value.modβ for reading at (1): No such file or directory compilation terminated.
def maxSideLength(mat, threshold):
m, n = len(mat), len(mat[0])
dp = [[0] * (n + 1) for _ in range(m + 1)]
ans = 0
for i in range(1, m + 1):
for j in range(1, n + 1):
dp[i][j] = mat[i - 1][j - 1] + dp[i][j - 1] + dp[i - 1][j] - dp[i - 1][j - 1]
k = ans + 1
if i >= k and j >= k and dp[i][j] - dp[i][j - k] - dp[i - k][j] + dp[i - k][j - k] <= threshold:
ans += 1
return ans
We use dynamic programming to solve this problem. First, we create a 2D dp array of size (m+1) x (n+1) initialized with 0. The dp array stores the sum of all elements above and left of the current cell, including the current cell. Then, we iterate through each cell in the input matrix, and compute the sums for the dp array.
At each cell, we check if there is a possibility of extending the side length of the square. We do this by checking if the current cell's row and column are greater than or equal to the side length we are checking for (ans + 1). If true, we check if the sum of the square with side length (ans + 1) is less than or equal to the threshold. The sum is computed using the dp array's values by adding the top-left and bottom-right corners and subtracting the bottom-left and top-right corners. If the sum is within the threshold, we increment the ans variable.
Finally, we return the ans variable which stores the maximum side-length of a square with sum less than or equal to the threshold.
int maxSideLength(vector<vector<int>>& mat, int threshold) {
int m = mat.size();
int n = mat[0].size();
vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
int ans = 0;
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
dp[i][j] = mat[i-1][j-1] + dp[i][j-1] + dp[i-1][j] - dp[i-1][j-1];
int k = ans + 1;
if (i >= k && j >= k && dp[i][j] - dp[i][j-k] - dp[i-k][j] + dp[i-k][j-k] <= threshold) {
ans++;
}
}
}
return ans;
}
We use dynamic programming to solve this problem. First, we create a 2D dp array of size (m+1) x (n+1) initialized with 0. The dp array stores the sum of all elements above and left of the current cell, including the current cell. Then, we iterate through each cell in the input matrix, and compute the sums for the dp array.
At each cell, we check if there is a possibility of extending the side length of the square. We do this by checking if the current cell's row and column are greater than or equal to the side length we are checking for (ans + 1). If true, we check if the sum of the square with side length (ans + 1) is less than or equal to the threshold. The sum is computed using the dp array's values by adding the top-left and bottom-right corners and subtracting the bottom-left and top-right corners. If the sum is within the threshold, we increment the ans variable.
Finally, we return the ans variable which stores the maximum side-length of a square with sum less than or equal to the threshold.