LeetCode Problem 11: Container With Most Water What I Learned: • How the area is determined by the minimum height of the two lines, not the taller one • Why brute force (checking all pairs) is inefficient for large inputs • How the two-pointer approach reduces time complexity from O(n²) to O(n) • Why moving the pointer with the smaller height is the key optimization • How to balance width and height to maximize the container area • Translating a geometric problem into clean pointer-based logic Problem Summary: You’re given an array where each element represents the height of a vertical line. The task is to choose two lines such that, together with the x-axis, they form a container that can store the maximum amount of water. This problem is a great example of how understanding constraints leads to an optimal strategy instead of brute-force thinking. #100DaysOfLeetCode #leetcode #javascript #problemsolving #codingjourney #DSA #TwoPointers #AlgorithmicThinking Link: https://lnkd.in/gGPwsZb4
Maximizing Container Area with Two-Pointer Approach
More Relevant Posts
-
LeetCode Problem: Longest Common Prefix What I Learned: • How nested loops simulate 2D comparison (character index vs string index) • Comparing each string character with the first string as a reference • Using the OR operator (||) to handle multiple stopping conditions • Understanding short-circuit evaluation to prevent unnecessary checks • How .slice(0, i) extracts the valid prefix before mismatch • Why return immediately stops the entire function execution • Handling edge cases like complete match or no common prefix • Writing an efficient O(n × m) solution using simple iteration logic Problem Summary: You’re given an array of strings and need to find the longest common prefix among them. The core idea is: Compare each character of the first string With the same position in all other strings. If: • A string becomes shorter • OR characters don’t match → Stop immediately and return the prefix until that index. If no mismatch is found after full iteration → return the entire first string. This problem strengthened my understanding of control flow, string indexing, and early returns in JavaScript. A great example of breaking execution at the right time instead of overcomplicating logic. #100DaysOfLeetCode #leetcode #javascript #problemsolving #codingjourney #DSA #StringManipulation #AlgorithmicThinking Link: https://lnkd.in/gnvmJK-r
To view or add a comment, sign in
-
-
Rotate Without Extra Space: Think in Transformations 🔄 Solved LeetCode 48 - Rotate Image today. The elegant trick: 1. Transpose the matrix (swap rows ↔ columns) 2. Reverse each row That’s it. No extra matrix needed. Instead of simulating rotation element by element, the problem becomes a composition of two simple transformations. Key takeaway: Hard matrix problems often reduce to recognizing the right geometric transformation. When you see the pattern, the implementation becomes clean and in-place. Another strong boost to 2D intuition. #LeetCode #DSA #Matrix #InPlaceAlgorithm #JavaScript #ProblemSolving #LearnInPublic
To view or add a comment, sign in
-
I thought I understood map(), filter() and reduce()… until I wrote their polyfills. We use these methods daily in JavaScript. But I wanted to understand what actually happens behind the scenes. Building polyfills is one of the best ways to master: 1️⃣ Prototypes: How inheritance actually works in JS. 2️⃣ Execution Context: Mastering the behavior of this. 3️⃣ Edge Cases: Handling reduce() accumulators without an initial value. I’ve put together a deep dive into these internals. What’s inside: The logic behind Array.prototype. Step-by-step custom implementations. The "hidden" index logic in .reduce(). Which polyfill was the hardest for you to learn? Let’s discuss below! #Javascript #Chai aur Code #Polyfills
To view or add a comment, sign in
-
## DAY 2 — Captcha Generator Day 2/30: Built a Captcha Generator from scratch. It creates randomized text and validates user input against it. No libraries, no APIs — just canvas rendering and string manipulation. The tricky part: making the captcha distorted enough to be hard to OCR but readable enough for humans. Turns out that balance is harder than it sounds. 🔗 Live: https://lnkd.in/gPzprnKc 🔗 Code: https://lnkd.in/gbqKTYrp #30DaysOfCode #JavaScript #WebDevelopment #BuildInPublic #WomenInTech
To view or add a comment, sign in
-
-
Fixing an Unbalanced Tree the Systematic Way 🌳 Solved LeetCode 1382 - Balance a Binary Search Tree today. The approach was clean and systematic: -Use inorder traversal to extract nodes in sorted order -Pick the middle element as the root -Recursively build left and right subtrees This guarantees a height-balanced BST without complex rotations. Key takeaway: Sometimes the best way to fix a structure is to rebuild it with the right invariants, not patch it incrementally. This problem nicely connected traversal, sorting, and divide-and-conquer thinking. #LeetCode #DSA #BinarySearchTree #TreeTraversal #DivideAndConquer #JavaScript #ProblemSolving #LearnInPublic
To view or add a comment, sign in
-
When Division Is Not Allowed: Thinking in Prefix and Suffix 📊 Solved LeetCode 238 – Product of Array Except Self today. The naive solution would use division, but the constraint pushes you to think differently. The elegant approach: - Build a prefix product array (product of elements to the left) - Build a suffix product pass (product of elements to the right) - Combine them in O(n) time without extra division No nested loops. No division. Linear time. Key takeaway: Many array problems become simple when you think in terms of precomputed cumulative information. 💡 Practical insight: Whenever you see “for each element, compute something excluding itself”, think: - Prefix - Suffix - Two-pass strategy This pattern shows up in range queries, cumulative sums, and DP optimizations. #LeetCode #DSA #Arrays #JavaScript #Algorithms #ProblemSolving #LearnInPublic
To view or add a comment, sign in
-
Day 9: The Variable Environment (Hoisting and TDZ) Today was really about understanding the variable environment, like what actually happens before JavaScript runs my code I got to understand JavaScript doesn’t just execute line by line immediately. Before any code runs, JavaScript scans the scope for variable declarations and sets aside space for them in the variable environment object. Nothing physically moves, it's just JavaScript doing preparation work ahead of execution. That's why some variables appear usable before the line they're written on. This setup phase is what we call hoisting. What is Hoisting? Hoisting makes some types of variables accessible and usable in the code before they are actually declared, as if the variable were defined at the top of their scope. The diagram below illustrates how each type of variable works; #JavaScriptJourney #LearningToCode
To view or add a comment, sign in
-
-
I tried implementing the internal working of forEach and map to better understand how they operate under the hood. Both are higher order functions because they accept another function as an argument. Internally, they iterate over the array (typically via a loop) and execute the callback for each element. forEach : Iterates through the array. Executes the provided callback on each element. Does not return a new array (returns undefined). Primarily used for logging map : Iterates through the array. Executes the callback on each element. Stores the callback’s return value in a new array. Returns the new transformed array. Does not mutate the original array. Rewriting these methods manually with a for loop really clarifies their behavioral difference : forEach is about execution, map is about transformation. #JavaScript #WebDevelopment #FunctionalProgramming #LearnInPublic #ChaiAurCode
To view or add a comment, sign in
-
-
build a small practice programme: unit convertor Registry Pattern: Instead of massive if/else chains, I used a lookup object (reg) to define unit types and factors. Base Unit Normalization: I converted everything to a "Base Unit" first (e.g., meters), then to the target. This avoids needing a unique formula for every possible pair (like km -> cm). Type Validation: I added a strict check (from.type !== to.type) to prevent incompatible conversions, like trying to convert Weight to Length. Handling Exceptions: Since Temperature requires offsets (like +32) and not just multiplication factors, I separated it into its own switch case logic. #Javascript #WebDev #LearningInPublic #Coding
To view or add a comment, sign in
-
-
n8n expression gotcha: Why your ternary isn't working This looks right but fails: {{ $json.count ? "Has items" : "Empty" }} Problem: 0 is "falsy" in JavaScript. If count is 0, it shows "Empty" even though it's a valid value. Fix - be explicit: {{ $json.count > 0 ? "Has items" : "Empty" }} JavaScript truthy/falsy catches everyone eventually. Full pitfalls list: https://lnkd.in/gs2RKUyp #n8n #automation #javascript
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