Day 104 of #200DaysOfCode Leveling up The grind continues — consistency in action. Today I solved "Kth Largest Element in an Array" on LeetCode using a Max Heap (Priority Queue) approach. Key Idea: The largest element can be efficiently accessed using a heap. By removing the top element k-1 times, we land on the kth largest element. Approach: • Build a max heap from the array • Pop the top element k-1 times • The top now represents the kth largest element Concepts Used: • Heap / Priority Queue • STL (C++) • Sorting Alternative Time Complexity: O(n log n) Space Complexity: O(n) Takeaway: Heaps are powerful when dealing with top K problems and can often replace full sorting for better clarity and efficiency. New day, new problem Consistency is becoming a habit #Day104 #200DaysOfCode #LeetCode #Heap #PriorityQueue #Cpp #CodingJourney #ProblemSolving #KeepGoing
Kth Largest Element in Array Solved with Max Heap
More Relevant Posts
-
👉Day 156 of 160 – LeetCode Challenge Problem: Path With Minimum Effort 👉 Problem:we have to return the minimum effort required to travel from the top-left cell to the bottom-right cell. 👉Key Concept: Use Dijkstra minimizing maximum edge difference, track effort with max(), relax neighbors, priority queue ensures optimal minimax path.
To view or add a comment, sign in
-
-
Day 36/75 🚀 Solved Longest ZigZag Path in a Binary Tree (LeetCode 1372) today! ✅ All 58/58 test cases passed ⚡ Runtime: 0 ms (Beats 100%) 💾 Memory: 94.28 MB (Beats ~98%) 🔍 Approach: Used DFS (recursion) while tracking direction and path length. ✔️ At each node, kept track of left and right zigzag lengths ✔️ If moving left → increment left count, reset right ✔️ If moving right → increment right count, reset left ✔️ Updated maximum length at every step This ensures we explore all possible zigzag paths efficiently. 💡 Key Learning: Tree problems often require tracking state + direction together. Passing multiple parameters in recursion makes solutions cleaner. Consistency + pattern recognition = stronger intuition 🌳⚡ #LeetCode #CPP #DSA #ProblemSolving #CodingJourney #75DaysOfCode #Focused
To view or add a comment, sign in
-
-
🚀 Day 71 of #100DaysOfCode Today, I solved LeetCode 1572 – Matrix Diagonal Sum, a problem that focuses on matrix traversal and efficient computation. 💡 Problem Overview: Given a square matrix, the task is to calculate the sum of its primary and secondary diagonals, ensuring that the center element (if overlapping) is counted only once. 🧠 Approach: ✔️ Traversed the matrix to calculate: Primary diagonal → elements where row == column Secondary diagonal → elements where row + column == n - 1 ✔️ Handled the center element separately to avoid double counting ⚡ Key Takeaways: Understanding index relationships simplifies matrix problems Edge cases (like overlapping elements) are important Clean logic can avoid unnecessary loops 📊 Complexity Analysis: Time Complexity: O(n) Space Complexity: O(1) Small problems strengthen big concepts 🚀 #LeetCode #100DaysOfCode #DSA #Matrix #ProblemSolving #CodingJourney #SoftwareDevelopment
To view or add a comment, sign in
-
-
🚀 Day 248 of #500DaysOfCode Solved LeetCode 396 – Rotate Function 🔄 💡 Approach: Instead of recomputing every rotation (which would be O(n²)), we use a mathematical relation Let: sum = total sum of array F(0) = initial rotation value Then: F(k) = F(k-1) + sum - n * nums[n-k] ⚡ Key Insight: Each rotation shifts weights → reuse previous result This reduces redundant computation drastically ⏱️ Complexity: Time: O(n) Space: O(1) ✨ Takeaway: Whenever rotations are involved, look for a pattern / recurrence instead of brute force. Consistency continues 🔥 #LeetCode #DSA #Arrays #Math #Optimization #Consistency
To view or add a comment, sign in
-
-
Day 113 Solved 2615. Sum of Distances today — a solid problem on prefix sums + hashing. 💡 Key Insight: Instead of calculating distances naively (which would be too slow), group indices of the same values and use prefix sums to compute distances efficiently in linear time. 🔹 Learned how to: Optimize repeated distance calculations Use prefix sums to avoid nested loops Think in terms of contribution from left & right sides ⚡ Result: ✅ All test cases passed ⏱️ Runtime: 71 ms 📊 Beat ~56% submissions Every day, patterns get clearer and thinking gets sharper. #LeetCode #Day113 #DSA #CodingJourney #PrefixSum #ProblemSolving
To view or add a comment, sign in
-
-
Solving Leetcode problem | Day 2 Problem: Length of last Word | Approach: Traverse the string from end | Time Complexity: O(n) & Space Complexity: O(1) Problem I faced : Initially when last idx contains space code return wrong length | Cause: Not using proper condition |
To view or add a comment, sign in
-
-
Turning a classic into an efficient solution 💡 Day 32🚀 Solved 3Sum by leveraging sorting + two-pointer technique to find all unique triplets summing to zero, while carefully handling duplicates for correctness. 🔹 Time Complexity: O(n²) 🔹 Runtime: 33 ms (Beats 73.50%) 🔹 Memory Usage: 59.16 MB Problems like this show how the right pattern can transform a brute-force idea into an optimized solution. On to the next challenge — Day 32 🚀 #LeetCode #DSA #ProblemSolving #CodingJourney #SoftwareEngineering #Consistency #KeepLearning
To view or add a comment, sign in
-
-
Day 78/150 🚀 LeetCode 117: Populating Next Right Pointers in Each Node II 🧠 Problem Given a binary tree, populate each next pointer to point to its next right node. If there is no next right node, set it to NULL. 💡 Approach • Use level order traversal • Create dummy node for next level • Connect left and right children • Move level by level ⏱ Time Complexity O(n) 📦 Space Complexity O(1) ✅ Result: Accepted ⚡ Runtime: 11 ms #Day78 #LeetCode #DSA #BinaryTree #LevelOrder #CodingJourney #ProblemSolving #SoftwareEngineering
To view or add a comment, sign in
-
-
Day 5️⃣ /15 — Consistency Challenge 🚀 Today’s problem: LeetCode 1855 — Maximum Distance Between a Pair of Values Solved using a clean two-pointer approach. 🔹 Problem Idea: We need to find the maximum value of j - i such that: i <= j nums1[i] <= nums2[j] Both arrays are non-increasing (sorted in descending order), which makes this a perfect two-pointer problem. 🔹 Approach (Two Pointers): Start two pointers: i = 0 for nums1 j = 0 for nums2 Traverse while both pointers are in bounds: If nums1[i] <= nums2[j] ✅ Valid pair found Update answer with j - i Move j forward to try for a larger distance Else (nums1[i] > nums2[j]) ❌ Invalid pair Move i forward to reduce nums1[i] and try to make condition valid 🔹 Time Complexity: Each pointer moves at most once through its array 👉 O(n + m) 🔹 Space Complexity: 👉 O(1) Clean logic, efficient solution, and another step in the consistency journey 💯 #LeetCode #DSA #TwoPointers #Consistency #CodingJourney #LearnInPublic
To view or add a comment, sign in
-
-
🚀 Day 70 of #100DaysOfCode Today, I solved LeetCode 1559 – Detect Cycles in 2D Grid, a problem that combines graph traversal with cycle detection in a matrix. 💡 Problem Overview: Given a 2D grid of characters, the task is to determine whether there exists a cycle formed by adjacent cells having the same value. 🧠 Approach: ✔️ Treated the grid as a graph where each cell is a node ✔️ Applied DFS/BFS to traverse connected components ✔️ Tracked the parent cell to detect cycles correctly ✔️ If a visited cell is encountered again (not the parent), a cycle exists ⚡ Key Takeaways: Cycle detection in grids requires careful parent tracking Matrix problems can be converted into graph problems DFS/BFS are versatile for both traversal and cycle detection 📊 Complexity Analysis: Time Complexity: O(n × m) Space Complexity: O(n × m) 70 days of consistency — building stronger problem-solving skills every day 🚀 #LeetCode #100DaysOfCode #DSA #Graphs #CycleDetection #DFS #BFS #ProblemSolving #CodingJourney #SoftwareDevelopment #Consistency
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