💡 LeetCode Learning Log — Two Sum II (Input Array is Sorted) 🔗 Problem: https://lnkd.in/gkcqGnfD Today I explored three different approaches to solve this problem: 1️⃣ 𝗕𝗿𝘂𝘁𝗲 𝗙𝗼𝗿𝗰𝗲 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵 • Checked all possible pairs using nested loops. • 𝗧𝗶𝗺𝗲 𝗖𝗼𝗺𝗽𝗹𝗲𝘅𝗶𝘁𝘆: O(n²) 𝗦𝗽𝗮𝗰𝗲: O(1) • Simple but inefficient for large inputs. 2️⃣ 𝗕𝗲𝘁𝘁𝗲𝗿 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵 (𝗨𝘀𝗶𝗻𝗴 𝗛𝗮𝘀𝗵𝗠𝗮𝗽) • Stored elements in a map while traversing. • For every number, checked if (𝘵𝘢𝘳𝘨𝘦𝘵 - 𝘯𝘶𝘮) exists. • 𝗧𝗶𝗺𝗲 𝗖𝗼𝗺𝗽𝗹𝗲𝘅𝗶𝘁𝘆: O(n) 𝗦𝗽𝗮𝗰𝗲: O(n) • Much faster, but takes extra space. • Great when index matters — because it keeps track of original positions. 3️⃣ 𝗢𝗽𝘁𝗶𝗺𝗮𝗹 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵 (𝗧𝘄𝗼 𝗣𝗼𝗶𝗻𝘁𝗲𝗿𝘀) • Since the array is sorted, used two pointers from both ends. • Moved pointers based on the sum comparison. • 𝗧𝗶𝗺𝗲 𝗖𝗼𝗺𝗽𝗹𝗲𝘅𝗶𝘁𝘆: O(n) 𝗦𝗽𝗮𝗰𝗲: O(1) • Clean, elegant, and efficient. 𝗥𝗲𝘀𝘂𝗹𝘁: ✅ All 24 test cases passed ⏱ Runtime: 4 ms 💾 Memory: 12.77 MB 𝗞𝗲𝘆 𝗟𝗲𝗮𝗿𝗻𝗶𝗻𝗴𝘀: • Understand why 𝘁𝘄𝗼 𝗽𝗼𝗶𝗻𝘁𝗲𝗿𝘀 shine when arrays are sorted. • When 𝗶𝗻𝗱𝗲𝘅 𝗽𝗼𝘀𝗶𝘁𝗶𝗼𝗻𝘀 𝗮𝗿𝗲 𝗶𝗺𝗽𝗼𝗿𝘁𝗮𝗻𝘁, sorting may not work. • Don’t just solve, explore multiple approaches to strengthen problem-solving intuition. #LeetCode #DSA #ProblemSolving #Python #TwoPointers #CodingJourney #DataEngineer #100DaysOfCode
Shashi Singh’s Post
More Relevant Posts
-
🔹 Day 2 of 30 – LeetCode Challenge: Postorder Traversal of Binary Tree 🌲Today’s problem was about implementing Postorder Traversal of a binary tree — one of the most fundamental tree traversal techniques in Data Structures and Algorithms. 🧩 Problem: Return the postorder traversal (Left → Right → Root) of a given binary tree. Example: Input: root = [1, null, 2, 3] Output: [3, 2, 1] 💡 Approach: In Postorder Traversal, we: Visit Left Subtree Visit Right Subtree Visit Root Node I implemented both recursive and iterative approaches. The recursive version is simpler, while the iterative version helps understand how to simulate recursion using a stack. ⚙️ Complexity: Time Complexity: O(n) Space Complexity: O(h) 🏆 Result: ✅ All test cases passed 🚀 Runtime Efficiency: 100% 💬 Learning: This problem helped me deepen my understanding of recursion and stack-based traversal logic. Iterative postorder traversal is tricky but a great exercise to strengthen logical thinking in DSA. #LeetCode #Day2 #Python #DataStructures #BinaryTree #PostorderTraversal #Algorithms #Recursion #30DaysOfCode #MTech #CodingChallenge
To view or add a comment, sign in
-
-
Excited to share my latest project! I built a webpage summarization tool using Python and LLMs that can automatically condense website content into clear, professional summaries. Here’s what it does: Fetches content from any URL using a custom scraper. Sends the content to a local LLM (llama3.2 via Ollama) for summarization. Outputs a clean, readable summary in Markdown for easy reading. The workflow leverages: Python for scripting and automation Ollama and LLaMA 3.2 for local AI-powered summaries This tool is perfect for quickly digesting large amounts of web content, whether for research, competitive analysis, or personal learning. Check out the code here: https://lnkd.in/etKVFH_A #AI #MachineLearning #Python #LLM #WebScraping #ProductivityTools #Ollama #LLaMA3
To view or add a comment, sign in
-
-
Title: From Clicks to Confidence: Simulating an A/B Test from Scratch in Python More About Project: What's the real-world difference between a 20% and a 22% conversion rate? And how many users do you need to proveit? To answer this, I built a complete A/B testing simulation from the ground up. Instead of just using a library, I wanted to explore the statistical engine behind the test. In this notebook, I: Built a Frequentist Test: Coded a two-sample z-test for proportions to compare two simulated website versions (A and B). Analyzed Statistical Power: My key finding (see plot) shows how Statistical Power—the probability of detecting a real effect—dramatically increases with sample size. Running a test with too few users is just guessing! Calculated Sample Size: Implemented the formula to determine the exact number of users needed to get a statistically significant result, saving time and resources. Explored a Bayesian Approach: Used the Beta distribution to calculate the "Probability that B is better than A," offering a more intuitive result than a simple p-value. This project was a fantastic hands-on way to solidify my understanding of hypothesis testing, power analysis, and the trade-offs between frequentist and Bayesian methods. Next up: Applying these principles to a full-scale portfolio project. Check out the complete notebook on my GitHub!-https://lnkd.in/gyDpK4er #ABTesting #DataScience #Statistics #Python #Portfolio #StatisticalPower #Bayesian #DataAnalytics
To view or add a comment, sign in
-
🚀 DSA Challenge – Day 89 Problem: Subsets II (Handling Duplicates in Power Set) ⚙️✨ This problem was a great exploration of recursion, backtracking, and how to systematically avoid duplicate subsets using careful pruning. 🧠 Problem Summary: You are given an integer array nums that may contain duplicates. Your goal: return all possible subsets (the power set) without including any duplicate subsets. ⚙️ My Approach: 1️⃣ First, sort the array — this step helps group duplicates together, which is crucial for skipping them efficiently. 2️⃣ Use recursive backtracking to explore all possible inclusion/exclusion combinations. 3️⃣ Whenever a duplicate element is found (i.e., nums[i] == nums[i-1]), skip it if it’s at the same recursion depth — ensuring unique subset generation. 4️⃣ Keep track of the current subset and add a copy to the result whenever we reach a new state. 📈 Complexity: Time: O(2ⁿ) → Each element can be either included or excluded. Space: O(n) → For recursion and temporary subset storage. ✨ Key Takeaway: Sorting before recursion is often the simplest way to handle duplicates in backtracking problems. With one small condition check, you can turn exponential chaos into structured exploration. ⚡ 🔖 #DSA #100DaysOfCode #LeetCode #ProblemSolving #Recursion #Backtracking #Algorithms #CodingChallenge #Python #TechCommunity #InterviewPrep #EfficientCode #LearningByBuilding #CodeEveryday
To view or add a comment, sign in
-
-
💡 Day 43 / 100 – Search in Rotated Sorted Array (LeetCode #33) Today’s problem was a twist on the classic binary search — quite literally! The challenge was to find a target element in a rotated sorted array. At first glance, the array looks unsorted, but there’s actually a pattern. By identifying which part of the array is properly sorted at every step, we can still apply binary search logic efficiently — achieving O(log n) time complexity. This problem beautifully blends pattern recognition with logical precision. 🔍 Key Learnings Even when data looks “unsorted,” patterns often exist beneath. Modified binary search can adapt to many problem variations. Understanding midpoint relationships helps in avoiding brute force. 💭 Thought of the Day Adaptability is key — in coding and in life. Just like binary search adjusts to a rotated array, we can adjust to challenges by recognizing the underlying order in the chaos. Clear logic turns confusion into clarity. 🔗 Problem Link: https://lnkd.in/gS8FcbeE #100DaysOfCode #Day43 #LeetCode #Python #BinarySearch #ProblemSolving #Algorithms #CodingChallenge #DataStructures #CodingJourney #PythonProgramming #LogicBuilding #KeepLearning #TechGrowth #Motivation
To view or add a comment, sign in
-
-
A Great Lesson in Optimization: From O(n^2) to O(n) with "Two Sum" My initial instinct was to develop a brute-force solution. This approach, which involved two nested loops, correctly found the answer but had a time complexity of O(n^2). I knew there had to be a more efficient method. This prompted me to explore optimization, and I soon discovered the power of using a HashMap (or Dictionary). By leveraging this data structure, I could store the values I had already encountered and their indices. This new approach allowed me to find the solution in a single Loop, completely eliminating the nested loop and achieving a linear time complexity of O(n). Valuable lesson:- the first solution that comes to mind isn't always the best one. It highlighted the critical importance of analyzing time complexity and selecting the right data structure for the job. A simple change in approach can be the difference between a functional solution and a truly efficient one. #DataStructures #Algorithms #ProblemSolving #Optimization #Python #SoftwareDevelopment #LearningJourney
To view or add a comment, sign in
-
-
⚡ Understanding Quick Sort 🎯 Problem: Given an array of numbers, sort them in ascending order using the Quick Sort algorithm. 🧠 Concept: - Quick Sort is a powerful Divide & Conquer algorithm that sorts by selecting a pivot and placing it in its correct position. - Once the pivot settles, elements smaller than it move to the left, and larger ones move to the right — forming two sub-arrays that get sorted recursively. 🔹 Select a pivot (commonly the first, last, or a random element). 🔹 Move all smaller elements to the left of the pivot. 🔹 Move all larger elements to the right. 🔹 Recursively sort the left and right sub-arrays. 🔹 The array becomes fully sorted after all pivots settle into their correct spots. ⚙️ How the Partitioning Works: - Two pointers i (from the left) and j (from the right) move inward. - i moves until it finds an element greater than the pivot. - j moves until it finds an element smaller than the pivot. - If i < j, the elements are swapped. - Finally, the pivot is swapped into its correct position (j). - This makes j the partition index, and sorting continues around it. 🕒 Time Complexity: Average / Best: O(N log N) – Efficient due to effective partitioning. Worst: O(N²) – Happens when pivots are chosen poorly (like already sorted arrays without randomization). 💾 Space Complexity: O(1) – Only in-place swaps are used (excluding recursion stack). #Python #DSA #DsaStriver #LearnDaily #Share #ProblemSolving
To view or add a comment, sign in
-
-
Hey everyone 👋 Today's learning the very most Important Machine learning Concept, 1. Cross-Validation (https://lnkd.in/gM_mhpEf) 2. Hyperparameter Tuning(https://lnkd.in/gcH9WpQp) and put the ideas into practice in Python on Google Colab. I’ve uploaded the notebook to my GitHub so you can see the code and results. What I learned: Cross-Validation (k-fold): split data into k parts to train and test the model multiple times so the model’s performance is more reliable and not just lucky on one split. Why it matters: it helps avoid overfitting and gives a better estimate of how the model will work on new data. Hyperparameter Tuning: tried methods like Grid Search and Random Search to find the best combination of parameters (for example: regularization strength, number of trees, max depth). Why it matters: tuning hyperparameters can improve model accuracy and generalization without changing the model itself. What I did: implemented k-fold cross-validation and hyperparameter search in Python (scikit-learn) on Colab, compared results, and logged the best model and metrics in the notebook. Check out the notebook here 👇 🔗 [https://lnkd.in/gfURYdt9] I’m still learning and will keep sharing what I build. Feedback and suggestions are welcome ,they help me improve. 🫡 💪 #MachineLearning #DataScience #CrossValidation #HyperparameterTuning #Python #ScikitLearn #GitHub #LearningJourney
To view or add a comment, sign in
-
𝐒𝐉-𝐏𝐲𝐭𝐡𝐨𝐧-𝟎𝟐 — 𝐎𝐩𝐞𝐫𝐚𝐭𝐨𝐫𝐬: 𝐀𝐫𝐢𝐭𝐡𝐦𝐞𝐭𝐢𝐜, 𝐂𝐨𝐦𝐩𝐚𝐫𝐢𝐬𝐨𝐧, 𝐋𝐨𝐠𝐢𝐜𝐚𝐥 & 𝐌𝐨𝐫𝐞 AIOps Study Journal · Python Series 𝐃𝐨𝐜 𝐈𝐃: 𝐒𝐉-𝐏𝐲𝐭𝐡𝐨𝐧-𝟎𝟐 | 𝐕𝐞𝐫𝐬𝐢𝐨𝐧: 𝟏.𝟎 What happens when numbers, logic, and data start to interact? This chapter of my Python Study Journal dives into Operators — the symbols that transform values into results, comparisons, and decisions. 𝐕𝐢𝐞𝐰 𝐟𝐮𝐥𝐥 𝐧𝐨𝐭𝐞𝐛𝐨𝐨𝐤 𝐨𝐧 𝐆𝐢𝐭𝐇𝐮𝐛 Link is in first comment 𝐖𝐡𝐚𝐭 𝐈𝐭 𝐂𝐨𝐯𝐞𝐫𝐬 ▪ Arithmetic operators — mastering + − * / % ** // and their precedence (PEMDAS) ▪ Comparison operators — learning to ask Python “is this greater, equal, or less?” ▪ Assignment shortcuts — +=, -=, *=, /= and how they simplify updates ▪ Logical operators — combining conditions with and, or, not ▪ Identity vs Membership — is / is not vs in / not in ▪ Bitwise operations — the binary world behind the numbers ( &, |, ^, ~, <<, >> ) ▪ Mini-tasks like even/odd checks, age-eligibility logic, and bitwise playgrounds 𝐂𝐨𝐫𝐞 𝐈𝐧𝐬𝐢𝐠𝐡𝐭 Every decision a program makes begins with an operator. Once you understand how they combine values and conditions, you start thinking like the interpreter itself — in logic, not lines. This is Part 2 of the Python Series — Operators: Arithmetic, Comparison, Logical & More. Next, we’ll move to Conditional Statements — bringing logic to life with if, elif, and else. #Python #AIOps #StudyJournal #LearningInPublic #ProgrammingBasics #PythonForBeginners #CodeNewbie #TechEducation #SoftwareEngineering #OpenSource #DevOps #Eduqual #AlNafi #PythonLearning #Operators #CodingJourney #Alnafi
To view or add a comment, sign in
-
-
🚀 LeetCode #560: Subarray Sum Equals K — Cracked! This problem stands out as one of those that looks simple at first, but reveals an elegant pattern once you dive deeper. It beautifully combines logic, optimization, and mathematical thinking 💡 🧩 The Challenge Given an array of integers and a target value k, we need to find the number of continuous subarrays whose sum equals k. At first glance, a brute-force approach (checking all subarrays) might seem natural — but that quickly becomes inefficient as the array grows. ⚡ The Insight The key breakthrough comes from understanding prefix sums — the running total of elements as you move through the array. Instead of recalculating sums repeatedly, we track these prefix sums using a hashmap. Whenever the difference between the current prefix sum and k has appeared before, it means a subarray that sums to k exists. In one pass, we can find all such subarrays — turning a quadratic-time solution into a linear-time one 🔥 🧠 Why It’s Brilliant This approach teaches an important lesson: Many complex problems can be simplified by rethinking how we track and reuse information. It’s not always about doing more work — often, it’s about doing the right work. ⏱️ Complexity Time Complexity: O(n) Space Complexity: O(n) 💬 Takeaway This problem is a perfect example of how combining mathematical insight with data structures (like hashmaps) leads to elegant and efficient solutions. #LeetCode #DSA #Algorithms #ProblemSolving #Coding #Python #ComputerScience #LearningJourney #Tech
To view or add a comment, sign in
-
Explore related topics
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
Keep it up Shashi Singh 😇 Looking forward to see you solving tries like smooth as cake cutting.