🚀 Day 180 of #200DaysOfCoding Today I solved “Special Positions in a Binary Matrix” on LeetCode. 🧠 Problem Idea: We are given a binary matrix and need to count positions where the value is 1 and all other elements in the same row and column are 0. Such positions are called special positions. 💡 Approach: Instead of checking the entire row and column every time, we can optimize by: • Counting the number of 1s in each row • Counting the number of 1s in each column • A cell (i, j) is special if: mat[i][j] == 1 rowCount[i] == 1 colCount[j] == 1 This reduces unnecessary checks and keeps the solution efficient. ⏱ Time Complexity: O(m × n) 📦 Space Complexity: O(m + n) 💻 Language Used: C++ Consistency is the real key. Every day of problem solving improves logical thinking and problem-solving ability. Just 20 more days to complete the #200DaysOfCoding challenge! 💪 #leetcode #codingchallenge #programming #cpp #datastructures #algorithms #softwaredevelopment
Counting Special Positions in Binary Matrix with C++
More Relevant Posts
-
🚀 19 of #100DaysOfCode Solved LeetCode 1456 – Maximum Number of Vowels in a Substring of Given Length today. 🔍 Approach: Used the Sliding Window Technique to efficiently track the number of vowels in a substring of size k. Instead of recalculating vowels for every substring, we adjust the count as the window slides forward. 💡 Key Idea: Add the new character entering the window. Remove the character leaving the window. Keep track of the maximum vowel count. ⏱ Time Complexity: O(n) 📦 Space Complexity: O(1) This approach optimizes the brute-force solution and makes the algorithm scalable for large inputs. Consistent practice is helping me strengthen my problem-solving and data structures fundamentals. #leetcode #dsaalgorithms #slidingwindow #cpp #codingpractice #programming #softwaredevelopment #100daysofcode #developers #techlearning
To view or add a comment, sign in
-
-
🚀 Day 181 of #200DaysOfCoding Today I solved “Minimum Changes To Make Alternating Binary String” on LeetCode. 🔹 Problem: We are given a binary string consisting of 0 and 1. In one operation, we can flip any character (0 → 1 or 1 → 0). The goal is to make the string alternating, meaning no two adjacent characters are the same. Example of alternating strings: 0101, 1010 🔹 Key Insight: An alternating binary string can only have two possible patterns: 1️⃣ 010101... 2️⃣ 101010... So the idea is simple: Count how many changes are needed to convert the string to pattern 1. Count how many changes are needed to convert it to pattern 2. The minimum of the two will be the answer. ⏱ Time Complexity: O(n) 📦 Space Complexity: O(1) Consistent practice is helping me improve my problem-solving and pattern recognition skills every day. #leetcode #coding #programming #cpp #100daysofcode #200daysofcoding #softwaredevelopment
To view or add a comment, sign in
-
-
🚀 Day 69 of #100DaysOfCode 🔥 Problem: Majority Element II (LeetCode 229) Today’s challenge was all about finding elements that appear more than ⌊n/3⌋ times in an array. Sounds simple, but the twist is doing it efficiently! 💡 Key Insight: Instead of counting frequencies using extra space, we can use an optimized approach based on the Boyer-Moore Voting Algorithm. ✨ Approach Highlights: • There can be at most 2 majority elements (> n/3) • Maintain 2 candidates and their counts • First pass → find potential candidates • Second pass → verify their frequency ⚡ Why this works? Because any element appearing more than n/3 times will survive the elimination process. 📈 Complexity: • Time: O(n) • Space: O(1) 🎯 Takeaway: Smart algorithms > brute force. Understanding patterns like voting algorithms can save both time and space! #DSA #LeetCode #CodingJourney #ProblemSolving #100DaysOfCode #Algorithms #Programming
To view or add a comment, sign in
-
-
🚀 Day 6 of 100 Days LeetCode Challenge Problem: Check if Binary String Has at Most One Segment of Ones Today’s problem is all about pattern observation in strings—simple, but easy to overthink. 💡 Key Insight: The string should contain only one continuous block of '1's. 👉 That means: Once a 0 appears after a 1, There should be no more '1's later 🔍 Simplest Trick: Just check if the pattern "01" appears more than once OR even better → check if "10" appears followed by another "1" 💡 Cleaner approach: Traverse the string Count transitions from 1 → 0 If you ever see 1 again after that → ❌ Invalid 🔥 What I Learned Today: Many problems are just pattern validation Clean logic beats complex conditions Always try to reduce the problem to a simple rule 📈 Challenge Progress: Day 6/100 ✅ Consistency building strong! LeetCode, Strings, Pattern Recognition, Greedy, DSA Practice, Coding Challenge, Problem Solving, Algorithm Thinking, Programming #100DaysOfCode #LeetCode #DSA #CodingChallenge #Strings #ProblemSolving #TechJourney #ProgrammerLife #SoftwareDeveloper #CodingLife #LearnToCode #Developers #Consistency #GrowthMindset #InterviewPrep
To view or add a comment, sign in
-
-
🚀 Day 42 of #GeekStreak60 ✅ Problem Solved: Consecutive 1's Not Allowed Today’s problem looked simple at first, but it turned into a really clean Dynamic Programming pattern. The task was to count all binary strings of length "n" such that no two 1’s are consecutive. Instead of generating all possibilities (which would be exponential), I learned how to break it down using a recurrence relation. The key observation was that a valid string ending with "0" can come from any valid string of length "n-1", while a string ending with "1" must come from a string of length "n-2". This leads to a Fibonacci-like relation, making the solution efficient and elegant. 💡 Key Takeaways: • Recognizing patterns (like Fibonacci) simplifies complex problems • DP is powerful when overlapping subproblems exist • Thinking in terms of “choices at each step” is a game changer 📈 Consistency > Motivation 🔥 Current Streak: 42 days Let’s keep building, one problem at a time. #DSA #DynamicProgramming #CodingJourney #ProblemSolving #GeeksforGeeks #100DaysOfCode #Consistency #Learning #Growth
To view or add a comment, sign in
-
-
🚀 LeetCode 2840 : Check if Strings Can be Made Equal With Operations II 📈 Difficulty: Medium 👀 At first glance this looks like a swap simulation problem… You are allowed to swap characters at indices i and j as long as j - i is even. Unlimited swaps. Anywhere in the string. So it initially feels like we should be able to rearrange the string almost freely. But if you look closely at the condition, there’s a hidden restriction that completely changes the problem. 🧠 Key Insight If j - i is even, then the two indices must have the same parity. even − even = even odd − odd = even even − odd = odd So the only swaps that are actually possible are: • even index ↔ even index • odd index ↔ odd index A character at an even index can never move to an odd index, and vice versa. This means the string is effectively divided into two independent groups: • Even indices: 0, 2, 4, 6, ... • Odd indices: 1, 3, 5, 7, ... Characters can move freely inside their group, but they can never cross between groups. ⚙️ Generalized Way to Think About It Since swaps inside each group are unrestricted, we can generate any permutation within that group. That means the order inside the group no longer matters. So the transformation is possible if and only if: 1️⃣ Characters at even indices in s1 match characters at even indices in s2 2️⃣ Characters at odd indices in s1 match characters at odd indices in s2 In other words, we just need to compare the frequency of characters within each parity group. ⚡ But There’s an Even Cleaner Trick… Instead of building separate structures for both strings: • increment counts using characters from s1 • decrement counts using characters from s2 If every count returns to zero, the two strings contain the same characters within each parity group. Which means the transformation is always possible. ⏱ Complexity Time: O(n) Space: O(1) (fixed alphabet size) 💡 Takeaway Many problems become easier once you identify the invariants created by the allowed operation. Here the invariant is: Index parity never changes. Recognizing this instantly turns a problem that looks like swap simulation into a simple frequency check. #LeetCode #Algorithms #LearningInPublic #SoftwareEngineering #DSA #Programming #ProblemSolving
To view or add a comment, sign in
-
-
Day 42 on LeetCode — Merge Sorted Array (Two Pointer Approach) ✅ Today’s problem focused on efficient in-place array manipulation using the two-pointer technique. 🔹 Approach Used in My Solution The key insight was to compare elements from the back of both arrays instead of the front. Since nums1 already has extra space at the end to accommodate elements from nums2, we can fill the array from the last index backwards. Key points in the logic: • Initialize pointers at the end of the valid elements of nums1 and nums2 • Compare the elements and place the larger one at the back of nums1 • Move the pointers accordingly until all elements are merged • This allows the final sorted array to be stored directly in nums1 This strategy avoids unnecessary shifting of elements and keeps the solution efficient. ⚡ Complexity: • Time Complexity: O(m + n) • Space Complexity: O(1) (in-place merge) 💡 Key Takeaways: • Learned how working from the back can simplify in-place merges • Strengthened understanding of the two-pointer technique • Practiced optimizing array operations without using extra space #LeetCode #DSA #Algorithms #DataStructures #TwoPointers #Arrays #ProblemSolving #Coding #Programming #Cpp #STL #SoftwareEngineering #ComputerScience #CodingPractice #DeveloperLife #TechJourney #CodingDaily #100DaysOfCode #BuildInPublic #AlgorithmPractice #CodingSkills #Developers #TechCommunity #SoftwareDeveloper #EngineeringJourney
To view or add a comment, sign in
-
-
🚀 Day 18/60 — LeetCode Discipline Problem Solved: Valid Parentheses (Revision) Difficulty: Easy Today’s session revisited one of the most classic stack-based problems — validating whether a sequence of brackets is correctly balanced. The key idea is simple yet powerful: whenever an opening bracket appears, it is pushed onto the stack, and when a closing bracket appears, it must correctly match the most recent opening bracket. Problems like this beautifully demonstrate how the stack data structure naturally models nested structures, making it ideal for tasks involving balanced expressions and ordered matching. 💡 Focus Areas: • Reinforced stack-based problem solving • Practiced bracket matching logic • Strengthened understanding of nested structures • Improved clean implementation of stack operations • Focused on writing simple and readable logic ⚡ Performance Highlight: Achieved ~87% runtime efficiency on submission. Even the most fundamental problems continue to sharpen the foundations of algorithmic thinking. #LeetCode #60DaysOfCode #100DaysOfCode #DSA #Stack #Algorithms #DataStructures #ProblemSolving #CodingJourney #SoftwareEngineering #Programming #Developers #TechCareers
To view or add a comment, sign in
-
-
#Day44: LeetCode 153 – Find Minimum in Rotated Sorted Array Another interesting Binary Search problem today! The challenge: Given a sorted array that has been rotated at an unknown pivot, find the minimum element in O(log n) time. Key Insight: A rotated sorted array can be divided into two sorted parts. The minimum element is exactly where the rotation happens. Instead of scanning the whole array (O(n)), we can apply Binary Search to locate the pivot efficiently. Binary Search Logic: • If nums[mid] > nums[right], the minimum lies in the right half. • Otherwise, the minimum lies in the left half (including mid). This approach keeps shrinking the search space until both pointers meet at the minimum element. Time Complexity: O(log n) Space Complexity: O(1) This problem is a great reminder that Binary Search is not only for fully sorted arrays ,it also works on rotated sorted arrays when we identify the right pattern. Consistent practice is helping me recognize patterns faster in DSA problems. #leetcode #dsa #binarysearch #programming #codinginterview #cpp #100daysofcode
To view or add a comment, sign in
-
-
Day 43 on LeetCode — Squares of a Sorted Array ✅ Today’s problem focused on two-pointer technique and handling absolute values for efficient array transformation. 🔹 Approach Used in My Solution The goal was to return a sorted array of squares from a non-decreasing sorted input array, which may contain negative numbers. Key points in the logic: • Use two pointers at the start (left) and end (right) of the array • Compare absolute values of elements at both ends • Place the larger square at the current last position in the result array (pos) • Move the pointer of the larger absolute value and fill the result array from back to front • Continue until all elements are squared and placed This ensures a single-pass O(n) solution without extra sorting. ⚡ Complexity: • Time Complexity: O(n) • Space Complexity: O(n) 💡 Key Takeaways: • Practiced the two-pointer technique for array transformations • Learned to handle negative numbers when squaring • Strengthened understanding of in-place logic using a separate result array #LeetCode #DSA #Algorithms #DataStructures #Arrays #TwoPointers #ProblemSolving #Coding #Programming #Cpp #STL #SoftwareEngineering #ComputerScience #CodingPractice #DeveloperLife #TechJourney #CodingDaily #100DaysOfCode #BuildInPublic #AlgorithmPractice #CodingSkills #Developers #TechCommunity #SoftwareDeveloper #EngineeringJourney
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