Review of 2 Problems

In the problem Maximum of all subarrays of size k, here we find the maximum in each subarray of length k. First you initially consider the first subarray and calculate its max, now when the new incoming element comes we need to check few things and those are:

  1. The tail element that is leaving my subarray is max? if so update the max of the current sub array.
  2. If the incoming element is greater than or equal to the current max then just update the max and with this we need to traverse through the subarray to check for the max and if the incoming element is less than the current max then no need to update it.

So checking the tail element whether its max or not does save us lot of unecessary traversals!!

Another Problem that i would like to talk about is Length of the longest substring. Here we need to fing the substring that has unrepeated characters in it. Here we fix two pointers p1 and p2 at the starting of the string and move pointer p2 ahead and add each element in the set and check if its not already present. Initialize the max as 1 and each time you see the the element that is already present in the set, you update your max value and clear the set.

The key step here is that once we come across the element that is already in the set, we move pointer p1 to the next element of that repeated element. say "abedfrg" and the incoming character is e ,then we need to move the p1 and p2 at d.

要查看或添加评论,请登录

Vamshi Krishna Raj S的更多文章

社区洞察

其他会员也浏览了