Given an integer array nums
, return the largest integer that only occurs once. If no integer occurs once, return -1
.
Example 1:
Input: nums = [5,7,3,9,4,9,8,3,1] Output: 8 Explanation: The maximum integer in the array is 9 but it is repeated. The number 8 occurs only once, so it is the answer.
Example 2:
Input: nums = [9,9,8,8] Output: -1 Explanation: There is no number that occurs only once.
Constraints:
1 <= nums.length <= 2000
0 <= nums[i] <= 1000
program main
implicit none
integer, parameter :: n = 9
integer, dimension(n) :: nums
integer :: i, j, max_occurence, unique_num
! Example 1
nums = [5, 7, 3, 9, 4, 9, 8, 3, 1]
write (*, '(A, I0)') 'Example 1: ', largest_unique_integer(nums)
! Example 2
nums = [9, 9, 8, 8]
write (*, '(A, I0)') 'Example 2: ', largest_unique_integer(nums)
contains
function largest_unique_integer(nums) result(unique_num)
implicit none
integer, intent(in) :: nums(:)
integer :: i, j, max_occurence, unique_num
max_occurence = 0
unique_num = -1
do i = 1, size(nums)
do j = 1, size(nums)
if (nums(i) == nums(j)) then
max_occurence = max(max_occurence, 2)
end if
end do
if (max_occurence == 1) then
unique_num = nums(i)
exit
else
max_occurence = 0
end if
end do
end function largest_unique_integer
end program main
temp.f95:13:4: 13 | nums = [9, 9, 8, 8] | 1 Error: Different shape for array assignment at (1) on dimension 1 (9 and 4)
module LargestInteger
implicit none
private
public :: largest_integer
contains
function largest_integer(nums) result(largest)
integer, intent(in) :: nums(:)
integer :: largest
integer :: i, count
largest = -1
do i = 1, size(nums)
count = 0
do j = 1, size(nums)
if (nums(i) == nums(j)) then
count = count + 1
end if
end do
if (count == 1) then
largest = nums(i)
exit
end if
end do
end function largest_integer
end module LargestInteger
program test_largest_integer
use LargestInteger
implicit none
integer, parameter :: nums1(8) = [5, 7, 3, 9, 4, 9, 8, 3]
integer, parameter :: nums2(4) = [9, 9, 8, 8]
integer :: largest
largest = largest_integer(nums1)
write (*,*) 'Example 1:', largest
largest = largest_integer(nums2)
write (*,*) 'Example 2:', largest
end program test_largest_integer
temp.f95:16:16: 16 | do j = 1, size(nums) | 1 Error: Symbol ‘j’ at (1) has no IMPLICIT type temp.f95:30:9: 30 | use LargestInteger | 1 Fatal Error: Cannot open module file ‘largestinteger.mod’ for reading at (1): No such file or directory compilation terminated.
def last_substring(s: str) -> str:
i, j, k, n = 0, 1, 0, len(s)
while j + k < n:
if s[i + k] == s[j + k]:
k += 1
elif s[i + k] < s[j + k]:
i = j
j += 1
k = 0
else:
j += 1
k = 0
return s[i:]
Iterate through the string using three pointers i, j, and k. At each step, compare s[i+k] and s[j+k]. If s[i+k] < s[j+k], set i=j and increment j. If s[i+k] == s[j+k], increment k to continue comparing the next characters. If s[i+k] > s[j+k], just increment j. When j+k reaches the end of the string, the last substring in lexicographical order will start at index i.
std::string lastSubstring(std::string s) {
int i = 0, j = 1, k = 0, n = s.length();
while (j + k < n) {
if (s[i + k] == s[j + k]) {
k++;
} else if (s[i + k] < s[j + k]) {
i = j;
j++;
k = 0;
} else {
j++;
k = 0;
}
}
return s.substr(i);
}
Iterate through the string using three pointers i, j, and k. At each step, compare s[i+k] and s[j+k]. If s[i+k] < s[j+k], set i=j and increment j. If s[i+k] == s[j+k], increment k to continue comparing the next characters. If s[i+k] > s[j+k], just increment j. When j+k reaches the end of the string, the last substring in lexicographical order will start at index i.