Day 95/100 – #100DaysOfCode 🚀 | #Java #HashMap #Randomization ✅ Problem Solved: Random Flip Matrix (LeetCode 519) 🧩 Problem Summary: You are given a binary matrix initialized with all 0s. Each call to flip() should randomly return the position of a 0 and change it to 1. No position should be flipped more than once until reset() is called. 💡 Approach Used: ✔ Used HashMap + Randomization to simulate shuffling ✔ Treat the matrix as a flattened 1D array Steps: Map each index to its actual value using a HashMap Randomly pick an index from the remaining unflipped range Swap it with the last available index Decrease the available range On reset(), clear the map and restore the range ⚙ Time Complexity: O(1) per flip() 📦 Space Complexity: O(K) (where K = number of flipped cells) ✨ Takeaway: This problem is a great example of achieving uniform randomness without storing the entire matrix — smart indexing beats brute force. #Java #LeetCode #HashMap #Randomization #100DaysOfCode #CodingChallenge
Java HashMap Randomization for LeetCode 519
More Relevant Posts
-
Day 99/100 – #100DaysOfCode 🚀 | #Java #PrefixSum #HashMap ✅ Problem Solved: Continuous Subarray Sum (LeetCode 523) 🧩 Problem Summary: Given an integer array and an integer k, determine if the array has a continuous subarray of size at least 2 whose sum is a multiple of k. 💡 Approach Used: ✔ Used Prefix Sum + HashMap ✔ Key observation: If two prefix sums have the same remainder when divided by k, the subarray between them has a sum divisible by k. Steps: Maintain running sum Store (prefixSum % k) → earliest index in a HashMap If the same remainder appears again and the subarray length ≥ 2 → return true Initialize map with (0 → -1) to handle edge cases ⚙ Time Complexity: O(N) 📦 Space Complexity: O(K) (where K = number of unique remainders) ✨ Takeaway: Modular arithmetic paired with prefix sums is a powerful pattern for detecting divisible subarrays in linear time. #Java #LeetCode #PrefixSum #HashMap #100DaysOfCode #CodingChallenge
To view or add a comment, sign in
-
-
Day 97/100 – #100DaysOfCode 🚀 | #Java #Strings #Logic ✅ Problem Solved: Longest Uncommon Subsequence I (LeetCode) 🧩 Problem Summary: Given two strings, find the length of the longest uncommon subsequence — a subsequence that appears in one string but not in the other. 💡 Approach Used: ✔ Observational / logical approach ✔ If both strings are equal, no uncommon subsequence exists ✔ If they are different, the longer string itself is the answer Why it works: A string is always a subsequence of itself If strings differ, the longer one cannot be a subsequence of the shorter ⚙ Time Complexity: O(1) 📦 Space Complexity: O(1) ✨ Takeaway: Some problems look like DP but are solved with pure logic. Always check for simple observations before overengineering. #Java #LeetCode #Strings #Logic #100DaysOfCode #CodingChallenge
To view or add a comment, sign in
-
-
#200DaysOfCode – Day 105 Backtracking & Recursion 🔹 Problem:- Combination Sum (LeetCode 39) Task: Given an array of distinct integers and a target value, find all unique combinations where the chosen numbers sum to the target. Each number can be used multiple times. Example: Input: candidates = [2,3,6,7], target = 7 Output: [[2,2,3], [7]] My Approach: Used Backtracking to explore all possible combinations. At each step, decided to pick or skip the current element. Allowed reuse of elements by staying on the same index. Backtracked once the target became negative or a valid combination was found. Time Complexity: Exponential (based on number of combinations) Space Complexity: O(Target) (recursion stack + current path) Key Takeaway: Backtracking is all about choices and reversals. Once the pick / not-pick pattern becomes clear, complex problems start feeling much simpler. #takeUforward #200DaysOfCode #Java #Backtracking #Recursion #ProblemSolving #LeetCode #DSA #CodeNewbie #LearningInPublic #Consistency
To view or add a comment, sign in
-
-
Day 29/100 ✅ LeetCode 138: Copy List with Random Pointer Solved this problem by creating a deep copy of a linked list where each node contains an additional random pointer. The approach ensures that both next and random pointers are copied correctly without modifying the original list. Key steps involved: Create copied nodes Link copied nodes with original nodes Assign random pointers efficiently Separate the original and cloned lists This problem helped me strengthen my understanding of linked list manipulation and pointer mapping. Solution:-https://lnkd.in/gD9TWWsg #LeetCode #LeetCode138 #CopyListWithRandomPointer #LinkedList #DSA #Java #ProblemSolving #CodingPractice #DeepCopy #DataStructures #Day29
To view or add a comment, sign in
-
-
LeetCode Daily — Problem #3794: Reverse String Prefix Today’s challenge was a fun string manipulation task: Problem: Given a string s and an integer k, reverse the first k characters and return the resulting string. Example: Input: s = "abcd", k = 2 Output: "bacd" Approach: I converted the string to a character array, reversed the first k characters using a loop, and then appended the rest. Here's the Java solution:✅ Result: Accepted with 0 ms runtime! Takeaway: This problem reinforced how simple logic and clean iteration can solve string-based challenges efficiently. It’s a great reminder that even “Easy” problems can sharpen your fundamentals. #LeetCode #Java #StringManipulation #CodingChallenge #100DaysOfCode #ProblemSolving #LinkedInLearning
To view or add a comment, sign in
-
-
Day 92/100 – #100DaysOfCode 🚀 | #Java #DynamicProgramming #Strings ✅ Problem Solved: Longest Palindromic Subsequence (LeetCode) 🧩 Problem Summary: Given a string, find the length of the longest subsequence that is also a palindrome. (A subsequence does not need to be contiguous.) 💡 Approach Used: ✔ Used Dynamic Programming (2D DP) ✔ Define dp[i][j] as the length of the longest palindromic subsequence in substring s[i…j] Logic: If s[i] == s[j] → dp[i][j] = 2 + dp[i+1][j-1] Else → dp[i][j] = max(dp[i+1][j], dp[i][j-1]) Fill the table bottom-up by increasing substring length ⚙ Time Complexity: O(N²) 📦 Space Complexity: O(N²) ✨ Takeaway: Problems involving subsequences often hint toward DP. Breaking the string into overlapping subproblems makes complex patterns manageable. #Java #LeetCode #DynamicProgramming #Strings #100DaysOfCode #CodingChallenge
To view or add a comment, sign in
-
-
🚀 Day 68 of #100DaysOfCode Solved LeetCode Problem #1200 – Minimum Absolute Difference ✅ A clean and elegant problem where sorting does most of the heavy lifting. By comparing adjacent elements after sorting, we can efficiently find all pairs with the minimum absolute difference. Key Learnings: -> Sorting simplifies difference-based problems -> Adjacent comparison is enough after sorting -> Clear logic beats complex data structures -> Collecting results while tracking the minimum difference Language Used: Java -> Runtime: 20 ms (Beats 97.70%) -> Memory: 64.05 MB (Beats 32.62%) Step by step, day by day 🚀 On to the next problem 💪 #LeetCode #Java #ProblemSolving #Algorithms #Sorting #Arrays #100DaysOfCode
To view or add a comment, sign in
-
-
50 lines of code vs. 1 line. Who wins? 🥊 If you’re still writing getters, setters, constructors, and toString methods manually in 2026, you are working too hard. Java Records changed the game by killing "Boilerplate Hell." It turns a massive, messy POJO into a single, elegant line of code. In my latest blog post, I break down: ✅ Why "Data as Data" is better than "Data as Behavior." ✅ The "Freebies" you get from the compiler. ✅ Real-world scenarios where Records shine. Read the full breakdown here: 👇 https://lnkd.in/gUkyPU8c #Java #CleanCode #SoftwareEngineering #BackendDevelopment #JavaRecords #ProgrammingTips
To view or add a comment, sign in
-
-
day 93/100 #leetcodegrind “Find Minimum in a Sorted Rotated Array” problem — a classic example of Binary Search in action. 💡 Key idea: In a rotated sorted array, the minimum element is the pivot. By comparing mid and high elements, we can efficiently narrow the search space to find the minimum in O(log n) time. 🧠 What I practiced: Binary search on rotated arrays Identifying the pivot element efficiently Handling edge cases like non-rotated arrays Writing clean and optimized Java code It’s a simple problem, but it reinforces the power of binary search in non-traditional ways. Rotation? No problem! 🔄🚀 #DSA #ProblemSolving #Java #BinarySearch #Arrays #LeetCode #CodingPractice #DailyLearning
To view or add a comment, sign in
-
-
🧠 DSA Wednesday | LeetCode – 4Sum (Java) Solved 4Sum using sorting + two pointers 💡 🔍 Problem: Given an array, find all unique quadruplets whose sum equals the target. ✨ Key ideas that make this work: • Sorting the array → enables two-pointer approach • Skipping duplicates → avoids repeated answers • Using long sum → prevents integer overflow (🔥 important line!) 📌 Why this approach matters: Brute force is slow ❌ Optimized logic = clean, efficient, and interview-ready ✅ DSA is not about writing long code — it’s about thinking smart and handling edge cases. 👉 Would you like a breakdown of the duplicate-skipping logic next? #DSA #LeetCode #Java #ProblemSolving #WednesdayWisdom #CodingJourney #TwoPointers #InterviewPrep
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