𝗗𝗮𝘆 𝟯𝟲 𝗼𝗳 #𝟭𝟴𝟬𝗗𝗮𝘆𝘀𝗢𝗳𝗖𝗼𝗱𝗲 Today, I solved 𝗦𝗲𝗮𝗿𝗰𝗵 𝗜𝗻𝘀𝗲𝗿𝘁 𝗣𝗼𝘀𝗶𝘁𝗶𝗼𝗻 — a practical variation of binary search. The goal was to find the index of a target in a sorted array, or the position where it should be inserted to maintain order. Using binary search, I efficiently located the correct spot in O(log n) time. The key was to track the first position where the element is greater than or equal to the target — which is exactly the insert position if the target isn’t found. This is a great example of how small tweaks to a classic algorithm can solve new problems elegantly. A useful technique for search, insertion, and maintaining sorted data dynamically! #Python #Algorithms #BinarySearch #LeetCode #Coding #ProblemSolving
Solved Seaarch Insert Position in O(log n) time with binary search
More Relevant Posts
-
𝗗𝗮𝘆 𝟯𝟵 𝗼𝗳 #𝟭𝟴𝟬𝗗𝗮𝘆𝘀𝗢𝗳𝗖𝗼𝗱𝗲 Today, I built on the first/last occurrence solution to 𝗰𝗼𝘂𝗻𝘁 𝗼𝗰𝗰𝘂𝗿𝗿𝗲𝗻𝗰𝗲𝘀 𝗼𝗳 𝗮 𝘁𝗮𝗿𝗴𝗲𝘁 𝗶𝗻 𝗮 𝘀𝗼𝗿𝘁𝗲𝗱 𝗮𝗿𝗿𝗮𝘆 𝘄𝗶𝘁𝗵 𝗱𝘂𝗽𝗹𝗶𝗰𝗮𝘁𝗲𝘀. Using the same lower_bound and upper_bound helpers: Lower bound → first index where element ≥ target Upper bound → first index where element > target The count is simply: count = upper_bound - lower_bound This gives an O(log n) solution — much faster than scanning the entire array, especially with many duplicates. It’s a great example of how breaking a problem into reusable pieces leads to clean and efficient code. Perfect for analytics, frequency analysis, and search optimizations! 📊 #Python #Algorithms #BinarySearch #FrequencyCount #Coding #ProblemSolving
To view or add a comment, sign in
-
-
🔢 Understanding Selection Sort 🎯 Problem: Given an array of numbers, sort them in ascending order using the Selection Sort algorithm. 🧩 Concept: Selection Sort works by repeatedly finding the minimum element from the unsorted part of the array and placing it at the beginning. 🔹In each iteration, we assume the first element is the smallest. 🔹Then, we find the actual minimum from the remaining unsorted part. 🔹Finally, we swap it with the current index element. 🕒 Time Complexity: O(N^2) - For each element, we search the rest of the array for the smallest element. 💾 Space Complexity: O(1) - Sorting is done in place (no extra memory used). #Python #DataStructures #Algorithms #Sorting #CodingJourney #ProblemSolving #LearningInPublic
To view or add a comment, sign in
-
-
Day 8 of #100DaysOfLeetCode Problem: 9. Palindrome Number Category: Math / Two Pointers / Logic Today’s problem focused on determining whether an integer reads the same backward as forward — essentially checking if a number is a palindrome. 🧠 Key Learnings: Reversed the number mathematically using modulo and integer division. Avoided converting integers to strings, focusing purely on arithmetic logic. Understood how to handle special cases like negative numbers and numbers ending with zero. Strengthened my skills in digit manipulation and loop-based reversal logic. 🎯 Takeaway: Sometimes, avoiding built-in functions helps build a deeper understanding of fundamental logic — every digit and operation counts! #LeetCode #100DaysOfCode #ProblemSolving #CodingJourney #Math #LogicBuilding #Python #AIEngineer #Consistency
To view or add a comment, sign in
-
-
Day 66: Find Minimum in Rotated Sorted Array II 📉 (Simple Version) I'm tackling an advanced Binary Search problem on Day 66 of #100DaysOfCode: "Find Minimum in Rotated Sorted Array II." The challenge is to find the minimum element in a rotated sorted array that may contain duplicates. My solution uses a modified Binary Search approach. The key difficulty is when the middle element (nums[mid]) equals the rightmost element (nums[right]). In this case, we can't tell if the minimum is on the left or the right. To resolve this ambiguity, I simply discard the rightmost element by setting right -= 1. This adjustment keeps the search on track, achieving an optimal O(log n) complexity in the typical case. My solution achieved 100% runtime efficiency! #Python #DSA #Algorithms #BinarySearch #Arrays #100DaysOfCode #ProblemSolving
To view or add a comment, sign in
-
-
Day 41 of #180DaysOfCode Today, I leveled up the 𝗿𝗼𝘁𝗮𝘁𝗲𝗱 𝘀𝗼𝗿𝘁𝗲𝗱 𝗮𝗿𝗿𝗮𝘆 𝗽𝗿𝗼𝗯𝗹𝗲𝗺: 𝗦𝗲𝗮𝗿𝗰𝗵 𝘄𝗶𝘁𝗵 𝗗𝘂𝗽𝗹𝗶𝗰𝗮𝘁𝗲𝘀! 🔍 When the array contains duplicates, the previous approach needed a tweak. If nums[low] == nums[mid] == nums[high], we can't decide which side is sorted. The solution? Trim duplicates by moving low and high inward, then proceed with the standard rotated search logic. This ensures we still achieve O(log n) performance in most cases, though it can degrade to O(n) with many duplicates. A subtle but important edge case that makes binary search even more interesting! #Python #Algorithms #BinarySearch #RotatedArray #Duplicates #Coding
To view or add a comment, sign in
-
-
Today's LeetCode problem was the foundational 509. Fibonacci Number. While there are faster ways to solve this, I focused on implementing the classic recursive solution. It's such a fundamental concept to understand, as it directly mirrors the mathematical definition: $F(n) = F(n-1) + F(n-2)$. It's a great reminder that coding is often about translating mathematical logic directly into code. The pursuit of efficiency (like using memoization next!) never stops, but understanding the basics is paramount. #LeetCode #Recursion #DSA #Algorithms #Python #ComputerScience
To view or add a comment, sign in
-
-
Guido (Python's creator and BDFL) published a talk claiming that structured RAG is better than classical RAG. Here's an explanation. 𝐂𝐥𝐚𝐬𝐬𝐢𝐜𝐚𝐥 𝐑𝐀𝐆: - Retrieve text chunks via embedding similarity. - Feed them (or summaries) into a generative model. - The model answers based on that unstructured context. 𝐒𝐭𝐫𝐮𝐜𝐭𝐮𝐫𝐞𝐝 𝐑𝐀𝐆: - Don’t pass raw text chunks. - Enforce structure (e.g. knowledge graphs, schemas). - The model gives you less hallucination, more control, more composability. I hope this is useful.
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