English Explanation : kzbin.info/www/bejne/g6DLgKGqqMmWZ5Y
@darshankumar55467 күн бұрын
//TIME COMPLEXITY=O(n) //SPACE COMPLEXITY=O(1) class Solution { public int[] resultsArray(int[] nums, int k) { int n=nums.length; int dp_prev=0; int dp_curr=0; int[] ans=new int[n-k+1]; for(int i=0;i0 && nums[i]==(nums[i-1]+1)){ dp_curr=dp_prev+1; } else{ dp_curr=0; } if(i>=(k-1)){ if(dp_curr>=(k-1)){ ans[i-(k-1)]=nums[i]; } else{ ans[i-(k-1)]=-1; } } dp_prev=dp_curr; } return ans; } }
@darshankumar55467 күн бұрын
# approach-1 #TIME COMPLEXITY=O(n) #SPACE COMPLEXITY=O(n) class Solution: def resultsArray(self, nums: List[int], k: int) -> List[int]: n=len(nums) dp=[0 for i in range(n)] for i in range(1,n): if(nums[i]==nums[i-1]+1): dp[i]=dp[i-1]+1 print(dp) ans=[] for i in range(k-1,n): if(dp[i]>=(k-1)): ans.append(nums[i]) else: ans.append(-1) return ans
@darshankumar55467 күн бұрын
# approach -2 #TIME COMPLEXITY=O(n) #SPACE COMPLEXITY=O(1) class Solution: def resultsArray(self, nums: List[int], k: int) -> List[int]: n=len(nums) dp_prev=0 dp_curr=0 ans=[] for i in range(n): if(i>0 and nums[i]==nums[i-1]+1): #dp[i]=dp[i-1]+1 dp_curr=dp_prev+1 else: dp_curr=0 if(i>=(k-1)): if(dp_curr>=(k-1)): ans.append(nums[i]) else: ans.append(-1) dp_prev=dp_curr print('dp=',dp_curr,end=' ') return ans