🔹 Day 50: Maximum Average Subarray I (LeetCode #643) 📌 Problem Statement: Given an integer array nums and an integer k, find the contiguous subarray of length k that has the maximum average value and return this value. ✅ My Approach: I used the sliding window technique to efficiently calculate the sum of subarrays of length k. First, I computed the sum of the first k elements. Then, as the window slid forward, I subtracted the element going out and added the new element entering the window. I continuously updated the maximum sum encountered and finally returned the average by dividing it by k. 📊 Complexity: Time Complexity: O(n) Space Complexity: O(1) ⚡ Submission Stats: Runtime: 2 ms (Beats 99.90%) Memory: 56.27 MB (Beats 66.43%) 💡 Reflection: This problem highlighted how the sliding window technique can drastically improve performance over recalculating sums for each subarray, making the approach both elegant and efficient. 🚀 #LeetCode #Java #SlidingWindow #Optimization #100DaysOfCode #Day50
"Max Average Subarray I: Sliding Window Technique"
More Relevant Posts
-
🌳 Day 60 of #100DaysOfCode 🌳 🔹 Problem: Balanced Binary Tree – LeetCode ✨ Approach: Used a post-order DFS traversal to calculate subtree heights while checking balance at every node. If the height difference of any subtree exceeds 1, return -1 immediately for an early exit — efficient and elegant! ⚡ 📊 Complexity Analysis: Time: O(n) — each node visited once Space: O(h) — recursion stack space, where h is the tree height ✅ Runtime: 0 ms (Beats 100%) ✅ Memory: 44.29 MB 🔑 Key Insight: A balanced tree isn’t just about equal heights — it’s about smart recursion that detects imbalance early, saving both time and memory. 🌿 #LeetCode #100DaysOfCode #Java #DSA #BinaryTree #Recursion #ProblemSolving #AlgorithmDesign #CodeJourney #ProgrammingChallenge
To view or add a comment, sign in
-
-
💻 Day 11 of #100DaysOfLeetCode – Rotate Image Today’s challenge was “Rotate Image”, a classic matrix manipulation problem that tests both logic and spatial reasoning. 🔹 Problem Statement: Given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise) — all in place, without using extra memory for another matrix. 🔹 My Approach (in Java): I used a two-step in-place transformation technique: 1️⃣ Transpose the matrix — swap elements across the diagonal (matrix[i][j] ↔ matrix[j][i]). 2️⃣ Reverse each row — to achieve the 90° clockwise rotation. This approach ensures O(1) extra space and O(n²) time complexity, which is optimal for this problem. 🔹 Key Takeaways: ✅ Learned how matrix transformations can be broken down into simpler operations. ✅ Improved understanding of in-place algorithms and memory efficiency. ✅ Reinforced clean coding habits and edge case handling. Every rotation brings me one step closer to mastering problem-solving patterns! 💪 #100DaysOfLeetCode #CodingJourney #RotateImage #Java #DSA #ProblemSolving #LeetCode #CodingChallenge #LearningEveryday
To view or add a comment, sign in
-
-
🔹 Day 48: Container With Most Water (LeetCode #11) 📌 Problem Statement: Given an array height[], where each element represents the height of a vertical line, find two lines that together form a container holding the maximum amount of water. ✅ My Approach: I used the two-pointer technique — starting one pointer at the beginning and another at the end of the array. At each step, I calculated the area using width * min(height[left], height[right]) and kept track of the maximum area. Then, I moved the pointer pointing to the shorter line, since that’s the limiting factor for water storage. 📊 Complexity: Time Complexity: O(n) Space Complexity: O(1) ⚡ Submission Stats: Runtime: 5 ms (Beats 76.45%) Memory: 58.32 MB (Beats 14.37%) 💡 Reflection: This problem reinforced how the two-pointer approach can transform a brute-force O(n²) problem into an efficient O(n) solution. 🚀 #LeetCode #Java #TwoPointers #Greedy #100DaysOfCode #Day48
To view or add a comment, sign in
-
-
🔥 **Blind 75 Challenge – Day 8: Longest Consecutive Sequence (LeetCode #128)** Today’s problem focuses on **detecting consecutive sequences efficiently** using a HashSet — one of the most elegant O(n) solutions in the #Blind75 list. 🧩 **Problem Statement:** Given an unsorted array of integers, return the length of the longest consecutive elements sequence. You must solve it in **O(n)** time. **Example:** Input: `[100, 4, 200, 1, 3, 2]` Output: `4` (sequence: 1, 2, 3, 4) 💭 **My Approach:** * Store all numbers in a **HashSet** for O(1) lookups * For each number, start counting only if it’s the **beginning of a sequence** (`num - 1` not in set) * Expand forward until the sequence breaks * Track the longest streak 🧠 **Time Complexity:** O(n) 💾 **Space Complexity:** O(n) 💡 **Key Takeaway:** This problem reinforces how **sets and linear scans** can replace nested loops — a simple yet powerful optimization mindset. #LeetCode #DSA #CodingChallenge #Java #ProblemSolving #DailyCoding #CareerGrowth #Blind75
To view or add a comment, sign in
-
-
🚀 Day 392 of #500DaysOfCode Today I solved LeetCode 661: Image Smoother 🖼️ This problem was all about applying a 3x3 smoothing filter on an image (represented as a 2D matrix). For each pixel, we calculate the average value of the surrounding pixels — considering only the valid neighbors — and take the floor of the result. It’s a great exercise for mastering matrix traversal and boundary condition handling in Java! 💡 🔍 Key Learnings: How to navigate a 2D grid using directional arrays. Handling edge cells carefully (like corners and borders). Efficiently applying averaging filters using nested loops. 🧠 Example: Input: [[100,200,100],[200,50,200],[100,200,100]] Output: [[137,141,137],[141,138,141],[137,141,137]] 💻 Language: Java 🔹 Difficulty: Easy Every problem like this strengthens my fundamentals in array manipulation and spatial algorithms — one step closer to writing cleaner, more efficient code. #Day392 #LeetCode #Java #CodingChallenge #100DaysOfCode #500DaysOfCode #ProblemSolving #SoftwareEngineering #MatrixAlgorithms
To view or add a comment, sign in
-
-
🔹 Day 62 of #100DaysOfLeetCodeChallenge 🔹 🚀 Problem: String to Integer (atoi) 🔑 Topic: String Manipulation 🧠 Approach: Step 1: Trim leading whitespaces. Step 2: Check for a sign (+ or -). Step 3: Convert valid digits to integer until a non-digit is found. Step 4: Clamp values beyond 32-bit signed range to [-2³¹, 2³¹ - 1]. Used a long to handle overflow safely before final conversion. ✅ ⏳ Time Complexity: O(n) 💾 Space Complexity: O(1) 📌 Example: Input: "42" → Output: 42 Input: " -042" → Output: -42 🎯 Takeaway: Always handle edge cases — whitespace, signs, and overflow — while converting strings to numbers. 💡 #LeetCode #String #Parsing #Java #ProblemSolving #100DaysOfLeetCodeChallenge 🚀
To view or add a comment, sign in
-
-
🌟 Day 92 of #100DaysOfLeetCode — Shuffle String (LeetCode 1528) Today’s problem was a simple yet elegant string manipulation challenge — “Shuffle String”, which tests your ability to rearrange elements based on given indices. 🧩 Problem: We are given a string s and an integer array indices. Each character in s should be placed at the new position given by indices[i]. The goal is to return the newly shuffled string. 🧠 Logic: Create a character array result of the same length as s. Loop through each index i. Place s.charAt(i) in result[indices[i]]. Finally, return the new string formed from result. ⏱️ Time Complexity: O(n) 💾 Space Complexity: O(n) 💬 This problem reminded me how simple array manipulation techniques can efficiently solve string rearrangement tasks — a great warm-up for more complex mapping problems! #Day92 #LeetCode #100DaysOfCode #Java #ProblemSolving #CodingJourney #WomenInTech
To view or add a comment, sign in
-
-
✅Day 41 : Leetcode 153 - Find Minimum in Rotated Sorted Array #60DayOfLeetcodeChallenge 🧩 Problem Statement Given a sorted array that has been rotated at an unknown pivot, find the minimum element in the array. The array contains unique elements, and the solution must run in O(log n) time. 💡 My Approach I used a binary search technique to efficiently find the minimum element. I maintained two pointers, low and high. At each step, I calculated the mid-point. If the left part (nums[low] to nums[mid]) was sorted, I updated my answer with the smaller of nums[low] and current ans, and moved low to mid + 1. Otherwise, I updated my answer with nums[mid] and moved high to mid - 1. This approach ensures we keep narrowing the search space toward the minimum element. ⏱️ Time Complexity O(log n) — Because the search space is halved in each iteration. #BinarySearch #LeetCode #RotatedSortedArray #DSA #CodingPractice #Java #ProblemSolving
To view or add a comment, sign in
-
-
🔥 LeetCode Daily Challenge – Day 14 🧩 Problem: 3312. Number of Substrings With One’s Count at Least Square of Zero’s Count 🚀 Approach: Zero-Positions + Prefix Optimization Today’s problem was an interesting blend of math and string analysis. The key insight was recognizing that zero-count grows slowly, and feasible substrings can be efficiently tracked using prefix logic and previous zero positions. ⭐ My approach: Precompute previous zero indices for fast block calculations Track zeros and compute required ones using ones ≥ zeros² Use a mathematical bound (sqrt(n)) to safely prune infeasible ranges Achieved near-linear behavior on large inputs Clean, optimized, and significantly faster than brute force. 💡 This method avoids triple-nested loops and leverages structure in binary strings. 📈 Result: ✔️ Accepted ⚡ Runtime: 115ms (Beats 83.33%) 📦 Memory: 47.07MB (Beats 26.85%) Day 14 streak still going strong! 🔥 #LeetCode #DailyChallenge #Java #DSA #ProblemSolving #DailyCodingChallenge #CodingStreak
To view or add a comment, sign in
-
-
#Day-70) LeetCode #3234 – Count Substrings With Dominant Ones Just solved an interesting binary string problem where a substring is considered dominant if the number of 1s is greater than or equal to the square of the number of 0s. 🔍 Challenge: Efficiently count all such substrings in a given binary string. 🧠 My Approach (Java): Used a prefix sum array to track the balance between 1s and 0s Explored substring ranges with a smart condition check Focused on clean logic and edge case handling 📈 Why it matters: This problem blends math with string manipulation and highlights how preprocessing (like prefix sums) can drastically reduce brute-force overhead. 📎 Code snippet attached — open to feedback or alternate strategies! #LeetCode #Java #DSA #BinaryStrings #ProblemSolving #CodingInPublic #TechJourney
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