LeetCode 2314: The First Day of the Maximum Recorded Degree in Each City Solution

Master LeetCode problem 2314 (The First Day of the Maximum Recorded Degree in Each City), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2314. The First Day of the Maximum Recorded Degree in Each City

Medium

Problem Explanation

Explanation

To solve this problem, we can iterate over the given array of temperatures and keep track of the maximum temperature recorded in each city. For each city, we need to find the first day when the maximum temperature was recorded. We can do this by storing the day when the maximum temperature was recorded for each city in a hashmap.

  1. Iterate over the temperatures array.
  2. For each city, update the maximum temperature recorded.
  3. If the current temperature is greater than the maximum temperature recorded for that city, update the maximum temperature and the day.
  4. Store the city and the first day when the maximum temperature was recorded in a hashmap.
  5. Finally, return an array containing the first day of the maximum recorded degree in each city.

Time Complexity: O(n), where n is the number of temperatures given. Space Complexity: O(n) for the hashmap storing the maximum recorded temperature and the first day for each city.

Solution Code

import java.util.HashMap;

class Solution {
    public int[] findDayOfMaxDegree(int[] temperatures) {
        HashMap<Integer, Integer> maxDegreeDay = new HashMap<>();
        int[] result = new int[temperatures.length];
        
        for (int i = 0; i < temperatures.length; i++) {
            if (!maxDegreeDay.containsKey(temperatures[i])) {
                maxDegreeDay.put(temperatures[i], i);
            } else {
                int day = maxDegreeDay.get(temperatures[i]);
                if (i < day || temperatures[i] > temperatures[day]) {
                    maxDegreeDay.put(temperatures[i], i);
                }
            }
        }
        
        for (int i = 0; i < temperatures.length; i++) {
            result[i] = maxDegreeDay.get(temperatures[i]);
        }
        
        return result;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2314 (The First Day of the Maximum Recorded Degree in Each City)?

This page provides optimized solutions for LeetCode problem 2314 (The First Day of the Maximum Recorded Degree in Each City) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2314 (The First Day of the Maximum Recorded Degree in Each City)?

The time complexity for LeetCode 2314 (The First Day of the Maximum Recorded Degree in Each City) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2314 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2314 in Java, C++, or Python.

Back to LeetCode Solutions