Given an integer array nums
sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order.
Example 1:
Input: nums = [-4,-1,0,3,10] Output: [0,1,9,16,100] Explanation: After squaring, the array becomes [16,1,0,9,100]. After sorting, it becomes [0,1,9,16,100].
Example 2:
Input: nums = [-7,-3,2,3,11] Output: [4,9,9,49,121]
Constraints:
1 <= nums.length <= 104
-104 <= nums[i] <= 104
nums
is sorted in non-decreasing order.Follow up: Squaring each element and sorting the new array is very trivial, could you find an O(n)
solution using a different approach?
program main
implicit none
integer, parameter :: n = 5
integer :: nums(n) = [-4, -1, 0, 3, 10]
integer :: i, j
integer :: squares(n)
do i = 1, n
squares(i) = nums(i) * nums(i)
end do
call sort_array(squares)
write (*, '(A)') 'Squares:'
do i = 1, n
write (*, '(I0)', advance='no') squares(i)
end do
write (*, *)
contains
subroutine sort_array(arr)
implicit none
integer, intent(inout) :: arr(:)
integer :: temp
integer :: i, j
do i = 1, size(arr)-1
do j = i+1, size(arr)
if (arr(j) < arr(i)) then
temp = arr(j)
arr(j) = arr(i)
arr(i) = temp
end if
end do
end do
end subroutine sort_array
end program main
Squares: 01916100
! This script solves the problem of finding the squares of a sorted array of integers.
! Input:
! nums: an integer array sorted in non-decreasing order
! Output:
! an array of the squares of each number sorted in non-decreasing order
! Example 1:
! Input: nums = [-4, -1, 0, 3, 10]
! Output: [0, 1, 9, 16, 100]
! Example 2:
! Input: nums = [-7, -3, 2, 3, 11]
! Output: [4, 9, 9, 49, 121]
! Constraints:
! 1 <= nums.length <= 104
! -104 <= nums[i] <= 104
! nums is sorted in non-decreasing order.
! Follow up:
! Squaring each element and sorting the new array is very trivial, could you find an O(n) solution using a different approach?
! Declare variables
integer :: i, j, n, num, square
integer, dimension(:), allocatable :: nums, squares
! Read input
read (*,*) n
allocate(nums(n))
read (*,*) nums
! Initialize output array
allocate(squares(n))
! Loop through input array and calculate squares
do i = 1, n
num = nums(i)
square = num * num
squares(i) = square
end do
! Sort output array
call sort(squares)
! Print output
do i = 1, n
write (*,*) squares(i)
end do
contains
! Sorting subroutine
subroutine sort(arr)
integer, dimension(:), intent(inout) :: arr
integer :: i, j, temp
do i = 1, size(arr) - 1
do j = i + 1, size(arr)
if (arr(i) > arr(j)) then
temp = arr(i)
arr(i) = arr(j)
arr(j) = temp
end if
end do
end do
end subroutine sort
end program
At line 30 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x7dbc22aaf960 in ??? #1 0x7dbc22ab04d9 in ??? #2 0x7dbc22d0417b in ??? #3 0x7dbc22cfd684 in ??? #4 0x7dbc22cfe2aa in ??? #5 0x57be89b3e50f in MAIN__ #6 0x57be89b3e9ab in main
def distinctSubseqII(s):
mod = 10**9 + 7
n = len(s)
dp = [0] * (n + 1)
dp[0] = 1
last = [-1] * 26
for i in range(1, n + 1):
dp[i] = (dp[i - 1] * 2) % mod
if last[ord(s[i - 1]) - ord('a')] != -1:
dp[i] = (dp[i] - dp[last[ord(s[i - 1]) - ord('a')]] + mod) % mod
last[ord(s[i - 1]) - ord('a')] = i - 1
dp[n] = (dp[n] - 1 + mod) % mod
return dp[n]
dp
with length n+1
to store the number of distinct subsequences.last
, with length 26, to store the last occurrences of each letter 'a'-'z'.i
ranging from 1
to n
.-1
, subtract the number of distinct subsequences at the index of the last occurrence from the current dp[i] value.n
(removes the empty subsequence) and return the result modulo 1e9+7.#include <iostream>
#include <string>
int distinctSubseqII(std::string s) {
int mod = 1e9 + 7;
int n = s.size();
std::vector<int> dp(n + 1);
dp[0] = 1;
std::vector<int> last(26, -1);
for (int i = 1; i <= n; ++i) {
dp[i] = (dp[i - 1] * 2) % mod;
if (last[s[i - 1] - 'a'] != -1) {
dp[i] = (dp[i] - dp[last[s[i - 1] - 'a']] + mod) % mod;
}
last[s[i - 1] - 'a'] = i - 1;
}
dp[n] = (dp[n] - 1 + mod) % mod;
return dp[n];
}
dp
with length n+1
to store the number of distinct subsequences.last
, with length 26, to store the last occurrences of each letter 'a'-'z'.i
ranging from 1
to n
.-1
, subtract the number of distinct subsequences at the index of the last occurrence from the current dp[i] value.n
(removes the empty subsequence) and return the result modulo 1e9+7.