Leetcode Weekly contest Discussion 360

92_prajak sen92_prajak sen
4 min read

I just wanted to share my experience participating in the LeetCode weekly contest 360 . So lets analyze each algorithm of every problem of today's contest.

The problems are as follows:-

1. The first one is "Furthest Point From Origin" which is a basically leetcode easy problem πŸ€™πŸ»

Basically my approach to solving this problem:-

  1. Initialize two variables left and dash to 0.

  2. Iterate over the characters in the string moves

    • If the character is _, then increment dash.

    • If the character is L, then decrement is left.

    • If the character is R, then increment left.

  3. Return abs(left)+dash.

time complexity : O(n) Space complexity :O(1)

The "Find the Minimum Possible Sum of a Beautiful Array" LC- medium

for getting distinct numbers we create a set to store the elements that have already been added to the array.

now we can traverse the range from 1 to target and for each element i we will try to get a distinct number that can satisfy the condition target-i;

  • it does not, then we add i to set s and we will maintain a variable sum, to store the summation value as per the given condition in the problem. This is because if s does not contain an integer that is equal to target-i, then i cannot be paired with any other element in the array to form a sum of target.

  • If it does, then we do not add i to s because this would create a pair of distinct indices i and j such that nums[i] and nums[j].

  • finally, return sum

The Third one is a pretty good problem "Minimum Operations to Form Subsequence With Target Sum" LC-hardπŸ€”

Here we have to visualize two facts:

  • Any power of 2 that is present in target must also be present in nums.

  • We can obtain a subsequence whose elements sum to target by repeatedly replacing a power of 2 with a larger power of 2

    The first fact ensures that we can use the vector V to track the powers of 2 that are present in the target. The second fact ensures that we can use the vector V1 to track the number of occurrences of each power of 2 in nums.

  1. Calculate the total sum of the elements in the array numbers.

  2. If the sum is less than the target, then return -1.

  3. Sort the array numbers in increasing order.

  4. Create a vector v to store the binary representation of target.

  5. Create a vector V1 to store the number of occurrences of each power of 2 in the array nums.

  6. Iterate through the vector V.

    • If v[i]==1 and v1[i]>0:

      • Decrement V1[i] and increment v1[i+1].
    • If and V1[i]==0:

      • Find the smallest power of 2 (2^j) that is greater than or equal to 2^i and that is present in nums.

      • Increment v1[j].

    • If V[i]==0:

      • Do nothing.
  7. Return ans.

Maximize Value of Function in a ball passing game:- LC Hard πŸ₯΅

The approach works by dynamic programming. The array dp stores the maximum value of the function f(x) for any starting player x and any number of passes K that have been made so far. The code then iteratively updates the values of the array dp until it has reached the final state, where k is equal to the number of passes that need to be made.

The final value of the array dp is the maximum value of the function f(x) for any starting player x.

  1. Create a 2D array dp to store the maximum value of the function f(x) for a starting player having id X and a number of passes K.

  2. Iterate through all possible starting players x.

    • Iterate through all possible numbers of passes K.

      • If k==1, then the value of f(X) is simply X.

      • If k>!, then the value of f(X) is equal to the maximum of the following two values:

        • The value of f(receiver[x]).

        • The value of x+f(x-1).

      • Update the value of dp[x][k]to the maximum value of f(x).

  3. Return the maximum value of f(x) for any starting player x and any number of passes K.

I hope this blog post is helpful for those people who are interested in CP πŸ€—

happy Hacking πŸ‘¨β€πŸ’»

0
Subscribe to my newsletter

Read articles from 92_prajak sen directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

92_prajak sen
92_prajak sen

πŸ” Who's this? It's your go-to tech enthusiast, full-stack dev, and problem solver at your service! πŸš€ I'm known for my fuzzy mind (constantly buzzing with ideas) 🀯 and my passion for twikinfg with AI and Web3. πŸ’» My motto: Code doesn't lie, comments sometimes do. ✍ So, do you wanna push boundaries? πŸ’ͺ Let's Think Different and build the impossible together.🌐