📌 Day 2/100 – #100DaysOfDSA Continuing the journey 🚀 🔍 Problem Solved: Best Time to Buy and Sell Stock (LeetCode 121) 🧠 What I learned: How to track the minimum price so far while iterating Calculating maximum profit in a single pass Optimizing from brute force O(n²) → O(n) ✅ Solution Approach: Traverse the array once 👣 Update the minimum price Calculate profit at each step and track the maximum 📈 💡 Key Insight: Instead of checking all pairs, track the lowest price before the current day 💻 Code (JavaScript): var maxProfit = function (prices) { let min = prices[0]; let maxProfit = 0; for (let i = 0; i < prices.length; i++) { if (min > prices[i]) { min = prices[i]; } if (prices[i] - min > maxProfit) { maxProfit = prices[i] - min; } } return maxProfit; }; ⚡ Takeaway: A simple observation can drastically reduce complexity — that’s the power of DSA Consistency is the goal — Day 2 done ✅ If you're also solving daily, let’s connect and grow together 🤝 #100DaysOfDSA #DataStructures #Algorithms #LeetCode #Programming #DeveloperJourney #ProblemSolving
Best Time to Buy and Sell Stock LeetCode 121 Solution
More Relevant Posts
-
🚀 Day 8/100 – #100DaysOfDSA Today’s challenge pushed me to go beyond basic sorting and think about efficient algorithms with optimal time complexity. 🔹 Problem Solved: Sort an Array(Merge sort) (without using built-in functions) 💡 Key Learning: 👉 To achieve O(n log n) time complexity, we need to use advanced sorting algorithms like: Merge Sort (Divide & Conquer) Quick Sort (Partition-based approach) 👉 Approach I focused on: Merge Sort Divide the array into halves Recursively sort each half Merge the sorted halves ✅ Time Complexity: O(n log n) ✅ Stable sorting algorithm ⚠️ Space Complexity: O(n) (extra space required) 🔥 Alternative: Quick Sort Faster in practice (on average) Works in-place ⚠️ Worst case: O(n²) ✅ Average: O(n log n) 🔥 What I learned today: Not all sorting algorithms are equal — choosing the right one depends on constraints like time, space, and input size. Moving from basic → advanced concepts step by step 🚀 #100DaysOfCode #DSA #Sorting #MergeSort #QuickSort #ProblemSolving #CodingJourney #JavaScript #TechGrowth #SoftwareEngineer #LearningInPublic
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
-
-
🚀 LeetCode — 207. Course Schedule Solved | Medium | Graph | Cycle Detection (DFS) 🔗 Solution Link: https://lnkd.in/gNerrUfM At first, this doesn’t look like a graph problem. But once you model prerequisites as edges, it becomes a directed graph: b → a (to take a, you must complete b) 💡 Core Idea The question reduces to: Can we complete all courses? → Equivalent to: Does the graph contain a cycle? If there’s a cycle → impossible to finish all courses. 🧠 Approach (DFS + State Tracking) Initially, I tried applying undirected graph cycle logic — but that doesn’t work here. In directed graphs, we need an extra state: 0 → not visited 1 → visited 2 → in recursion stack (instack) While doing DFS: If we visit a node already in instack, we found a cycle If visited but not in stack → safe After exploring, remove it from stack (backtrack) This “instack” idea is the key difference from undirected graphs. 📈 Complexity Time: O(V + E) Space: O(V) A classic problem that teaches the subtle difference between undirected vs directed cycle detection. #LeetCode #Graph #DFS #CycleDetection #TopologicalSort #DSA #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
📄 New preprint published on Zenodo today. FlowBridge: A Zero-Cost Generalized Framework for Cross-Platform Email-to-Messaging Automation Using Browser Automation in Containerized Virtual Display Environments. 5 named subsystems: → DedupGuard — multi-signal deduplication → ClipCast — Unicode injection bypassing ChromeDriver BMP limit → SessionHeal — self-healing browser sessions in Docker → SheetConfig — spreadsheet-as-configuration → BatchForge — message aggregation engine Production result: 100% delivery accuracy, zero duplicates, $0/month infrastructure. Sole-author preprint #5. Open source. MIT license. DOI: https://lnkd.in/gidKy6BQ GitHub: https://lnkd.in/ge3RaEUr Full deep-dive article coming this week. #FlowBridge #OpenSource #Research #Zenodo #Automation #Docker #Python
To view or add a comment, sign in
-
Continuing my 100 Days of DSA journey. Day 69 — LeetCode (Balanced Binary Tree) Balanced Binary Tree – Given a binary tree, determine if it is height-balanced (i.e., the heights of left and right subtrees of every node differ by no more than 1). Approach (DFS + Height Optimization): a) Use a recursive function to calculate the height of each subtree b) For each node, recursively get the height of left and right subtrees c) If any subtree returns -1, propagate -1 upwards (indicating imbalance) d) Check if the absolute difference between left and right heights is greater than 1 e) If unbalanced, return -1 immediately f) Otherwise, return 1 + max(leftHeight, rightHeight) Time Complexity: O(n) Space Complexity: O(h) (recursion stack) On to Day 70... #100DaysOfCode #DSA #LeetCode #Cpp #Algorithms #CodingJourney #ProblemSolving #SoftwareEngineering #InterviewPrep #LearningInPublic #geeksforgeeks #Microsoft #Greedy #Strings #Optimization
To view or add a comment, sign in
-
-
🚀 𝐃𝐚𝐲 𝟒𝟖/𝟏𝟎𝟎 – #𝟏𝟎𝟎𝐃𝐚𝐲𝐬𝐎𝐟𝐋𝐞𝐞𝐭𝐂𝐨𝐝𝐞 Almost halfway to the century mark! Today’s challenge was all about array reorganization and interleaving, and I’m keeping the streak alive with another 𝟎𝐦𝐬 (𝐁𝐞𝐚𝐭𝐬 𝟏𝟎𝟎%) solution! ✅ 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐨𝐥𝐯𝐞𝐝: Shuffle the Array 🧠 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐭𝐚𝐭𝐞𝐦𝐞𝐧𝐭: Given an array nums consisting of $2n$ elements in the form $[x_1, x_2, ..., x_n, y_1, y_2, ..., y_n]$, return the array in the form $[x_1, y_1, x_2, y_2, ..., x_n, y_n]$. 💡 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡 𝐔𝐬𝐞𝐝: • 𝐒𝐢𝐧𝐠𝐥𝐞 𝐏𝐚𝐬𝐬 𝐈𝐭𝐞𝐫𝐚𝐭𝐢𝐨𝐧: Since the array is split into two halves of size $n$, I iterated from $0$ to $n-1$. • 𝐏𝐚𝐢𝐫𝐞𝐝 𝐈𝐧𝐬𝐞𝐫𝐭𝐢𝐨𝐧: In each step of the loop, I simultaneously picked the element from the first half ($i$) and its corresponding element from the second half ($i+n$). • 𝐕𝐞𝐜𝐭𝐨𝐫 𝐏𝐮𝐬𝐡: I used ans.push_back(nums[i]) followed by ans.push_back(nums[i+n]) to interleave the elements in the required order. 📊 𝐓𝐢𝐦𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: $O(n)$ — We process the $2n$ elements in a single linear pass. 🗂 𝐒𝐩𝐚𝐜𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: $O(n)$ — To store the $2n$ shuffled elements in a new result vector. ✔️ 𝐒𝐭𝐚𝐭𝐮𝐬: Accepted (𝟎𝐦𝐬 - 𝐁𝐞𝐚𝐭𝐬 𝟏𝟎𝟎%) 📍 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠: This problem highlights the importance of index mapping. By identifying the relationship between the two halves of the array ($i$ and $i+n$), we can perform the transformation efficiently without complex swaps. 𝟒𝟖/𝟏𝟎𝟎 𝐜𝐨𝐦𝐩𝐥𝐞𝐭𝐞. Small steps every day lead to big results! 🚀 #LeetCode #DSA #CodingJourney #ProblemSolving #Consistency #100DaysOfCode #Cpp #Algorithm #SoftwareEngineering #NavneetRaj
To view or add a comment, sign in
-
-
🚀 Day 11 of My LeetCode Journey Today’s problem: Container With Most Water This problem looked tricky at first, but the solution turned out to be a clean application of the Two Pointer technique. 💡 Key Idea: Start with two pointers at both ends Calculate area using width × minimum height Move the pointer with smaller height to maximize area 🔍 Instead of brute force O(n²), optimized it to O(n) 🚀 ⚡ Result: Accepted ⏱️ Runtime: 60 ms 🧠 What I Learned: Two Pointers can drastically reduce complexity Always think about optimizing from both ends Logic > brute force #LeetCode #DSA #CodingJourney #100DaysOfCode #TwoPointers #Algorithms #ProblemSolving
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
-
🚀 Today’s LeetCode Solve: 3Sum (Problem #15) Today I solved LeetCode 15 – 3Sum, a classic problem that strengthens two-pointer and sorting concepts. 🔍 Problem summary: Given an array, find all unique triplets such that: nums[i] + nums[j] + nums[k] = 0 💡 Approach used: --> First, sort the array --> Fix one element and use two pointers (left, right) for the remaining part --> Skip duplicates to ensure only unique triplets --> Move pointers based on the sum: --> If sum < 0 → move left pointer --> If sum > 0 → move right pointer -->If sum == 0 → store result and move both ⚡ Efficiency: --> Time Complexity: O(n²) --> Space Complexity: O(1) (excluding result list) 📌 Key takeaway: Combining sorting + two-pointer technique is a powerful pattern for many array problems. Challenging problem, but great learning experience 💪 #LeetCode #DSA #3Sum #TwoPointers #ProblemSolving #Algorithms #CodingPractice #LearningInPublic
To view or add a comment, sign in
-
-
🚀 LeetCode 3741 : Minimum Distance Between Three Equal Elements II We are given an array and need to find indices (i, j, k) such that: nums[i] == nums[j] == nums[k] All indices are distinct The distance of a triple is: |i-j| + |j-k| + |k-i| We must return the minimum possible distance among all valid triples. 🔍 Key Insight For sorted indices ( i < j < k ): |i-j| + |j-k| + |k-i| = (j-i) + (k-j) + (k-i) which simplifies to: 2 * (k - i) This means the middle index doesn’t matter at all. The distance only depends on: 👉 the first occurrence 👉 the third occurrence So instead of checking every triple, we just track consecutive occurrences of each number. 🧠 Efficient Strategy While scanning the array: 1️⃣ Track the last two occurrences of each value. 2️⃣ When a new occurrence appears, it becomes the third candidate. 3️⃣ Compute the span between the current index and the earliest stored index. 4️⃣ Update the minimum. This effectively evaluates every valid triple window of size 3. ⚡ Why This Works Any optimal triple must consist of three consecutive occurrences of the same value. If we skip an occurrence, the span (k-i) only becomes larger, increasing the distance. So checking sliding windows of three identical values guarantees the optimal solution. 📈 Complexity Time Complexity: O(n) Space Complexity: O(n) (tracking last indices) Efficient enough for n up to 10⁵. 💡 Takeaway Many problems with absolute values become much simpler when you: • Sort the indices mentally • Expand the absolute expression • Look for cancellations Here, the entire expression collapses into a simple span calculation. #LeetCode #Algorithms #LearningInPublic #DSA #CompetitiveProgramming #ProblemSolving
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