LeetCode Problem 1784: "Check if binary string has atmost one segment of ones": Given a binary string s without leading zeros, return true if s contains at most one contiguous segment of ones. Otherwise, return false. Approach: The idea is simple and straightforward, the given string has no leading zeroes, while looping ensure that after encountering the first segment of ones, no 1 is encountered. If this condition holds, then return True else False. The implementation in Python correctly resolves this problem optimally and efficiently. Time Complexity: O(n) where n is length of given string Space Complexity: constant #LeetCode #Python #Strings #DataStructures #Algorithms #OptimalSolution #ProblemSolving #CompetitiveProgramming #DailyCoding #Programming
Check Binary String for One Segment of Ones
More Relevant Posts
-
Tried solving a sliding window problem today in Python Found the maximum average subarray of size k without recalculating everything again and again — just shifting the window and updating the sum. Simple logic, but really powerful for optimization! nums = [1,12,-5,-6,50,3] n = len(nums) k = 4 max_avrg = float("-inf") window = sum(nums[:k]) max_avrg = window/k for j in range (k,n): window = window - nums[j-k] + nums[j] if max_avrg < window/k: max_avrg = window/k print(max_avrg) Learning DSA step by step #Python #DSA #SlidingWindow
To view or add a comment, sign in
-
Finding Maximum Sum of k Consecutive Elements in Python Check out this simple sliding window trick to get the maximum sum of k consecutive numbers in an array. I used my own code to solve it: Python Copy code nums = [7,1,5,1,3,2] k = 3 n = len(nums) window = sum(nums[:k]) sums = window for i in range(k,n): window = window - nums[i-k] + nums[i] if sums < window: sums = window print(sums) How it works: Start with the sum of the first k numbers Slide the window by removing the first number and adding the next Keep track of the maximum sum Fast, simple, and efficient! #Python #DSA #Coding #Algorithms #LearnByDoing #DeveloperLife
To view or add a comment, sign in
-
⚠️ Strings look simple… until they aren’t. Back with my Python MahaRevision — and today’s focus was on: 📘 Chapter 3: Strings From basic text handling to powerful operations, this chapter showed me that strings are way more than just “text” in Python 👇 🔹 String slicing & indexing 🔹 String functions & methods 🔹 Escape sequences 🔹 Immutability of strings 🧠 Practice Set Completed! Worked on problems like: • Manipulating and slicing strings • Finding and replacing words • Formatting outputs • Real-world text-based logic 💡 Biggest takeaway: Small concepts like slicing and methods can solve surprisingly complex problems when used right. Learning in public. Staying consistent. Improving daily 🚀 #Python #CodingJourney #LearningInPublic #Programming #Tech #100DaysOfCode
To view or add a comment, sign in
-
Simple way to understand vector search in RAG I made a small Python example using SentenceTransformers + FAISS to understand how retrieval works in RAG. What happens here: A few documents are converted into embeddings Those embeddings are stored in FAISS A user question is also converted into an embedding FAISS finds the most similar document chunks This is the basic idea behind RAG: store meaning as vectors, then retrieve the most relevant context before generation Very small code, but it explains a very important concept. Text → Embedding → Similarity Search → Relevant Chunks That is why vector databases are so important in RAG systems. #RAG #FAISS #Embeddings #AIEngineering #Python #LLM Code source: https://lnkd.in/g-cm4BB2
To view or add a comment, sign in
-
-
It’s funny how even code needs to “decide” what to do next. Today’s Python MahaRevision was all about making decisions in code 👇 📘 Chapter 6: Conditional Expressions Finally getting into the part where programs start to feel a bit “smart”: • if, elif, else • using logic to control flow • combining conditions with and/or/not 🧠 Did the practice set too: Tried problems like checking numbers, comparing values, simple decision-making stuff… nothing too crazy, but really made me think. Honestly, this chapter felt different. It’s not just about syntax anymore—it’s about how you think. Taking it one chapter at a time 🚀 #Python #LearningInPublic #CodingJourney #Programming
To view or add a comment, sign in
-
LeetCode Problem 1009: "Complement of base 10": The complement of an integer is the integer you get when you flip all the 0's to 1's and all the 1's to 0's in its binary representation. For example, The integer 5 is "101" in binary and its complement is "010" which is the integer 2. Given an integer n, return its complement. Approach: First get the binary string representation of given number using bin() and str() functions, second iterate through the string and append '0' to a new string variable if you get '1' and '1' if you get '0', last return the integer value of the new string variable using the int() function with base 2. #Python #Strings #LeetCode #CompetitiveProgramming #DataStructures #DSA #Algorithms #Programming #ProblemSolving
To view or add a comment, sign in
-
-
📌 NumPy Built-in Methods NumPy provides several built-in functions to quickly create arrays. 🔹 arange() – Creates an array with a range of numbers np.arange(0,10) 🔹 zeros() – Creates an array filled with zeros np.zeros(3) np.zeros((5,5)) → creates a 5×5 matrix of zeros 🔹 ones() – Creates an array filled with ones np.ones(3) np.ones((3,3)) → creates a 3×3 matrix of ones These built-in methods make array creation fast and efficient in NumPy. #Python #NumPy #Programming #DataAnalytics #LearningPython
To view or add a comment, sign in
-
-
Learning namespaces and decorators in Python today, and it changed the way I think about how Python organizes code. Namespaces helped me understand how Python keeps variables and functions separated to avoid conflicts. The LEGB rule (Local → Enclosing → Global → Built-in) made it much clearer how Python decides where to look for a variable. Then I explored decorators. At first they looked confusing, but they’re actually a powerful way to modify the behavior of a function without changing the function itself. For example, adding logging, timing execution, or access control becomes much cleaner with decorators. Small concepts like these make Python feel much more elegant once they click. Currently focusing on improving my fundamentals in Python and machine learning, one concept at a time. #Python #Programming #LearningInPublic #100DaysOfCode #MachineLearning
To view or add a comment, sign in
-
Solving Subarray Sums with Ease in Python with No Prior Experience Discover how to crack the Subarray Sum Equals Zero III challenge with ease. Understand the concept of prefix sums and its application in solving subarray sums. Read the full article 👉 https://lnkd.in/d3Mg-Sjy #PythonForFresher #DataProcessing #AlgorithmsAndProgramming #ITinterviewPreparation #TechLab Code. Learn. Build. — TechLab by Neeraj
To view or add a comment, sign in
-
#Duck_Typing A traditional idea says: “The king’s son will become the next king.” A more modern interpretation would be: “The one who is capable will become the king.” This transition from identity-based selection to capability-based evaluation loosely resembles Python’s duck typing. In duck typing, an object’s suitability is determined not by its type, but by its behavior. If an object implements the required methods, it can be used — regardless of its class hierarchy. However, it is important to avoid overextending this analogy. Duck typing does not imply merit-based selection or optimization. It simply enables flexibility by accepting any object that satisfies the expected interface. This distinction is critical. #Python #SoftwareEngineering #Programming #Coding #TechConcepts #CleanCode #SoftwareDesign #LearnToCode #DeveloperMindset #TechLearning
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