You have a RecentCounter
class which counts the number of recent requests within a certain time frame.
Implement the RecentCounter
class:
RecentCounter()
Initializes the counter with zero recent requests.int ping(int t)
Adds a new request at time t
, where t
represents some time in milliseconds, and returns the number of requests that has happened in the past 3000
milliseconds (including the new request). Specifically, return the number of requests that have happened in the inclusive range [t - 3000, t]
.It is guaranteed that every call to ping
uses a strictly larger value of t
than the previous call.
Example 1:
Input [ "RecentCounter ", "ping ", "ping ", "ping ", "ping "] [[], [1], [100], [3001], [3002]] Output [null, 1, 2, 3, 3]
Explanation RecentCounter recentCounter = new RecentCounter(); recentCounter.ping(1); // requests = [1], range is [-2999,1], return 1 recentCounter.ping(100); // requests = [1, 100], range is [-2900,100], return 2 recentCounter.ping(3001); // requests = [1, 100, 3001], range is [1,3001], return 3 recentCounter.ping(3002); // requests = [1, 100, 3001, 3002], range is [2,3002], return 3
Constraints:
1 <= t <= 109
ping
with strictly increasing values of t
.104
calls will be made to ping
.program main
use, intrinsic :: iso_fortran_env, only : error_unit, iostat_end
implicit none
type(RecentCounter) :: recentCounter
integer :: t, n, i, status
character(len=:), allocatable :: line
! Read input from stdin
do
read (unit=input_unit, fmt='(A)', iostat=status) line
if (status == iostat_end) exit
if (status /= 0) then
write (unit=error_unit, fmt='(2A)') 'error: ', trim(line)
stop 1
end if
backspace input_unit
read (unit=input_unit, fmt=*) t
n = recentCounter%ping(t)
write (unit=output_unit, fmt='(I0)') n
end do
contains
type RecentCounter
integer :: count = 0
integer, dimension(:), pointer :: requests => null()
contains
procedure :: ping
end type RecentCounter
integer function ping(this, t) result(n)
class(RecentCounter), intent(inout) :: this
integer, value :: t
integer :: i, j
! Check if we need to reallocate the array
if (.not. associated(this%requests)) then
allocate(this%requests(100))
else if (size(this%requests) < size(this%requests) + 1) then
allocate(this%requests(size(this%requests)*2))
end if
! Increment the count
this%count = this%count + 1
! Shift the requests down by one position
do i = this%count, 2, -1
this%requests(i) = this%requests(i-1)
end do
! Store the new request
this%requests(1) = t
! Count the number of requests in the past 3000 ms
n = 0
do i = 1, this%count
if (this%requests(i) >= t - 3000) then
n = n + 1
end if
end do
end function ping
end program main
temp.f95:4:24: 4 | type(RecentCounter) :: recentCounter | 1 Error: Derived type ārecentcounterā at (1) is being used before it is defined temp.f95:18:27: 18 | n = recentCounter%ping(t) | 1 Error: Symbol ārecentcounterā at (1) has no IMPLICIT type temp.f95:24:22: 24 | type RecentCounter | 1 Error: Unexpected derived type declaration statement in CONTAINS section at (1) temp.f95:25:28: 25 | integer :: count = 0 | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:26:60: 26 | integer, dimension(:), pointer :: requests => null() | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:27:12: 27 | contains | 1 Error: Unexpected CONTAINS statement in CONTAINS section at (1) temp.f95:29:7: 29 | end type RecentCounter | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:32:29: 32 | class(RecentCounter), intent(inout) :: this | 1 Error: Derived type ārecentcounterā at (1) is being used before it is defined temp.f95:37:35: 37 | if (.not. associated(this%requests)) then | 1 Error: Symbol āthisā at (1) has no IMPLICIT type temp.f95:38:27: 38 | allocate(this%requests(100)) | 1 Error: Symbol āthisā at (1) has no IMPLICIT type temp.f95:39:28: 39 | else if (size(this%requests) < size(this%requests) + 1) then | 1 Error: Symbol āthisā at (1) has no IMPLICIT type temp.f95:40:27: 40 | allocate(this%requests(size(this%requests)*2)) | 1 Error: Symbol āthisā at (1) has no IMPLICIT type temp.f95:41:11: 41 | end if | 1 Error: Expecting END FUNCTION statement at (1) temp.f95:44:14: 44 | this%count = this%count + 1 | 1 Error: Symbol āthisā at (1) has no IMPLICIT type temp.f95:47:21: 47 | do i = this%count, 2, -1 | 1 Error: Symbol āthisā at (1) has no IMPLICIT type temp.f95:48:18: 48 | this%requests(i) = this%requests(i-1) | 1 Error: Symbol āthisā at (1) has no IMPLICIT type temp.f95:49:11: 49 | end do | 1 Error: Expecting END FUNCTION statement at (1) temp.f95:52:14: 52 | this%requests(1) = t | 1 Error: Symbol āthisā at (1) has no IMPLICIT type temp.f95:56:24: 56 | do i = 1, this%count | 1 Error: Symbol āthisā at (1) has no IMPLICIT type temp.f95:57:22: 57 | if (this%requests(i) >= t - 3000) then | 1 Error: Symbol āthisā at (1) has no IMPLICIT type temp.f95:59:15: 59 | end if | 1 Error: Expecting END FUNCTION statement at (1) temp.f95:60:11: 60 | end do | 1 Error: Expecting END FUNCTION statement at (1) temp.f95:31:30: 31 | integer function ping(this, t) result(n) | 1 Error: Symbol āthisā at (1) has no IMPLICIT type temp.f95:10:29: 10 | read (unit=input_unit, fmt='(A)', iostat=status) line | 1 Error: Symbol āinput_unitā at (1) has no IMPLICIT type; did you mean āerror_unitā? temp.f95:19:31: 19 | write (unit=output_unit, fmt='(I0)') n | 1 Error: Symbol āoutput_unitā at (1) has no IMPLICIT type
module recent_counter
implicit none
type :: RecentCounter
integer :: count = 0
integer :: last_ping = 0
contains
procedure :: ping
end type RecentCounter
contains
function ping(this, t) result(count)
class(RecentCounter), intent(inout) :: this
integer, intent(in) :: t
integer :: count
this%count = this%count + 1
this%last_ping = t
if (t - 3000 <= this%last_ping) then
count = this%count
else
count = 0
end if
end function ping
end module recent_counter
program main
use recent_counter
implicit none
integer :: t
type(RecentCounter) :: recent_counter
! Example 1
recent_counter = RecentCounter()
t = 1
write (*,*) recent_counter%ping(t)
t = 100
write (*,*) recent_counter%ping(t)
t = 3001
write (*,*) recent_counter%ping(t)
t = 3002
write (*,*) recent_counter%ping(t)
! Example 2
recent_counter = RecentCounter()
t = 1
write (*,*) recent_counter%ping(t)
t = 100
write (*,*) recent_counter%ping(t)
t = 3001
write (*,*) recent_counter%ping(t)
t = 3002
write (*,*) recent_counter%ping(t)
! Example 3
recent_counter = RecentCounter()
t = 1
write (*,*) recent_counter%ping(t)
t = 100
write (*,*) recent_counter%ping(t)
t = 3001
write (*,*) recent_counter%ping(t)
t = 3002
write (*,*) recent_counter%ping(t)
end program main
temp.f95:36:41: 36 | type(RecentCounter) :: recent_counter | 1 Error: Symbol āmainā at (1) cannot have a type temp.f95:39:19: 39 | recent_counter = RecentCounter() | 1 Error: ārecent_counterā at (1) is not a variable temp.f95:41:31: 41 | write (*,*) recent_counter%ping(t) | 1 Error: Symbol at (1) is not appropriate for an expression temp.f95:43:31: 43 | write (*,*) recent_counter%ping(t) | 1 Error: Symbol at (1) is not appropriate for an expression temp.f95:45:31: 45 | write (*,*) recent_counter%ping(t) | 1 Error: Symbol at (1) is not appropriate for an expression temp.f95:47:31: 47 | write (*,*) recent_counter%ping(t) | 1 Error: Symbol at (1) is not appropriate for an expression temp.f95:50:19: 50 | recent_counter = RecentCounter() | 1 Error: ārecent_counterā at (1) is not a variable temp.f95:52:31: 52 | write (*,*) recent_counter%ping(t) | 1 Error: Symbol at (1) is not appropriate for an expression temp.f95:54:31: 54 | write (*,*) recent_counter%ping(t) | 1 Error: Symbol at (1) is not appropriate for an expression temp.f95:56:31: 56 | write (*,*) recent_counter%ping(t) | 1 Error: Symbol at (1) is not appropriate for an expression temp.f95:58:31: 58 | write (*,*) recent_counter%ping(t) | 1 Error: Symbol at (1) is not appropriate for an expression temp.f95:61:19: 61 | recent_counter = RecentCounter() | 1 Error: ārecent_counterā at (1) is not a variable temp.f95:63:31: 63 | write (*,*) recent_counter%ping(t) | 1 Error: Symbol at (1) is not appropriate for an expression temp.f95:65:31: 65 | write (*,*) recent_counter%ping(t) | 1 Error: Symbol at (1) is not appropriate for an expression temp.f95:67:31: 67 | write (*,*) recent_counter%ping(t) | 1 Error: Symbol at (1) is not appropriate for an expression temp.f95:69:31: 69 | write (*,*) recent_counter%ping(t) | 1 Error: Symbol at (1) is not appropriate for an expression
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def increasingBST(root):
def inorder(node):
nonlocal prev
if not node: return
inorder(node.left)
prev.right = node
prev = node
node.left = None
inorder(node.right)
dummy = TreeNode(0)
prev = dummy
inorder(root)
return dummy.right
The algorithm uses a recursive inorder traversal of the given binary search tree. It starts by visiting the left child, if any, and then processes the root node, and finally the right child. The important part of this traversal is the processing of each node. When a node is encountered, its left pointer is set to null and its right pointer is set to the current node being processed.
The algorithm maintains a prev
variable to keep the previous node encountered during the inorder traversal. Initially, we have a dummy node, and during the traversal, we set the right pointer of the prev
node to the current node and update the prev
node to the current node.
Finally, after the traversal, the right child of the dummy node points to the root of the modified tree.
class TreeNode {
public:
int val;
TreeNode* left;
TreeNode* right;
};
void inorder(TreeNode* node, TreeNode*& prev) {
if (node == nullptr) return;
inorder(node->left, prev);
prev->right = node;
prev = node;
node->left = nullptr;
inorder(node->right, prev);
}
TreeNode* increasingBST(TreeNode* root) {
TreeNode dummy(0);
TreeNode* prev = &dummy;
inorder(root, prev);
return dummy.right;
}
The algorithm uses a recursive inorder traversal of the given binary search tree. It starts by visiting the left child, if any, and then processes the root node, and finally the right child. The important part of this traversal is the processing of each node. When a node is encountered, its left pointer is set to null and its right pointer is set to the current node being processed.
The algorithm maintains a prev
variable to keep the previous node encountered during the inorder traversal. Initially, we have a dummy node, and during the traversal, we set the right pointer of the prev
node to the current node and update the prev
node to the current node.
Finally, after the traversal, the right child of the dummy node points to the root of the modified tree.