After recently using the reversal trick to rotate arrays in O(1) space, I realized the exact same logic applies beautifully to strings. Today I tackled LeetCode 151: Reverse Words in a String in C++. Instead of taking the easy route by allocating extra memory to split the string into a new array of words, I solved it entirely in-place. The strategy: 1️⃣ Reverse the entire string. 2️⃣ Reverse each individual word back to its correct orientation. 3️⃣ Use a two-pointer pass to clean up any leading, trailing, or multiple spaces. Connecting the dots between different data structures and seeing how array pointer techniques translate perfectly to strings is incredibly rewarding. #LeetCode #Cpp #DataStructures #Algorithms #TwoPointers
In-place string reversal with two-pointer pass
More Relevant Posts
-
Day#2 🚀 Solved: LeetCode 344 – Reverse String | Clean In-Place Solution Revisited one of the simplest yet most fundamental problems in string manipulation. 🔹 Problem: Reverse a character array in-place without using extra memory. 💡 Key Idea – Two Pointers • Start with one pointer at the beginning and one at the end • Swap elements and move inward • Continue until both pointers meet ⏱️ Complexity • Time: O(n) • Space: O(1) 🧠 What I learned : 🔹 Simplicity scales No need for extra data structures—clean in-place swapping does the job efficiently. 🔹 Patterns repeat Two-pointer technique shows up everywhere: strings, arrays, linked lists. 🔹 In-place thinking matters Optimizing space is often as important as optimizing time in real-world systems. 🔁 Sometimes revisiting “easy” problems strengthens the foundation for solving complex ones. #LeetCode #SoftwareEngineering #CodingInterview #Algorithms #DataStructures #ProblemSolving
To view or add a comment, sign in
-
🎯Day 6 of #150DaysOfCode 🚀 👨💻 Platform: #LeetCode 🧠 Today’s Problem: ⚡ 6. Maximum Subarray Given an integer array nums, find the subarray with the largest sum, and return its sum. Example 1: Input: nums = [-2,1,-3,4,-1,2,1,-5,4] Output: 6 Explanation: The subarray [4,-1,2,1] has the largest sum 6. Example 2: Input: nums = [1] Output: 1 Explanation: The subarray [1] has the largest sum 1. Example 3: Input: nums = [5,4,-1,7,8] Output: 23 Explanation: The subarray [5,4,-1,7,8] has the largest sum 23.
To view or add a comment, sign in
-
-
🚀 Cracked one of the toughest 🔥 problems on LeetCode — Median of Two Sorted Arrays." 💡 Problem: Find the median of two sorted arrays in O(log (m+n)) time. 🧠 Key Insight: Instead of merging arrays (O(n)), I used Binary Search + Partitioning to split both arrays such that: ✔ Left half contains equal elements ✔ All left elements ≤ right elements ⚙️ Approach Highlights: ✅ Binary search on smaller array ✅ Handle edge cases using ±∞ ✅ Partition validation logic ✅ Works for both even & odd lengths 🔥 Journey: After 3 attempts, finally nailed this problem 💪 Each attempt taught something new — edge cases, partition logic, and precision. ⚡ Result: ✔ Accepted ✅ ⚡ Runtime: 0 ms (Beats 100%) 💻 Clean & optimal solution 🎯 What I learned: Thinking beyond brute force is key Binary search isn’t just for searching Edge cases define correctness 💬 Challenge for you: Can you solve this without merging arrays? 😉 #LeetCode #DataStructures #Algorithms #BinarySearch #CodingInterview #SoftwareEngineer #ProblemSolving #TechInterview #100DaysOfCode
To view or add a comment, sign in
-
-
🌲 Day 50/90: Cracking the Code on Binary Tree Views! We are officially past the halfway mark! 🚀 Today was all about changing perspectives—literally. Dealing with Binary Trees can get dizzying, but once you master the "views," the logic starts to feel like second nature. Here’s a breakdown of the logic I tackled today: 🔍 Problems Solved 1. Right Side View: Imagine standing to the right of the tree. Which nodes can you see? I used a modified DFS (Recursive) approach to prioritize the right child, ensuring I only grabbed the first node visible at each level. 2. Bottom-Left Most Element: This one is a classic. It’s not just the leftmost node; it’s the leftmost node on the very last level. A simple BFS (Level Order Traversal) made this a breeze. 3. Top View: This was the boss battle of the day! I used Vertical Level Order Traversal with a coordinate system (hd, level). By using a Map to store the first node encountered at each horizontal distance, I captured exactly what’s visible from the "sky." 💡 Key Takeaway: For tree "view" problems, Level Order Traversal is your best friend. If you can visualize the tree on a 2D coordinate plane, you can solve almost anything. Current Status: 50 days down, 40 to go. The momentum is real. #100DaysOfCode #DataStructures #Algorithms #BinaryTree #SoftwareEngineer #Vlog #CodingLife #LeetCode #TechJourney
To view or add a comment, sign in
-
🎯Day 5 of #150DaysOfCode 🚀 👨💻 Platform: #LeetCode 🧠 Today’s Problem: ⚡ 5. Product of Array Except Self Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i]. The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer. You must write an algorithm that runs in O(n) time and without using the division operation. Example 1: Input: nums = [1,2,3,4] Output: [24,12,8,6] Example 2: Input: nums = [-1,1,0,-3,3] Output: [0,0,9,0,0]
To view or add a comment, sign in
-
-
LeetCode Daily | Day 84 🔥 LeetCode POTD – 1559. Detect Cycles in 2D Grid (Medium) ✨ 📌 Problem Insight Given a 2D grid of characters: ✔ Move in 4 directions (up, down, left, right) ✔ Only move to cells with same value ✔ Detect a cycle of length ≥ 4 ✔ Cannot go back to immediate previous cell 🔍 Initial Thinking – Brute DFS ⚙️ 💡 Idea: ✔ Start DFS from every cell ✔ Track visited path ⚠️ Concern: ✔ Might revisit parent → false cycle ✔ Need a way to avoid backtracking confusion 💡 Key Observation 🔥 ✔ This is graph cycle detection in a grid ✔ While moving, track parent cell ✔ If we reach a visited cell ≠ parent → cycle found ✅ 🚀 Optimized Approach ✔ Use DFS with: → visited matrix → parent tracking (px, py) ✔ For each neighbor: → If not visited → continue DFS → If visited and not parent → cycle exists 🔧 Core Idea ✔ Grid = graph ✔ Same values = connected component ✔ Cycle detection = revisit node (not parent) ⏱ Complexity ✔ Time: O(m × n) ✔ Space: O(m × n) 🧠 Key Learning ✔ Always track parent in DFS cycle problems ✔ Grid problems often map to graph concepts ✔ Avoid false cycles by ignoring immediate back edge 🚀 Takeaway When dealing with grids, think like a graph — a small tweak like tracking parent turns a tricky problem into a standard cycle detection pattern ⚡ #LeetCode #DSA #Algorithms #GraphTheory #CPlusPlus #CodingJourney
To view or add a comment, sign in
-
-
Day 46 of my #50DaysOfCode challenge is done ✅ 📌 Problem Solved Geometric Sum using Recursion We were given an integer n. Task was to find the sum: 1 + 1/3 + 1/3² + ... + 1/3ⁿ Using recursion. 💻 Approach 🔹️Define a recursive function sum(n). 🔹️Base case: ▪️If n == 0 → return 1 🔹️Recursive case: ▪️Return sum(n-1) + 1/(3ⁿ) Each call adds one term. And moves toward smaller n. 📊 Complexity Analysis Time Complexity: O(n) Space Complexity: O(n) Due to recursion stack. 📚 What I learned today: ▫️Recursion can be used for summation of series. ▫️Each recursive call adds one term to the result. ▫️Understanding base case is important to stop recursion. ▫️Power calculations are common in series problems. Day 46 completed. Getting more comfortable with recursive formulas 🚀 #50DaysOfCode #CodingChallenge #Consistency #LearningInPublic
To view or add a comment, sign in
-
Claude-Controller Utility - An Update --------------------------------------------------------------- I'm continuing my experiments with the Claude Code wrapper and different models, which I had started as a weekend project. After getting multi-model switching working with Claude Code, I ran it for real across the last few days. Four backends, some real tasks, and the session logs that Claude Code quietly writes to disk after every run. Here's what the data actually showed: ✅ claude-sonnet-4.6 (native) — the only backend with a clean record. 92% prompt cache efficiency, 3-minute sessions, correct tool calls throughout. ✅ devstral-small-2:24b (Ollama, local) — 52 tok/s on a liquid-cooled 3090, but only after tuning the context window to 49152. At 65536 context, VRAM headroom was ~0.2 GB, and the model silently spilled to RAM. One number made all the difference. ⚠️ qwen3-coder:131k (Ollama) — reliable, but 33 minutes needed for what Claude-sonnet does in 3. Usable when I have time and need a wide context. The biggest surprise wasn't these failures — it was the JSONL files. Claude Code writes a detailed session log for every run: every tool call, every token count, cache hit rates, error injections. Once I started reading them, I could see exactly why each session succeeded or failed. Two small tools came out of this: 1. cc-diagnose — a live Ollama profiler. Before a session, it checks GPU offload %, VRAM headroom, KV cache allocation at different context sizes, token speed, and whether the model is reloading cold between requests. This is what caught the devstral VRAM spill issue. 2. cc-health — a small Python tool that automatically detects loop patterns, incorrect parameters, zero-output sessions, and cache inefficiencies in the logs. Full write-up in the comments 👇 #ClaudeCode #LocalLLM #Ollama #AIEngineering #DeveloperTools
To view or add a comment, sign in
-
61 of #100DaysOfCode 🚀 Solved LeetCode 907 – Sum of Subarray Minimums using an optimized Monotonic Stack + Contribution Technique 🔥 🔍 Approach: Instead of generating all subarrays, I focused on how each element contributes as the minimum in different subarrays. 👉 For every element: • Find how far it can extend to the left (Previous Smaller Element) • Find how far it can extend to the right (Next Smaller Element) 📌 Using a monotonic increasing stack: • Left array stores distance to previous smaller element • Right array stores distance to next smaller element 💡 Formula Used: Each element contributes: arr[i] * left[i] * right[i] ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(n) #LeetCode #Stack #Algorithms #DataStructures #CodingJourney #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
-
-- Solved LeetCode 100: Same Tree -- Today I worked on the classic binary tree problem “Same Tree”, and it turned out to be a great exercise in strengthening recursion fundamentals -- Problem Summary: -- Given two binary trees, determine whether they are identical in both structure and node values. -- Approach (Recursive DFS): At each node, I focused on 3 key checks: • If both nodes are NULL → trees match at this point • If one is NULL → structure mismatch • If values differ → not the same If all checks pass, recursively compare left and right subtrees. -- Complexity: Time: O(n) – visiting every node once Space: O(h) – recursion stack (depends on tree height) Consistency > Complexity. #LeetCode #DSA #Recursion #BinaryTree #CodingJourney #SoftwareEngineering #algorithm
To view or add a comment, sign in
-
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