🔥 Day 84/100 of Code – Maximum Points from Cards: Sliding Window on a Circular Array! Today solved a problem that cleverly reframes picking from ends as finding the minimum subarray sum in the middle: ✅ Problem 1423: Maximum Points You Can Obtain from Cards Task: Pick k cards from either end of the array to maximize sum. Approach: Complementary sliding window: Total sum of picking k from ends = total sum of array - sum of middle subarray of length n-k Find minimum sum of any subarray of length n-k Answer = total sum - min middle sum Implemented with initial leftSum of first k cards, then slide by removing from left, adding from right Key Insight: Instead of simulating all combinations of left/right picks, think about the unchosen middle segment — minimizing it maximizes chosen ends. Complexity: O(n) time, O(1) space — efficient two-pass solution. A great example of how reframing the problem reveals a simpler sliding window approach! 🃏➕➖ #100DaysOfCode #LeetCode #Java #SlidingWindow #Array #Algorithm #CodingInterview
Maximizing Card Points with Sliding Window on Circular Array
More Relevant Posts
-
LeetCode POTD #3013 - Divide an Array Into Subarrays With Minimum Cost II (02 February 2026) This one looks messy at first glance, but the trick is how you frame it. The first subarray is forced to start at index 0. So the real decision is where the last subarray starts. Fix the starting index of the last subarray at i. Now the constraint i(k-1) - i1 <= dist forces the remaining k-2 subarrays to start inside the window: [i - dist, i - 1] At that point, the problem becomes clean: From a sliding window of length dist, continuously maintain the smallest k-2 values. To do this efficiently: -> Use two ordered sets -> One keeps the k-2 smallest elements -> The other holds the rest -> Maintain the sum while the window slides Total cost for each i: nums[0] + nums[i] + sum_of_k-2_smallest Minimum across all valid i is the answer. Key takeaway: Hard problems usually aren’t about complex code -> they’re about choosing the right perspective. Time Complexity -> O(n log n) Space Complexity -> O(n) #LeetCode #POTD #HardProblems #DataStructures #SlidingWindow #Java #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 67 of #100DaysOfCode Today’s problem focused on 2D Prefix Sum optimization — Range Sum Query 2D – Immutable (NumMatrix) 📊 At first glance, this problem looks like repeated matrix traversal, but the real win comes from preprocessing smartly. 📌 Problem Summary You’re given a 2D matrix and multiple queries asking for the sum of elements inside a sub-rectangle. Brute force would be too slow for repeated queries. 🧠 My Approach: Prefix Sum (Row-wise) Precompute prefix sums for each row For every query, calculate the sum in O(rows) time using prefix subtraction Avoid recalculating sums for every query This transforms repeated heavy computation into a fast query process ⚡ ⚙️ Time & Space Complexity Preprocessing: O(rows × cols) Each Query: O(rows) Space: O(rows × cols) 🔥 Key Learning Prefix sums are not limited to 1D arrays— they scale beautifully to 2D problems and are extremely powerful for range queries. Another solid step forward in mastering optimization techniques 💪 On to Day 68 🚀 #Day67 #100DaysOfCode #Java #LeetCode #PrefixSum #DSA #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
#200DaysOfCode – Day 111 Word Search Problem: Given a 2D grid of characters and a word, determine whether the word exists in the grid. The word must be formed using adjacent cells (up, down, left, right), and each cell can be used only once. Example: Input: board = [[A, B, C, E], [S, F, C, S], [A, D, E, E]] word = "ABCCED" Output: true My Approach: Used DFS (Depth First Search) starting from every cell. Matched characters step-by-step with the given word. Marked cells as visited during the path to avoid reuse. Applied backtracking to explore all possible directions safely. Time Complexity: O(m × n × 4^L) Space Complexity: O(L) (recursion stack) Grid problems often look complex, but breaking them down with DFS + backtracking makes the logic clear and manageable. Patience and careful state handling are the real keys here #takeUforward #100DaysOfCode #Java #LeetCode #ProblemSolving #Backtracking #DFS #DataStructures #Algorithms #CodeNewbie #Consistency
To view or add a comment, sign in
-
-
Day 49/100 – LeetCode Challenge ✅ Problem: Maximum Sum Triconic Array Difficulty: Hard Language: Java Approach: Three-Pointer Expansion with Prefix/Suffix Optimization Time Complexity: O(n²) in worst case Space Complexity: O(n) Key Insight: Find trionic subarray with maximum sum: increasing → decreasing → increasing. For each middle peak a, expand left for decreasing segment and right for second increasing segment. Track maximum sum across all possible trionic subarrays. Solution Brief: Used nested loops to try each possible peak position. For each peak, expanded left while decreasing, right while increasing. Tracked maximum sum with proper handling of edge cases and large numbers (used long). Optimizing sum calculation for complex array patterns #LeetCode #Day49 #100DaysOfCode #Array #Java #Algorithm #CodingChallenge #ProblemSolving #MaxSumTriconicArray #HardProblem #ThreePointer #Subarray #Optimization #DSA
To view or add a comment, sign in
-
-
LeetCode POTD #2976 - Minimum Cost to Convert String I (29/01/2026) This problem looks deceptively simple. Two strings, some character conversions, find the minimum cost. But the moment you try to do it greedily per character, things start breaking. The real problem is not string manipulation. It’s a shortest path problem on characters. Each character is a node. Each allowed conversion is a directed edge with a cost. And conversions can be chained. Once you see that, the solution becomes straightforward: build a graph of 26 characters and run Floyd Warshall to compute the minimum cost between every pair. Why Floyd Warshall? Because the graph is tiny, fixed size, and we need all-pairs shortest paths, not just one source. After preprocessing: iterate once over the strings and sum up the cost of converting source[i] to target[i]. If any conversion is unreachable, return -1. What I liked about this problem is how quickly it punishes surface-level thinking. The constraints force you to model the problem correctly, or you’ll keep patching edge cases. Clean graph modeling beats clever hacks every single time. #LeetCode #POTD #Graphs #FloydWarshall #ProblemSolving #Java #Consistency
To view or add a comment, sign in
-
-
I used to overcomplicate sliding window problems. Then I found this template after solving multiple sliding window questions. If you’re grinding LeetCode, stop memorizing individual solutions and start recognizing the pattern. Here is the 3-step "Template" I’ve been using: 1. The Expansion: Move your end pointer to grow the window and collect data (sum, counts, etc.). 2. The Window Hit: Once end - start + 1 == k, you’ve reached the size you need. 3. The Shift: Process your result, then subtract the start element and increment the start pointer to keep the window sliding. Master the pattern, not the problem. #SoftwareEngineering #LeetCode #CodingTips #Java #Algorithms #CareerGrowth
To view or add a comment, sign in
-
-
Day 39/100 – LeetCode Challenge ✅ Problem: #1984 Minimum Difference Between Highest and Lowest of K Scores Difficulty: Easy Language: Java Approach: Sorting + Sliding Window Time Complexity: O(n log n) Space Complexity: O(1) Key Insight: After sorting, the minimum range in k elements must come from consecutive elements in sorted order. Sliding window of size k finds the minimum difference between first and last element in window. Solution Brief: Sorted the array to bring close values together. Initialized answer with first k elements. Slided window across array, updating minimum difference. Finding minimal range in sorted array with sliding window #LeetCode #Day39 #100DaysOfCode #Sorting #SlidingWindow #Java #Algorithm #CodingChallenge #ProblemSolving #MinimumDifference #EasyProblem #Array #Optimization #DSA
To view or add a comment, sign in
-
-
🚀 Day 46/50 – LeetCode POTD 🔍 3. Longest Substring Without Repeating Characters 📊 Medium 🧠 Key Idea (Sliding Window + Two Pointers) Whenever a problem asks for a substring without repeating characters, the best approach is 👉 Sliding Window. ✔️ Use two pointers: left (l) and right (r) ✔️ Use a boolean array (arr[128]) to track characters (ASCII set) ✔️ If the current character is not present in the window ➡️ expand the window (r++) ➡️ update the maximum length ✔️ If a duplicate character is found ➡️ shrink the window from the left (l++) ➡️ remove characters until the duplicate is gone ✔️ The window always contains unique characters 📌 Example s = "abcabcbb" ➡️ Longest substring = "abc" ➡️ Output = 3 ⚙️ How the Code Works (Java) arr[ch] = true → character exists in the current window On duplicate → move the left pointer and clear characters Track the answer using maxLen = Math.max(maxLen, r - l + 1) ⚙️ Complexity Analysis ⏱ Time Complexity: O(n) 💾 Space Complexity: O(1) (fixed-size array of 128 characters) 💡 Key Takeaway “Substring + uniqueness constraint” ➡️ Think Sliding Window ➡️ Two pointers give an optimal linear-time solution ➡️ Avoid brute force and nested loops 🔁 Consistency beats motivation 🚀 #LeetCode #DSA #SlidingWindow #TwoPointers #Strings #Java #ProblemSolving #CodingJourney #50DaysChallenge
To view or add a comment, sign in
-
-
𝗗𝗮𝘆 𝟯𝟲/𝟭𝟬𝟬 | 𝗠𝗲𝗿𝗴𝗲 𝗧𝘄𝗼 𝗦𝗼𝗿𝘁𝗲𝗱 𝗟𝗶𝘀𝘁𝘀 Day 36 ✅ — Recursion over iteration. 𝗧𝗼𝗱𝗮𝘆'𝘀 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: ✅ 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 #𝟮𝟭: Merge Two Sorted Lists (Easy) 𝗪𝗵𝗮𝘁 𝗖𝗹𝗶𝗰𝗸𝗲𝗱: Merge two sorted linked lists. Instead of the iterative dummy node approach, I used 𝗿𝗲𝗰𝘂𝗿𝘀𝗶𝗼𝗻. Compare the heads. Attach the smaller one, then recursively merge the rest. Base case: if one list is empty, return the other. Elegant. Three lines of logic. 𝗠𝘆 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵: 👉 Base cases: if l1 is null, return l2 (and vice versa) 👉 Compare l1.val and l2.val 👉 Attach smaller node and recurse with remaining lists Time: O(n + m), Space: O(n + m) for recursion stack 𝗠𝘆 𝗥𝗲𝗮𝗹𝗶𝘇𝗮𝘁𝗶𝗼𝗻: Seven linked list problems, and recursion just made this one cleaner than iteration. Sometimes the simplest code is the most powerful. 𝗖𝗼𝗱𝗲:🔗 https://lnkd.in/g4f4_B69 𝗗𝗮𝘆 𝟯𝟲/𝟭𝟬𝟬 ✅ | 𝟲𝟰 𝗺𝗼𝗿𝗲 𝘁𝗼 𝗴𝗼! #100DaysOfCode #LeetCode #LinkedList #Recursion #DataStructures #CodingInterview #SoftwareEngineer #Java #Algorithms #Programming
To view or add a comment, sign in
-
🚀 Day 71 of #100DaysOfCode – Pattern Problems 🧩 Problem of the Day: ✅ Bridge Pattern Today, I practiced the Bridge pattern, which is a good exercise to strengthen conditional logic inside nested loops. 💡 Approach (Space + Star Control): The bridge pattern visually looks like two vertical pillars connected by a horizontal bridge on the top. 🔍 Core Logic: First row: Print continuous stars to form the bridge (top connection) Middle rows: Print a star at the beginning and end Print spaces in between Last row (optional based on pattern variation): Similar to the first row or open bottom 📌 The key idea is to print stars only at fixed positions and spaces everywhere else. 🧮 Time Complexity: O(N × M) (N = rows, M = columns) 💾 Space Complexity: O(1) ✨ Lesson of the Day (Approach-Focused): Pattern problems sharpen attention to row–column positioning. Conditional checks are more important than loops in such patterns. Visualizing the output before coding makes implementation easier. #100DaysOfCode #DSA #PatternProblems #Java #LogicBuilding #CodingJourney #Consistency #Practice #NestedLoops
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
Keep going! 💪🔥