𝗗𝗮𝘆 𝟰𝟴/𝟭𝟬𝟬 | 𝗖𝗼𝗻𝘃𝗲𝗿𝘁 𝗕𝗶𝗻𝗮𝗿𝘆 𝗡𝘂𝗺𝗯𝗲𝗿 𝗶𝗻 𝗔 𝗟𝗶𝗻𝗸𝗲𝗱 𝗟𝗶𝘀𝘁 𝘁𝗼 𝗜𝗻𝘁𝗲𝗴𝗲𝗿 Day 48 ✅ — Binary logic meets linked list traversal. 𝗧𝗼𝗱𝗮𝘆'𝘀 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: ✅ 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 #𝟭𝟮𝟵𝟬: Convert Binary Number in a Linked List to Integer (Easy) From LeetCode 𝗪𝗵𝗮𝘁 𝗖𝗹𝗶𝗰𝗸𝗲𝗱: Each node represents a binary digit (0 or 1). The head is the most significant bit. So this isn’t just a linked list problem — it’s a 𝗯𝗶𝗻𝗮𝗿𝘆 𝘁𝗼 𝗱𝗲𝗰𝗶𝗺𝗮𝗹 𝗰𝗼𝗻𝘃𝗲𝗿𝘀𝗶𝗼𝗻 problem wrapped inside linked list traversal. Two key steps: 1️⃣ Compute the length of the list 2️⃣ Traverse again and apply positional binary weights (2^(length-1)) If a node contains 1, add 2^(position) to the result. Classic bit-weight logic. 𝗠𝘆 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵: 👉 First pass: calculate linked list length 👉 Second pass: • If node value is 1 → add 2^(length-1) • Decrement length 👉 Handle last node separately Time Complexity: O(n) Space Complexity: O(1) Two traversals, constant space, clean logic. 𝗠𝘆 𝗥𝗲𝗮𝗹𝗶𝘇𝗮𝘁𝗶𝗼𝗻: This problem reinforces something important: Linked list questions aren’t always about pointer manipulation. Sometimes they test whether you can layer fundamental concepts (like binary math) on top of traversal mechanics. The more I practice, the clearer patterns become: Traversal is routine. The real thinking is in identifying the hidden concept. 𝗖𝗼𝗱𝗲:🔗 https://lnkd.in/g9TyHZGk 𝗗𝗮𝘆 𝟰𝟴/𝟭𝟬𝟬 ✅ | 𝟱𝟮 𝗺𝗼𝗿𝗲 𝘁𝗼 𝗴𝗼! #100DaysOfCode #LeetCode #LinkedList #Binary #DataStructures #CodingInterview #SoftwareEngineer #Java #Algorithms #TimeComplexity #Programming
More Relevant Posts
-
🚀 Day 13/100 — Recursive Deconstruction Today’s Challenge: LeetCode 761 - Special Binary String 🧩 Yesterday was about bits; today was about Structural Grammar. Special Binary Strings aren't just 1s and 0s; they are a language. The Realization: A special string is like a valid set of parentheses. 1 is ( and 0 is ). To find the "lexicographically largest" version, you have to break the string into its most basic components, sort them, and put them back together. My 0ms Optimization Strategy: 1. Mental Mapping: Treated the string as a nested structure. If 1...0 is a special string, I stripped the outer layer, solved the inside, and then re-wrapped it. 2. Greedy Reassembly: Used Collections.sort() with a reverse comparator. In binary, "larger" just means the 1s appear as early as possible. 3. Memory Management: Minimized object creation by identifying "split points" first before committing to substring allocations. Reflection: Sometimes the fastest way to solve a problem isn't to iterate forward, but to look at the problem as a recursive tree. Every "Special" chunk is a node that can be reordered to maximize the total value. 13 days down. The logic is getting deeper. 87 days to go. 🛠️ #Java #DSA #LeetCode #100DaysOfCode #Recursion #StringAlgorithms #SoftwareEngineer #CodingJourney #Optimization
To view or add a comment, sign in
-
-
𝗗𝗮𝘆 𝟱𝟳/𝟭𝟬𝟬 — 𝗪𝗵𝗲𝗻 𝗦𝘁𝗮𝗰𝗸𝘀 𝗚𝗲𝘁 𝗚𝗿𝗲𝗲𝗱𝘆 Yesterday: Valid Parentheses. Basic stack. Today: Remove Duplicate Letters. Stack + greedy + frequency tracking. Level up. 𝗧𝗼𝗱𝗮𝘆'𝘀 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: ✅ #𝟯𝟭𝟲: Remove Duplicate Letters (Medium) 𝗧𝗵𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲: Remove duplicate letters so the result is: Smallest in lexicographical order (dictionary order) Contains each letter exactly once Example: "bcabc" → "abc" The trick? Knowing when to remove a character from the stack to make room for a better one. 𝗠𝘆 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵: Monotonic stack + greedy decisions. 👉 Track frequency: how many times each character appears ahead 👉 Track visited: have we already used this character? 👉 For each character, pop stack if: Stack top is larger (worse for lexicographical order) Stack top appears again later (we can use it then) 👉 Push current character and mark visited Time: O(n), Space: O(1) — only 26 letters max. 𝗪𝗵𝗮𝘁 𝗖𝗹𝗶𝗰𝗸𝗲𝗱: This combines three patterns: Monotonic stack (maintaining order) Greedy algorithm (making locally optimal choices) Frequency tracking (knowing what's ahead) Solving it wasn't about knowing one technique. It was about combining them. 𝗖𝗼𝗱𝗲: https://lnkd.in/gzw6ACFr 57 down. 43 to go. 𝗗𝗮𝘆 𝟱𝟳/𝟭𝟬𝟬 ✅ #100DaysOfCode #LeetCode #Stack #MonotonicStack #GreedyAlgorithm #DataStructures #Algorithms #CodingInterview #Programming #Java #MediumLevel #PatternCombination
To view or add a comment, sign in
-
Day 72: Binary Search Squared 🏔️ Problem 3296: Minimum Number of Seconds to Make Mountain Height Zero Today’s problem was a masterclass in optimization. The goal: reduce a mountain's height to zero using workers who take progressively more time for each unit of height they remove. The Strategy: • Binary Search on Answer: Since the time needed is monotonic, I used binary search to find the minimum seconds required to finish the job. • Nested Binary Search: Inside that loop, I ran a second binary search for each worker to calculate the maximum height they could handle within that time limit. • Efficiency: This "Double Binary Search" approach kept the runtime lean even with a massive search space up to 10^16. Solving a "Binary Search inside a Binary Search" problem is a great way to test your grip on time complexity. It’s all about finding that optimal boundary. 🚀 #LeetCode #Java #BinarySearch #Algorithms #ProblemSolving #DailyCode
To view or add a comment, sign in
-
🔥 Day 349 – Daily DSA Challenge! 🔥 Problem: 🧱 Pyramid Transition Matrix You are stacking blocks to form a pyramid. Each block is represented by a letter. Given a bottom row and a list of allowed triples "ABC" meaning: A B → C Return true if you can build the pyramid to the top. 💡 Key Insight — Bitmask + DFS Instead of storing allowed transitions as lists, we encode them using bitmasks. For each pair (A, B) we store possible top blocks using a bitmask. Conceptually: Example: ABC ABD mask[A][B] = {C, D} stored as bits. 🧠 Recursive Construction We build the pyramid row by row: 1️⃣ Current row → cur 2️⃣ Generate next row → next 3️⃣ For each adjacent pair (cur[i], cur[i+1]) check all possible blocks above 4️⃣ Recursively continue until: length = 1 → pyramid complete ⚡ Optimization Trick To extract possible blocks from bitmask: bit = m & -m This isolates the lowest set bit, letting us iterate through candidates efficiently. ⚙️ Complexity Let n be bottom length. ✅ Time Complexity: ~ O(7ⁿ) worst-case (pruned heavily) ✅ Space Complexity: O(n) recursion stack But pruning via allowed transitions keeps it practical. 💬 Challenge for you 1️⃣ Why does using bitmasks make transitions faster than lists? 2️⃣ How would you add memoization to avoid recomputing rows? 3️⃣ Can you solve this using DP with states instead of DFS? #DSA #Day349 #LeetCode #DFS #Bitmask #Backtracking #Java #ProblemSolving #KeepCoding
To view or add a comment, sign in
-
-
𝐋𝐞𝐞𝐭𝐂𝐨𝐝𝐞 𝐃𝐚𝐢𝐥𝐲 𝐂𝐡𝐚𝐥𝐥𝐞𝐧𝐠𝐞: 1784.Check if Binary String Has At Most One Segment of Ones Today’s challenge was a short but insightful problem that demonstrates how recognizing patterns in a binary string can lead to an extremely elegant solution. 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐮𝐦𝐦𝐚𝐫𝐲: We are given a binary string s consisting only of '0' and '1'. The task is to determine whether the string contains at most one continuous segment of '1'. In other words, once a '0' appears after a '1', there should not be another '1' later in the string. If there is more than one segment of '1', return false. Otherwise, return true. 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: The key observation is very simple. If a binary string has more than one segment of '1', the pattern "01" must appear somewhere in the string. Why? A valid string with only one segment of ones looks like: 0001111000 But an invalid string with multiple segments would look like: 11100111 Notice the transition "01" which indicates that ones started again after a zero. Therefore, the entire problem reduces to checking whether the substring "01" exists. If "01" is present → there are multiple segments of '1'. If "01" is absent → there is at most one segment. This allows us to solve the problem in a single line using the built-in string function. 𝐂𝐨𝐝𝐞 𝐒𝐧𝐢𝐩𝐩𝐞𝐭 (Java): class Solution { public boolean checkOnesSegment(String s) { return !s.contains("01"); } } 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲 𝐀𝐧𝐚𝐥𝐲𝐬𝐢𝐬: Time Complexity: O(n) Space Complexity: O(1) We scan the string once to check if the pattern "01" exists. 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠𝐬: Sometimes the best solution is identifying a simple pattern instead of simulating the entire process. Understanding how transitions occur in binary strings can simplify many problems. Leveraging built-in string functions can make solutions both clean and efficient. A great reminder that even small problems can reinforce strong pattern-recognition skills. #LeetCode #DSA #Java #ProblemSolving #Algorithms #BinaryStrings #CodingPractice #Consistency #100DaysOfCode #LearningJourney
To view or add a comment, sign in
-
-
🚀 LeetCode Daily Challenge Today’s problem was Find Unique Binary String. Problem: We are given n unique binary strings of length n. The task is to return any binary string of length n that does not exist in the given array. At first glance, brute force might come to mind: Generate all 2^n binary strings and check which one is missing. But that’s inefficient. 💡 Better Idea – Diagonal Trick (Inspired by Cantor’s Diagonal Argument) Instead of checking all possibilities, we construct a new string by flipping the i-th bit of the i-th string. The new string will differ from every string in the array at least at one position. Therefore, it cannot match any existing string. ⚡ Time Complexity: O(n) ⚡Space Complexity: O(n) 🔍 Key Insight: By flipping the diagonal bits, we guarantee the generated binary string is different from every given string. Problems like this remind me that a simple mathematical idea can lead to a really elegant algorithm. #LeetCode #DailyCodingChallenge #Java #Algorithms #DataStructures #ProblemSolving #CodingInterview
To view or add a comment, sign in
-
-
Day 74/365 – Remove Duplicate Letters Problem: Given a string s, remove duplicate letters so that each letter appears once and the result is the smallest lexicographical order possible. Example: "bcabc" → "abc" "cbacdcbc" → "acdb" 💡 Key Idea Use a Monotonic Stack to maintain characters in lexicographically smallest order. We also track: • Last occurrence of each character • Whether a character is already used in the result 🧠 Approach 1️⃣ Record the last index of each character. 2️⃣ Traverse the string. 3️⃣ Skip characters already included. 4️⃣ While stack top is bigger than current character and it appears later again, remove it to get a smaller result. 5️⃣ Push current character into the stack. 📦 Key Logic while(!stack.isEmpty() && stack.peek() > c && lastIndex[stack.peek() - 'a'] > i) { visited[stack.pop() - 'a'] = false; } This ensures: Lexicographically smallest result Each character appears only once 📊 Complexity ⏱ Time: O(n) 📦 Space: O(1) (since only 26 letters) ✨ Key Learning This is a classic Monotonic Stack + Greedy problem. Pattern to remember: 👉 Remove previous larger elements if they appear again later. This pattern appears in problems like: Smallest Subsequence Remove K Digits Monotonic stack optimization problems #Day74 #365DaysOfCode #LeetCode #MonotonicStack #GreedyAlgorithm #DataStructures #Algorithms #Java #DSA #CodingInterview
To view or add a comment, sign in
-
-
Day 8/60 – LeetCode Challenge 🔥 Problem: Longest Substring Without Repeating Characters Today’s concept: Sliding Window + HashMap 💡 The question is: Given a string s, find the length of the longest substring without repeating characters. Brute force approach? Generate all substrings and check duplicates → O(n²) ❌ Better approach? Use Sliding Window with a HashMap. Intuition (Based on My Code): I used two pointers: • low → start of the window • high → end of the window As high moves forward: • I add the character to the HashMap • The map stores character frequency Now here’s the important observation: If there are no duplicates in the current window, then: map.size() == window length But if a duplicate exists, then: map.size() < window length So while map.size() < (high - low + 1), I shrink the window by: • Reducing frequency of s.charAt(low) • Removing it from map if frequency becomes 0 • Moving low forward After the window becomes valid again, I update the result with: res = max(res, current window length) Why this works? Because: • Each character enters the window once • Each character leaves the window once So the total complexity stays linear. Time Complexity: O(n) Space Complexity: O(n) Key Learning: When the question involves: • Longest substring • No repeating characters • Continuous segment Think → Sliding Window + Hashing Day 8 completed ✅ Consistency continues 🚀 #LeetCode #DSA #SlidingWindow #HashMap #Java #CodingChallenge #PlacementPreparation
To view or add a comment, sign in
-
-
𝗗𝗮𝘆 𝟱𝟲/𝟭𝟬𝟬 — 𝗕𝗮𝗰𝗸 𝘁𝗼 𝘁𝗵𝗲 𝗖𝗹𝗮𝘀𝘀𝗶𝗰𝘀 Day 56. Valid Parentheses. The problem everyone sees on Day 1 of learning stacks. Except this time? I actually get why it works. 𝗧𝗼𝗱𝗮𝘆'𝘀 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: ✅ #𝟮𝟬: Valid Parentheses (Easy) 𝗧𝗵𝗲 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: Given a string of brackets: (), {}, []. Check if they're properly matched and nested. Examples: "()" → Valid "([)]" → Invalid (wrong order) "{[]}" → Valid (properly nested) 𝗧𝗵𝗲 𝗦𝗼𝗹𝘂𝘁𝗶𝗼𝗻: Stack. That's it. Push opening brackets. When you see a closing bracket, check if it matches the stack top. If yes, pop. If no, invalid. Empty stack at the end = valid. First time I saw this problem, I thought "why use a stack?" Now I see it—LIFO matches the nesting structure perfectly. 𝗪𝗵𝘆 𝗜𝘁 𝗠𝗮𝘁𝘁𝗲𝗿𝘀: This isn't just about parentheses. It's about recognizing when a problem needs LIFO behavior. Compilers use this. Code editors use this. Expression parsing uses this. Pattern recognition >> memorization. 𝗖𝗼𝗱𝗲: https://lnkd.in/gdCu84Ja 56 down. 44 to go. 𝗗𝗮𝘆 𝟱𝟲/𝟭𝟬𝟬 ✅ #100DaysOfCode #LeetCode #Stack #DataStructures #Algorithms #ProblemSolving #CodingInterview #Programming #Java #PatternRecognition
To view or add a comment, sign in
-
Day 72/100 – LeetCode Challenge ✅ Problem: #7 Reverse Integer Difficulty: Medium Language: Java Approach: Digit Extraction with Overflow Check Time Complexity: O(log₁₀ x) Space Complexity: O(1) Key Insight: Reverse integer by repeatedly extracting last digit and building result. Critical: Check overflow before final cast using long to detect > Integer.MAX_VALUE or < Integer.MIN_VALUE. Solution Brief: Extracted digits using modulo 10. Built reversed number step by step in long to detect overflow. After loop, divided by 10 to remove extra multiplication. Checked if result fits in 32-bit integer range. Handled sign separately at the end. #LeetCode #Day72 #100DaysOfCode #Math #Java #Algorithm #CodingChallenge #ProblemSolving #ReverseInteger #MediumProblem #Overflow #DigitManipulation #DSA
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