🔍 Problem Summary: Given an array and multiple queries, for each query index, we need to find the minimum circular distance to another index having the same value. If no such element exists, return -1. 💡 Key Idea: Since the array is circular, distance can be calculated in two ways: forward and backward. For each value, store all its indices. For every query, check the nearest occurrence of the same value and compute the minimum circular distance. ⚡ Example Insight: For value 1 at index 0, nearest same value is at index 2 → distance = 2 If an element appears only once → result = -1 🧠 What I Learned: Efficient use of hash maps for indexing Handling circular traversal logic Optimizing search using preprocessing #DataStructures #ProblemSolving #CodingPractice #Python #Java #SoftwareEngineering #LearningJourney
Minimum Circular Distance in Array with Hash Map
More Relevant Posts
-
Day 105 of 365 days of code :" 1) Daily temperatures Approach: monotonic stack 1) create an array of the size same as of the given temperatures array 2) fill it with 0; declare an empty stack-> used for holding the index and the temperature 3)iterate through the temperatures array 4) while stack is not empty and the temperature in the top element of the stack is less than the current value in the temperatures array pop the index,temperature pair and assign it to resindex and restemp a) res[resindex]=index-resindex //index is the current position in temperatures array 6) insert the index and temperatures[index] as a pair inside the stack 7) return res g'night #365daysOfCode #NeetCode #leetcode #DSA #python #LeetCode #ProblemSolving #Algorithms #365dayschallenge
To view or add a comment, sign in
-
-
What’s the difference between: self.MIN_BALANCE and BankAccount.MIN_BALANCE ? In my BankAccount class, MIN_BALANCE is a class variable — shared across all instances. So using it via `self` or via the class name… should be the same. Right? Until someone does this: acc.MIN_BALANCE = 999 No error. No warning. But something fundamental changes. The class variable is still there. But now the instance has its own version. Same name. Different value. Different behavior. I broke down exactly why this happens (and how to avoid it) in the slides 👇 Which one would you trust in production code? #Python #OOP #SoftwareEngineering #LearningInPublic #PythonTips
To view or add a comment, sign in
-
LeetCode Problem 1448. Count Good Nodes in Binary Tree: "Given a binary tree root, a node X in the tree is named good if in the path from root to X there are no nodes with a value greater than X. Return the number of good nodes in the binary tree." Approach: make use of pre-order traversal i.e., visit root->left->right child, in each visit, keep track of the max element upto that point by maintaining a stack, top of the stk should represent the max element upto that point in the path, pop() elements from stack when both the children (left,right) are explored. Time Complexity: O(n) Space Complexity: O(n) b/c of maintaining a stack #Python #LeetCode #DSA #DataStructures #Algorithms #Stack #Recursion #DFS #BinaryTree #ProblemSolving
To view or add a comment, sign in
-
-
I bet your advisor has already told you to make your "code faster"! Want to solve the problem? Then use an emulator, my friend! We have released PyGLAM 0.2.2 (beta version), a Python framework for emulating probability distributions based on Generalized Lambda Distributions. Our implementation is based on the paper "Replication-based emulation of the response distribution of stochastic simulators using Generalized Lambda Distributions" by Professor Bruno Sudret. Thanks to the partners Prof. Ketson dos Santos, Prof. Marcos Luiz, and the students Victor Hugo Moreira and Renata Maria Pensin. Simple to install: pip install pyglam Learn all about it at: https://lnkd.in/gutJNpDj Feedback is more than welcome! 👇 #Python #DataScience #Math #Coding #Reliability #Reliability #Statistics #Mechanics #CivilEngineering
To view or add a comment, sign in
-
Day 14/100 – Data Structures & Algorithms Today, I worked on the problem “First Unique Character in a String.” Overview The task is to identify the first non-repeating character in a string and return its index. If no such character exists, the result is -1. Approach I used a two-pass strategy: • First pass to store character frequencies using a hashmap • Second pass to identify the first character with a frequency of one Complexity • Time Complexity: O(n) • Space Complexity: O(1) Key Takeaway This problem reinforces how effective hashmaps are for frequency-based problems and how a simple two-pass approach can lead to optimal solutions. Staying consistent and building problem-solving intuition step by step. #Day14 #100DaysOfCode #DSA #Python #LeetCode #ProblemSolving #SoftwareEngineering
To view or add a comment, sign in
-
-
You write for loops every day. Do you know what actually runs underneath them? Day 03 of 30 -- Generators and Iterators Deep Dive Advanced Python + Real Projects Series Python calls iter() to get the iterator, then next() repeatedly until StopIteration is raised. That is every for loop you have ever written. And yield pauses the function, hands the value out, and resumes from the exact same line next time. Today's topic covers: The lazy vs eager evaluation problem -- why loading 10GB into a list crashes servers The full iterator protocol -- what powers every for loop 3 types -- generator function, expression, async generator Annotated syntax -- basic, yield from, and the send() two-way pattern Real fintech pipeline -- 52GB log file, 4.2MB memory used 5 production mistakes including exhausting a generator twice Generator pipeline architecture -- identical to Unix pipes Key insight: Don't store what you can stream. #Python #PythonProgramming #DataEngineering #BackendDevelopment #LearnPython #100DaysOfCode #PythonDeveloper #SoftwareEngineering #TechContent #BuildInPublic #TechIndia #CleanCode #CodingTips #CodeNewbie #LinkedInCreator #PythonTutorial
To view or add a comment, sign in
-
Did you know we now have a Hugging Face repo with pre-built encoderfiles? These are self-contained executables for embedding models like all-MiniLM-L6-v2. No Python or ML dependencies required. Just download, run, and generate embeddings via HTTP or CLI. Explore the repo: https://lnkd.in/errJpkWm
To view or add a comment, sign in
-
-
Most FastAPI codebases look clean at first glance. Until you try to change something. I’ve noticed a pattern — a lot of complexity doesn’t come from the problem itself, but from where the logic lives. When routes start handling more than just request/response, things get harder to reason about. Lately, I’ve been keeping one constraint: Routes should stay thin. They handle the HTTP layer. All business logic moves to services. It’s a small shift, but it changes a lot: 1) Clearer separation of concerns 2) Easier testing 3) Fewer side effects when making changes Also started appreciating dependency injection more. Not as a framework feature, but as a way to keep things decoupled and predictable. Nothing groundbreaking here. But in a time where a lot of code is being generated faster than it’s being designed, maintainability comes down to how consistently we apply these basics — not whether we know them. Curious how others approach structuring FastAPI projects at scale. #FastAPI #BackendDevelopment #CleanCode #SoftwareEngineering #Python
To view or add a comment, sign in
-
Day 20 of My DSA Journey – Daily Temperatures Today’s problem: Daily Temperatures 🌡️ A great example of how switching from brute force to the right data structure changes everything. 🔍 Problem Given a list of temperatures, find how many days you need to wait for a warmer day. If no warmer day exists, return 0. 💡 What I Learned At first glance, a nested loop feels natural… but that leads to O(n²) time complexity ❌ The optimized solution uses a Monotonic Stack to reduce it to O(n) ✅ ⚡ Approach Use a stack to store indices of unresolved temperatures Traverse the array once When a warmer temperature appears → resolve previous days Store the difference in indices #Day20 #DSA #LeetCode #Python #CodingJourney #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 75 of #100DaysOfCode 🔥 LeetCode 179 – Largest Number 💡 Problem: Given a list of non-negative integers, arrange them such that they form the largest possible number. 🧠 Key Insight: Normal sorting won't work here ❌ We need a custom comparator based on string concatenation. 👉 Compare: - ""a + b"" vs ""b + a"" - Whichever gives a larger value should come first. ⚙️ Approach: 1. Convert numbers to strings 2. Sort using custom comparison logic 3. Join the result 4. Handle edge case (like "[0,0] → "0"") ⚡ Complexity: - Time: O(n log n) - Space: O(n) 🎯 Result: ✅ Accepted ⚡ Runtime: 0 ms (100%) 📌 Lesson Learned: Sometimes sorting logic depends on combination, not value. #LeetCode #Python #CodingJourney #DSA #100DaysOfCode #Sorting #ProblemSolving
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