Day 26 of 50 days of LeetCode The Problem: Best Time to Buy and Sell Stock. The Mistake: My initial logic used global max() and min() functions. This failed because it didn't account for time—you can't sell a stock before you've actually bought it! The Correction: I refactored the code to a single-pass approach. By tracking the min_price seen so far and updating the max_profit dynamically, I ensured the "buy" always happens before the "sell" while keeping the efficiency at O(n). #LeetCode #Python #ProblemSolving #CodingJourney #CSDesign
Best Time to Buy and Sell Stock LeetCode Solution
More Relevant Posts
-
New blog post! Live Life on the Edge: A Layered Strategy for Testing Data Models This post is about a three-layer testing pattern for complex software systems I've landed on in python: structural coverage with Polyfactory, value-level probing with Hypothesis, cross-field invariants with icontract. Includes a practical example, an honest tradeoffs section, and a note on what schema-first design and consumer-driven contract testing solve instead. Link in comments. #Python #SoftwareTesting #SoftwareArchitecture #Pydantic #PropertyBasedTesting
To view or add a comment, sign in
-
-
🚀 Day 75 of #100DaysOfCode 🔥 LeetCode 179 – Largest Number 💡 Problem: Given a list of non-negative integers, arrange them such that they form the largest possible number. 🧠 Key Insight: Normal sorting won't work here ❌ We need a custom comparator based on string concatenation. 👉 Compare: - ""a + b"" vs ""b + a"" - Whichever gives a larger value should come first. ⚙️ Approach: 1. Convert numbers to strings 2. Sort using custom comparison logic 3. Join the result 4. Handle edge case (like "[0,0] → "0"") ⚡ Complexity: - Time: O(n log n) - Space: O(n) 🎯 Result: ✅ Accepted ⚡ Runtime: 0 ms (100%) 📌 Lesson Learned: Sometimes sorting logic depends on combination, not value. #LeetCode #Python #CodingJourney #DSA #100DaysOfCode #Sorting #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 38 – LeetCode Journey Today’s problem: Gray Code ✔️ Generated sequence using bit manipulation ✔️ Applied formula: "i ^ (i >> 1)" ✔️ Ensured only one bit changes between consecutive numbers 💡 Key Insight: Gray Code is useful in minimizing errors in digital communication, as only one bit changes at a time. Using bitwise operations makes the solution both elegant and efficient. This problem improved my understanding of bit manipulation and binary patterns. Exploring deeper into low-level concepts 🔥💪 #LeetCode #Day38 #BitManipulation #Binary #Python #ProblemSolving #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 41 – LeetCode Journey Today’s problem: Add Binary ✔️ Implemented binary addition manually ✔️ Handled carry propagation efficiently ✔️ Worked with string manipulation and indexing 💡 Key Insight: Binary addition follows the same rules as decimal addition, but with base 2. Managing the carry correctly is the key to building the final result. This problem improved my understanding of bit-level operations and string handling. Step by step, mastering the fundamentals 🔥💪 #LeetCode #Day41 #Binary #Strings #Python #ProblemSolving #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 20 of #DSAStreak 📌 Problem: Partition Linked List (LeetCode 86) 🧠 Problem Summary: Given a linked list and a value x, rearrange the list such that: ✔ All nodes with values less than x come before nodes greater than or equal to x ✔ Maintain the original relative order 💡 Approach: Partition Technique 🔥 ✔ Traverse the list once ✔ Separate nodes into two groups: → smaller than x → greater than or equal to x ✔ Finally, connect both parts 🎯 Key Takeaway: 👉 Break problem into smaller parts (divide & merge) 👉 Using proper pointers makes linked list problems much easier 👉 Clean logic > complex implementation ⏱ Time Complexity: O(n) 📦 Space Complexity: O(1) #LinkedList #DSA #LeetCode #CodingJourney #100DaysOfCode #Python
To view or add a comment, sign in
-
-
🚀 Just solved the “Valid Number” problem on LeetCode! This problem looks simple at first glance—but handling edge cases like decimals, signs, and exponents makes it a great test of attention to detail and logical thinking. ✅ Key takeaways: Careful handling of edge cases is crucial Validating input step-by-step can simplify complex parsing problems Writing clean, readable logic beats overcomplicated solutions 💡 Performance: ⚡ Runtime: 3 ms 🧠 Efficient space usage ✅ All test cases passed Problems like this remind me that consistency in practice is what builds strong problem-solving skills. On to the next one! 🔥 #LeetCode #Coding #Python #ProblemSolving #SoftwareEngineering #AIEngineerJourney link of #Solution :- https://lnkd.in/ga9b5pVb
To view or add a comment, sign in
-
-
Top K Frequent: Bucket Sort Beats Heap with O(n) Time Heap-based solution costs O(n log k). Bucket sort achieves O(n) by leveraging frequency bounds — frequencies can't exceed array length. Index = frequency, value = list of elements with that frequency. Traverse buckets high-to-low, collecting k elements. Bucket Sort Advantage: When value range is bounded and known (frequencies ≤ n), bucket sort beats comparison-based O(n log k). Exploiting constraints transforms complexity. Time: O(n) | Space: O(n) #BucketSort #TopK #FrequencyAnalysis #ComplexityReduction #Python #AlgorithmOptimization #SoftwareEngineering
To view or add a comment, sign in
-
-
Today: LeetCode 88 — Merge Sorted Array. Saw "sorted arrays" in the problem. My brain immediately said: two pointers. Started coding. Got stuck pretty fast. The issue? When you're merging into an existing array without extra space, two pointers aren't enough — you need a third one tracking where to place elements. And you have to go backwards through nums1, or you'll overwrite values you still need. Took longer than I'd like to admit. Used hints. Eventually got it — and it beat 100% on runtime. The real lesson wasn't the algorithm. It was this: pattern recognition gets you to the door, but you still have to figure out which version of the pattern fits. "Two pointers" is a family of techniques, not a single move. Knowing the name isn't the same as understanding the shape of the problem. Day 36 of #1000DaysOfLearning #DSA #Python #LearningInPublic
To view or add a comment, sign in
-
-
𝗣𝘆𝘁𝗵𝗼𝗻'𝘀 𝗠𝗥𝗢 𝗶𝘀𝗻'𝘁 𝗷𝘂𝘀𝘁 𝗮 𝘁𝗿𝗶𝘃𝗶𝗮 𝗾𝘂𝗲𝘀𝘁𝗶𝗼𝗻 — 𝗶𝘁'𝘀 𝘁𝗵𝗲 𝘁𝗵𝗶𝗻𝗴 𝘁𝗵𝗮𝘁 𝗯𝗶𝘁𝗲𝘀 𝘆𝗼𝘂 𝗶𝗻 𝗽𝗿𝗼𝗱𝘂𝗰𝘁𝗶𝗼𝗻 𝘄𝗵𝗲𝗻 𝗮 𝗺𝗲𝘁𝗵𝗼𝗱 𝘀𝗶𝗹𝗲𝗻𝘁𝗹𝘆 𝗿𝗲𝘀𝗼𝗹𝘃𝗲𝘀 𝘁𝗼 𝘁𝗵𝗲 𝘄𝗿𝗼𝗻𝗴 𝗽𝗮𝗿𝗲𝗻𝘁. Most codebases with deep inheritance chains rely on MRO without knowing the exact resolution order. C3 linearization guarantees consistency, but only if you actually trace it. 🔹 MRO resolves left-to-right, depth-first, with C3 ensuring no base appears before its subclasses. Calling 𝗥𝗲𝗰𝗼𝗿𝗱.𝗺𝗿𝗼() prints the exact chain Python uses. 🔹 The Golden Rule: If you can't determine the resolution order without running the code, your inheritance tree is too implicit. 🔹 Surface it, flatten it, or better yet: favor 𝘤𝘰𝘮𝘱𝘰𝘴𝘪𝘵𝘪𝘰𝘯 𝘰𝘷𝘦𝘳 𝘪𝘯𝘩𝘦𝘳𝘪𝘵𝘢𝘯𝘤𝘦 to keep your architecture predictable. #Python #SoftwareEngineering #BackendDevelopment #SoftwareArchitecture #CleanCode
To view or add a comment, sign in
-
-
LeetCode Day 13 – Container With Most Water Today I solved the classic “Container With Most Water” problem — a great example of optimizing from brute force to an efficient solution! Problem Insight: We are given an array of heights, and we need to find two lines that together with the x-axis form a container that holds the maximum water. Approaches: Brute Force (O(n²)) Check all possible pairs Calculate area = width × min(height[i], height[j]) Keep track of maximum Optimal Approach – Two Pointer (O(n)) Start with two pointers at both ends Calculate area Move the pointer with smaller height inward Repeat until pointers meet Key Idea: The limiting factor is always the smaller height Moving the larger height won’t help increase area Complexity: Time: O(n) Space: O(1) What I learned: How two-pointer technique drastically reduces complexity Importance of understanding constraints before coding #LeetCode #Day13 #DSA #CodingJourney #Python #TwoPointers #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