📌 LeetCode Daily Challenge — Day 10 Remember yesterday's problem? Here's the plot twist 🔄 Yesterday I solved: 3145. Stable Binary Arrays I — Medium Today I'm solving: 3146. Stable Binary Arrays II — Hard Same problem. Same logic. Same code. But the constraints jumped from 200 to 1000 and that one change decides whether your solution passes or fails! Topic: Dynamic Programming, Prefix Sum 📌 What Changed: Problem 1 → zero, one ≤ 200 → DP table = 40K states Problem 2 → zero, one ≤ 1000 → DP table = 1M states If your solution has any hidden inner loop, Problem 2 catches it immediately with TLE 💥 🧠 Why The Same Code Still Works: 🔹 The solution I wrote yesterday already used the subtraction trick 🔹 Instead of looping over run lengths (O(limit) per state), I subtract the boundary case (O(1) per state) 🔹 dp[i][j][0] = dp[i-1][j][0] + dp[i-1][j][1] - dp[i-limit-1][j][1] 🔹 That one subtraction collapses the entire run length dimension 🔹 Total complexity → O(zero × one), scales perfectly even when constraints jump 5x 🏃 Quick Recap: Input: zero = 1, one = 1, limit = 2 Valid arrays with one 0 and one 1: "01" ✅ "10" ✅ return 2 ✅ Same input, same output but Problem 2 would reject any O(n²×limit) approach instantly. ⏱️ Time Complexity: O(zero × one) Every state computed in O(1) no inner loops 📦 Space Complexity: O(zero × one) 3D DP with last digit dimension = 2 💭 Here's the lesson: LeetCode often pairs a Medium and Hard version of the same problem. Medium lets you verify your logic even with a slightly naive approach. Hard cranks the constraints and forces you to write the truly optimized version. If you solve Medium with the optimal approach from day one, the Hard version is just copy-paste ✅ That's why thinking about optimization early always pays off! Same article from yesterday applies here the code hasn't changed one bit: https://lnkd.in/gMRVXyKx Have you ever had a solution that scaled perfectly from Medium to Hard? Drop it in the comments 👇 See you in the next problem 👋 #java #SoftwareEngineer #CodingInterview #DynamicProgramming #BackendDeveloper
LeetCode Challenge: Stable Binary Arrays II
More Relevant Posts
-
I've spent the last couple months using Claude Code exclusively, no hand-written code, to get a real read on where things are at. TL;DR: Vast net gain if you're already a solid engineer. Complete bug factory if you're not. When you first use Claude Code, you'll be ecstatic. The code works, and it gets done fast. But working code isn't necessarily shippable code. And as your codebase grows toward 50 KLOC, Claude gets noticeably dumber, slower, and costlier. You'll burn more tokens on refactors than on features, especially as your CLAUDE․md and memory files grow; they're all context, every time. The number 1 problem: code duplication. Multiple code paths for state, validation, and DB updates are a nightmare, and duplication is Claude's default behavior. Yes, even with Opus. So you add rules to CLAUDE․md. That file is still just context, not enforced config. Ask Claude if it's about to duplicate code before plan execution. 90% of the time, it is. Ask again after. 90% of the time, it did anyway. Defense in layers: narrow sub-agents, daily refactor sessions, one biggest issue at a time. You still need everything you always needed: tests, static analysis, hooks, CI gates, PR reviews. We may no longer care about *writing code*, but we absolutely still need to care *about code*. 2: File bloat. Claude piles on lines by default, which makes duplication (problem #1) harder to detect. Watch file sizes. Break them down aggressively. 3: Magic numbers. Claude avoids constants unless pushed. Well-named constants help the model understand intent and cut duplication. Same reason they helped humans. Project memory is largely useless, for the same reason CLAUDE․md alone isn't followed. There is no enforcement model. It's context turtles all the way down. Enforcement happens outside the model. Cost and time won't be what early enthusiasm suggests. A quality codebase requires slowing down. Nothing new. One human note: if you scrutinize plans, read changed code, and actually engage with the model, you'll understand the codebase as well as when you wrote everything by hand. A huge change is that you're context-switching across many features at once now, not just in and out of code. Development moves fast. It *feels* even faster, until the big refactors hit. Fast is slow, slow is fast. Even in 2026.
To view or add a comment, sign in
-
Claude Code's source code leaked via an npm .map file and enthusiasts and tinkerers are already analyzing the code. Anthropic will DMCA the original, but a Python rewrite is already circulating and that's legally untouchable. What did we actually learn? * CLAUDE.MD gets loaded on every single turn — 40,000 characters of context that most people (myself included) have barely touched. That's now changing. * Parallelism is a first-class citizen. Three execution models for sub-agents: fork (shared cache), tmux pane (file-based mailbox), and git worktree (isolated branch per agent). Running a single agent is the slow path. * The permission system was never meant to ask you anything. Every prompt is a configuration failure. There's a settings.json for a reason — use it. * Compaction is the real secret sauce. Five modes, from micro-compact (clearing stale tool results) to full session summarization. The insight: what the model forgets matters as much as what it remembers. * 66 built-in tools, split into concurrent (read-only, parallel) and serialized (mutations, one at a time). Clean architecture. The broader point: Claude Code's edge isn't just the harness — it's the harness tuned specifically for the Claude model family. The prompt design and the model co-evolved. For everyone building agentic systems: this is a rare chance to study production-grade agent architecture at scale. The insights around context management, sub-agent orchestration, and permission design will propagate through the open-source ecosystem fast.
To view or add a comment, sign in
-
✅ Day 606: Accepted on Codeforces! Problem B — Not Dividing | Round 856 (Div. 2) Divisibility, minimality, and a sneaky edge case — this problem packs a lot into a compact solution! 🔢 Let me walk you through the thinking! 🔗Problem Link: https://lnkd.in/gTYKfHTc 🧩 The Problem: Given an array of n positive integers, modify each element (by increasing it) with the minimum total number of increments such that no element divides any other element in the array. Find the resulting array! 💡 My Approach — Two-Pass Greedy: The Critical Observation: 1 divides everything! Any element equal to 1 must be increased first — it will always divide every other element, making a solution impossible unless we fix it. The minimum fix: bump all 1s to 2. ✅ Pass 1 — Fix all 1s: python for i in range(n): if a[i] == 1: a[i] = 2 Pass 2 — Fix divisibility between adjacent elements: After sorting (implicit in the problem structure), scan left to right. If a[i+1] % a[i] == 0 (next element is divisible by current), increment a[i+1] by 1 to break the divisibility: python for i in range(n-1): if a[i+1] % a[i] == 0: a[i+1] += 1 Why only check adjacent pairs? Because we process left to right on a sorted (or near-sorted) array — if a[i] doesn't divide a[i+1], it can't divide anything further right either (they're larger). One pass is sufficient! 🎯 Examples: [2, 4, 3, 6] → 4%2=0 → 4→5, 6%3=0 → 6→7 → [2, 5, 3, 7] ✅ [1, 2, 3] → 1→2, then 2%2=0 → 2→3, 3%3=0 → 3→4 → [2, 3, 4] ✅ [4, 2] → 4%2=0... wait, array is processed as given — [4, 2] → 2%4≠0 → [4, 2] ✅ 📌 Key Takeaways: 🔢 Always handle edge cases first — the value 1 is a special divisor of everything. Spotting and neutralising this edge case immediately simplifies all subsequent logic! 🕵️ 🔢 Greedy + single pass works because incrementing by just 1 is the minimum change, and after fixing a[i+1], it can only grow — never shrink — so earlier fixes stay valid! ✅ 🔢 Adjacent checking suffices on sorted input — divisibility violations in sorted arrays only occur between elements close in value. One left-to-right pass catches all violations! 🎯 🤯 The "Aha!" Moment: The entire problem collapses into two simple rules: kill all 1s first, then fix adjacent divisibility with a +1 nudge. Two passes, zero complex data structures, pure logic! 🧠 📧 sanjaykasaudhan09@gmail.com 📱 +91-9170580657 📋 Connect with me: https://lnkd.in/g_dRWtri #Codeforces #CompetitiveProgramming #Python #Divisibility #Greedy #NumberTheory #DSA #ProblemSolving #Accepted #CodingJourney #Programming
To view or add a comment, sign in
-
-
🚀 50 Important Coding Questions – Question 46/50 🔹 Decode String | LeetCode A classic Stack + String manipulation problem involving nested encoding patterns. 📌 Problem Statement Given an encoded string s, decode it using the rule: k[encoded_string] → repeat encoded_string k times The encoding may be nested. Example: Input s = "3[a2[c]]" Output "accaccacc" Explanation: a2[c] -> acc 3[acc] -> accaccacc 💡 Approach We use a stack to handle nested patterns. Steps: 1️⃣ Traverse the string 2️⃣ Push characters until ] is found 3️⃣ When ] appears: • Pop characters until [ → get substring • Extract the number (k) • Repeat the substring k times • Push back into stack 4️⃣ Continue until string ends 5️⃣ Build final result from stack ⏱ Time Complexity: O(n * k) (due to repeated strings) 📦 Space Complexity: O(n) 📌 LeetCode Result ✔ Accepted ⚡ Efficient stack-based decoding 🧠 Concepts Strengthened ✔ Stack for nested structures ✔ String parsing ✔ Handling multi-digit numbers ✔ Recursion-like behavior using stack 📍 Question 46 of 50 in my “50 Important Coding Questions” series. Only 4 problems left — final stretch! 💯🔥 👉 Question 47 coming next! #DSA #LeetCode #Stack #Strings #CodingInterview #ProblemSolving #CPlusPlus #TechJourney
To view or add a comment, sign in
-
-
✨ Mastering Clean Code Part 6: Design flexible systems by balancing objects and data structures ✨ In this Medium article, explore one of the most misunderstood areas in software design: how we expose or hide our data and why it matters more than you think. If you're serious about writing scalable and professional code, this one is for you. 🔗 Read the full article on Medium https://lnkd.in/eJyyjQHs #CleanCode #SoftwareEngineering #ObjectOrientedProgramming #Programming #CodeQuality #SoftwareDevelopment
To view or add a comment, sign in
-
📌 LeetCode Daily Challenge — Day 28 Problem: 1977. Reconstruct String from LCP Matrix Topic: Array, Matrix, String, Greedy, Dynamic Programming 📌 Quick Problem Sense: You're given an n × n matrix where lcp[i][j] = the length of the longest common prefix between suffix word[i..n-1] and suffix word[j..n-1]. Reconstruct the lexicographically smallest string that produces this exact matrix. If no valid string exists → return "" This is a Hard problem and the challenge is twofold: build the string AND prove it's correct! 🔥 🧠 Approach (Simple Thinking): 🔹 Greedy assignment: lcp[i][j] > 0 means word[i] and word[j] must be the same character — scan left to right, assign the next smallest letter ('a', 'b', ...) to each unlinked position and propagate to all linked positions 🔹 More than 26 distinct groups? Impossible → return "" 🔹 Validate with a recurrence — build a fresh DP table bottom-up from the constructed word: word[i] == word[j] → dp[i][j] = 1 + dp[i+1][j+1] word[i] != word[j] → dp[i][j] = 0 🔹 If any dp[i][j] != lcp[i][j] → the matrix is a contradiction → return "" 🔹 Sanity-check upfront: diagonal must be lcp[i][i] = n - i, matrix must be symmetric, values must respect suffix length bounds ⏱️ Time Complexity: Sanity check + Greedy + Bottom-up DP → all O(n²) Overall → O(n²) — single matrix scan per phase! 📦 Space Complexity: DP table → O(n²) No suffix array, no heavy data structure needed! I wrote a full breakdown with dry run, real-life analogy, and step-by-step code walkthrough here 👇 https://lnkd.in/gFxNyz-i If you solved it using Union-Find to group same-character positions, or used a suffix array approach, drop it in the comments, always curious to see how others think 💬 See you in the next problem 👋 #LeetCode #DSA #CodingChallenge #Java #ProblemSolving #DynamicProgramming
To view or add a comment, sign in
-
-
It was just a matter of time: and here it is … the first programming language for AI specs. Interesting that it was build by one of the big IDE providers 😃 Shows IMO, that the direction of Software Engineering toward Spec Driven approaches is more than a hype.
To view or add a comment, sign in
-
[𝗢𝗢𝗣 𝗣𝗼𝘀𝘁 #1] 𝗪𝗵𝘆 𝗱𝗼 𝘄𝗲 𝗮𝗰𝘁𝘂𝗮𝗹𝗹𝘆 𝘂𝘀𝗲 𝗢𝗯𝗷𝗲𝗰𝘁-𝗢𝗿𝗶𝗲𝗻𝘁𝗲𝗱 𝗣𝗿𝗼𝗴𝗿𝗮𝗺𝗺𝗶𝗻𝗴 (𝗢𝗢𝗣)? Early programming was entirely imperative: you wrote a list of instructions, the computer executed them in order. As programs grew, programmers invented subroutines (functions), global variables, and modules. This worked fine for small programs. The problem emerged when programs grew to tens of thousands of lines. Global state became unmanageable. Functions reached across the entire codebase to modify data. A change in one place broke something in an unrelated place. Programs became brittle, hard to understand, and nearly impossible to maintain. This was called the 𝘀𝗼𝗳𝘁𝘄𝗮𝗿𝗲 𝗰𝗿𝗶𝘀𝗶𝘀, a real phenomenon identified in the late 1960s where software projects routinely ran over budget, over time, and failed. Object-Oriented Programming is a programming paradigm that organizes software around 𝗼𝗯𝗷𝗲𝗰𝘁𝘀 - bundles of state (data) and behavior (functions), rather than around procedures and logic alone. Here are the 4 major problems OOP solves to keep your projects from collapsing under their own weight: 1. 𝗧𝗵𝗲 "𝗪𝗵𝗼𝗱𝘂𝗻𝗻𝗶𝘁" 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 (𝗨𝗻𝗰𝗼𝗻𝘁𝗿𝗼𝗹𝗹𝗲𝗱 𝗦𝘁𝗮𝘁𝗲) In procedural code, global data is a free-for-all. Any function can change any variable. When a bug corrupts your data, you have to hunt through every line of code to find the culprit. • 𝗢𝗢𝗣 𝗦𝗼𝗹𝘂𝘁𝗶𝗼𝗻: 𝗘𝗻𝗰𝗮𝗽𝘀𝘂𝗹𝗮𝘁𝗶𝗼𝗻. Objects own their data. If you want to change a value, you go through a specific method. This contains the "blast radius" of bugs. 2. 𝗧𝗵𝗲 "𝗛𝗼𝘂𝘀𝗲 𝗼𝗳 𝗖𝗮𝗿𝗱𝘀" 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 (𝗖𝗼𝘂𝗽𝗹𝗶𝗻𝗴 & 𝗥𝗶𝗴𝗶𝗱𝗶𝘁𝘆) Ever changed one small function and watched ten unrelated parts of the app break? That’s tight coupling. • 𝗢𝗢𝗣 𝗦𝗼𝗹𝘂𝘁𝗶𝗼𝗻: 𝗔𝗯𝘀𝘁𝗿𝗮𝗰𝘁𝗶𝗼𝗻 & 𝗜𝗻𝘁𝗲𝗿𝗳𝗮𝗰𝗲𝘀. Objects hide 𝘩𝘰𝘸 they work and only show 𝘸𝘩𝘢𝘵 they can do. You can swap out the "engine" without changing the "dashboard." 3. 𝗧𝗵𝗲 "𝗖𝗼𝗽𝘆-𝗣𝗮𝘀𝘁𝗲" 𝗧𝗿𝗮𝗽 (𝗖𝗼𝗱𝗲 𝗗𝘂𝗽𝗹𝗶𝗰𝗮𝘁𝗶𝗼𝗻) If you have a Circle and a Rectangle, you shouldn’t have to rewrite the draw() logic from scratch for both. • 𝗢𝗢𝗣 𝗦𝗼𝗹𝘂𝘁𝗶𝗼𝗻: 𝗜𝗻𝗵𝗲𝗿𝗶𝘁𝗮𝗻𝗰𝗲 & 𝗣𝗼𝗹𝘆𝗺𝗼𝗿𝗽𝗵𝗶𝘀𝗺. You define common behavior in a base "Shape" class. Your code treats everything as a Shape, and the objects handle the specific details themselves. 4. 𝗧𝗵𝗲 "𝗠𝗲𝗻𝘁𝗮𝗹 𝗚𝘆𝗺𝗻𝗮𝘀𝘁𝗶𝗰𝘀" 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 (𝗖𝗼𝗴𝗻𝗶𝘁𝗶𝘃𝗲 𝗟𝗼𝗮𝗱) A 2,000-line file of raw procedures is impossible to hold in your head. It doesn't look like the real world. • 𝗢𝗢𝗣 𝗦𝗼𝗹𝘂𝘁𝗶𝗼𝗻: 𝗗𝗼𝗺𝗮𝗶𝗻 𝗠𝗼𝗱𝗲𝗹𝗶𝗻𝗴. By creating objects like User, Order, or PaymentProcessor, the code mirrors how we naturally think. It turns abstract logic into a relatable story. Feel free to add more in the comments! 👇
To view or add a comment, sign in
-
I have seen a lot of codebases without any null value. it's extremely easy, when you use a language where null just doesn't exist.
NULL is pure evil. In his book Elegant Objects, Yegor Bugyenko dedicates no less than 3 chapters to the NULL value. He gives 3 rules: - Never use NULL references - Never accept NULL arguments - Never return NULL Yes, he hates it. He even calls it “pure evil” ! If you are like me, you probably have never seen a codebase that does not use NULL values. We commonly use them to signify that a value is not found. But this has a cost. ➡️ Yegor explains that setting an object’s field to NULL is often used as a semantic signification. For example, we could put a NULL name in a User object to model a Customer. This is a bad practice that leads to unmaintainable code. Instead of creating meaningful objects we use NULL values to give different semantic meanings to the same class. ➡️ NULL arguments tend to turn real meaningful objects into dumb data structures. By checking if our method argument is NULL, instead of talking to an object that encapsulates its own behavior, we tell a data structure how to behave. This breaks the fundamental rule of encapsulation and leads to spaghetti code. ➡️ Returning NULL values breaks the trust we have in our objects. Since we never know whether an object will have a value or not, we always have to check if it exists. A developer can never really know how the code is supposed to behave and have to read more to understand it. As an alternative, Yegor proposes the null object design pattern. It simply is an object that represents the absence of the object we are looking for. For example, when a User is not found, we could return an object of type NullUser. The whole book is a gem regarding Object Oriented programming. I warmly recommend it to any developer! --- I’m Tim👨💻, senior back-end engineer. I talk everyday about system design, tech news and the fundamentals of back-end development.
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