Hello Everyone!
I’m Somuya Khandelwal, back with updates from Day 4 of Week 3 in my competitive programming journey. Today, I worked on Array-based problems, which are some of the most fundamental and versatile challenges in competitive programming. These problems required not only a strong grasp of array operations but also the ability to think critically about optimization and edge cases.
What I Worked On Today
-
Rotate Array (Medium Difficulty)
- Task: Rotate an array to the right by
k
steps. -
Approach:
- I used the three-step reversal technique to perform the rotation efficiently:
- Reverse the entire array.
- Reverse the first
k
elements. - Reverse the remaining elements.
- Took care to handle cases where
k
was greater than the length of the array by calculatingk % n
.
-
What I Learned:
- This reversal technique is a clever way to solve the problem in O(n) time while using only O(1) extra space.
- Properly addressing cyclic behavior (like
k > n
) ensures the solution is robust and works for all inputs.
- Task: Rotate an array to the right by
-
Best Time to Buy and Sell Stock II (Medium Difficulty)
- Task: Find the maximum profit by buying and selling stocks multiple times, ensuring that you sell before you buy again.
-
Approach:
- Adopted a greedy strategy: calculated the profit by summing up all the positive differences between consecutive days.
- Skipped days where the stock price decreased.
-
What I Learned:
- Greedy algorithms are particularly effective when local optimizations (like summing up small profits) naturally lead to a global solution.
- Understanding the problem constraints is essential to identify the simplest, most efficient approach.
What I Learned Today
-
In-Place Array Manipulations:
- Problems like Rotate Array showed me how simple operations like reversing can solve complex tasks without using extra memory.
-
Greedy Problem Solving:
- The Stock Buy and Sell problem demonstrated the effectiveness of greedy strategies when dealing with optimization tasks.
-
Edge Case Handling:
- Cyclic rotations and small array sizes required careful thought in Rotate Array to avoid incorrect results.
-
Simplifying Complex Solutions:
- Breaking problems into logical, manageable steps (like reversing specific segments in Rotate Array) made implementation easier and reduced debugging time.
Reflections and Challenges
The Rotate Array problem stood out as the more challenging task today. Initially, I tried a brute-force approach, but it was too slow for larger inputs. Switching to the reversal technique was a game-changer and helped me appreciate how important it is to balance clarity and efficiency. The Stock Buy and Sell problem, while simpler, reinforced how powerful greedy algorithms can be when applied correctly.
Looking Ahead
On Monday, I’ll conclude the week with more Array-based problems, including H-Index and Gas Station. These tasks will push me to further develop my skills in array traversal and optimization.
Thank you for following along with my journey! I’m excited to keep learning, solving, and sharing my progress as I continue to grow in competitive programming.
Best regards,
Somuya
Top comments (0)