LeetCode 2161: Partition array according to given pivot ➗ Topic: Simulation Imagine you want to rearrange an array so all numbers smaller than a given pivot come first, followed by all equals to the pivot, then all larger numbers—while preserving the original order within each group. How does the code work? - Initialization: ---- Create three empty lists: before for numbers < pivot, middle for numbers == pivot, and after for numbers > pivot. - Partitioning Loop: ---- Iterate through each number in the input nums array exactly once. ---- If num < pivot, append it to before. ---- If num == pivot, append it to middle. ---- Otherwise (num > pivot), append it to after. - Result Construction: ---- Concatenate the three lists in order: before + middle + after using - Python's list addition. ---- Return the new partitioned list (same length as input). Complexity: - Time Complexity: O(n) — single pass through n elements, each append is O(1) amortized, concatenation is O(n). - Space Complexity: O(n) — three lists store all n elements plus temporary result list. Check out the problem here: https://lnkd.in/gu5yPWb9 Keep going, keep revising, and keep building confidence! 💪🔥 #DSA #Coding #ProblemSolving #Learning
LeetCode 2161: Partition Array by Pivot
More Relevant Posts
-
Exploring Python inside Excel highlighted something important for me: The real value of a tool isn’t its technical power—it’s how effectively others can use it. When advanced analytics live inside a familiar platform like Excel: Insights move faster to decision‑makers Processes become easier to standardize and repeat Less effort goes into “how,” more into “why and what next” I’m increasingly interested in designing workflows that scale insight—not just execution. That mindset shift is what excites me most about Python in Excel. #GrowthMindset #Analytics #PythonInExcel #DataThinking #CareerDevelopment
To view or add a comment, sign in
-
-
𝗬𝗔𝗥𝗥𝗥𝗠𝗟 - 𝗠𝗮𝗸𝗶𝗻𝗴 𝗥𝗠𝗟 𝗛𝘂𝗺𝗮𝗻-𝗙𝗿𝗶𝗲𝗻𝗱𝗹𝘆 RML is 𝐩𝐨𝐰𝐞𝐫𝐟𝐮𝐥. But let’s be honest: writing it directly is not exactly joyful. YARRRML exists for a simple reason: make 𝐬𝐞𝐦𝐚𝐧𝐭𝐢𝐜 𝐦𝐚𝐩𝐩𝐢𝐧𝐠𝐬 𝐫𝐞𝐚𝐝𝐚𝐛𝐥𝐞 𝐚𝐧𝐝 𝐰𝐫𝐢𝐭𝐚𝐛𝐥𝐞 𝐛𝐲 𝐡𝐮𝐦𝐚𝐧𝐬. 𝐘𝐀𝐌𝐋 syntax, 𝐜𝐥𝐞𝐚𝐫 𝐬𝐭𝐫𝐮𝐜𝐭𝐮𝐫𝐞, and 𝐟𝐚𝐫 𝐥𝐞𝐬𝐬 𝐯𝐞𝐫𝐛𝐨𝐬𝐢𝐭𝐲 - while still compiling down to standard RML. That means: - 𝐀𝐮𝐭𝐡𝐨𝐫-𝐟𝐫𝐢𝐞𝐧𝐝𝐥𝐲 𝐦𝐚𝐩𝐩𝐢𝐧𝐠𝐬 - 𝐅𝐮𝐥𝐥 𝐢𝐧𝐭𝐞𝐫𝐨𝐩𝐞𝐫𝐚𝐛𝐢𝐥𝐢𝐭𝐲 with existing engines - 𝐍𝐨 𝐥𝐨𝐜𝐤-𝐢𝐧 for a specific ingestion engine For me, it feels a bit like the difference between Python and Assembly - the image below shows a concrete comparison. If you want to see how an 𝐄𝐓𝐋 𝐬𝐞𝐭𝐮𝐩 with 𝐘𝐀𝐑𝐑𝐑𝐌𝐋 and a mapping engine looks in practice, we’ve written a 𝐡𝐚𝐧𝐝𝐬-𝐨𝐧 𝐛𝐥𝐨𝐠 𝐩𝐨𝐬𝐭 about it: https://lnkd.in/eR-MnDck
To view or add a comment, sign in
-
-
Replace Slow Loops with map() + Dictionary Many use loops to transform column values — but Pandas gives a faster, cleaner way. ❌ Slow & non-Pythonic for i in range(len(df)): if df.loc[i, "status"] == 1: df.loc[i, "status"] = "Active" else: df.loc[i, "status"] = "Inactive" ✅ Fast & Pandas-friendly df["status"] = df["status"].map({1: "Active", 0: "Inactive"}) 💡 Why this works better? No explicit loops Cleaner & more readable Much faster on large datasets 📌 Rule of thumb: If you’re looping over rows in Pandas, there’s almost always a vectorized alternative. #Python #Pandas #DataEngineering #DataScience #DataAnalytics #CodingTips
To view or add a comment, sign in
-
02 #AI_ML_for_Process_Engineering TRADING SPREADSHEETS FOR PYTHON WHY THE SWITCH FROM EXCEL? 🚀 SCALABILITY: Python handles millions of sensor readings that would crash a standard spreadsheet. 🛠️ REPRODUCIBILITY: Unlike Excel, where one accidental keystroke can break a formula across 10,000 rows, Python logic is explicit, modular, and verifiable. 📊 AUTOMATED INSIGHT: With one line of code (.describe()), I can instantly get the mean, std dev, and ranges for every tag in a massive dataset. Considering The 80/20 rule is real ==> 80% of AI is "Data Cleaning. Python is the power tool that makes that 80% manageable, allowing us to stop "firefighting" data and start interrogating it for insights. [Question for the Engineers] What is the largest dataset you’ve ever tried to open in a spreadsheet? Did it survive, or did you see the "Not Responding" screen of death? 😅 #DJ2Tech #ProcessEngineering #Industry40 #DigitalTransformation
To view or add a comment, sign in
-
-
I’ve been spending some time getting my hands dirty with option pricing by building a Binomial Option Pricing Model from scratch in Python and Excel. The idea was simple: don’t just read about models, build them, break them, and test them. The result is a CRR-based binomial model that prices European calls and puts, shows the full stock and option trees, and makes every assumption and calculation transparent (no black boxes here). Python handles the logic and visualisation, while Excel keeps things intuitive and easy to sanity-check. Together, they’re a great way to connect theory with real-world intuition and to remind myself that models only make sense if you actually understand what’s going on under the hood. Still learning, still experimenting, still questioning assumptions. Quant work is fun like that… until you realise one missing minus sign ruined everything 😄 Always keen to learn from others on similar paths. Check it out here: https://lnkd.in/grEXS8-9 #optiontrading #Quant #Python #Excel #LearningByDoing #streamlit
To view or add a comment, sign in
-
❄️ takeUforward — Day 18 ✅ Two Sum | Hashing | LeetCode (Easy) Today I solved the classic Two Sum problem on LeetCode using an optimal hashing approach, focusing on reducing time complexity from brute force to linear time. Problem Statement Given an array of integers nums and an integer target, return the indices of the two numbers such that they add up to target. ✔ Exactly one solution exists ✔ Same element cannot be used twice Approach (Hash Map / Dictionary) Idea • Traverse the array once. • For each element, calculate remaining = target - nums[i]. • If remaining already exists in the hash map → solution found. • Otherwise, store the current number with its index in the map. This avoids nested loops and gives an optimal solution. Example Input: nums = [2, 7, 11, 15], target = 9 Output: [0, 1] ⏱ Complexity Analysis • Time Complexity: O(n) • Space Complexity: O(n) 📌 Key Learning (Day 18) • Hashing helps convert quadratic problems into linear ones. • Storing elements before moving ahead avoids reuse issues. • Understanding lookup trade-offs is crucial in array problems. #DSA #Arrays #Hashing #TwoSum #LeetCode #Python #ProblemSolving #LogicBuilding #CodingJourney #RestartDSA #100DaysOfCode #DailyCoding
To view or add a comment, sign in
-
-
did you know AMD has a separate code generator just for GEMMs? it's called Tensile, and here's what we've learned digging into it: Tensile is a Python script that writes AMDGCN assembly, benchmarks millions of kernel variants, and builds a lookup table mapping every M×N×K to its optimal kernel. at runtime, rocBLAS traverses a MessagePack catalog to find the best kernel for your exact problem size. the code generation is where it gets most interesting. KernelWriterAssembly dot py translates 20 parameters into raw assembly: tile sizes, unroll depths, prefetch strategies, and most importantly - how to interleave memory ops with MFMA instructions so the matrix units never sit idle. MFMA operates per-wavefront, not per-thread. for a 32×32×8 FP16 op, each of the 64 threads holds 4 elements of A, 4 of B, and 16 of C. the instruction takes 16-32 cycles. you hide that latency by interleaving VALU ops in MFMA's execution shadow. the search space is brutal. v1 benchmarking was brute force across tile sizes, unroll depths, and problem sizes - 23 million kernel launches total. v2 uses fork/join phases to make this tractable. the output is code objects indexed by a hierarchical schema. lazy loading means only kernels you actually call get mapped into memory. 23 million benchmarks distilled into one ds_read → mfma → buffer_load pipeline!
To view or add a comment, sign in
-
-
As someone who has just learned it, I’ve found Python to be more than just a programming language—it’s a toolkit that empowers clarity, speed, and innovation. Among its many libraries, NumPy stands out. 🔹 With NumPy, handling large datasets feels effortless. 🔹 Vectorized operations save time and reduce complexity. 🔹 Its integration with other libraries like Pandas makes it the backbone of modern data workflows. What I appreciate most is how NumPy transforms raw data into actionable insights with just often one line codes. Whether it’s numerical computation or data manipulation, NumPy consistently proves to be both efficient and reliable. For anyone starting their journey in data science or analytics, I’d highly recommend diving into NumPy. It’s a skill that pays dividends across industries. #Python #NumPy #DataScience #Analytics #MachineLearning
To view or add a comment, sign in
-
🚀 Day 8/30 | LeetCode Problem: Sort Colors (75) Problem: Given an array nums containing only 0s, 1s, and 2s, sort the array in-place so that colors are ordered as: 🔴 0 → ⚪ 1 → 🔵 2 Without using the built-in sort function. 🧠 Approach: Dutch National Flag Algorithm We use three pointers: a → position for 0 (left) b → position for 2 (right) c → current index Logic: If nums[c] == 0 → swap with a, move both forward If nums[c] == 2 → swap with b, move b backward If nums[c] == 1 → just move c This sorts the array in one pass. ⏱️ Complexity Time: O(n) Space: O(1) (in-place) 🧾 Python Code class Solution: def sortColors(self, nums): a = 0 # left pointer (0s) b = len(nums) - 1 # right pointer (2s) c = 0 # current index while c <= b: if nums[c] == 0: nums[a], nums[c] = nums[c], nums[a] a += 1 c += 1 elif nums[c] == 2: nums[b], nums[c] = nums[c], nums[b] b -= 1 else: c += 1 ✅ Result Accepted ✅ Runtime: 0 ms (Beats 100%) In-place & optimal solution 🎯 Takeaway Understanding pointer-based algorithms helps solve array problems efficiently without extra space . 🔖 Hashtags #LeetCode #30DaysOfLeetCode #Day8 #Python #Arrays #TwoPointers #DSA #ProblemSolving #CodingJourney #SoftwareEngineering
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
Nice solution. Stability matters here and this approach keeps it simple and correct.