You are given an array of characters letters that is sorted in non-decreasing order, and a character target. There are at least two different characters in letters.
Return the smallest character in letters that is lexicographically greater than target. If such a character does not exist, return the first character in letters.
Example 1:
Input: letters = [ "c ", "f ", "j "], target = "a " Output: "c " Explanation: The smallest character that is lexicographically greater than 'a' in letters is 'c'.
Example 2:
Input: letters = [ "c ", "f ", "j "], target = "c " Output: "f " Explanation: The smallest character that is lexicographically greater than 'c' in letters is 'f'.
Example 3:
Input: letters = [ "x ", "x ", "y ", "y "], target = "z " Output: "x " Explanation: There are no characters in letters that is lexicographically greater than 'z' so we return letters[0].
Constraints:
2 <= letters.length <= 104letters[i] is a lowercase English letter.letters is sorted in non-decreasing order.letters contains at least two different characters.target is a lowercase English letter.program main
implicit none
character(len=1), parameter :: letters(*) = ["c", "f", "j"]
character(len=1) :: target = "a"
character(len=1) :: result
result = next_larger_letter(letters, target)
print *, result
contains
function next_larger_letter(letters, target) result(result_)
implicit none
character(len=1), intent(in) :: letters(:)
character(len=1), intent(in) :: target
character(len=1) :: result_
integer :: i
do i = 1, size(letters)
if (letters(i) > target) then
result_ = letters(i)
exit
end if
end do
if (result_ == "") then
result_ = letters(1)
end if
end function next_larger_letter
end program main
c
module FindSmallestLexicographicallyGreater
implicit none
contains
function findSmallestLexicographicallyGreater(letters, target) result(result)
character(len=*), intent(in) :: letters(:)
character(len=*), intent(in) :: target
character(len=1) :: result
integer :: i
do i = 1, size(letters)
if (letters(i) > target) then
result = letters(i)
exit
end if
end do
if (result == '') then
result = letters(1)
end if
end function findSmallestLexicographicallyGreater
end module FindSmallestLexicographicallyGreater
program test_findSmallestLexicographicallyGreater
use FindSmallestLexicographicallyGreater
implicit none
character(len=1) :: result
character(len=*), parameter :: letters = ["c ", "f ", "j "]
result = findSmallestLexicographicallyGreater(letters, "a ")
write (*, '(a)') 'Example 1: ' // result
result = findSmallestLexicographicallyGreater(letters, "c ")
write (*, '(a)') 'Example 2: ' // result
result = findSmallestLexicographicallyGreater(["x ", "x ", "y ", "y "], "z ")
write (*, '(a)') 'Example 3: ' // result
end program test_findSmallestLexicographicallyGreater
temp.f95:6:49:
6 | function findSmallestLexicographicallyGreater(letters, target) result(result)
| 1
Error: MODULE attribute of ‘findsmallestlexicographicallygreater’ conflicts with PROCEDURE attribute at (1)
temp.f95:7:50:
7 | character(len=*), intent(in) :: letters(:)
| 1
Error: Unexpected data declaration statement in CONTAINS section at (1)
temp.f95:8:46:
8 | character(len=*), intent(in) :: target
| 1
Error: Unexpected data declaration statement in CONTAINS section at (1)
temp.f95:9:34:
9 | character(len=1) :: result
| 1
Error: Unexpected data declaration statement in CONTAINS section at (1)
temp.f95:11:20:
11 | integer :: i
| 1
Error: Unexpected data declaration statement in CONTAINS section at (1)
temp.f95:13:31:
13 | do i = 1, size(letters)
| 1
Error: Unexpected DO statement in CONTAINS section at (1)
temp.f95:14:41:
14 | if (letters(i) > target) then
| 1
Error: Unexpected block IF statement in CONTAINS section at (1)
temp.f95:15:35:
15 | result = letters(i)
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:16:20:
16 | exit
| 1
Error: EXIT statement at (1) is not within a construct
temp.f95:17:15:
17 | end if
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:18:11:
18 | end do
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:20:30:
20 | if (result == '') then
| 1
Error: Unexpected block IF statement in CONTAINS section at (1)
temp.f95:21:31:
21 | result = letters(1)
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:22:11:
22 | end if
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:23:7:
23 | end function findSmallestLexicographicallyGreater
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:27:9:
27 | use FindSmallestLexicographicallyGreater
| 1
Fatal Error: Cannot open module file ‘findsmallestlexicographicallygreater.mod’ for reading at (1): No such file or directory
compilation terminated.
import heapq
from collections import defaultdict
def networkDelayTime(times, n, k):
graph = defaultdict(list)
for u, v, w in times:
graph[u].append((v, w))
dist = {i: float('inf') for i in range(1, n + 1)}
dist[k] = 0
pq = [(0, k)]
while pq:
time, node = heapq.heappop(pq)
if time > dist[node]:
continue
for neighbour, neighbourTime in graph[node]:
candidate_dist = time + neighbourTime
if candidate_dist < dist[neighbour]:
dist[neighbour] = candidate_dist
heapq.heappush(pq, (candidate_dist, neighbour))
maxTime = max(dist.values())
return maxTime if maxTime < float('inf') else -1
The algorithm uses Dijkstra's shortest path algorithm to find the shortest time it takes for the signal to reach every node.
#include <vector>
#include <queue>
#include <algorithm>
#include <limits>
int networkDelayTime(std::vector<std::vector<int>>& times, int n, int k) {
std::vector<std::vector<std::pair<int, int>>> graph(n + 1);
for (const auto &edge : times)
graph[edge[0]].emplace_back(edge[1], edge[2]);
std::vector<int> dist(n + 1, std::numeric_limits<int>::max());
dist[k] = 0;
std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int>>, std::greater<std::pair<int, int>>> pq;
pq.emplace(0, k);
while (!pq.empty()) {
int time = pq.top().first, node = pq.top().second;
pq.pop();
if (time > dist[node]) continue;
for (const auto &neighbour : graph[node]) {
int nextNode = neighbour.first, nextTime = neighbour.second;
if (time + nextTime < dist[nextNode]) {
dist[nextNode] = time + nextTime;
pq.push({dist[nextNode], nextNode});
}
}
}
int maxTime = *std::max_element(dist.begin() + 1, dist.end());
return maxTime == std::numeric_limits<int>::max() ? -1 : maxTime;
}
The algorithm uses Dijkstra's shortest path algorithm to find the shortest time it takes for the signal to reach every node.