Given an array of integers nums, return the number of good pairs.
A pair (i, j) is called good if nums[i] == nums[j] and i < j.
Example 1:
Input: nums = [1,2,3,1,1,3] Output: 4 Explanation: There are 4 good pairs (0,3), (0,4), (3,4), (2,5) 0-indexed.
Example 2:
Input: nums = [1,1,1,1] Output: 6 Explanation: Each pair in the array are good.
Example 3:
Input: nums = [1,2,3] Output: 0
Constraints:
1 <= nums.length <= 1001 <= nums[i] <= 100program main
implicit none
integer, parameter :: n = 6
integer, dimension(n) :: nums
integer :: i, j
integer :: good_pairs
! Example 1
nums = [1, 2, 3, 1, 1, 3]
write (*, '(a, i0)') 'Example 1: ', good_pairs(nums)
! Example 2
nums = [1, 1, 1, 1]
write (*, '(a, i0)') 'Example 2: ', good_pairs(nums)
! Example 3
nums = [1, 2, 3]
write (*, '(a, i0)') 'Example 3: ', good_pairs(nums)
contains
function good_pairs(nums) result(count)
implicit none
integer, dimension(:), intent(in) :: nums
integer :: count
integer :: i, j
count = 0
do i = 1, size(nums) - 1
do j = i + 1, size(nums)
if (nums(i) == nums(j)) then
count = count + 1
end if
end do
end do
end function good_pairs
end program main
temp.f95:23:24:
23 | function good_pairs(nums) result(count)
| 1
Error: Procedure âgood_pairsâ at (1) has an explicit interface from a previous declaration
temp.f95:24:21:
24 | implicit none
| 1
Error: Unexpected IMPLICIT NONE statement in CONTAINS section at (1)
temp.f95:25:49:
25 | integer, dimension(:), intent(in) :: nums
| 1
Error: Unexpected data declaration statement in CONTAINS section at (1)
temp.f95:26:24:
26 | integer :: count
| 1
Error: Unexpected data declaration statement in CONTAINS section at (1)
temp.f95:27:23:
27 | integer :: i, j
| 1
Error: Unexpected data declaration statement in CONTAINS section at (1)
temp.f95:29:17:
29 | count = 0
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:30:32:
30 | do i = 1, size(nums) - 1
| 1
Error: Unexpected DO statement in CONTAINS section at (1)
temp.f95:31:36:
31 | do j = i + 1, size(nums)
| 1
Error: Unexpected DO statement in CONTAINS section at (1)
temp.f95:32:44:
32 | if (nums(i) == nums(j)) then
| 1
Error: Unexpected block IF statement in CONTAINS section at (1)
temp.f95:33:37:
33 | count = count + 1
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:34:19:
34 | end if
| 1
Error: Expecting END PROGRAM statement at (1)
temp.f95:35:15:
35 | end do
| 1
Error: Expecting END PROGRAM statement at (1)
temp.f95:36:11:
36 | end do
| 1
Error: Expecting END PROGRAM statement at (1)
temp.f95:37:7:
37 | end function good_pairs
| 1
Error: Expecting END PROGRAM statement at (1)
temp.f95:14:4:
14 | nums = [1, 1, 1, 1]
| 1
Error: Different shape for array assignment at (1) on dimension 1 (6 and 4)
temp.f95:18:4:
18 | nums = [1, 2, 3]
| 1
Error: Different shape for array assignment at (1) on dimension 1 (6 and 3)
module good_pairs
implicit none
contains
integer function good_pairs(nums) result(count)
integer, intent(in) :: nums(:)
integer :: i, j
count = 0
do i = 1, size(nums) - 1
do j = i + 1, size(nums)
if (nums(i) == nums(j) .and. i < j) then
count = count + 1
end if
end do
end do
end function good_pairs
end module good_pairs
program test_good_pairs
use good_pairs, only : good_pairs
implicit none
integer, parameter :: nums1(5) = [1, 2, 3, 1, 1, 3]
integer, parameter :: nums2(4) = [1, 1, 1, 1]
integer, parameter :: nums3(3) = [1, 2, 3]
write (*, '(a, i0)') 'Example 1:', good_pairs(nums1)
write (*, '(a, i0)') 'Example 2:', good_pairs(nums2)
write (*, '(a, i0)') 'Example 3:', good_pairs(nums3)
end program test_good_pairs
temp.f95:7:27:
7 | integer function good_pairs(nums) result(count)
| 1
Error: MODULE attribute of âgood_pairsâ conflicts with PROCEDURE attribute at (1)
temp.f95:9:30:
9 | integer, intent(in) :: nums(:)
| 1
Error: Unexpected data declaration statement in CONTAINS section at (1)
temp.f95:11:15:
11 | integer :: i, j
| 1
Error: Unexpected data declaration statement in CONTAINS section at (1)
temp.f95:13:9:
13 | count = 0
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:15:24:
15 | do i = 1, size(nums) - 1
| 1
Error: Unexpected DO statement in CONTAINS section at (1)
temp.f95:16:24:
16 | do j = i + 1, size(nums)
| 1
Error: Unexpected DO statement in CONTAINS section at (1)
temp.f95:17:40:
17 | if (nums(i) == nums(j) .and. i < j) then
| 1
Error: Unexpected block IF statement in CONTAINS section at (1)
temp.f95:18:17:
18 | count = count + 1
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:19:3:
19 | end if
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:20:3:
20 | end do
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:21:3:
21 | end do
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:23:3:
23 | end function good_pairs
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:29:33:
29 | use good_pairs, only : good_pairs
| 1
Error: The name âgood_pairsâ at (1) has already been used as an external module name
temp.f95:33:30:
33 | integer, parameter :: nums1(5) = [1, 2, 3, 1, 1, 3]
| 1
Error: Different shape for array assignment at (1) on dimension 1 (5 and 6)
temp.f95:37:51:
37 | write (*, '(a, i0)') 'Example 1:', good_pairs(nums1)
| 1
Error: Symbol ânums1â at (1) has no IMPLICIT type; did you mean ânums3â?
temp.f95:38:34:
1 | module good_pairs
| 2
......
38 | write (*, '(a, i0)') 'Example 2:', good_pairs(nums2)
| 1
Error: Global name âgood_pairsâ at (1) is already being used as a MODULE at (2)
temp.f95:37:45:
37 | write (*, '(a, i0)') 'Example 1:', good_pairs(nums1)
| 1
Warning: Interface mismatch in global procedure âgood_pairsâ at (1): 'good_pairs' is not a function
temp.f95:38:34:
38 | write (*, '(a, i0)') 'Example 2:', good_pairs(nums2)
| 1
Error: Function âgood_pairsâ at (1) has no IMPLICIT type
temp.f95:39:34:
1 | module good_pairs
| 2
......
39 | write (*, '(a, i0)') 'Example 3:', good_pairs(nums3)
| 1
Error: Global name âgood_pairsâ at (1) is already being used as a MODULE at (2)
temp.f95:37:45:
37 | write (*, '(a, i0)') 'Example 1:', good_pairs(nums1)
| 1
Warning: Interface mismatch in global procedure âgood_pairsâ at (1): 'good_pairs' is not a function
temp.f95:39:34:
39 | write (*, '(a, i0)') 'Example 3:', good_pairs(nums3)
| 1
Error: Function âgood_pairsâ at (1) has no IMPLICIT type
temp.f95:38:34:
38 | write (*, '(a, i0)') 'Example 2:', good_pairs(nums2)
| 1
Error: More actual than formal arguments in procedure call at (1)
temp.f95:39:34:
39 | write (*, '(a, i0)') 'Example 3:', good_pairs(nums3)
| 1
Error: More actual than formal arguments in procedure call at (1)
class UndergroundSystem:
def __init__(self):
self.check_in_data = {}
self.travel_data = {}
def checkIn(self, id: int, stationName: str, t: int) -> None:
self.check_in_data[id] = (stationName, t)
def checkOut(self, id: int, stationName: str, t: int) -> None:
start_station, start_time = self.check_in_data[id]
route = (start_station, stationName)
if route not in self.travel_data:
self.travel_data[route] = [0, 0]
self.travel_data[route][0] += t - start_time
self.travel_data[route][1] += 1
def getAverageTime(self, startStation: str, endStation: str) -> float:
route = (startStation, endStation)
total_time, num_trips = self.travel_data[route]
return total_time / num_trips
The algorithm uses two nested hashmaps (in Python, two nested dictionaries) to keep track of check-in/check-out data and travel-time data:
check_in_data hashmap stores the traveler's check-in station and time, with their ID as the key.check_in_data and combined with the check-out station and time to create a route key. The route key is then used to store the total travel time and the number of trips taken for that route in the travel_data hashmap.The algorithm has an O(1) average complexity for all operations (check-in, check-out, and getAverageTime) as it relies on hashing lookups and updates.
#include <unordered_map>
#include <string>
class UndergroundSystem {
public:
unordered_map<int, pair<string, int>> check_in_data;
unordered_map<string, pair<int, int>> travel_data;
UndergroundSystem() {}
void checkIn(int id, string stationName, int t) {
check_in_data[id] = {stationName, t};
}
void checkOut(int id, string stationName, int t) {
string startStation = check_in_data[id].first;
int startTime = check_in_data[id].second;
string route = startStation + "_" + stationName;
travel_data[route].first += (t - startTime);
travel_data[route].second += 1;
}
double getAverageTime(string startStation, string endStation) {
string route = startStation + "_" + endStation;
return (double)travel_data[route].first / travel_data[route].second;
}
};
The algorithm uses two nested hashmaps (in Python, two nested dictionaries) to keep track of check-in/check-out data and travel-time data:
check_in_data hashmap stores the traveler's check-in station and time, with their ID as the key.check_in_data and combined with the check-out station and time to create a route key. The route key is then used to store the total travel time and the number of trips taken for that route in the travel_data hashmap.The algorithm has an O(1) average complexity for all operations (check-in, check-out, and getAverageTime) as it relies on hashing lookups and updates.