Skip to main content

Day 8: Longest Consecutive Sequence - leetcode - Python3

Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence.

You must write an algorithm that runs in O(n) time.

Example 1:

Input: nums = [100,4,200,1,3,2]
Output: 4
Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4.

Example 2:

Input: nums = [0,3,7,2,5,8,4,6,0,1]
Output: 9

Constraints:

  • 0 <= nums.length <= 105
  • -109 <= nums[i] <= 109

SOLUTION:

class Solution:
    def longestConsecutive(self, nums: List[int]) -> int:
        num_set: set[int]=set(nums)
        longest_Seq: int=0

        for i in nums:
        #check if preceding element exist
            if i-1 not in num_set:
                current_seq: int=0
                while i+1 in num_set:
                    current_seq +=1
                    i+=1
                longest_Seq= max(current_seq, longest_Seq)
        return longest_Seq

Time Complexity: O(n)

  • The code iterates through each element in the nums list once, resulting in a linear time complexity relative to the size of the input.
  • The operations performed within the loop, such as checking element existence in the set and incrementing values, have constant time complexity.
  • Overall, the time complexity is dominated by the loop iteration, resulting in a linear time complexity of O(n).

Space Complexity: O(n)

  • The space complexity is determined by the additional memory used to store the set num_set.
  • In the worst case, where all elements in nums are unique, the set num_set would contain all elements of nums.
  • Therefore, the space complexity is proportional to the size of the input, resulting in O(n) space complexity.

How it works:
  1. num_set: set[int] = set(nums): Creates a set num_set containing unique elements from the input nums list.
  2. longest_Seq: int = 0: Initializes the variable longest_Seq with the value 0, which will store the length of the longest consecutive sequence found.
  3. for i in nums:: Iterates over each element i in the nums list.
  4. if i - 1 not in num_set:: Checks if the preceding element of i does not exist in the num_set. If so, it means i is the start of a consecutive sequence.
  5. current_seq: int = 0: Initializes the variable current_seq with the value 0, which will store the length of the current consecutive sequence.
  6. while i + 1 in num_set:: Enters a loop that continues as long as the next consecutive element of i exists in the num_set.
  7. current_seq += 1: Increments the current_seq by 1 to track the length of the consecutive sequence.
  8. i += 1: Increments the value of i by 1 to move to the next consecutive element in the sequence.
  9. longest_Seq = max(current_seq, longest_Seq): Updates the value of longest_Seq with the maximum value between current_seq and longest_Seq, ensuring it holds the length of the longest consecutive sequence encountered so far.
  10. return longest_Seq: Returns the value of longest_Seq as the result of the method.





Comments

Popular posts from this blog

Bug Boundy Methodology, Tools & Resources

Start by defining a clear objective, such as exploiting a remote code execution (RCE) vulnerability or bypassing authentication on your target. Then, consider how you can achieve this goal using various attack vectors like XSS, SSRF, or others - these are simply tools to help you reach your objective. Use the target as how a normal user would, while browsing keep these questions in mind: 1)How does the app pass data? 2)How/where does the app talk about users? 3)Does the app have multi-tenancy or user levels? 4)Does the app have a unique threat model? 5)Has there been past security research & vulnerabilities? 6)How does the app handle XSS, CSRF, and code injection?

Install & set up mitmweb or mitmproxy in Linux

Step 1: Go to the mitmproxy page and download the binaries. Step 2: Install the downloaded tar file with the command " tar -xzf <filename>.tar.gz " Step 3: In the FoxyProxy add the proxy 127.0.0.1:8080  and turn it on. Step 4 : In the terminal run command " ./mitmweb " Step 5: Go to the page  http://mitm.it/   and download the mitmproxy's Certificate. Step 6: If you downloaded the certificate for Firefox, then go to " settings -> Privacy & Security -> Click View Certificates -> Click  Import ", then import the certificate.  Step 7: Now you are ready to capture the web traffic. Step 8 : In terminal run " ./mitmweb"

API Bug Bounty Hunting: Reconnaissance and Reverse Engineering an API

  In order to target APIs, you must first be able to find them.APIs meant for consumer use are meant to be easily discovered. Typically, the API provider will market their API to developers who want to be consumers. So, it will often be very easy to find APIs, just by using a web application as an end-user. The goal here is to find APIs to attack and this can be accomplished by discovering the API itself or the API documentation. Bug Boundy Methodology, Tools & Resources Start by defining a clear objective, such as exploiting a remote code execution (RCE) vulnerability or bypassing… adithyakrishnav.blogspot.com Reconnaissance Passive Reconnaissance It is obtaining information about a target without directly interacting with the target’s systems. Google Dorking Firstly, google search for “<app name> API”. intitle:” api” site:”google.com” inurl:”/api/v2" site:”google.com” inurl:”/api/v1" intext:”index of /” inurl:json site:”google.com” intitle:”index.of” intext:”api.t...