𝗘𝘃𝗲𝗿 𝗻𝗼𝘁𝗶𝗰𝗲𝗱 𝘀𝗼𝗺𝗲 𝘀𝗼𝗹𝘂𝘁𝗶𝗼𝗻𝘀 𝗼𝗻 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝘁𝗵𝗮𝘁 “𝗯𝗲𝗮𝘁 𝟭𝟬𝟬%”? __import__("atexit").register( lambda: open("display_runtime.txt", "w").write("0") ) Looks like a hacker trick. Feels smart. 𝗪𝗵𝗮𝘁 𝘁𝗵𝗶𝘀 𝗰𝗼𝗱𝗲 𝗮𝗰𝘁𝘂𝗮𝗹𝗹𝘆 𝗱𝗼𝗲𝘀 (𝗹𝗶𝗻𝗲 𝗯𝘆 𝗹𝗶𝗻𝗲): • 𝗮𝘁𝗲𝘅𝗶𝘁 → runs code when the program is about to exit • 𝗿𝗲𝗴𝗶𝘀𝘁𝗲𝗿(...) → hooks a function to run at the very end • 𝗼𝗽𝗲𝗻(...).𝘄𝗿𝗶𝘁𝗲("𝟬") → overwrites the file LeetCode reads to show runtime So when execution finishes, the runtime file gets replaced with 0. The algorithm didn’t get faster. Only the displayed number did. 𝗟𝗲𝘀𝘀𝗼𝗻: If you don’t understand how a metric is calculated, you’ll optimize the metric — not the system. #moxcode #Python #LeetCode #Engineering #SystemsThinking #LearningInPublic
LeetCode Algorithm Optimization Trick
More Relevant Posts
-
🗄️ dataclass + Protocol Dict-based code scales until the first refactor. Passing dicts feels fast. Flexible. Obvious. Then the system grows. Key mismatches appear at runtime. Contracts live in Slack threads. Refactors feel risky. The fix is explicit contracts. Use dataclass for shape. frozen=True makes it immutable. Safer for shared state. Use Protocol for boundaries. Any class with a matching get() method satisfies the contract. No inheritance required. Refactors become predictable. Testing becomes simpler. Type checkers catch mismatches early.🔵 ⚪ #Python #SoftwareArchitecture #BackendDevelopment
To view or add a comment, sign in
-
-
🚀 𝟲𝟬 𝗗𝗮𝘆𝘀 𝗼𝗳 𝗖𝗼𝗱𝗶𝗻𝗴 | 𝗗𝗦𝗔 𝘅 𝗥𝗲𝗮𝗹 𝗪𝗼𝗿𝗹𝗱 𝗣𝗿𝗼𝗷𝗲𝗰𝘁𝘀 #Day32 | 𝗦𝗵𝗼𝗿𝘁𝗲𝘀𝘁 𝗖𝘂𝘀𝘁𝗼𝗺𝗲𝗿 𝗦𝘂𝗽𝗽𝗼𝗿𝘁 𝗣𝗮𝘁𝗵 Built a Shortest Customer Support Path system using Breadth-First Search (BFS) to determine the minimal escalation route in a support network. Focused on: • BFS for shortest path • Graph traversal • Queue-based level exploration • Understanding real-world routing and escalation systems 📌 𝗖𝗼𝗱𝗲 𝗮𝗻𝗱 𝗱𝗼𝗰𝘂𝗺𝗲𝗻𝘁𝗮𝘁𝗶𝗼𝗻: https://lnkd.in/gxzGJ4nB Feedback and suggestions are welcome. #DSA #Graphs #BFS #ShortestPath #Python #60DaysOfCoding #LearningInPublic #SoftwareEngineering
To view or add a comment, sign in
-
🚀 #DAY74 LeetCode Problem #1200 – Minimum Absolute Difference 🧩 Problem Summary Given a list of distinct integers, the goal is to find all element pairs whose absolute difference is the minimum possible and return them in sorted order. 🧠 How It Works First, sort the array to bring close values together Scan once to compute the minimum difference between adjacent elements Scan again to collect all pairs that match this minimum difference This approach leverages the fact that in a sorted array, the smallest difference must occur between neighboring elements. 📘 What I Learned ✔ Why sorting simplifies difference-based problems ✔ How breaking the task into two passes improves clarity ✔ Turning mathematical observations into efficient logic ✔ Writing clean and readable solutions even when performance isn’t maxed 📊 Performance ⏱ Runtime: 95 ms 💾 Memory: 21.57 MB ✅ 38 / 38 testcases passed 📌 Small optimizations, big clarity — consistency is the real win. #LeetCode #DSA #Algorithms #ProblemSolving #Python #CodingPractice #DataStructures #TechJourney #100DaysOfDSA #LearningByDoing #Consistency #InterviewPrep #CompetitiveProgramming
To view or add a comment, sign in
-
-
Pandas Contributor | Merged PR #63126 A quick dive into a regression test I wrote for pandas recently. The challenge: A subtle memory safety bug in groupby().apply() was causing silent state failures where internal object reuse corrupted externally stored DataFrames. The fix: Enforcing strict deep-copy validation prevents the data corruption from resurfacing and ensures thread-safe state management. Proud to have this merged into the 3.0 milestone! Check out the test logic below. #SoftwareEngineering #Python #TestDrivenDevelopment #OpenSource #DataEngineering
To view or add a comment, sign in
-
-
𝐒𝐭𝐨𝐩 𝐫𝐞𝐜𝐚𝐥𝐜𝐮𝐥𝐚𝐭𝐢𝐧𝐠 𝐭𝐡𝐞 𝐬𝐚𝐦𝐞 𝐭𝐡𝐢𝐧𝐠 𝟏𝟎,𝟎𝟎𝟎 𝐭𝐢𝐦𝐞𝐬. If you’re solving a problem where the data is static but the queries are frequent, you shouldn't be looping inside your functions. You should be using the __𝐢𝐧𝐢𝐭__ method. In LeetCode (and real-world APIs), we call this 𝐏𝐫𝐞-𝐜𝐨𝐦𝐩𝐮𝐭𝐚𝐭𝐢𝐨𝐧. Brute Force: O(N) per request. Total time = Slow. Pre-computation (Prefix Sums): O(1) per request. 𝐓𝐡𝐞 𝐑𝐮𝐥𝐞 𝐨𝐟 𝐓𝐡𝐮𝐦𝐛: If you have a loop inside a method that gets called repeatedly, ask yourself: "Can I do this work once in the constructor instead?" By shifting the work to the "initialization" phase, we turn a slow process into a high-performance system. This is often a repeated question pattern in LeetCode as well. #Python #CodingTips #SoftwareEngineering #LeetCode #Optimization #CleanCode #DataStructures
To view or add a comment, sign in
-
✨ Day 68 of #100DaysOfDSAChallenge 📌 LeetCode 66: Plus One A simple-looking problem that reinforces the importance of edge-case handling and digit manipulation. 🧩 Problem Overview: Given a large number represented as an array of digits, increment it by one without converting it into an integer. 🧠 Approach & How It Works: Traverse the array from right to left (least significant digit first) If the digit is less than 9, increment and return immediately If the digit is 9, convert it to 0 and propagate the carry If all digits are 9, prepend 1 to the array This ensures an in-place, efficient solution without extra conversions. ⚙️ Performance: 🚀 Runtime: 0 ms (beats 100%) 📦 Memory: 12.35 MB (beats 81.81%) ✅ Testcases Passed: 112 / 112 📘 What I Learned: ✔ Handling carry propagation cleanly ✔ Importance of scanning from the least significant digit ✔ Avoiding unnecessary data type conversions ✔ Writing concise and optimal logic Onward with consistency and problem-solving 💪 #LeetCode #DSAChallenge #PlusOne #Consistency #100DaysOfDSA #100DaysOfCode #ArrayProblems #Python #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
Day 10/100 – #100DaysOfLeetCode 🚀 🧩 Problem: LeetCode 844 – Backspace String Compare (Easy) 🧠 Approach: Simulate typing using stacks and compare final strings. 💻 Solution: class Solution: def build(self, s): stack = [] for i in s: if i=="#": if stack: stack.pop() else: stack.append(i) return stack def backspaceCompare(self, s: str, t: str) -> bool: return self . build(s)==self . build(t) ⏱ Time | Space: O(n) | O(n) 📌 Key Takeaway: Stack-based simulation is an effective way to model real-time text editing behavior. #leetcode #dsa #python #stack #codingchallenge
To view or add a comment, sign in
-
-
Solving the Bitwise OR Challenge 🔢 The problem: Find the smallest 'x' such that x OR (x + 1) equals a given prime number 'p'. The Logic: 💡 The only prime number that cannot satisfy this is 2. For all other primes (which are odd), the binary representation ends in at least one '1'. 💡 The operation x OR (x + 1) essentially fills the rightmost '0' bit of 'x' with a '1'. 💡 To minimize 'x' while satisfying the condition, we look for the rightmost sequence of consecutive 1s in the prime 'p'. 💡 By flipping the highest bit of that specific trailing sequence from 1 to 0, we create our answer. Example Walkthrough: 👉 For p = 13 (1101): The trailing 1 is at position 0. Flipping it gives 1100 (12). 12 OR 13 = 13. 👉 For p = 31 (11111): The sequence of 1s is long. Flipping the bit at the "boundary" gives 01111 (15). 15 OR 16 = 31. Code Implementation: https://htmlify.me/r/u0q7 Key Takeaway: When dealing with x OR (x + 1), you are looking at how a carry bit propagates through trailing ones in binary addition! #BitManipulation #Programming #Algorithms #Python #CompetitiveProgramming #TechTips
To view or add a comment, sign in
-
-
Recently solved "LeetCode 1970 – Last Day Where You Can Still Cross" and learned an interesting optimization. My first approach used DFS + binary search, which worked but wasn’t very efficient. Instead, I tried a reverse incremental BFS: - Start with all cells flooded - Unflood cells one by one in reverse order - Incrementally propagate reachability from the top row Each cell is processed at most once, giving amortized O(R×C) runtime — similar to Union-Find, but using BFS. Even if you’re not familiar with DSU, this method is intuitive: just track which cells are reachable from the top as you unflood the grid. While most online solutions use Union-Find or binary search + BFS/DFS, this incremental BFS approach is less commonly discussed and performs extremely well (~8× faster than DFS + binary search in Python). Sharing the solution here: 🔗 [https://lnkd.in/gVkzaSgj] #algorithms #problemSolving #softwareengineering
To view or add a comment, sign in
-
Python with DSA — Day 33 What I worked on: Prime checks: moved from naive divisibility to the √n optimization and the 6k±1 rule to cut unnecessary iterations. Time complexity intuition: compared O(n) vs O(√n) for primality tests; saw how early exits change best/worst cases. Clean loops & edge cases: handled n <= 1, negative inputs, and printed primes from 1–100 with a tight loop. Key snippet (conceptual): Idea: Only test divisors up to √n; if none divide, the number is prime. #Day33 #PythonWithDSA #DataStructures #DSAJourney #ProblemSolving #PythonProgramming #SoftwareEngineer
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