Leetcode Weekly contest Discussion 360
Table of contents
- 1. The first one is "Furthest Point From Origin" which is a basically leetcode easy problem π€π»
- The "Find the Minimum Possible Sum of a Beautiful Array" LC- medium
- The Third one is a pretty good problem "Minimum Operations to Form Subsequence With Target Sum" LC-hardπ€
- Maximize Value of Function in a ball passing game:- LC Hard π₯΅
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:-
Initialize two variables left and dash to 0.
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.
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.
Calculate the total sum of the elements in the array numbers.
If the sum is less than the target, then return -1.
Sort the array numbers in increasing order.
Create a vector v to store the binary representation of target.
Create a vector V1 to store the number of occurrences of each power of 2 in the array nums.
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.
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.
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.
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).
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 π¨βπ»
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.π