🚀 LeetCode Daily Challenge 🔗 Problem: https://lnkd.in/gzjgiw8d 💡 My thought process: First, we sort the robots by their positions. Instead of changing the original arrays, we store the indices and sort them using the positions array. This way, we process the robots from left to right. This order is necessary because collisions depend on it. We use a vector as a stack to track the robots that are still active. The stack stores indices, allowing us to access their direction and health. For each robot in sorted order, we check if a collision might happen. A collision occurs only when the current robot is moving left and the robot at the top of the stack is moving right. In all other situations, we simply add the current robot to the stack. If a collision is possible, we enter a loop. The current robot may collide with multiple robots in the stack. In each step, we compare the health of the current robot with the robot at the top of the stack: - If the robot in the stack has more health, it survives, its health decreases by one, and the current robot dies. - If the current robot has more health, it survives, its health decreases by one, and we continue checking with the next robot in the stack. - If both have the same health, both are removed. We repeat this until the current robot dies or no more collisions can occur. If the current robot is still alive after the loop, we add it to the stack. In the end, all robots with health greater than zero are survivors. We go through the health array and collect those values. 👉 My Solution: https://lnkd.in/gjSQCeTP If you found this breakdown helpful, feel free to ⭐ the repo or connect with me on LinkedIn 🙂🚀 #️⃣ #leetcode #cpp #dsa #coding #problemsolving #engineering #BDRM #BackendDevWithRahulMaheswari
LeetCode Challenge: Robot Collision Problem Solution
More Relevant Posts
-
🚀 LeetCode POTD – Maximum Walls Destroyed by Robots (Hard) Continuing my April challenge 💪 — solving every POTD consistently Today’s problem, Maximum Walls Destroyed by Robots, initially felt like a greedy / simple counting problem… but it turned out to be much deeper than expected 👀 🧠 My Thought Process: First instinct: Each robot has a range → just count walls in that range Then I realized a critical issue: ❌ Same wall can get counted multiple times Tried greedy → failed Tried simple DP → still wrong 👉 Clearly, something was missing in the state 🔥 Key Insight: This is actually a DP + state tracking problem Each robot has 2 choices → left or right But choice affects future robots So we must track: 👉 previous robot direction (left/right) ⚙️ Implementation Strategy: Sort robots based on position Precompute valid range for each robot Use binary search to count walls efficiently Apply Recursion + Memoization (DP) State: dp[i][prevDir] i → current robot prevDir → previous robot went left/right 💡 What I Learned: DP is not just index-based → state matters a lot If decisions affect future → include it in DP state Binary search can turn brute force into efficient solution Always check: “Am I double counting?” 💥 Honestly, I was stuck on this problem for quite some time Concept clear hi nahi ho raha tha Then I watched CodeWithMik’s video — and everything finally clicked 🙌 👉 Big thanks to him — explanation was super clear and intuitive 📈 Slowly improving at: Identifying correct DP states Avoiding overcounting mistakes Converting brute force → optimized solution 📌 April Challenge: Solve every LeetCode POTD consistently Let’s see how far consistency takes me 🚀 #LeetCode #DSA #DynamicProgramming #ProblemSolving #CodingJourney #Consistency #BinarySearch
To view or add a comment, sign in
-
-
LeetCode Daily | Day 69 🔥 LeetCode POTD – 657. Robot Return to Origin (Easy) ✨ 📌 Problem Insight Given a robot starting at (0, 0): ✔ Moves are given as a string of 'U', 'D', 'L', 'R' ✔ Each move shifts position by 1 unit ✔ Need to check → does it come back to origin? 🔍 Initial Thinking – Simulation ✔ 💡 Idea: ✔ Track x and y coordinates ✔ For every move: • 'U' → y-- (or y++) • 'D' → opposite of 'U' • 'R' → x++ • 'L' → x-- ✔ At the end → check (x == 0 && y == 0) ⏱ Complexity: O(n), Space: O(1) ✔ Single pass ✔ No extra data structures needed 💡 Cleaner Insight – Count Balance 🔥 👉 Instead of coordinates: ✔ Count 'U' == 'D' ✔ Count 'L' == 'R' ✔ If both balances hold → back to origin 🧠 Key Learning ✔ Simple simulation problems test clarity, not complexity ✔ Coordinate tracking is a powerful pattern ✔ Balance/count approach simplifies thinking ✔ Always look for symmetry in movement problems A very clean problem where observation > optimization ⚡ #LeetCode #DSA #Algorithms #CPlusPlus #ProblemSolving #CodingJourney #DataStructures
To view or add a comment, sign in
-
-
LeetCode — Day 40 Solved: Robot Return to Origin Today’s problem was about simulation and balance checking. Approach: • Count movements in all four directions • Check if left = right and up = down Key insight: To return to the origin, movements must cancel each other out. A simple problem, but a good reminder: Sometimes correctness comes down to maintaining balance and symmetry. #LeetCode #Day40 #DSA #ProblemSolving #Consistency #Cpp #CodingJourney
To view or add a comment, sign in
-
-
🚀 LeetCode Day Problem Solving 🚀 Day-39 📌 Problem: A robot starts at position (0, 0) on a 2D plane. You are given a string moves representing its movement sequence. Each character in the string denotes a move: ➡ 'R' → Right ⬅ 'L' → Left ⬆ 'U' → Up ⬇ 'D' → Down 👉 Your task is to determine whether the robot returns to the origin (0, 0) after completing all moves. Return true if it returns to origin, otherwise false. 🧠 Example 1: Input: moves = "UD" ✅ Output: true 📖 Explanation: Move Up → (0, 1) Move Down → (0, 0) ✔ Robot returns to origin 🧠 Example 2: Input: moves = "LL" ❌ Output: false 📖 Explanation: Move Left → (-1, 0) Move Left → (-2, 0) ❌ Robot does not return to origin 💡 Key Insight: ✔ Count movements: Number of 'U' should equal 'D' Number of 'L' should equal 'R' ✔ OR simulate coordinates: Track (x, y) position 📊 Complexity Analysis: ⏱ Time Complexity: O(n) 📦 Space Complexity: O(1) 🧠 What I Learned: ✔ Simple simulation problems can be solved efficiently ✔ Importance of balancing movements ✔ Good practice for coordinate tracking ✅ Day 39 Completed 🚀 Keep going strong with DSA & consistency 💪 #Leetcode #DSA #ProblemSolving #BitManipulation #CodingJourney #InterviewPreparation #Consistency #MilanSahoo 🚀
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 𝟕𝟑/𝟑𝟔𝟓 🚀 📌 𝐋𝐞𝐞𝐭𝐂𝐨𝐝𝐞 𝐏𝐎𝐓𝐃: 𝐑𝐨𝐛𝐨𝐭 𝐂𝐨𝐥𝐥𝐢𝐬𝐢𝐨𝐧𝐬 Continuing my 𝟑𝟔𝟓 𝐃𝐚𝐲𝐬 𝐨𝐟 𝐂𝐨𝐝𝐞 journey with a focus on 𝐩𝐫𝐨𝐛𝐥𝐞𝐦-𝐬𝐨𝐥𝐯𝐢𝐧𝐠, 𝐃𝐒𝐀, 𝐚𝐧𝐝 𝐜𝐨𝐧𝐬𝐢𝐬𝐭𝐞𝐧𝐜𝐲. 💪 🔎 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: Sort robots based on positions. Use a stack to track right-moving robots. When a left-moving robot appears, simulate collisions with stack elements. Resolve collisions based on health values and update survivors accordingly. 🔍 𝐀𝐥𝐠𝐨𝐫𝐢𝐭𝐡𝐦 𝐮𝐬𝐞𝐝: Sorting + Stack-based simulation. ⏱ 𝐓𝐢𝐦𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: 𝐎(𝐧 log 𝐧) 🧠 𝐒𝐩𝐚𝐜𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: 𝐎(𝐧) 📈 𝐊𝐞𝐲 𝐭𝐚𝐤𝐞𝐚𝐰𝐚𝐲: Collision problems can often be modelled using stacks to simulate interactions in order. #LeetCode #LeetCodeDaily #365DaysOfCode #DSA #Java #Stack #Simulation #ProblemSolving #LearningInPublic 👨💻 🔗 Problem link in comments 👇
To view or add a comment, sign in
-
-
🚀 #LeetCode Day Problem Solving 🚀 Day-20 📌 Problem: A robot starts at position (0, 0) on a 2D plane. You are given a string moves representing its movement sequence. Each character in the string denotes a move: ➡ 'R' → Right ⬅ 'L' → Left ⬆ 'U' → Up ⬇ 'D' → Down 👉 Your task is to determine whether the robot returns to the origin (0, 0) after completing all moves. Return true if it returns to origin, otherwise false. 🧠 Example 1: Input: moves = "UD" ✅ Output: true 📖 Explanation: Move Up → (0, 1) Move Down → (0, 0) ✔ Robot returns to origin 🧠 Example 2: Input: moves = "LL" ❌ Output: false 📖 Explanation: Move Left → (-1, 0) Move Left → (-2, 0) ❌ Robot does not return to origin 💡 Key Insight: ✔ Count movements: Number of 'U' should equal 'D' Number of 'L' should equal 'R' ✔ OR simulate coordinates: Track (x, y) position 📊 Complexity Analysis: ⏱ Time Complexity: O(n) 📦 Space Complexity: O(1) 🧠 What I Learned: ✔ Simple simulation problems can be solved efficiently ✔ Importance of balancing movements ✔ Good practice for coordinate tracking ✅ Day 20 Completed 🚀 Keep going strong with DSA & consistency 💪 #Leetcode #DSA #ProblemSolving #BitManipulation #CodingJourney #InterviewPreparation #Consistency
To view or add a comment, sign in
-
-
I recently built something exciting — RoboKpy ROS2 integration — and I wanted to share not just the result, but also what I learned along the way (especially if you're just getting into robotics. If you’re new to robotics, one thing you quickly realize is this: Getting a robot to move correctly is much harder than it looks. That’s where RoboKpy comes in. What is RoboKpy ROS2? It’s a Python-based toolkit integrated with ROS2 that helps you: - Compute forward kinematics (where is the robot?) - Solve inverse kinematics (how should the robot move to reach a point?) - Generate smooth trajectories in both task space and joint space - Connect everything to ROS2 for real-time control and visualization (RViz) 💡 Some real challenges I faced while building this: ⚠️ 1. IK Instability & Multiple Solutions Inverse kinematics isn’t always straightforward — a robot can reach the same point in multiple ways (elbow up vs elbow down). ⚠️ 2. Singularities Certain robot positions cause loss of control or unpredictable motion. ⚠️ 3. Trajectory Smoothness A robot shouldn’t just reach a point — it should move smoothly and naturally. ⚠️ 4. ROS2 Integration Complexity Connecting pure kinematics with ROS2 topics, nodes, and visualization wasn’t plug-and-play. 👉 Managing data flow (Pose → IK → Joint Commands) required careful structuring. ⚠️ 5. Orientation Handling Position is one thing, but orientation adds another layer of complexity. 👉 Handling rotations without causing instability was a major learning curve. quaternions instead of euler angles to prevent gimbal locks. 💡 Why this matters (especially for beginners): RoboKpy is designed to simplify your learning curve in robot manipulation. write your custom URDF model and easily control the robot by moving the interactive marker around in Rviz2. Whether you're: - A student learning robotics - A developer exploring ROS2 - Or building real-world automation systems This is a step toward making robot manipulation more accessible and practical. 🚧 Still building and improving. If you're working with ROS2, robotics, or automation, let’s connect and share ideas 🤝 #Robotics #ROS2 #Python #Engineering #Automation #Kinematics #LearningInPublic #TechJourney
To view or add a comment, sign in
-
LeetCode Daily | Day 70 🔥 LeetCode POTD – 874. Walking Robot Simulation (Medium) ✨ 📌 Problem Insight Given a robot on an infinite grid: ✔ Starts at (0, 0), facing North ✔ Commands include: • -2 → turn left • -1 → turn right • k → move forward k steps ✔ Obstacles block movement ✔ Find max distance² from origin reached 🔍 Initial Thinking – Simulation + Directions ✔ 💡 Idea: ✔ Maintain current position (x, y) ✔ Track direction using index (N, E, S, W) ✔ Use dx, dy arrays for movement ✔ Store obstacles in a set for fast lookup ✔ Move step-by-step (important!) • Before each step → check obstacle • If blocked → stop current command ⏱ Complexity: O(n + steps), Space: O(n) ✔ Efficient due to hashing ✔ Each move handled once 💡 Key Optimization – Hashing Obstacles 🔥 👉 Use set / unordered_set ✔ O(1) obstacle check ✔ Encode (x, y) → faster lookup possible 🧠 Key Learning ✔ Simulation problems need careful handling of edge cases ✔ Direction control using modulo is very useful ✔ Step-by-step movement avoids logical bugs ✔ Hashing turns brute-force into efficient solution A great mix of simulation + hashing — classic interview pattern ⚡ #LeetCode #DSA #Algorithms #CPlusPlus #ProblemSolving #CodingJourney #DataStructures
To view or add a comment, sign in
-
-
🚀 My Coding Journey — Only 90 Days to Go! Today’s problem was a really interesting one involving robots, walls, and constrained shooting ranges — and it turned out to be a great exercise in greedy thinking + binary search. 💡 Key Learning: At first glance, it feels like a simulation problem. But the real trick is: Each robot can shoot either left or right (not both) Its range is limited not just by distance, but also by neighboring robots blocking the shot This naturally forms independent intervals 🔍 Approach I Used: Sort robots by position For each robot: Compute valid shooting range (considering blocking robots) Count walls on left and right using binary search Choose the direction that destroys more walls ⚡ Big Insight: Because robots block each other, their decisions become independent, which allows a greedy solution instead of complex DP. ⏱️ Time Complexity: O((n + m) log m) 🎯 Takeaway: Sometimes problems that look like simulation can be reduced to: 👉 interval problems 👉 greedy decisions 👉 efficient counting (binary search) 📉 Challenges like this remind me how important it is to: Break constraints carefully Translate real-world rules into clean intervals 🔥 90 days remaining in this journey. Consistency > Motivation. Let’s keep going. #DSA #CodingJourney #LeetCode #Greedy #BinarySearch #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
-
Day 7/30 – LeetCode Challenge Today’s problem: Robot Return to Origin (657) Problem Overview A robot starts from the origin (0, 0) and moves according to the given string. Each character represents a move: - 'U' → up - 'D' → down - 'L' → left - 'R' → right We need to determine whether the robot comes back to the origin after completing all the moves. Approach I solved this using simple simulation. - maintain `x` for left/right movement - maintain `y` for up/down movement - update the coordinates for each move Finally: - if both x and y become 0, the robot has returned to the origin - otherwise, it has not Why this works For the robot to return to the starting point: - upward and downward moves must balance - left and right moves must balance So checking the final coordinates is enough. Complexitys - Time Complexity: O(n) - Space Complexity: O(1) Key Learning Not every problem needs an advanced approach. Some problems are just about clean observation and correct simulation. #LeetCode #Day7 #DSA #ProblemSolving #CPP #CodingJourney
To view or add a comment, sign in
-
Explore related topics
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development