Similar Problems
Similar Problems not available
Last Day Where You Can Still Cross - Leetcode Solution
Companies:
LeetCode: Last Day Where You Can Still Cross Leetcode Solution
Difficulty: Hard
Topics: binary-search depth-first-search union-find breadth-first-search matrix array
Problem statement:
You are given an array of non-negative integers nums, where each element represents the maximum number of steps you can take from that position. On the ith day, you play tennis at the ith position. Unfortunately, rain is forecasted for exactly one day.
If there is rain on the ith day, you can still cross the court but can't add any steps to your maximum number of steps.
Return the maximum number of consecutive days you can still play tennis starting from the first day.
Solution:
Approach 1: Brute Force
The brute-force approach is to consider all possible subsequences of consecutive days starting from the first day, and for each subsequence, we check if it's possible to play tennis on all the days in the subsequence. We return the length of the longest such subsequence.
The time complexity of this approach is O(n^3), where n is the length of the input array nums. This is because we have nC2 = n * (n-1) / 2 possible subsequences, and for each subsequence, we need to check if it's possible to play on all the days in the subsequence, which takes O(n) time.
Code:
def can_play(nums, start, end): for i in range(start, end+1): if nums[i] == 0: return False return True
def max_consecutive_days(nums): n = len(nums) max_days = 0 for i in range(n): for j in range(i, n): if can_play(nums, i, j): max_days = max(max_days, j-i+1) return max_days
Approach 2: Two-Pointer
We can optimize the above approach by using two pointers to keep track of the start and end of the current subsequence of consecutive days that we are considering. We start with both pointers pointing to the first day, and we move the end pointer forward until we reach a day where we cannot play tennis. At this point, we move the start pointer forward until we can play tennis again, and then we repeat the process.
The time complexity of this approach is O(n), where n is the length of the input array nums. This is because we only iterate through the array once.
Code:
def max_consecutive_days(nums): n = len(nums) max_days = 0 start = end = 0 while end < n: if nums[end] == 0: max_days = max(max_days, end-start) while start < end and nums[start] != 0: start += 1 start += 1 end += 1 max_days = max(max_days, end-start) return max_days
Approach 3: Dynamic Programming
We can use dynamic programming to solve this problem. Let dp[i] be the maximum number of consecutive days we can still play tennis starting from day i. We can compute dp[i] recursively as follows:
- If nums[i] = 0, then dp[i] = 0, since we cannot play on day i.
- Otherwise, dp[i] = 1 + dp[i+1], since we can play on day i and we can make use of the remaining consecutive days starting from i+1.
The answer is the maximum value in the dp array.
The time complexity of this approach is O(n), where n is the length of the input array nums. This is because we only iterate through the array once.
Code:
def max_consecutive_days(nums): n = len(nums) dp = [0] * n
# Base case
if nums[-1] != 0:
dp[-1] = 1
# Recursive case
for i in range(n-2, -1, -1):
if nums[i] != 0:
dp[i] = 1 + dp[i+1]
return max(dp)
Last Day Where You Can Still Cross Solution Code
1