-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFrogRiverOne.js
More file actions
35 lines (26 loc) · 1.04 KB
/
Copy pathFrogRiverOne.js
File metadata and controls
35 lines (26 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// 2. Frog River One
// Problem:
// A frog wants to jump across a river. The river is divided into positions 1...X. Given an array A where A[K] represents a leaf falling at position A[K], find the earliest time when all positions are covered.
// Example:
// Input: X = 5, A = [1, 3, 1, 4, 2, 3, 5, 4]
// Output: 6
// Solution Idea:
// Use a set to track positions where leaves have fallen.
// If the size of the set equals X, return the index.
// Time Complexity: O(n).
function frogRiverOne(X, A) {
// Create a set to track positions where leaves fall
const positions = new Set();
// Loop through the list of leaves
for (let i = 0; i < A.length; i++) {
// Add the current position to the set
positions.add(A[i]);
// Check if all positions are covered (i.e., if the size of the set is X)
if (positions.size === X) {
// If all positions are covered, return the index (time) when this happens
return i;
}
}
// If not all positions are covered, return -1
return -1;
}