LeetCode Day-2

Hey there,

Welcome to Day 2 of my LeetCode journey! I'm thrilled to have you here as I continue my quest for coding. As promised, I'm staying committed to solving one problem every day and sharing my progress with you through this blog.

So, without further ado, let's dive into the problem and its solution!


26. Remove Duplicates from Sorted Array

Intuition

  • integer array nums sorted in ascending order

  • remove duplicates - unique elements must appear only once

  • order of elements must be the same

  • In place - the change must be in the nums array itself

  • return k - the number of unique elements in that array

Approach :

  • We can use a set. Set removes the duplicate elements and has unique elements.

  • We convert the set to a list

s = list(set(nums))
  • We can sort the list so that it is in ascending order.
s = sorted(s)
  • Calculate the length of the sorted list. This will be your k, as this is the list with unique elements
n = len(s)
  • Now, iterate over the length and assign the numbers in s to nums. Only the unique elements will be placed because we do not care what the rest of the elements are
for i in range(n):
    nums[i] = s[i]

We only care about the first k (here, n) elements of nums

  • return n
return n

Here is the full code :

class Solution:
    def removeDuplicates(self, nums: List[int]) -> int:

        s = list(set(nums))
        s = sorted(s)
        n = len(s)

        for i in range(n):
            nums[i] = s[i]

        return n

Time Complexity : O(n)


1732. Find the Highest Altitude

Intuition :

  • given is an integer array gain of length n, where gain[i] is the net gain between point i and i+1

  • starts from 0 with altitude being

  • We have to return the highest altitude of a point

Understanding :

Input: gain = [-5,1,5,0,-7]
Output: 1
Explanation: The altitudes are [0,-5,-4,1,1,-6]. The highest is 1.

gain is [ -5, 1 , 5 , 0 , -7 ]

The biker starts from 0. The initial gain is -5. Thus, 0 + -5 = -5.

Biker is now at altitude -5. The next gain is 1. Thus, -5 + 1 = -4.

Biker is not at altitude -4. The next gain is 5. Thus, -4 + 5 = 1. (highest )

Biker is not at altitude 1. The next gain is 0. Thus, 1 + 0 = 1.

Biker is not at altitude 1. The next gain is -7. Thus, 1 + -7 = -6.

Approach :

  • Have a max variable to keep track of the highest altitude. Initialize it to 0 ( as the start altitude is 0)
max = 0
  • Have a prev variable to keep track of the successive altitudes. Initialize it to 0.
prev = 0
  • Using a for loop, traverse through the gain array.
for i in range(len(gain)):
  • prev would be the previous altitude + new gain
prev = prev + gain[i]
  • If you encounter an altitude where the new altitude is greater than the max altitude so far, then the new altitude will be the max.
if prev + gain[i] > max:
    max = prev + gain[i]
  • return max ( the highest altitude)
return max

Here is the full code :

class Solution:
    def largestAltitude(self, gain: List[int]) -> int:

        max = 0
        prev = 0

        for i in range(len(gain)):

            if prev + gain[i] > max:
                max = prev + gain[i]

            prev = prev + gain[i]

        return max

Time complexity : O(n)


That wraps up Day 2 of my LeetCode journey. I hope you found the problem and its solution insightful. If you have any questions, doubts, or suggestions for improvement, please feel free to share them. Your support and engagement mean a lot to me.

Happy Coding!