🚀 LeetCode: Map Word Weights The goal is to calculate a transformation of words based on a given weight array and map the resulting total weight (modulo 26) to a character from a reversed alphabet. 🛠️ My Approach 1. Weight Calculation: I iterated through each word and calculated its total weight by mapping each character to its corresponding value in the weights array using charCodeAt(0) - 97. ⚖️ 2. Reversed Mapping: Instead of complex math to find the character, I used a pre-defined reversed_alpha string. By taking the totalWeight % 26, I could instantly grab the correct character. 🔡 3. Optimization: By using a nested loop structure and direct index access, I ensured the logic remains lean and lightning-fast. ⚡ 📊 Efficiency Analysis ⏱️ Time Complexity: O(N * M) where 'N' is the number of words and 'M' is the average length of each word. 💾 Space Complexity: O(1) (excluding the result string) as the alphabet mapping and variables use constant space. #LeetCode #JavaScript #CodingLife #Algorithms #WebDevelopment #ProblemSolving #SoftwareEngineering #TechCommunity
Devesh Shukla’s Post
More Relevant Posts
-
I recently built a simple file compressor from scratch using Huffman coding to understand the logic behind data compression. I implemented it starting from counting character frequencies, building the Huffman tree using a priority queue, generating optimal binary codes, and then encoding and decoding text using that structure. It was interesting to see how something as simple as a tree and a greedy approach can significantly reduce data size. If you want to check it out you can view the project on my GitHub: https://lnkd.in/gGpbwZdk #JavaScript #Algorithms #DSA #Coding #ComputerScience
To view or add a comment, sign in
-
-
Today’s LeetCode POTD really humbled me, but it also introduced me to one of the most brilliant code optimizations I have ever seen. 🤯 Problem: Find All Possible Stable Binary Arrays I 🔗 https://lnkd.in/gYXAAYku My initial thought process: I spotted a few patterns early on and immediately tried to force a Greedy approach. I spent a good amount of time on it but ultimately hit a wall. Instead of giving up, I looked for resources and stumbled upon a video by Mazhar Imam Khan. His explanation was an absolute masterclass—taking the logic from a beginner-friendly breakdown all the way to advanced DP state transitions. 🎥 Video link: https://lnkd.in/ggzzrkNh Even though I didn’t solve it entirely on my own today, the insights I gained from this optimized code are insane: ✅ Time Complexity: Optimized down to O(zero x one). Instead of a 3rd nested loop for the limit, it uses a sliding window concept to subtract out-of-bound states in O(1)time! ✅ Space Complexity: Compressed to O(limit x min(zero, one)). By conditionally swapping zero and one and using a deque to store only the strictly necessary previous states (limit + 1), it completely avoids the memory overhead of a 3D DP matrix. Sometimes, the best way to level up your problem-solving intuition is to study code that is leagues ahead of your current approach. Huge thanks to Mazhar for the crazy insights today! #LeetCode #DynamicProgramming #Algorithms #DataStructures #PlacementPrep #LearningJourney #C++
To view or add a comment, sign in
-
-
I built a code converter a while back — Python to JS and back, with LLM under the hood. I've since rebuilt the pipeline significantly. Added test coverage checks, refinement loops, and a step where the model decides whether the converted code actually needs another pass. The reason: LLMs hallucinate. A single-pass conversion isn't reliable enough. So the pipeline now runs the actual test suite after each conversion, feeds the results back to the model, and loops until the tests pass and coverage meets the threshold. But loops cost time. And inference isn't instant to begin with. So now the pipeline takes a while. And a long-running pipeline with no feedback is flying blind — you don't know if it's thinking, stuck, or silently wrong. That's why I added a visualizer. Which got me thinking about the "GUI is dead, future is CLI" takes I've been seeing lately. I don't think the container matters — it can be a web UI, a TUI, a progress stream in the terminal. Even Claude Code streams every step rather than returning one big answer at the end. The interface will keep changing. The need to see what's happening won't. Here's what it looks like in action 👇 Demo: https://lnkd.in/gjxkTrEX #AIEngineering #LLM #AgenticAI
To view or add a comment, sign in
-
🛑 🤓 I have the complete, end-to-end, working prototype of a Provably Safe Computing Paradigm. AAAAAAAAAAAHHHHHH, SHIZZNIT! It's on like Donkey KONG!!! 🐵 These can be #transpiled into any language, so sick! I'll set it up for the main ones, C/C++, Python, JS and of course the new MDO Engine. MDOs Everywhere for Everything... "Now we just need to make a ton of MDOs..." Yes. I have invented Cognitive Legos. The MDO is the Lego brick. GenLang is the mold that makes the bricks. The MDO Engine is the Hand that snaps them together. Want a UI button? Write a Button.gen file that emits a "Clicked" event. Want a Database connection? Write a Database.gen that has a Check for SQL Injection. Want an entire web server? You assemble a RequestParser.mdo, a Security.mdo, a Database.mdo, and a Response.mdo 🙃
To view or add a comment, sign in
-
🚀 Efficiency Wins: Why Two Pointers Beat Nested Loops 👈👉 Today’s deep dive into the Two Pointer Technique was a perfect reminder that "thinking before coding" is a developer's best skill. Here is how I optimized my approach to solving Two Sum II: 🛠️ Key Technical Takeaways Kill Redundant Flags: I initially used a while(!flag) setup. I quickly realized this was extra baggage—using while (left < right) is the cleaner, more idiomatic "guardrail" that naturally prevents infinite loops. The 1-Indexed Challenge: The problem requires a 1-indexed return. I learned to handle the logic in 0-index but adjust the final output by adding 1 to avoid off-by-one errors. The "Not Found" Safety Net: If no pair exists, returning [-1, -1] is the standard way to signal "not found." Without the left < right condition, the code could crash or loop forever if the target isn't present. ⚡ The Efficiency Edge O(n) vs O(n²): Instead of checking every possible pair (slow), we use two "bookmarks" and find the answer in a single pass. Space Efficiency: By using only two variables, the space complexity stays O(1), no matter how massive the dataset. 🏆 The Milestone Successfully solved Two Sum II using a clean, linear approach! Moving from brute-force to optimized logic feels like a massive level-up. 💻 Code Snippet: function two_pointers(input, target){ let left = 0 let right = input.length - 1 while(left < right){ const currentSum = input[left] + input[right] if(currentSum === target){ return [left + 1, right + 1] } else if(currentSum > target){ right -= 1 } else{ left += 1 } } return [-1, -1] } let input = [2, 7, 11, 15] let target = 9 console.log(two_pointers(input, target)) #DataStructures #Algorithms #LeetCode #CodingInterviews #CleanCode #JavaScript #SoftwareEngineering #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 312 of solving 365 medium questions on LeetCode! 🔥 Today’s challenge: “622. Design Circular Queue” ✅ Problem: Design a circular queue (also called a "Ring Buffer"). It uses a fixed-size array and follows the FIFO (First In, First Out) principle, but connects the end of the array back to the front to reuse empty spaces left behind by dequeued elements! ✅ Approach (Array + Modulo Arithmetic) Instead of relying on built-in queue libraries, I implemented it from scratch using a fixed-size list and two running counters: Initialization: Create an array of size k and set absolute start and end counters to 0. Enqueue / Dequeue: Instead of manually resetting the pointers to 0 when they hit the end of the array, I just let them grow indefinitely! I used modulo arithmetic (index % k) to figure out exactly where that absolute counter maps to within the fixed circular array. Status Checks: isEmpty() is beautifully simple: start == end. isFull() is just: end == start + k. ✅ Key Insight The biggest headache when building a circular queue is distinguishing between a "Full" state and an "Empty" state, since traditional read/write pointers overlap in both scenarios. By letting the pointers act as absolute operation counters (and only applying modulo during array access), the capacity math becomes incredibly elegant. It completely eliminates the need for messy if/else wrap-around logic! ✅ Complexity Time: O(1) — Every single operation (enqueue, dequeue, front, rear, checks) executes in constant time. Space: O(K) — Where K is the predefined maximum capacity of the queue. 🔍 Python solution attached! 🔥 Flexing my coding skills until recruiters notice! #LeetCode365 #DataStructures #Queue #Python #ProblemSolving #DSA #Coding #SoftwareEngineering
To view or add a comment, sign in
-
-
From 𝐌𝐚𝐜𝐡𝐢𝐧𝐞 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠 𝐌𝐨𝐝𝐞𝐥 𝐭𝐨 𝐖𝐞𝐛-𝐁𝐚𝐬𝐞𝐝 𝐑𝐞𝐜𝐨𝐦𝐦𝐞𝐧𝐝𝐚𝐭𝐢𝐨𝐧 𝐓𝐨𝐨𝐥 𝐑𝐞𝐜𝐞𝐧𝐭𝐥𝐲 , I built a Laptop Recommendation System using Python and cosine similarity. Now I implemented the same 𝐛𝐫𝐨𝐰𝐬𝐞𝐫-𝐛𝐚𝐬𝐞𝐝 𝐢𝐧𝐭𝐞𝐫𝐚𝐜𝐭𝐢𝐯𝐞 𝐭𝐨𝐨𝐥 𝐮𝐬𝐢𝐧𝐠 𝐇𝐓𝐌𝐋, 𝐂𝐒𝐒, 𝐚𝐧𝐝 𝐉𝐚𝐯𝐚𝐒𝐜𝐫𝐢𝐩𝐭 𝐓𝐡𝐢𝐬 𝐯𝐞𝐫𝐬𝐢𝐨𝐧 𝐟𝐨𝐜𝐮𝐬𝐞𝐬 𝐨𝐧 𝐩𝐫𝐚𝐜𝐭𝐢𝐜𝐚𝐥 𝐩𝐫𝐨𝐝𝐮𝐜𝐭 𝐭𝐡𝐢𝐧𝐤𝐢𝐧𝐠: • Takes user inputs (budget, RAM, storage, processor level) • Calculates a dynamic match score • Applies weighted scoring logic • Filters laptops within a flexible budget range • Sorts results manually based on score • Displays top recommendations instantly 𝐖𝐡𝐚𝐭 𝐜𝐡𝐚𝐧𝐠𝐞𝐝 𝐟𝐫𝐨𝐦 𝐭𝐡𝐞 𝐌𝐋 𝐯𝐞𝐫𝐬𝐢𝐨𝐧? 𝐓𝐡𝐞 𝐌𝐋 𝐯𝐞𝐫𝐬𝐢𝐨𝐧 𝐮𝐬𝐞𝐝: – Feature scaling – Cosine similarity – Structured numeric modeling 𝐓𝐡𝐞 𝐖𝐞𝐛 𝐯𝐞𝐫𝐬𝐢𝐨𝐧 𝐮𝐬𝐞𝐬: – Scoring logic instead of similarity – Manual ranking (custom sorting logic) – Real-time frontend interaction 𝐁𝐢𝐠 𝐥𝐞𝐚𝐫𝐧𝐢𝐧𝐠 𝐟𝐫𝐨𝐦 𝐭𝐡𝐢𝐬 𝐭𝐫𝐚𝐧𝐬𝐢𝐭𝐢𝐨𝐧: Building a model is one skill. Turning logic into a usable interface is another. 𝐓𝐡𝐢𝐬 𝐡𝐞𝐥𝐩𝐞𝐝 𝐦𝐞 𝐮𝐧𝐝𝐞𝐫𝐬𝐭𝐚𝐧𝐝 𝐭𝐡𝐞 𝐝𝐢𝐟𝐟𝐞𝐫𝐞𝐧𝐜𝐞 𝐛𝐞𝐭𝐰𝐞𝐞𝐧: Algorithm thinking vs Product thinking. 𝐍𝐨𝐰 𝐈 𝐜𝐚𝐧 𝐚𝐩𝐩𝐫𝐨𝐚𝐜𝐡 𝐩𝐫𝐨𝐛𝐥𝐞𝐦𝐬 𝐟𝐫𝐨𝐦 𝐛𝐨𝐭𝐡 𝐚𝐧𝐠𝐥𝐞𝐬: Data Science + Frontend Implementation. 𝐆𝐢𝐭𝐇𝐮𝐛 𝐑𝐞𝐩𝐨𝐬𝐢𝐭𝐨𝐫𝐲: https://lnkd.in/gKmkaaFm #WebDevelopment #MachineLearning #JavaScript #Frontend #ProductThinking #LearningInPublic #MCA
To view or add a comment, sign in
-
Sorting large datasets efficiently is a must-have skill for every developer. The Heap Sort Algorithm in JavaScript is one of the most powerful comparison-based sorting techniques. It works by transforming data into a binary heap structure, allowing developers to sort elements efficiently with O(n log n) time complexity. Our JavaScript Heap Sort guide simplifies the concept so developers can clearly understand: 🔹 How Heap Sort works 🔹 Building a max heap 🔹 Extracting elements in sorted order 🔹 Time and space complexity 🔹 Real-world use cases in data processing Mastering algorithms like Heap Sort helps developers write more optimized, scalable, and performance-driven applications. At Silver Sparrow Studios, we believe strong fundamentals in algorithms and data structures lead to stronger software solutions. 🚀 Save this post if you're learning JavaScript algorithms or preparing for coding interviews. #JavaScript #HeapSort #Algorithms #DataStructures #CodingAlgorithms #Programming #LearnToCode #WebDevelopment #FrontendDeveloper #CodingTips #SoftwareEngineering #FullStackDeveloper #CodeNewbie #CodingInterviewPrep #SilverSparrowStudios
To view or add a comment, sign in
-
🚀 LeetCode 110 – Balanced Binary Tree Today I solved LeetCode #110 – Balanced Binary Tree. 📌 Problem Statement Given a binary tree, determine whether it is height-balanced. A binary tree is considered height-balanced if: For every node, the height difference between its left and right subtree is not more than 1. 🧠 Approach & Technique Used To solve this problem efficiently, I used the Depth-First Search (DFS) approach with a bottom-up recursion strategy. 🔎 Key Idea: Instead of checking height separately for every node (which would increase time complexity), We calculate height while simultaneously verifying balance. If at any node the height difference is greater than 1, we return -1 immediately. This avoids unnecessary recalculations. ⚡ Time & Space Complexity Time Complexity: O(n) → Each node is visited once. Space Complexity: O(h) → Recursive stack space (h = height of tree). 💻 Optimized Python Code class Solution: def isBalanced(self, root: Optional[TreeNode]) -> bool: def checkHeight(node): if not node: return 0 left = checkHeight(node.left) if left == -1: return -1 right = checkHeight(node.right) if right == -1: return -1 if abs(left - right) > 1: return -1 return 1 + max(left, right) return checkHeight(root) != -1 🎯 Why This Approach Is Better? ✅ Avoids repeated height calculations ✅ Stops early when imbalance is detected ✅ Clean and optimized recursion ✅ Industry-level approach for tree problems Consistently solving tree problems is strengthening my understanding of recursion and DFS patterns. Looking forward to solving more problems and sharing my journey 🚀 🔥 Hashtags for Better Reach #LeetCode #CodingInterview #DSA #BinaryTree #Recursion #PythonDeveloper #SoftwareEngineer #ProblemSolving #TechCareer #CodingJourney #100DaysOfCode #DataStructures #Algorithms #InterviewPreparation
To view or add a comment, sign in
-
-
Lately I've been revisiting some old side-projects of mine, and this time I went back to the one project that sparked my interest in programming: writing parsers for Logics (e.g. Propositional Logic, Predicate Logic). Back in the day I wrote this project in C (which was the only language I knew), so I thought it would be interest to give it another shot in TypeScript. Following the "agile philosophy" of building and releasing incrementally, one step at a time, I started with building a lexer for Propositional Logic and a visual demo to showcase it. If like me you're interested in compilers, parsers and logic, you might want to check the demo out: https://lnkd.in/dvnCSz_D
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