✅ Solution — LeetCode 739. Daily Temperatures (Java) 🧠 Idea (Monotonic Stack) We use a stack to store indices of days. The stack keeps temperatures in decreasing order. When we find a warmer temperature, we: Pop the previous index from stack Calculate the difference in days Store it in the result array 💻 Java Code class Solution { public int[] dailyTemperatures(int[] temperatures) { int n = temperatures.length; int[] res = new int[n]; Stack<Integer> stack = new Stack<>(); for(int i = 0; i < n; i++){ while(!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]){ int prev = stack.pop(); res[prev] = i - prev; } stack.push(i); } return res; } } ⏱ Time Complexity O(n) Each element is pushed and popped once. 💾 Space Complexity O(n) 🚀 100 Days of Learning & Problem Solving – Day 79 🚀 📅 Day 79 / 100 Continuing my #100DaysOfLeetCode journey by exploring the Monotonic Stack pattern today. ✅ Today’s Progress: 🔹 Solved LeetCode 739 – Daily Temperatures 🔹 Implemented the monotonic stack technique for efficient comparisons 🔹 Practiced solving “next greater element” type problems 🔹 Improved understanding of stack-based problem-solving patterns 🧠 Key Learnings: ✔️ Using stacks to track unresolved elements ✔️ Identifying the “next greater element” pattern ✔️ Avoiding brute-force nested loops with a linear-time solution ✔️ Writing efficient solutions with better time complexity Problems like this show how the right data structure can reduce complexity dramatically. From nested loops to linear time — the power of patterns 🚀 📌 Alongside DSA, I continue focusing on: ✔️ Strengthening core programming fundamentals ✔️ Recognizing reusable problem-solving patterns ✔️ Writing clean and efficient code ✔️ Staying consistent with daily practice Learning every day, improving one problem at a time 💪 #100DaysOfLearning #LeetCode #DSA #Java #ProblemSolving #CodingJourney #LearningInPublic #Consistency
LeetCode 739 Daily Temperatures Java Solution
More Relevant Posts
-
🚀 Day 52 of My DSA Journey Today, I solved Reverse Words in a String (LeetCode 151) 💻 This problem tested my understanding of string manipulation, trimming, and efficient traversal. 🔹 Problem Statement: Given a string s, reverse the order of words while removing extra spaces. 🔹 Key Learnings: ✅ Handling leading, trailing, and multiple spaces ✅ Using split() with regex (\\s+) ✅ Efficient use of StringBuilder for better performance ✅ Traversing array in reverse order 🔹 Approach: Trim the string to remove extra spaces Split words using regex Traverse from end to start Append words with single space 🔹 Example: Input: " hello world " Output: "world hello" Another Example: Input: "the sky is blue" Output: "blue is sky the" 🔹 Code Insight (Java): Used StringBuilder to efficiently build the reversed string without extra space issues. Consistency is the key 🔑 — showing up every day and improving step by step. #Day52 #DSA #Java #LeetCode #CodingJourney #100DaysOfCode #Programming #PlacementPreparation 🚀 If you want, I can also create a more viral version (with hooks + storytelling) to get more reach 🔥Here’s a clean and engaging LinkedIn post for your Day 52 of DSA Journey with an example: 🚀 Day 52 of My DSA Journey Today, I solved Reverse Words in a String (LeetCode 151) 💻 This problem tested my understanding of string manipulation, trimming, and efficient traversal. 🔹 Problem Statement: Given a string s, reverse the order of words while removing extra spaces. 🔹 Key Learnings: ✅ Handling leading, trailing, and multiple spaces ✅ Using split() with regex (\\s+) ✅ Efficient use of StringBuilder for better performance ✅ Traversing array in reverse order 🔹 Approach: Trim the string to remove extra spaces Split words using regex Traverse from end to start Append words with single space 🔹 Example: Input: " hello world " Output: "world hello" Another Example: Input: "the sky is blue" Output: "blue is sky the" 🔹 Code Insight (Java): Used StringBuilder to efficiently build the reversed string without extra space issues. Consistency is the key 🔑 — showing up every day and improving step by step. #Day52 #DSA #Java #LeetCode #CodingJourney #100DaysOfCode #Programming #PlacementPreparation 🚀
To view or add a comment, sign in
-
-
🚀 LeetCode Practice Update: Concatenation of Array Solved an interesting array problem today that reinforces indexing fundamentals. 🔍 Problem: Given an array nums of size n, create a new array ans of size 2n such that: ans[i] = nums[i] ans[i + n] = nums[i] 💡 My Approach: ✔️ Created a new array of size 2 * n ✔️ Used a single loop to fill both halves of the array ✔️ Leveraged index shifting (i + n) for duplication 👨💻 Code (Java): class Solution { public int[] getConcatenation(int[] nums) { int[] ans = new int[nums.length * 2]; int n = nums.length; for(int i = 0; i < n; i++) { ans[i] = nums[i]; ans[i + n] = nums[i]; } return ans; } } ⚡ Key Learning: Using index manipulation helps avoid extra loops and keeps the solution efficient. 📊 Complexity: ⏱ Time: O(n) 💾 Space: O(n) 📌 Small problem, big clarity boost! #LeetCode #Java #DSA #CodingJourney #ProblemSolving #Developers
To view or add a comment, sign in
-
-
🚀 Week 8 – Java + DSA Journey Update This week was all about strengthening my problem-solving skills and diving deeper into Data Structures using Java. 💻 🔹 What I focused on: Arrays & Advanced Array Problems Two Pointer Technique Sliding Window Concepts Solved multiple problems on LeetCode 🔹 Key Learnings: Learned how to optimize brute force solutions into efficient ones (O(n)) Understood real use of two pointers in problems like pair sum & sorting-based questions Sliding window made problems like longest substring much more efficient 🔹 Challenges faced: Some problems looked easy but required deep thinking to optimize. Debugging logic took time, but consistency helped 💪 🔹 Progress: Improved coding speed ⏱️ Better understanding of patterns instead of just memorizing solutions 📌 Goal for next week: Start Linked List Practice medium-level problems consistently Consistency is the only key 🔑 #Java #DSA #LeetCode #CodingJourney #SoftwareEngineering #LearningInPublic
To view or add a comment, sign in
-
-
🔥 Day 99 of #100DaysOfCode Today’s problem: LeetCode – Merge Two Sorted Lists 🔗📊 📌 Problem Summary You are given two sorted linked lists. 👉 Merge them into one sorted linked list and return the head. Example: list1 = [1,2,4] list2 = [1,3,4] Output → [1,1,2,3,4,4] 🧠 Approach: Recursion We compare nodes from both lists and recursively build the merged list. ⚙️ Logic 1️⃣ Base cases: If list1 == null → return list2 If list2 == null → return list1 2️⃣ Compare current nodes: If list1.val <= list2.val → attach list1 and recurse Else → attach list2 and recurse 💻 Code Insight if(list1.val <= list2.val){ list1.next = mergeTwoLists(list1.next, list2); return list1; }else{ list2.next = mergeTwoLists(list1, list2.next); return list2; } 💡 Why Recursion Works? Each step: Picks the smaller node Reduces the problem size Eventually reaches base case → builds sorted list naturally. ⏱ Time Complexity: O(n + m) 💾 Space Complexity: O(n + m) (recursion stack) 🚀 Performance Runtime: 0 ms Beats 100% submissions 🔥 Memory: 44.23 MB 🧠 Key Learning This problem is fundamental for: Linked list manipulation Divide & conquer Merge sort logic 🚨 Almost There! Just 1 day left to complete the #100DaysOfCode challenge 🎯🔥 From basics → advanced patterns → consistency. On to the final Day 100 🚀 #100DaysOfCode #LeetCode #LinkedList #Recursion #Java #DSA #InterviewPrep
To view or add a comment, sign in
-
-
🚀 Day 99 of My 100 Days LeetCode Challenge | Java Today’s problem was all about randomization and linked list traversal — a nice break from heavy DP and matrices. The challenge was to design a system that returns a random node’s value from a singly linked list, ensuring that every node has an equal probability of being chosen. Since linked lists don’t allow direct indexing, the key idea was to first determine the size of the list, and then generate a random index to fetch the corresponding node. This approach ensures uniform randomness while keeping the implementation simple and efficient. ✅ Problem Solved: Linked List Random Node ✔️ All test cases passed (8/8) ⏱️ Runtime: 11 ms 🧠 Approach: Linked List Traversal + Randomization 🧩 Key Learnings: ● Randomization problems require ensuring uniform probability distribution. ● Linked lists limit direct access, so traversal becomes essential. ● Precomputing size can simplify random selection. ● Sometimes simple approaches are the most effective. ● Understanding data structure limitations helps design better solutions. This problem highlighted how probability + data structures can come together in elegant ways. 🔥 Day 99 complete — sharpening my understanding of randomization and linked list behavior. #LeetCode #100DaysOfCode #Java #LinkedList #Randomization #Algorithms #ProblemSolving #DSA #CodingJourney #Consistency
To view or add a comment, sign in
-
-
🚀 Day 17/30– DSA Challenge 📌 LeetCode Problem – Plus One 📝 Problem Statement You are given a large integer represented as an array of digits. Increment the integer by one and return the resulting array. 📌 Example Input: digits = [1,2,3] Output: [1,2,4] 📌 Edge Case Input: [9,9,9] Output: [1,0,0,0] 💡 Key Insight Addition starts from the last digit. 👉 If digit < 9 → just add 1 and return 👉 If digit == 9 → it becomes 0 and carry moves left If all digits are 9 → create a new array. 🚀 Algorithm 1️⃣ Traverse from right → left 2️⃣ If digit < 9: Increment it Return array 3️⃣ Else: Set digit = 0 Continue 4️⃣ If loop ends → all were 9 Create new array of size n+1 Set first element = 1 ✅ Java Code (Optimal O(n)) class Solution { public int[] plusOne(int[] digits) { for (int i = digits.length - 1; i >= 0; i--) { if (digits[i] < 9) { digits[i]++; return digits; } digits[i] = 0; } // All digits were 9 int[] result = new int[digits.length + 1]; result[0] = 1; return result; } } ⏱ Complexity Time Complexity: O(n) Space Complexity: O(1) (except edge case) 📚 Key Learnings – Day 17 ✔ Handle carry carefully ✔ Think from right to left ✔ Edge cases matter more than logic here ✔ Small problems test attention to detail Simple idea. Tricky edge case. Clean implementation. Day 17 completed. Consistency continues 💪🔥 #30DaysOfCode #DSA #Java #InterviewPreparation #ProblemSolving #CodingJourney #Arrays #LeetCode
To view or add a comment, sign in
-
-
📌 LeetCode Daily Challenge — Day 29 Problem: 3442. Make Two Strings Equal by Swapping Topic: String, Hash Table 📌 Quick Problem Sense: Two strings of length 4. You can swap characters at indices i and j only if j - i = 2. That means only swaps (0↔2) and (1↔3) are allowed, on either string, any number of times. Can you make both strings equal? 🤔 🧠 Approach (Simple Thinking): 🔹 Ask the key question first: which positions can actually interact? 🔹 Swap (0↔2) and (0↔1)? NEVER, gap must be exactly 2 🔹 So even positions {0, 2} are forever isolated from odd positions {1, 3} 🔹 Within each group of 2, you can freely swap, so any arrangement within the group is reachable 🔹 The strings are equalizable if and only if: {s1[0], s1[2]} and {s2[0], s2[2]} contain the same characters (even group) ✅ {s1[1], s1[3]} and {s2[1], s2[3]} contain the same characters (odd group) ✅ 🔹 Just sort each pair and compare, done! 🎯 ⏱️ Time Complexity: Fixed length 4 → everything is constant → O(1) 📦 Space Complexity: Four char arrays of size 2 → O(1) I wrote a full breakdown with dry run, real-life analogy, and step-by-step code walkthrough here 👇 https://lnkd.in/g_ExG2Xd If you solved it with a frequency map or a different parity grouping trick, drop it in the comments, always curious to see how others think about it 💬 See you in the next problem 👋 #LeetCode #DSA #CodingChallenge #Java #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 93 of My 100 Days LeetCode Challenge | Java Today’s problem explored recursion and backtracking, focusing on generating valid strings under specific constraints. The challenge was to generate happy strings of length n, where: The string consists only of 'a', 'b', and 'c' No two adjacent characters are the same From all possible happy strings, the goal was to find the k-th lexicographical string. To solve this, I used a backtracking approach that builds the string step by step. At each step, we try adding characters while ensuring they don't match the previous character. This naturally generates all valid happy strings in lexicographical order. Once all valid strings are generated, we simply return the k-th one if it exists. ✅ Problem Solved: The k-th Lexicographical Happy String of Length n ✔️ All test cases passed (345/345) ⏱️ Runtime: 22 ms 🧠 Approach: Backtracking + Recursion 🧩 Key Learnings: ● Backtracking is ideal for generating constrained combinations. ● Pruning invalid states early improves efficiency. ● Recursion makes exploring possible paths intuitive. ● Lexicographical generation often comes naturally with ordered iteration. ● Constraint-based string generation is a common interview pattern. This problem was a great exercise in systematically exploring possibilities while enforcing constraints. 🔥 Day 93 complete — strengthening recursion and backtracking intuition. #LeetCode #100DaysOfCode #Java #Backtracking #Recursion #ProblemSolving #DSA #CodingJourney #Consistency
To view or add a comment, sign in
-
-
🚀Day 24 of #75DaysofLeetCode LeetCode Problem Solved: Removing Stars From a String (2390) Today I solved the problem “Removing Stars From a String.” 🔹 Problem Statement: We are given a string containing characters and *. Each * removes the closest non-star character to its left, along with the * itself. The task is to return the final string after all stars are processed. 💡 Approach: The most efficient way to solve this is using a Stack-like approach. 1️⃣ Traverse the string from left to right. 2️⃣ If the character is not *, add it to the result (stack). 3️⃣ If the character is *, remove the last added character. 4️⃣ Continue until the string is processed. ⏱ Time Complexity: O(n) 📦 Space Complexity: O(n) 💻 Java Implementation: class Solution { public String removeStars(String s) { StringBuilder sb = new StringBuilder(); for(char c : s.toCharArray()) { if(c == '*') { sb.deleteCharAt(sb.length() - 1); } else { sb.append(c); } } return sb.toString(); } } ✨ Key Learning: Using a stack or StringBuilder helps efficiently simulate the removal of the closest character on the left. #LeetCode #Java #DataStructures #ProblemSolving #CodingPractice #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Learning Dependency #Injection in #SpringFramework As part of my backend learning journey with the Spring Framework, today I explored how Spring manages relationships between objects using Dependency Injection. 🧩 I learned about the Has-A relationship, where one class depends on another class. For example, a Student class can have a Location object. Instead of creating the object manually, the Spring Container injects it automatically. 📌 Some key concepts I explored today: 🔹 #ref vs value value → used to inject primitive or simple values ref → used to inject another bean (object reference) 🔹 #primary = true When multiple beans of the same type exist, Spring uses the primary bean as the default. 🔹 #Autowiring Spring can automatically inject dependencies. Types of autowiring I learned: • #byName – matches bean name with property name • #byType – matches bean based on its data type Understanding these concepts helps reduce manual object creation and makes applications more modular and loosely coupled. Excited to continue exploring more advanced concepts in Spring and Spring Boot! 💻 #SpringFramework #Java #BackendDevelopment #SpringBoot #DependencyInjection #LearningJourney #10000 Coders
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