🚀 Day 66 of #100DaysOfCode Solved 12. Integer to Roman on LeetCode 🔗 🧠 Key Insight: Roman numerals are built using fixed value-symbol pairs 👉 Including special cases like IV (4), IX (9), XL (40), CM (900) ⚙️ Approach (Greedy - Largest to Smallest): 1️⃣ Maintain two arrays: 🔹 Values → [1000, 900, 500, 400, ..., 1] 🔹 Symbols → ["M", "CM", "D", "CD", ..., "I"] 2️⃣ Traverse values from largest to smallest 3️⃣ For each value: 🔹 While num >= value[i]: • Append corresponding symbol • Subtract value from num 4️⃣ Stop when num == 0 ⏱️ Time Complexity: O(1) 📦 Space Complexity: O(1) 💡 Edge Cases: ✔️ 4 → "IV" ✔️ 9 → "IX" ✔️ 40 → "XL" ✔️ 900 → "CM" #100DaysOfCode #LeetCode #DSA #Greedy #Strings #Java #InterviewPrep #CodingJourney
Solving 12. Integer to Roman on LeetCode with Greedy Algorithm
More Relevant Posts
-
🧠 Day 39 / 100 – DSA Practice Solved Integer to Roman on LeetCode 🏛️🔢✅ 🔹 Problem: Convert an integer into its Roman numeral representation following standard rules. 🔹 Approach: Used a Greedy Algorithm: Maintain arrays of values and corresponding Roman symbols Iterate from largest to smallest value Subtract while possible and append symbols to result 🔍 Key Insight: Always pick the largest possible value first to build the Roman numeral efficiently 🔹 Complexity: ⏱ Time → O(1) (constant since values are fixed) 📦 Space → O(1) 💯 Result: ✔️ All test cases passed ⚡ Runtime: 3 ms (Beats 99.93%) A clean and efficient problem to understand greedy strategies 🚀 #Day39 #100DaysOfCode #LeetCode #Java #DSA #Greedy #Algorithms #CodingJourney
To view or add a comment, sign in
-
-
Day 90/100 🚀 | #100DaysOfDSA Solved LeetCode 240 – Search a 2D Matrix II today. Approach: Used the top-right corner traversal (greedy + matrix property). Key observation: • Each row is sorted left → right • Each column is sorted top → bottom So: • Start at top-right corner • If current value > target → move left (col--) • If current value < target → move down (row++) • If equal → found This eliminates one row or column in each step. Time Complexity: O(m + n) Space Complexity: O(1) Key takeaway: For sorted 2D matrices, don’t jump to binary search immediately — sometimes a clever starting point (like top-right) gives a simpler linear solution. #100DaysOfDSA #LeetCode #DSA #Java #Matrix #BinarySearch #Greedy #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 𝟔𝟏 – 𝐃𝐒𝐀 𝐉𝐨𝐮𝐫𝐧𝐞𝐲 | 𝐀𝐫𝐫𝐚𝐲𝐬 🚀 Today’s problem focused on arranging numbers to form the largest possible number. 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐨𝐥𝐯𝐞𝐝 • Largest Number 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡 • Converted all numbers to strings • Sorted them using a custom comparator Logic: • Compare (b + a) with (a + b) • Place the number first which forms a larger combined value Example: • "9" + "34" → "934" • "34" + "9" → "349" → So "9" comes before "34" • Joined all elements to form the final result • Handled edge case where all numbers are zero 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠𝐬 • Custom sorting can solve non-obvious problems • Greedy decisions can be applied during sorting • String comparison is sometimes more useful than numeric comparison • Edge cases (like all zeros) must be handled explicitly 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲 • Time: O(n log n) • Space: O(n) 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲 Sometimes the problem is not about numbers — but about how you arrange them. 61 days consistent 🚀 On to Day 62. #DSA #Arrays #Sorting #Greedy #LeetCode #Java #ProblemSolving #DailyCoding #LearningInPublic #SoftwareDeveloper
To view or add a comment, sign in
-
-
🔢 LeetCode 13 – Roman to Integer | Turning Patterns into Logic Worked on a classic problem today that highlights how recognizing patterns can simplify logic significantly. The task: convert a Roman numeral string into its integer equivalent. At first glance, it looks like a straightforward mapping problem—but the real challenge lies in handling the subtractive cases (like IV = 4, IX = 9). 💡 Key Insight: Instead of blindly adding values, compare adjacent symbols: If the current value is smaller than the next → subtract it Otherwise → add it This simple rule transforms the entire problem into a clean single-pass solution. ⚙️ Approach: Map Roman symbols to their integer values Traverse the string from left to right Apply the add/subtract rule based on the next character Accumulate the final result 🚀 Complexity: Time: O(n) Space: O(1) 📌 What I learned: Importance of identifying patterns in problems Writing efficient logic with minimal passes Handling edge cases with clarity Problems like this are a great reminder that strong fundamentals and observation skills can often beat brute force approaches. #LeetCode #DSA #Algorithms #ProblemSolving #CodingJourney #SoftwareEngineering #Java
To view or add a comment, sign in
-
-
✨ Day 40 of 90 – Pattern Mastery Journey 🧠 Pattern:Alphabet Hash Pattern 💡 Approach: ✔ Used nested loops for rows and columns ✔ Printed alphabets using ASCII logic `(char)('A' + i - 1)` ✔ Printed alphabet `i` times in each row ✔ Filled remaining positions with `#` using `(n - i)` ✔ Maintained proper structure and alignment 🚀 This problem helped me understand how to combine **characters and symbols in a single pattern**, improving control over loops and output formatting. #PatternMasteryJourney #Java #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
Day 55/75 — Shortest Subarray with Sum at Least K Today’s problem was a challenging one involving prefix sums and a monotonic deque. Approach: • Use prefix sum to convert subarray sum into difference of indices • Maintain a deque to keep indices in increasing order of prefix sums • Remove useless indices to keep it optimal Key logic: while (!dq.isEmpty() && ps[j] - ps[dq.peekFirst()] >= k) { minLen = Math.min(minLen, j - dq.pollFirst()); } while (!dq.isEmpty() && ps[j] <= ps[dq.peekLast()]) { dq.pollLast(); } dq.offerLast(j); Time Complexity: O(n) Space Complexity: O(n) A tough problem that builds strong intuition for prefix sums + deque optimization. 55/75 🚀 #Day55 #DSA #PrefixSum #Deque #Java #Algorithms #LeetCode
To view or add a comment, sign in
-
-
✨ Day 41 of 90 – Pattern Mastery Journey 🧠 Pattern:Reverse Alphabet Hash Pattern 💡 Approach: ✔ Used reverse looping (n → 1) to build the pattern ✔ Printed alphabets using ASCII logic `(char)('A' + i - 1)` ✔ Printed alphabet `i` times in each row ✔ Filled remaining positions with `#` using `(n - i)` ✔ Maintained proper structure and alignment 🚀 This problem helped me understand how reversing loops can change the entire pattern structure and improve control over output formatting. #PatternMasteryJourney #Java #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
🚀 LeetCode Challenge 15/50 💡 Approach: Dutch National Flag Algorithm The problem says no library sort — and even a two-pass counting sort feels like cheating when there's a legendary one-pass algorithm designed exactly for this! Edsger Dijkstra's Dutch National Flag algorithm sorts 3 distinct values in a single traversal! 🔍 Key Insight: → Maintain 3 pointers: low, mid, high → nums[mid] == 0 → swap with low, advance both low & mid → nums[mid] == 1 → already in place, just advance mid → nums[mid] == 2 → swap with high, shrink high only → Stop when mid crosses high 📈 Complexity: ❌ Two-pass Counting Sort → O(n) Time, O(1) Space (2 passes) ✅ Dutch National Flag → O(n) Time, O(1) Space (1 pass only!) Sometimes knowing the right algorithm is the solution itself. Dijkstra didn't just solve sorting — he solved elegance! 🇳🇱 #LeetCode #DSA #DutchNationalFlag #Java #ADA #PBL2 #LeetCodeChallenge #Day15of50 #CodingJourney #ComputerEngineering #AlgorithmDesign #SortColors
To view or add a comment, sign in
-
-
✅ LeetCode Top Interview 150 – Day 94 Today I solved Evaluate Division Key Idea : Model the problem as a graph Each variable is a node, equations form weighted edges Build adjacency list with forward and reciprocal values Use DFS/BFS traversal to find path between variables Multiply edge weights along the path to get result This approach efficiently evaluates division queries using graph traversal #DSA #Java #Leetcode #Day94
To view or add a comment, sign in
-
-
🧠 Day 6/450: Pick Smart, Not Hard Today’s problem—Divide an Array Into Subarrays With Minimum Cost I—is a great example of simplifying constraints. 📌 Problem Split the array into 3 contiguous subarrays. The cost = first element of each subarray. Goal 👉 Minimize total cost. ❌ Brute-force thinking: Try all possible ways to split into 3 parts → O(n²) or worse → Not efficient 💡 Pattern: Greedy + Observation 👉 First subarray must start at index 0 👉 So first cost is always: nums[0] Now the problem reduces to: 👉 Pick 2 more starting points (for subarray 2 & 3) 👉 Minimize their values 🔍 Approach: First cost = nums[0] From remaining elements → pick 2 smallest values Add them to result 🧠 Pattern recognition clues: Look for: “Minimize cost based on specific positions” “Only certain elements contribute to answer” “You don’t need full partition simulation” ✅ Complexity: Time: O(n) Space: O(1) 🔥 Key takeaway: When only specific elements impact the result, 👉 Focus on selecting them optimally instead of simulating everything. #LeetcodeChallenge #DSA #Greedy #Consistency #KeepItUp #LearnAndGrow #Java
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