Day 43 of 365 days of code Qn 1) simplify path approach used: stack initialize a stack 1) iterate the string path using var i from 0 to len(path) 2) ignore forward slashes 3) initialize ch="" 4) while i<len(path) and path[i] not equal to forward slash: ch+=path[i];i+=1 5) if ch==.. stack.pop elif ch== . continue else: stack.push(ch) 6)i+=1 7) pop all the element of the stack and put them in another stack 8) initialize a string starting with/ as u iterate pop the stack and add the forward slash to the popped string. #365daysOfCode #NeetCode #leetcode #DSA #python #LeetCode #ProblemSolving #Algorithms
Stack Implementation for Simplifying Path
More Relevant Posts
-
Day 44 of 365 Days of code 😵💫 Qn 1) Decode String approach: stack took me a while to solve, but the problem was worth the time taken. 1)keep on pushing the characters into the stack until you encounter a " ] " closing bracket. 2) if a closing bracket is encountered initialize an empty string and keep popping the elements and add it to a string until a open square bracket is found on top of the stack 3) If the top element in the stack is an integer, multiply the integer with the string, and append it to the string 4) elif the top element is a string add the string to the top element 5) else append the string 6) reverse the stack, pop the reversed stack and add it to string res 7) return res gn #365daysOfCode #NeetCode #leetcode #DSA #python #LeetCode #ProblemSolving #Algorithms #365dayschallenge
To view or add a comment, sign in
-
-
Weekly Challenge 6: Two Pointers Reversing an array the "Senior" way (Two Pointers) Reversing a list in Python is easy. Just type `list.reverse()` or `list[::-1]` and you are done, right? But what if you are in a technical interview and you are asked to do it from scratch, using **zero extra memory**? For Week 6 of my coding challenge, I implemented the **Two Pointers Technique** to reverse an array *in-place*. **The Logic:** You place one pointer at the start (index 0) and one at the end. You swap their values, and then step both pointers toward the middle until they meet. **Why it matters:** By doing this *in-place*, our Space Complexity is $O(1)$ (Constant Space). We don't need to duplicate the array in our RAM. If you are processing massive datasets, this memory optimization is critical! Check out the console trace below to see how the pointers move step-by-step. Code on my GitHub: https://lnkd.in/eyZ4KqiN #Python #Algorithms #TwoPointers #CodingChallenge #Optimization #DataStructures
To view or add a comment, sign in
-
Day 68 of 365 Days of code Qn 1) Min stack Approach: use an extra stack(to store minimum element up to the particular index) for push: push the value into the stack, check if the val is lesser than the element at the top of minstack, if yes, push the value, else push the copy of the top element in minstack for pop: pop from both of the stacks for returnmin: return minstack[top] :) good night #365daysOfCode #NeetCode #leetcode #DSA #python #LeetCode #ProblemSolving #Algorithms #365dayschallenge
To view or add a comment, sign in
-
-
🔹 LinkedIn Post – Day 42 | LeetCode 162: Find Peak Element 🚀 Problem: Find Peak Element A peak element is an element in the array that is strictly greater than its neighbors. The task is to return the index of any peak element present in the array. 💡 Key Idea Instead of checking every element sequentially, we can use a Binary Search approach to reduce the search space efficiently. ⚙️ Logic Compare the middle element with its next element. If the middle element is greater, it means a peak exists on the left side (including mid). If the next element is greater, the peak must lie on the right side. Continue reducing the search range until both pointers meet. The final position will be the index of a peak element. ⏱️ Complexity Time Complexity: O(log n) Space Complexity: O(1) 🎯 Insight The array can have multiple peaks, but the problem only requires returning any one peak index. Binary search works because the array always moves towards a peak when following the increasing slope. #Day41 #LeetCode #DSA #BinarySearch #ProblemSolving #CodingJourney #Python
To view or add a comment, sign in
-
-
🚀 Day 51 of #100DaysOfCode ✅ Solved: 1689. Partitioning Into Minimum Number of Deci-Binary Numbers Today’s problem looked tricky at first, but the solution turned out to be beautifully simple once the pattern was understood. 🔎 Problem Insight: A deci-binary number contains only digits 0 or 1. To form a number like "82734", think about each digit: If a digit is 8, we need at least 8 deci-binary numbers contributing 1 at that position. If a digit is 3, we need at least 3 deci-binary numbers at that position. 👉 So the minimum number of deci-binary numbers required is simply: 📌 The maximum digit in the given number 💡 Example: Input: "82734" Output: 8 🧠 Python Code: Python Copy code class Solution: def minPartitions(self, n: str) -> int: return int(max(n)) ⏱ Time Complexity: O(N) 💾 Space Complexity: O(1) This problem is a great example of how understanding the pattern simplifies the solution dramatically. #LeetCode #ProblemSolving #Python #DataStructures #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 10/30 | LeetCode Problem: Reverse Linked List (206) Problem: Given the head of a singly linked list, reverse the list and return the new head. 💡 Approach (Iterative – Three Pointers) To reverse a linked list, we carefully change the direction of pointers. We use three variables: prev → previous node current → current node next_node → temporarily stores next node Steps: Store next node Reverse the current node’s pointer Move prev and current one step forward Repeat until the list ends Finally, prev becomes the new head. ⏱ Complexity Time Complexity: O(n) Space Complexity: O(1) 🧠 Python Code class Solution: def reverseList(self, head): prev = None current = head while current: nxt = current.next current.next = prev prev = current current = nxt return prev 📌 Example Input: [1,2,3,4,5] Output: [5,4,3,2,1] 🎯 Key Takeaway Linked List problems are all about pointer manipulation. Understanding pointer flow is more important than memorizing code. ✅ Accepted 🔖 Hashtags #LeetCode #30DaysOfLeetCode #Day10 #Python #LinkedList #DataStructures #Algorithms #ProblemSolving #CodingJourney #SoftwareEngineering
To view or add a comment, sign in
-
-
🔥 Day 98 — #100DaysOfLeetCode ✅ Problem: Special Binary String (Hard) Today’s problem focused on special binary strings — strings where: • Number of 1s = number of 0s • Every prefix has ≥ as many 1s as 0s 🎯 Goal: Find the lexicographically largest string obtainable by swapping adjacent special substrings. 🧠 Approach: Use a counter to split the string into valid special segments. Recursively process inner substrings. Sort segments in descending order. Join them back together. ⚡ Example Input: "11011000" Output: "11100100" ⏱ Complexity Time: O(n log n) Space: O(n) 💡 Key Insight: Each valid segment behaves like a balanced block → reorder blocks for maximum lexicographic value. Hard problem ✔️ Confidence +1 🚀 #LeetCode #DSA #CodingJourney #HardProblems #Python
To view or add a comment, sign in
-
-
🚀 Day 169 of My LeetCode Journey 🚀 1092. Shortest Common Supersequence 🫧 In this problem, we are given two strings, and we need to build the shortest string that contains both of them as subsequences. ▪️ The key idea is related to the Longest Common Subsequence. If both strings share some common characters in order, we should include those characters only once in the final string. ▪️ First, I built a DP table to find the LCS length between the two strings. This helps us understand which characters are common between them. ▪️ After filling the DP table, I traversed it backwards to construct the final string. ▪️ If characters in both strings match, I add that character once to the result and move diagonally in the table. ▪️ If they don’t match, I move in the direction that gave the larger LCS value and add that character to the result. ▪️ If one string finishes before the other, I simply added the remaining characters from the other string. ▪️ In the end, I reversed the collected characters to get the shortest common supersequence, which contains both strings while keeping the length as small as possible. #LeetCode #DynamicProgramming #Strings #Python #CodingJourney #Day169 🔥
To view or add a comment, sign in
-
-
🚀 Day 163 of My LeetCode Journey 🚀 416. Partition Equal Subset Sum 🫧 We are given an array of integers and need to determine whether it can be split into two subsets with equal sum. ▪️ First, we calculate the total sum of the array. If the sum is odd, it cannot be divided into two equal parts, so return False. ▪️ If the sum is even, the problem becomes finding a subset whose sum equals total/2. ▪️ Treat this as a subset sum problem, where we check if we can form the target sum using the given numbers. ▪️ Our DP state is defined as (i, target), which represents whether we can form the target sum using elements from index. ▪️ At each index, we have two choices: either pick the element or not pick it. ▪️ If we pick the element, we reduce the target by nums[i]; if we skip it, the target remains the same. ▪️used memoization (dp[i][target]) to store results and avoid recomputing the same. ▪️ If successfully found a subset with a sum of total/ 2, it means the remaining elements automatically form the other subset with the same sum. #LeetCode #DynamicProgramming #Python #CodingJourney #Day163 🔥
To view or add a comment, sign in
-
-
A teammate recently ran into this Pandas error: TypeError: argument of type 'float' is not iterable The code looked correct: df.query("Overseas Collection in Crores == 0") The issue? The column name had spaces. query() evaluates the condition as a Python expression. Because the column name contained the word in, Pandas interpreted it as the membership operator. The Fix: df.query("`Overseas Collection in Crores` == 0") Or: df[df["Overseas Collection in Crores"] == 0] Clean column naming conventions help prevent subtle bugs. #Python #Pandas #DataEngineering
To view or add a comment, sign in
More from this author
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