LeetCode 1892: Page Recommendations II Solution

Master LeetCode problem 1892 (Page Recommendations II), a hard 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.

1892. Page Recommendations II

Problem Explanation

Explanation:

To solve this problem, we can use a combination of data structures like HashMap and PriorityQueue. We will maintain a HashMap to store the page recommendations for each user, where the key is the user id and the value is a PriorityQueue of recommended pages. The PriorityQueue will be sorted based on the page recommendation score.

We will iterate through the user visits data and update the page recommendations for each user. For each visited page, we will update the recommendation score for other pages based on the visited page's score and the similarity score between pages. We will keep track of the top k recommended pages for each user. Solution:

Solution Code

import java.util.*;

class PageRecommendationsII {
    public Map<Integer, PriorityQueue<Integer>> getPageRecommendations(int[][] userVisits, int k) {
        Map<Integer, PriorityQueue<Integer>> recommendations = new HashMap<>();
        Map<Integer, Map<Integer, Integer>> similarityScores = new HashMap<>();

        for (int[] visit : userVisits) {
            int userId = visit[0];
            int visitedPage = visit[1];

            if (!recommendations.containsKey(userId)) {
                recommendations.put(userId, new PriorityQueue<>((a, b) -> similarityScores.get(userId).get(b) - similarityScores.get(userId).get(a)));
            }

            if (!similarityScores.containsKey(visitedPage)) {
                similarityScores.put(visitedPage, new HashMap<>());
            }

            for (int page : recommendations.get(userId)) {
                similarityScores.get(visitedPage).put(page, similarityScores.get(visitedPage).getOrDefault(page, 0) + 1);
            }

            similarityScores.get(visitedPage).put(visitedPage, similarityScores.get(visitedPage).getOrDefault(visitedPage, 0) + 1);

            recommendations.get(userId).add(visitedPage);

            if (recommendations.get(userId).size() > k) {
                recommendations.get(userId).poll();
            }
        }

        return recommendations;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1892 (Page Recommendations II)?

This page provides optimized solutions for LeetCode problem 1892 (Page Recommendations II) 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 1892 (Page Recommendations II)?

The time complexity for LeetCode 1892 (Page Recommendations II) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1892 on DevExCode?

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

Back to LeetCode Solutions