🚀 Day 63 of #100DaysOfCode Today I tackled LeetCode 84 — Largest Rectangle in Histogram using C++ 💪 📌 Problem Summary: Given an array representing the heights of bars in a histogram, the goal is to find the largest rectangular area that can be formed within the histogram. For example: Input: [2,1,5,6,2,3] Output: 10 (formed between heights 5 and 6) 📌 Approach: This is a classic stack-based problem involving Next Smaller Element (NSE) and Previous Smaller Element (PSE) logic. Steps: 1️⃣ Use a monotonic stack to find the index of the next smaller element for each bar. 2️⃣ Similarly, find the previous smaller element for each bar. 3️⃣ Compute width = nsi[i] - psi[i] - 1 4️⃣ Calculate area = height[i] * width and keep track of the maximum. 📌 Complexity: ⏱ Time: O(n) — each element is pushed and popped once. 💾 Space: O(n) — for storing stack and helper arrays. 📌 Key Learning: This problem strengthens the understanding of monotonic stacks, boundary calculations, and range-based area determination — a foundation for many advanced DSA problems (like Maximal Rectangle). Consistency pays off — 60 days strong and still coding daily! 🔥 Raj Vikramaditya Raghav Garg Nancy Solanki Shweta Arora Harsh Raj Harshita Verma Love Babbar Prince Singh Shivam Mahajan Rohit Negi Neeraj Walia Nishant Chahar Kushal Vijay #LeetCode #100DaysOfCode #CodingChallenge #Cplusplus #DataStructures #LinkedList #ProblemSolving #SoftwareEngineering #Developers #Programming #TechJourney #KeepLearning #DailyCoding #CodeNewbie#LeetCode #100DaysOfCode #Programming #Cplusplus #LinkedList #ProblemSolving #SoftwareEngineering #TechJourney #CodeNewbie #DSA
Solved LeetCode 84: Largest Rectangle in Histogram with C++
More Relevant Posts
-
🟩 Day 56 of Leetcode 150 days challenge – Add Two Numbers (LeetCode #2) #LeetCode150 #Day56 #AddTwoNumbers #LinkedList #DataStructures #Algorithms #ProblemSolving #LeetCode #CodingChallenge #CPP #Programming #150DaysOfCode #LearnDSA #SDEPrep #CodingJourney 🔹 Problem: Given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each node contains a single digit. Add the two numbers and return the sum as a linked list. Example: Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 Explanation: 342 + 465 = 807 🧠 Approach: We perform the addition just like we do on paper: Initialize a carry as 0. Traverse both lists simultaneously. Add the digits from both lists + carry. Store the last digit in a new node. Update carry = sum / 10. Continue until both lists are done and carry is 0. 🕒 Time Complexity: O(max(M, N)) 💾 Space Complexity: O(max(M, N)) 🏁 Key Takeaway: Linked list addition is a classic problem that strengthens your understanding of pointer manipulation, carry handling, and edge cases like unequal list lengths. 💡
To view or add a comment, sign in
-
-
🚀 Day 46 of 150 Days LeetCode Challenge #150DaysOfCode #LeetCode #CodingChallenge #DSA #HashMap #CPlusPlus #Programming #SoftwareEngineering #TechLearning #ProblemSolving #CodeDaily #LeetCode150DaysChallenge #Algorithm #DataStructures 🔹 Problem Statement:Contains Duplicate II Given an integer array nums and an integer k, check if there exist two distinct indices i and j in the array such that: nums[i] == nums[j] |i - j| <= k In simple words: “Does the array contain a duplicate within a distance of k?” 🔹 Example: Input: nums = [1,2,3,1], k = 3 Output: true Explanation: nums[0] and nums[3] are both 1, and the distance between them is 3 ≤ k. 🔹 Approach: Use a hash map to store each number with its most recent index. Iterate through the array: If the number already exists in the map, calculate the distance between the current index and the previous index. If this distance ≤ k, return true. Update the map with the current index. Return false if no such pair is found. Time Complexity: O(n) | Space Complexity: O(n) 🔹 Key Insights: ✅ Hash maps allow O(1) lookups for duplicates. ✅ Always update the index to track the most recent occurrence. ✅ Brute-force approaches take O(n²), but hash maps reduce it to O(n).
To view or add a comment, sign in
-
-
🚀 Day 66 of #100DaysOfCode Today’s problem was LeetCode 155 — Min Stack 🧠💪 📌 Problem Summary: We need to design a stack that supports: push() pop() top() getMin() (returns minimum element in O(1) time) All operations must be O(1) — that’s the tricky part. ⚡ 📌 Approach: 👉 Use a single stack to store modified values 👉 Track the minimum separately using a variable 👉 When pushing a smaller value, encode it cleverly so you can retrieve the previous min during pop() 💡 Key Insight: When inserting a value smaller than the current minimum: st.push(2*x - min) min = x This way, the stack encodes both data and min info efficiently. 📌 Complexity: ⏱ Time: O(1) for all operations 💾 Space: O(n) ✨ Learning: This is a great example of space optimization and bit manipulation logic — classic low-level trick that feels like magic the first time you see it! ⚙️ Raj Vikramaditya Raghav Garg Nancy Solanki Shweta Arora Harsh Raj Harshita Verma Love Babbar Prince Singh Shivam Mahajan Rohit Negi Neeraj Walia Nishant Chahar Kushal Vijay #LeetCode #100DaysOfCode #CodingChallenge #Cplusplus #DataStructures #LinkedList #ProblemSolving #SoftwareEngineering #Developers #Programming #TechJourney #KeepLearning #DailyCoding #CodeNewbie#LeetCode #100DaysOfCode #Programming #Cplusplus #LinkedList #ProblemSolving #SoftwareEngineering #TechJourney #CodeNewbie #DSA
To view or add a comment, sign in
-
-
📅 Day 47 of #100DaysOfCode Problem: Combination Sum II (LeetCode 40) Approach: 1️⃣ Sorted the array to handle duplicates easily. 2️⃣ Used recursion to explore each number — either include it or skip it. 3️⃣ Reduced the target as I added elements, and when the target hit 0, that path became a valid combination. 4️⃣ Skipped duplicates using a simple check: if (i > idx && candidates[i] == candidates[i-1]) continue. #100DaysOfCode #LeetCode #DSA #ProblemSolving #Backtracking #Recursion #Cplusplus #CodingChallenge #Algorithms #Programming #DeveloperLife #CodeNewbie #SoftwareEngineering #DailyDSA #CodingJourney #KeepLearning #TechCommunity #Motivation #GrowthMindset
To view or add a comment, sign in
-
-
While experimenting with nested classes, I tried using a static class in C++ — but realized that it’s not valid syntax! Here’s what I learned 👇 🚫 static class is not allowed in C++. The static keyword can only be used with: Variables (to make them retain value or be class-level) Functions (to call them without an object) Global variables/functions (to limit scope within a file) ✅ Correct way to define a nested class: #include <iostream> using namespace std; class A { public: class A1 { public: int x; A1() { x = 10; } }; A1 obj; // Object of inner class }; int main() { A obj1; cout << obj1.obj.x; // Output: 10 } 🧠 Takeaway: C++ allows nested classes, but not static class. static is used only with data members, member functions, and local variables, not with class declarations. #Cplusplus #LearningJourney #Programming #OOPs #Coding #Developers #CppLearning
To view or add a comment, sign in
-
🚀 Day 16 of my 120 Days LeetCode Challenge! 🧩 Problem 16: 3Sum Closest (Medium) The problem statement is: Given an integer array nums and an integer target, find three integers in nums such that their sum is closest to the target. Return the sum of these three integers. You may assume that each input would have exactly one solution. 💡 Example: Input: nums = [-1, 2, 1, -4], target = 1 Output: 2 Explanation: The sum closest to 1 is 2 (-1 + 2 + 1 = 2). ⚙️ Algorithm Used: For this problem, I implemented a two-pointer approach after sorting the array. Here’s the step-by-step logic: Sort the array to arrange elements in increasing order. Fix one element and then use two pointers — one starting from the next index (left) and another from the end (right). Calculate the sum of these three elements. If the current sum is closer to the target than the previously recorded closest sum, update the closest sum. Move pointers accordingly: If currentSum < target, move left++ (to increase sum). If currentSum > target, move right-- (to decrease sum). If an exact match is found, return it immediately since it’s the best possible. ⏱ Time Complexity: O(n²) 💾 Space Complexity: O(1) 🔥 This problem helped me strengthen my understanding of sorting and two-pointer techniques for array-based problems. I’m steadily progressing with consistency and learning something new every day! 💪 #Day16 #LeetCode #CodingChallenge #Cplusplus #DSA #ProblemSolving #TwoPointer #LearningJourney #100DaysOfCode #Programming
To view or add a comment, sign in
-
-
Master the most essential Hash Map and Set problems on LeetCode — from simple frequency checks to advanced logic patterns. Each tutorial walks you step by step through clean, modern C++ solutions, explained visually and intuitively. 🔹 Ideal for coding interview preparation 🔹 Covers both Easy and Medium problems 🔹 Topics: frequency maps, sets, hash tables, string comparison Patterns. Logic. Frequency. Learn to recognize them all in this focused Hash Map & Set Series in C++ — from easy to medium, explained clearly and efficiently. 🎥 Watch the full playlist 👉 https://lnkd.in/dd_chzrm #LeetCode #Cplusplus #Programming #DataStructures #Algorithms #CodingInterview #LANAcademy #LearnToCode
To view or add a comment, sign in
-
-
🎯 Day 100 — Core CS Subjects: Object-Oriented Programming in C++ (Constructors, Destructors & ‘this’ Pointer) Hitting 💯 days of consistent learning in Core CS Subjects! Today’s milestone was all about understanding how C++ manages object lifecycle — from creation to destruction — using constructors, destructors, and the ‘this’ pointer. 🚀 Constructors in C++ Constructors are special functions automatically invoked when an object is created. They share the same name as the class and have no return type. 🧱 Types of Constructors: 1️⃣ Default Constructor – Created automatically if no other constructor exists. 2️⃣ Parameterized Constructor – Initializes objects with custom values. 3️⃣ Copy Constructor – Creates a new object as a copy of another. 4️⃣ Move Constructor – Transfers ownership of resources (using std::move), avoiding deep copies. 💡 Tip: Constructors can be overloaded (known as Constructor Overloading) — defined multiple times with different parameter lists. 🧩 Destructors in C++ A Destructor (~ClassName) is automatically called when an object is destroyed — cleaning up resources like dynamically allocated memory. 📌 Key Facts: Only one destructor per class. Called in reverse order of creation. Essential when handling pointers or dynamic memory (to prevent memory leaks). 👆 ‘this’ Pointer in C++ The ‘this’ pointer refers to the current object — helping distinguish between local variables and class members, and enabling method chaining. 💬 Example: this->age = age; return *this; It’s implicitly available in all non-static member functions and helps write cleaner, more flexible code. 💭 Takeaway: Understanding constructors, destructors, and the ‘this’ pointer gives you precise control over object behavior and memory management — the foundation of robust, efficient C++ design. 📸 Screenshot of my learning progress 🔗 Course: https://lnkd.in/gmYrzVYY 🙏 Thanks: GeeksforGeeks #skillupwithgfg #nationskillup #Day100 #CoreCS #OOP #CPlusPlus #Constructors #Destructors #ThisPointer #ObjectOrientedProgramming #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
Master the most essential Hash Map and Set problems on LeetCode — from simple frequency checks to advanced logic patterns. Each tutorial walks you step by step through clean, modern C++ solutions, explained visually and intuitively. 🔹 Ideal for coding interview preparation 🔹 Covers both Easy and Medium problems 🔹 Topics: frequency maps, sets, hash tables, string comparison Patterns. Logic. Frequency. Learn to recognize them all in this focused Hash Map & Set Series in C++ — from easy to medium, explained clearly and efficiently. 🎥 Watch the full playlist 👉 https://lnkd.in/dMv6jfNp #LeetCode #Cplusplus #Programming #DataStructures #Algorithms #CodingInterview #LANAcademy #LearnToCode
To view or add a comment, sign in
-
-
🚀 Day 61 of my 100DaysOfDSA 🚀 Today, I explored one of the most fascinating and powerful concepts in Object-Oriented Programming — Polymorphism and Virtual Functions in C++ 👨💻 🔹 Polymorphism — It allows a single function, operator, or object to behave differently based on the context. In simple terms, it means “one interface, many forms.” There are two main types: Compile-time (Static) Polymorphism: Achieved through function overloading and operator overloading. Runtime (Dynamic) Polymorphism: Achieved using virtual functions and inheritance. 🔹 Virtual Functions — These are special member functions in a base class that can be overridden in derived classes to provide specific behavior. When a base class pointer points to a derived class object and calls a virtual function, the derived class’s version is executed — thanks to the virtual table (vtable) mechanism in C++. 🔹 Key Rules: ✅ Virtual functions must be declared in the base class using the virtual keyword. ✅ They work only through base class pointers or references. ✅ Constructors cannot be virtual, but destructors should be when using inheritance. 🔹 Real-Life Analogy: Think of a “Remote Control” (base class) that can operate different devices — TV, AC, or Music System (derived classes). The same button (function call) performs different actions depending on the device — that’s polymorphism in action! 💡 Why it matters: Polymorphism promotes flexibility, reusability, and scalability in software design — making your code truly object-oriented and modular. 🔥 Key Takeaway: “Polymorphism isn’t just a feature — it’s the essence of OOP that allows code to evolve gracefully.” #Cplusplus #OOP #100DaysOfCode #Polymorphism #VirtualFunctions #DSA #CodingJourney
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