LeetCode 635: Design Log Storage System Solution

Master LeetCode problem 635 (Design Log Storage System), 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.

635. Design Log Storage System

Problem Explanation

Explanation:

To design a log storage system, we can use a data structure that allows us to store logs and query logs based on a given range of timestamps. One approach is to use a TreeMap where we store the logs with their timestamps as keys. This will allow us to efficiently query logs within a given time range.

Algorithmic Idea:

  1. Use a TreeMap to store logs with timestamps as keys.
  2. For each log operation, insert the log with its timestamp into the TreeMap.
  3. When querying logs within a time range, iterate through the TreeMap and filter logs within the given range.

Time Complexity:

  • Inserting a log: O(log n) where n is the number of logs.
  • Querying logs within a time range: O(n) where n is the number of logs within the given range.

Space Complexity:

  • O(n) where n is the number of logs stored in the TreeMap.

: :

Solution Code

import java.util.TreeMap;

class LogSystem {
    TreeMap<Long, String> logs;

    public LogSystem() {
        logs = new TreeMap<>();
    }

    public void put(int id, String timestamp) {
        logs.put(Long.parseLong(timestamp.replace(":", "")), String.valueOf(id));
    }

    public List<Integer> retrieve(String s, String e, String gra) {
        long start = getStartTime(s, gra);
        long end = getEndTime(e, gra);
        List<Integer> result = new ArrayList<>();

        for (Map.Entry<Long, String> entry : logs.subMap(start, true, end, true).entrySet()) {
            result.add(Integer.parseInt(entry.getValue()));
        }

        return result;
    }

    private long getStartTime(String timestamp, String gra) {
        timestamp = timestamp.replace(":", "");
        if (gra.equals("Year")) {
            return Long.parseLong(timestamp.substring(0, 4) + "0000000000");
        } else if (gra.equals("Month")) {
            return Long.parseLong(timestamp.substring(0, 6) + "00000000");
        } else if (gra.equals("Day")) {
            return Long.parseLong(timestamp.substring(0, 8) + "000000");
        } else if (gra.equals("Hour")) {
            return Long.parseLong(timestamp.substring(0, 10) + "0000");
        } else if (gra.equals("Minute")) {
            return Long.parseLong(timestamp.substring(0, 12) + "00");
        } else {
            return Long.parseLong(timestamp);
        }
    }

    private long getEndTime(String timestamp, String gra) {
        timestamp = timestamp.replace(":", "");
        if (gra.equals("Year")) {
            return Long.parseLong(timestamp.substring(0, 4) + "1231235959");
        } else if (gra.equals("Month")) {
            return Long.parseLong(timestamp.substring(0, 6) + "31235959");
        } else if (gra.equals("Day")) {
            return Long.parseLong(timestamp.substring(0, 8) + "235959");
        } else if (gra.equals("Hour")) {
            return Long.parseLong(timestamp.substring(0, 10) + "5959");
        } else if (gra.equals("Minute")) {
            return Long.parseLong(timestamp.substring(0, 12) + "59");
        } else {
            return Long.parseLong(timestamp);
        }
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 635 (Design Log Storage System)?

This page provides optimized solutions for LeetCode problem 635 (Design Log Storage System) 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 635 (Design Log Storage System)?

The time complexity for LeetCode 635 (Design Log Storage System) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 635 on DevExCode?

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

Back to LeetCode Solutions