Given head which is a reference node to a singly-linked list. The value of each node in the linked list is either 0 or 1. The linked list holds the binary representation of a number.
Return the decimal value of the number in the linked list.
The most significant bit is at the head of the linked list.
Example 1:
Input: head = [1,0,1] Output: 5 Explanation: (101) in base 2 = (5) in base 10
Example 2:
Input: head = [0] Output: 0
Constraints:
30.0 or 1.program main
! Solves the problem described in the comment block
implicit none
type :: Node
integer :: value
type(Node), pointer :: next => null()
end type
type(Node), pointer :: head => null(), current => null()
integer :: i, n, dec_val
! Read input
read(*,*) n
allocate(head)
head%value = 0
do i=2,n+1
allocate(current)
current%value = 0
if (i <= n) then
read(*,*) current%value
end if
current%next => head
head => current
end do
! Solve problem
dec_val = solve(head)
! Output result
write(*,*) dec_val
contains
function solve(head) result(dec_val)
implicit none
type(Node), pointer :: head
integer :: dec_val
dec_val = 0
do while (associated(head))
dec_val = dec_val * 2 + head%value
head => head%next
end do
end function
end program
At line 14 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x780ee390a960 in ??? #1 0x780ee390b4d9 in ??? #2 0x780ee3b5f17b in ??? #3 0x780ee3b58684 in ??? #4 0x780ee3b592aa in ??? #5 0x5c82c1bfb287 in MAIN__ #6 0x5c82c1bfb4dd in main
module problem_172
implicit none
type :: node
integer :: value
type(node), pointer :: next => null()
end type
interface
function solve(head) result(result)
type(node), pointer :: head
integer :: result
end function
end interface
contains
function solve(head) result(result)
type(node), pointer :: head
integer :: result, value
! Initialize the result to 0
result = 0
! Loop through the linked list
do while (associated(head))
! Get the value of the current node
value = head%value
! Shift the result to the left by 1 bit
result = result * 2
! Add the value of the current node to the result
result = result + value
! Move to the next node
head => head%next
end do
end function
end module
program main
use problem_172
implicit none
type(node), pointer :: head
integer :: result
! Create a linked list with the binary representation of the number 5
head => null()
allocate(head%next)
head%next%value = 1
allocate(head%next%next)
head%next%next%value = 0
allocate(head%next%next%next)
head%next%next%next%value = 1
! Solve the problem
result = solve(head)
! Print the result
write (*,*) "The decimal value of the linked list is: ", result
! Create a linked list with the binary representation of the number 0
head => null()
allocate(head%next)
head%next%value = 0
! Solve the problem
result = solve(head)
! Print the result
write (*,*) "The decimal value of the linked list is: ", result
end program
temp.f95:12:19:
12 | type(node), pointer :: head
| 1
Error: Derived type ‘node’ at (1) is being used before it is defined
temp.f95:19:15:
11 | function solve(head) result(result)
| 2
......
19 | function solve(head) result(result)
| 1
Error: Procedure ‘solve’ at (1) is already defined at (2)
temp.f95:20:31:
20 | type(node), pointer :: head
| 1
Error: Unexpected data declaration statement in CONTAINS section at (1)
temp.f95:21:28:
21 | integer :: result, value
| 1
Error: Unexpected data declaration statement in CONTAINS section at (1)
temp.f95:24:14:
24 | result = 0
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:27:31:
27 | do while (associated(head))
| 1
Error: Unexpected DO statement in CONTAINS section at (1)
temp.f95:29:15:
29 | value = head%value
| 1
Error: Syntax error in VALUE statement at (1)
temp.f95:32:27:
32 | result = result * 2
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:35:31:
35 | result = result + value
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:38:22:
38 | head => head%next
| 1
Error: Symbol ‘head’ at (1) has no IMPLICIT type
temp.f95:39:7:
39 | end do
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:41:3:
41 | end function
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:46:9:
46 | use problem_172
| 1
Fatal Error: Cannot open module file ‘problem_172.mod’ for reading at (1): No such file or directory
compilation terminated.
def min_operations(arr1, arr2):
n = len(arr1)
dp = [float('inf')] * n
for a in arr2:
new_dp = [float('inf')] * n
p = 0
for i in range(n):
if a < arr1[i]:
new_dp[i] = p
if i > 0 and dp[i - 1] < p:
p = dp[i - 1]
if arr1[i] > arr1[i + 1]:
return -1
dp = new_dp
return dp[-1]
dp array with the same length as arr1, fill it with infinity.arr2 (as a).dp array called new_dp.p to track the minimum number of operations needed to make the subarray arr1[0:i] strictly increasing.arr1 (as i).a is smaller than the current element in arr1, update new_dp[i] with the minimum operations p.dp[i-1] is smaller than the current minimum operations, update p.arr1, swap dp with new_dp.dp array (minimum operations needed to make the entire arr1 strictly increasing).#include <vector>
#include <limits>
int minOperations(std::vector<int>& arr1, std::vector<int>& arr2) {
int n = arr1.size();
std::vector<int> dp(n, std::numeric_limits<int>::max());
for (int a : arr2) {
std::vector<int> new_dp(n, std::numeric_limits<int>::max());
int p = 0;
for (int i = 0; i < n; ++i) {
if (a < arr1[i]) new_dp[i] = p;
if (i > 0 && dp[i - 1] < p) p = dp[i - 1];
if (arr1[i] > arr1[i + 1]) return -1;
}
dp.swap(new_dp);
}
return dp.back();
}
dp array with the same length as arr1, fill it with infinity.arr2 (as a).dp array called new_dp.p to track the minimum number of operations needed to make the subarray arr1[0:i] strictly increasing.arr1 (as i).a is smaller than the current element in arr1, update new_dp[i] with the minimum operations p.dp[i-1] is smaller than the current minimum operations, update p.arr1, swap dp with new_dp.dp array (minimum operations needed to make the entire arr1 strictly increasing).