Day 115: Grinding Through the Hard Problems 📉 Problem 3464: Maximize the Distance Between Points on a Square Today was a tough one. I went up against a "Hard" problem that pushed my current limits. Honestly, I couldn't crack the logic on my own and had to spend time watching tutorials to grasp the optimal approach. The Learning Curve: • Perimeter Mapping: I learned how to "unroll" a square's boundary into a single linear dimension, making it easier to handle distances along the edges. • Binary Search on Answer: The core of the problem relied on searching for the maximum possible minimum distance, a classic but tricky optimization pattern. • Greedy Validation: Implementing a check function to see if K points can maintain a specific distance requires precise pointer management and binary search techniques within the search space. I'm not proud of needing a guide today, but I'm glad I didn't just copy-paste—I took the time to understand the why behind the solution. Growth isn't always a straight line of wins; sometimes it's about failing, learning, and coming back stronger. Day 115 in the books. 🚀 #LeetCode #Cpp #Algorithms #BinarySearch #ProblemSolving #DailyCode
Maximizing Distance Between Points on a Square with Binary Search
More Relevant Posts
-
🚀 Day 9 — Check if the Array is Sorted Continuing my journey of improving problem-solving skills with consistency and discipline — one problem every day. Today’s problem focused on verifying order — a simple concept, but very important in many algorithms. 🧩 Problem Solved: • Check if the Array is Sorted 📚 Topic: Arrays (Traversal) 💡 Key Insight: To check if an array is sorted, we only need to ensure that every element is less than or equal to the next one. ⚡ Approach: • Traverse the array from start to end • Compare each element with the next • If any element is greater than the next → return False • If no violations found → return True 🎯 Takeaway: Simple checks like this are often building blocks for more complex algorithms like binary search and sorting techniques. ⏱ Complexity: • Time → O(n) • Space → O(1) 💻 Example: Input → [1, 2, 3, 4, 5] → Output → True Input → [1, 2, 1, 4, 5] → Output → False Strong fundamentals make advanced problems easier 🚀 #ProblemSolving #Arrays #CodingJourney #100DaysOfCode #Consistency #LearningInPublic
To view or add a comment, sign in
-
-
💡 “In DSA, the smartest solutions often come from transforming the problem into something you already know.” 🚀 #geekstreak60 — Day 46 Day 46 of the streak! Today’s problem was a perfect example of how problem transformation can turn a tricky question into a familiar one. 📌 Problem Statement Given an array, we can assign + or - before each element and form an expression. Goal: ➡️ Count the number of ways to reach a given target sum 🧠 My Thought Process At first, this looked like a brute-force recursion problem: 👉 Try all combinations of + and - → exponential complexity (2ⁿ) But then came the key transformation: Let: S1 = sum of elements with + S2 = sum of elements with - We know: S1 - S2 = target S1 + S2 = totalSum Solving: 👉 S1 = (totalSum + target) / 2 🛠️ Optimized Approach The problem reduces to: ✅ Count number of subsets with sum = S1 Applied Dynamic Programming (subset sum counting) to efficiently compute the answer. ⚡ Complexity Analysis Time Complexity: O(n × sum) Space Complexity: O(sum) Efficient for given constraints. 💡 Key Learning Transforming a problem is often more powerful than solving it directly. Today strengthened my understanding of: ✅ Problem transformation techniques ✅ Subset sum DP ✅ Counting-based dynamic programming ✅ Optimizing exponential problems Consistency continues strong 💪🔥 #geekstreak60 #npci #DSA #CPP #DynamicProgramming #ProblemSolving #CodingJourney #ContinuousLearning
To view or add a comment, sign in
-
-
Cracking LeetCode Medium: Maximum Distance Between a Pair of Values 🚀 I recently solved a classic Two-Pointer problem: Maximum Distance Between a Pair of Values. The Problem: Given two non-increasing arrays, find the maximum j - i such that i <= j and nums1[i] <= nums2[j]. The Strategy: Two Pointers 💡 Since both arrays are already sorted (non-increasing), a nested loop (O(n²)) would be too slow. Instead, I used the Two-Pointer technique: 1️⃣ Start both pointers i and j at 0. 2️⃣ If nums1[i] <= nums2[j], it's a valid pair! Record the distance j - i and move j forward to find an even larger gap. 3️⃣ If nums1[i] > nums2[j], the current nums1[i] is too large. Move i forward to find a smaller value. Complexity: Time: O(n + m) — much faster than the O(n * m) brute force! Space: O(1) — no extra memory used. Efficient algorithms are all about leveraging the properties of the data (like sorting). Happy coding! 💻 #LeetCode #Coding #TwoPointers #Algorithms #DataStructures #TechLearning #Programming #ProblemSolving
To view or add a comment, sign in
-
🚀 Day 13 of 100 Days LeetCode Challenge Problem: Minimum Number of Seconds to Make Mountain Height Zero Today’s problem was a strong mix of Binary Search + Mathematical Optimization 🔥 💡 Key Insight: Each worker takes time in an increasing pattern: For x units of work → time = t * (1 + 2 + ... + x) = t * x(x+1)/2 👉 This converts the problem into: “Given time T, how much total work can all workers complete?” 🔍 Core Approach: 1️⃣ Binary Search on Time (Answer) Guess a time T Check if all workers together can reduce height ≥ mountainHeight 2️⃣ For Each Worker: Solve: t * x(x+1)/2 ≤ T Find maximum x using math / binary search 3️⃣ Sum all x values If total ≥ required height → ✅ possible 🔥 What I Learned Today: Converting real-world problems into mathematical formulas is powerful Binary Search is not just for arrays—it works on answers too Combining math + search = efficient solution 📈 Challenge Progress: Day 13/100 ✅ Getting stronger every day! LeetCode, Binary Search, Mathematical Optimization, Greedy, Algorithms, DSA Practice, Coding Challenge, Problem Solving #100DaysOfCode #LeetCode #DSA #CodingChallenge #BinarySearch #Mathematics #Optimization #ProblemSolving #TechJourney #ProgrammerLife #SoftwareDeveloper #CodingLife #LearnToCode #Developers #Consistency #GrowthMindset #InterviewPrep
To view or add a comment, sign in
-
-
🚀 Day 96 of 100 Days of DSA 📌 LeetCode #3 (Longest Substring Without Repeating Characters) 📈 "Consistency over motivation, Progress over perfection" A classic problem that introduces one of the most important techniques in DSA: Sliding Window. 🧩 Problem Statement Given a string s: Find the length of the longest substring without repeating characters Important: • Substring must be continuous • Characters must be unique 🧠 Thought Process Initial idea: Generate all substrings Check if each has unique characters But quickly: ⚠️ Too many substrings → inefficient Needed: A way to dynamically maintain a valid substring 🚫 Brute Force Approach 1. Generate all substrings 2. For each substring: • Check for duplicates using a set Complexity: • Substrings → O(n²) • Checking uniqueness → O(n) Overall → O(n³) ❌ 🔄 Better Approach (Set + Sliding Window) 1. Use two pointers (left, right) 2. Expand window 3. If duplicate found → shrink from left Improves to: O(n) time But still involves repeated removals. 🔍 Key Insight Instead of removing characters one by one: Directly jump the left pointer How? Store the last seen index of each character ✅ Approach Used Optimized Sliding Window + Last Seen Index ⚙️ Strategy 1. Maintain: • left → start of window • right → current index • lastSeen → last index of each character 2. For each character: • If seen before within window → move left • Update last seen index • Calculate current window length 3. Track maximum length 💡 Intuition Think of a moving window: Expand when characters are unique Jump forward when duplicate appears No unnecessary backtracking. ⏱ Complexity Analysis Time Complexity: O(n) Each character processed once Space Complexity: O(1) (Fixed character set) 💡 Key Learnings - Sliding window is powerful for substring problems - Avoid recomputation using memory (hashing/indexing) - Moving pointers intelligently reduces complexity - Substring problems often require dynamic window resizing #100DaysOfDSA #Day96 #LeetCode #SlidingWindow #Strings #Algorithms #DSA #CodingJourney
To view or add a comment, sign in
-
-
LeetCode Daily | Day 77 🔥 LeetCode POTD – 2515. Shortest Distance to Target String in a Circular Array (Easy) ✨ 📌 Problem Insight Given a circular array: ✔ Move left or right from a start index ✔ Each move costs 1 step ✔ Find minimum steps to reach target string ✔ Return -1 if target not found 🔍 Initial Thinking – Simulation ⚙️ 💡 Idea: ✔ Try moving step by step left & right ✔ Stop when target is found ⚠️ Problem: ✔ Circular nature makes indexing tricky ✔ Need to handle wrap-around carefully 💡 Key Observation 🔥 ✔ Distance in circular array = min(direct, wrap-around) ✔ For any index i: → distance = min(|i - start|, n - |i - start|) 🚀 Optimized Approach ✔ Traverse entire array once ✔ For every index matching target: → Compute circular distance ✔ Take minimum of all 🔧 Core Idea ✔ Convert movement → distance calculation ✔ Avoid simulation, use math ✔ Handle circular nature smartly ⏱ Complexity ✔ Time: O(n) ✔ Space: O(1) 🧠 Key Learning ✔ Circular problems often reduce to min(dist, n - dist) ✔ Avoid simulation when direct formula exists ✔ Always check all possible target positions 🚀 Takeaway A simple problem that teaches an important pattern for circular arrays — think mathematically, not procedurally ⚡ #LeetCode #DSA #Algorithms #CPlusPlus #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
LeetCode #46 — Permutations | Classic Backtracking Back to fundamentals with Permutations — one of the best problems to truly understand recursion Problem Insight:Given distinct integers, generate all possible permutations (order matters). Approach: Backtracking (build → explore → undo) Try each number in every position Skip elements already used in the current path Once path length == n → valid permutation Core Idea: At each step, you have a choice among remaining elements → this forms a decision tree Key Line: if num in path: continue Ensures we don’t reuse elements What I Learned: Backtracking is about exploring all possibilities systematically “Choose → Explore → Unchoose” is the golden pattern #LeetCode #Backtracking #Recursion #Algorithms #CodingJourney #DSA #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Understanding Time Complexity — The Backbone of Efficient Code Ever wondered why some programs scale beautifully while others slow to a crawl? The answer lies in Time Complexity. 🔍 Here’s a quick breakdown: ✅ Best Case (Ω) – The minimum time an algorithm takes 👉 Example: Finding an element instantly → O(1) ⚖️ Average Case (Θ) – Typical performance across inputs 👉 Balanced scenario between best and worst ⚠️ Worst Case (O) – Maximum time taken 👉 Example: Searching through all elements → O(n) 💡 Common Time Complexities: • O(1) → Constant (Fastest) • O(log n) → Logarithmic • O(n) → Linear • O(n log n) → Efficient sorting • O(n²) → Quadratic (gets slow quickly) • O(2ⁿ), O(n!) → Exponential (avoid when possible!) 📈 Key Insight: As input size grows, inefficient algorithms become bottlenecks. Choosing the right approach can mean the difference between milliseconds and minutes. 🔥 Pro Tip: Don’t just make your code work — make it scale. #DataStructures #Algorithms #TimeComplexity #Coding #SoftwareEngineering #Tech #Learning #Programming
To view or add a comment, sign in
-
-
🗓️ Day 25 of #30DaysOfCode — Running Time & Complexity Today's challenge was all about understanding why algorithm efficiency matters. The task: determine if a number is Prime across T test cases. The naive approach checks every divisor from 2 to n — that's O(n) per query. The smarter approach? Only check up to √n. Why? Because divisors come in pairs. If d divides n, then n/d also divides n — and one of them is always ≤ √n. So we never need to look further. That brings the complexity down to O(√n) per query — a huge win at scale. A few extra optimizations I applied: → Return false immediately if n < 2 → Handle n == 2 as a special case (the only even prime) → Skip all even numbers — only iterate odd values from 3 The key snippet: for (int i = 3; i <= Math.sqrt(n); i += 2) { if (n % i == 0) return false; } Small decisions. Big impact on performance. This challenge reminded me that it's not just about making code work — it's about making it work efficiently. What's your go-to tip for writing efficient algorithms? Drop it below 👇 #Java #Algorithms #TimeComplexity #HackerRank #SoftwareDevelopment #LearningInPublic #Claude #Anthropic #AI
To view or add a comment, sign in
-
🚀 Day 3 of Consistent Problem Solving Today I worked on a classic problem that appears simple — but tests mathematical optimization and edge-case handling. 📌 Problem: Implement a function to compute (x^n) efficiently. 🧪 My Initial Approach (Brute Force): Multiply x by itself n times. Time Complexity: O(n) This quickly becomes infeasible for large n (up to (2^{31} - 1)) ❌ ⚡ Key Insight: Instead of multiplying repeatedly, we can break the exponent using binary representation. 👉 Example: (x^{10} = (x^2)^5) This reduces the number of operations significantly. 💡 Optimized Approach (Binary Exponentiation): x^n = \begin{cases}(x^2)^{n/2}, & n \text{ even}\ x \cdot (x^2)^{(n-1)/2}, & n \text{ odd}\end{cases} Repeatedly square the base Halve the exponent at each step Multiply only when the exponent is odd Time Complexity: O(log n) ✅ Space Complexity: O(1) 🧩 Key Learning: Large exponent problems → think in terms of binary decomposition Repeated work can often be reduced using divide & conquer Edge cases (like negative powers and overflow) are critical in interviews ⚠️ Important Edge Case: When (n = -2^{31}), taking absolute value overflows an integer → using long long is necessary. 🔗 Follow-up Insight: This is the foundation for Modular Exponentiation (pow with mod) — one of the most important patterns in competitive programming and ICPC. 📚 Prerequisites: Basic understanding of exponents Binary representation of numbers Divide and conquer approach Consistency + depth = real growth 📈 Have you implemented modular exponentiation or used this in contests? Let’s discuss 👇 #LeetCode #Algorithms #BinaryExponentiation #ProblemSolving #CodingJourney #CompetitiveProgramming
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