Today I learned why converting large binary strings using Integer.parseInt() is a bad idea. Solved LeetCode 1404 – Number of Steps to Reduce a Number in Binary Representation to One. Initially, I tried converting the binary string into an integer. Got NumberFormatException. Reason? Binary length > 31 bits → overflow. So instead of converting, I: Processed the string from right to left Simulated carry manually Counted steps without using BigInteger Result: Runtime: 0 ms Beat: 100% Key takeaway: Sometimes the optimal solution is not about bigger data types — It’s about avoiding conversion entirely. #LeetCode #Java #ProblemSolving #DataStructures #Binar
LeetCode 1404: Avoiding Integer.parseInt() for Binary Conversion
More Relevant Posts
-
Day 97/100 – LeetCode Challenge ✅ Problem: #105 Construct Binary Tree from Preorder and Inorder Traversal Difficulty: Medium Language: Java Approach: Recursive Construction with HashMap Lookup Time Complexity: O(n) Space Complexity: O(n) for map + recursion stack Key Insight: Preorder: [root, left subtree, right subtree] Inorder: [left subtree, root, right subtree] First element in preorder is always root Root's position in inorder splits left and right subtrees Solution Brief: Built HashMap to map inorder value → index for O(1) lookups. Maintained global index to track current root in preorder. Recursively built tree: Get root value from preorder at current index Find root position in inorder Build left subtree using elements before root Build right subtree using elements after root #LeetCode #Day97 #100DaysOfCode #Tree #BinaryTree #Java #Algorithm #CodingChallenge #ProblemSolving #ConstructBinaryTree #MediumProblem #Recursion #HashMap #DSA
To view or add a comment, sign in
-
-
🚀 Day 44 of #100DaysOfCode Solved 1011. Capacity To Ship Packages Within D Days on LeetCode 📦 🧠 Key Insight: We need to find the minimum ship capacity such that all packages can be shipped within D days. This is a classic case of Binary Search on Answer. ⚙️ Approach: 1️⃣ The minimum capacity = max(weight) (since one package must fit) 2️⃣ The maximum capacity = sum of all weights 3️⃣ Apply Binary Search between these bounds 4️⃣ For each capacity mid, simulate shipping: 🔹Keep adding weights until capacity exceeds 🔹Move to the next day when exceeded 5️⃣ If we can ship within D days → try smaller capacity 6️⃣ Else → increase capacity This helps us find the minimum valid capacity efficiently. ⏱️ Time Complexity: O(n log(sum)) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #BinarySearch #Algorithms #Java #InterviewPrep #CodingJourney
To view or add a comment, sign in
-
-
Day 89/100 – LeetCode Challenge ✅ Problem: #459 Repeated Substring Pattern Difficulty: Easy Language: Java Approach: Substring Division Check Time Complexity: O(n²) worst case Space Complexity: O(n) Key Insight: A string can be formed by repeating its substring if the substring length divides the string length. Check all divisors of length from largest to smallest for efficiency. Solution Brief: Iterated i from l/2 down to 1 (larger substrings first). If l % i == 0, substring length divides string length. Constructed repeated pattern by appending substring m = l/i times. Compared constructed string with original. #LeetCode #Day89 #100DaysOfCode #String #Java #Algorithm #CodingChallenge #ProblemSolving #RepeatedSubstring #EasyProblem #Substring #Pattern #DSA
To view or add a comment, sign in
-
-
Leetcode Problem || Minimum Operations to Equalize Binary String Solved(3666)🚀 Today I worked on an interesting problem involving string transformation operations. Instead of simulating every flip operation directly on the string (which would be expensive), I optimized the approach by: ✔ Converting the problem into tracking only the count of zeros ✔ Modeling the transformation as a graph problem ✔ Using BFS to find minimum operations ✔ Leveraging TreeSet for efficient range pruning ✔ Applying parity-based optimization for faster traversal #Java #DSA #BFS #ProblemSolving #CompetitiveProgramming #CodingJourney
To view or add a comment, sign in
-
-
#Day58 of my second #100DaysOfCode Today’s problem was about carefully handling overlapping intervals. DSA • Solved Merge Intervals (LeetCode 56) • Implemented a brute force approach by checking and merging overlapping intervals → O(2n) + O(n log n) time • Implemented the optimal approach by sorting intervals and merging them in a single traversal → O(n log n) + O(n) time • Key idea: once intervals are sorted by start time, overlapping ranges can be merged efficiently in one pass These interval problems really test attention to detail with edge cases. #DSA #Algorithms #LeetCode #Java #100DaysOfCode #WomenWhoCode #BuildInPublic #LearningInPublic
To view or add a comment, sign in
-
-
Day 16/100 – LeetCode Challenge Problem Solved: Binary Tree Level Order Traversal Today’s problem focused on traversing a binary tree level by level. Instead of visiting nodes in depth-first order, the goal is to group nodes according to their depth in the tree. The idea behind the solution is to track the level of each node while traversing the tree. Starting from the root at level 0, each recursive call increases the level for its child nodes. When visiting a node, we check whether a list for that level already exists. If not, we create one; otherwise, we append the node value to the existing level list. By doing this, the traversal naturally organizes nodes into separate lists representing each level of the tree. Time Complexity: O(n) Space Complexity: O(n) Key takeaway: Tree problems often become easier when you track additional context during traversal. In this case, maintaining the current level allows the recursion to build a structured level-by-level result. Day 16 completed. Continuing the consistency streak and strengthening tree traversal concepts. #100DaysOfLeetCode #Java #BinaryTree #TreeTraversal #Algorithms #ProblemSolving
To view or add a comment, sign in
-
-
Day 92/100 – LeetCode Challenge ✅ Problem: #49 Group Anagrams Difficulty: Medium Language: Java Approach: Sorting + HashMap Grouping Time Complexity: O(n × k log k) where k = max string length Space Complexity: O(n × k) Key Insight: Anagrams have identical sorted character sequences. Use sorted string as key to group original strings in HashMap. Solution Brief: Iterated through each string in array. Converted to char array and sorted to get canonical form. Used HashMap with sorted string as key, list of original strings as value. If key exists, add to existing list; otherwise create new list. Collected all values from map into result list. #LeetCode #Day92 #100DaysOfCode #HashMap #Sorting #Java #Algorithm #CodingChallenge #ProblemSolving #GroupAnagrams #MediumProblem #String #Anagram #DSA
To view or add a comment, sign in
-
-
Day 100/100 – LeetCode Challenge ✅ Problem: #41 First Missing Positive Difficulty: Hard Language: Java Approach: In-Place Index Marking Time Complexity: O(n) Space Complexity: O(1) Key Insight: Place each positive integer in its correct position (index i should contain i+1). Mark presence using negative values without extra space. Solution Brief: First pass: replace numbers outside range [1, n] with n+1 (ignore them) Second pass: treat each number as index, mark that index negative Third pass: first positive index = missing number If all marked, answer is n+1 #LeetCode #Day100 #100DaysOfCode #Array #Java #Algorithm #CodingChallenge #ProblemSolving #FirstMissingPositive #HardProblem #InPlace #Optimization #DSA
To view or add a comment, sign in
-
-
Day 82/100 – LeetCode Challenge ✅ Problem: #43 Multiply Strings Difficulty: Medium Language: Java Approach: Manual Multiplication with Result Array Time Complexity: O(n × m) Space Complexity: O(n + m) Key Insight: Multiply digits from right to left (least significant first). Store intermediate results in array where index i + j + 1 holds current digit. Handle carry by adding to previous index. Solution Brief: Edge case: if either number is "0", return "0". Created result array of size n1 + n2 (max possible digits). Nested loops multiply each digit of num1 with each digit of num2. Accumulated results with proper carry handling. Built final string skipping leading zeros. #LeetCode #Day82 #100DaysOfCode #Math #String #Java #Algorithm #CodingChallenge #ProblemSolving #MultiplyStrings #MediumProblem #Multiplication #Array #DSA
To view or add a comment, sign in
-
-
Headline: Cracking the "Largest Rectangle in Histogram" (LeetCode 84) 🚀 I just cleared this classic Hard problem with a 0ms runtime! The challenge is finding the largest area in a histogram in O(n) time. The secret sauce? A Monotonic Stack. By storing indices of bars in increasing order, we can identify the "boundary" for each height in a single pass. Key takeaway: Choosing the right data structure (like a Stack) can turn an expensive O(n^2) search into a lightning-fast linear solution. 👨💻 #LeetCode #Java #Algorithms #DataStructures #CodingLife
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