Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishekghoshh committed Dec 27, 2024
1 parent 79fe7b8 commit 239668a
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 50 deletions.
45 changes: 33 additions & 12 deletions src/com/problems/array/DesignAFoodRatingSystem.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.problems.array;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;
Expand All @@ -24,7 +23,25 @@ public static void main(String[] args) {
}


// optimized approach,
// rather than updating the rating of the food we will add another food object in the priority queue
// while getting the highest rated queue we will
private static void type1() {
String[] foods = {"kimchi", "miso", "sushi", "moussaka", "ramen", "bulgogi"};
String[] cuisines = {"korean", "japanese", "japanese", "greek", "japanese", "korean"};
int[] ratings = {9, 12, 8, 15, 14, 7};
FoodRatings foodRatings = new FoodRatings(foods, cuisines, ratings);
foodRatings.highestRated("korean"); // return "kimchi"
// "kimchi" is the highest rated korean food with a rating of 9.
foodRatings.highestRated("japanese"); // return "ramen"
// "ramen" is the highest rated japanese food with a rating of 14.
foodRatings.changeRating("sushi", 16); // "sushi" now has a rating of 16.
foodRatings.highestRated("japanese"); // return "sushi"
// "sushi" is the highest rated japanese food with a rating of 16.
foodRatings.changeRating("ramen", 16); // "ramen" now has a rating of 16.
foodRatings.highestRated("japanese"); // return "ramen"
// Both "sushi" and "ramen" have a rating of 16.
// However, "ramen" is lexicographically smaller than "sushi".
}

static class FoodRatings {
Expand All @@ -40,32 +57,36 @@ static class FoodRatings {
public FoodRatings(String[] foods, String[] cuisines, int[] ratings) {
for (int i = 0; i < foods.length; ++i) {
// Store 'rating' and 'cuisine' of the current 'food' in 'foodRatingMap' and 'foodCuisineMap' maps.
foodRatingMap.put(foods[i], ratings[i]);
foodCuisineMap.put(foods[i], cuisines[i]);
String food = foods[i];
int rating = ratings[i];
String cuisine = cuisines[i];
foodRatingMap.put(food, rating);
foodCuisineMap.put(food, cuisine);
// Insert the '(rating, name)' element into the current cuisine's priority queue.
cuisineFoodMap.computeIfAbsent(cuisines[i], k -> new PriorityQueue<>()).add(new Food(ratings[i], foods[i]));
cuisineFoodMap.computeIfAbsent(cuisine, k -> new PriorityQueue<>()).add(new Food(rating, food));
}
}

public void changeRating(String food, int newRating) {
// Update food's rating in the 'foodRating' map.
foodRatingMap.put(food, newRating);
// Insert the '(new food rating, food name)' element into the respective cuisine's priority queue.
String cuisineName = foodCuisineMap.get(food);
cuisineFoodMap.get(cuisineName).add(new Food(newRating, food));
String cuisine = foodCuisineMap.get(food);
// rather updating the existing food object we will add another one
cuisineFoodMap.get(cuisine).add(new Food(newRating, food));
}

public String highestRated(String cuisine) {
// Get the highest rated 'food' of 'cuisine'.
Food highestRated = cuisineFoodMap.get(cuisine).peek();
PriorityQueue<Food> foodRatingMaxHeap = cuisineFoodMap.get(cuisine);
Food highestRated = foodRatingMaxHeap.peek();

// If the latest rating of 'food' doesn't match the 'rating' on which it was sorted in the priority queue,
// then we discard this element from the priority queue.
// If the top food rating does not match with the rating from foodRatingMap then it is an old rating
// we can just poll it from the food rating max heap
while (foodRatingMap.get(highestRated.foodName) != highestRated.foodRating) {
cuisineFoodMap.get(cuisine).poll();
highestRated = cuisineFoodMap.get(cuisine).peek();
foodRatingMaxHeap.poll();
highestRated = foodRatingMaxHeap.peek();
}

// Return the name of the highest-rated 'food' of 'cuisine'.
return highestRated.foodName;
}
Expand Down
61 changes: 24 additions & 37 deletions src/com/problems/array/FindPolygonWithTheLargestPerimeter.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,21 @@
*/
// Tags: Arrays, Prefix Sum, Priority Queue, Greedy, Sorting
public class FindPolygonWithTheLargestPerimeter {

public static void main(String[] args) {
type1();
type2();
type3();
}

private static void type3() {
int[] nums = {1, 12, 1, 2, 5, 50, 3};
long ans = largestPerimeter3(nums);
System.out.println(ans);
}

private static long largestPerimeter3(int[] nums) {
return largestPerimeter3(nums, nums.length);
}

static long largestPerimeter3(int[] nums, int end) {
int maxIndex = 0;
long sum = 0;
for (int i = 0; i < end; i++) {
sum += nums[i];
if (nums[i] > nums[maxIndex])
maxIndex = i;
}
if ((sum - nums[maxIndex]) > nums[maxIndex])
return sum;
else {
int temp = nums[maxIndex];
nums[maxIndex] = nums[end - 1];
nums[end - 1] = temp;
if (end < 3)
return -1;
return largestPerimeter3(nums, end - 1);
}

}

// using priority queue
// here we are doing it in a reverse way
// here we will calculate the total sum and using a max heap we are saving all the num
// now we are polling sides from heap one by one
// if we subtract side from the sum we will get sum[a1..ak-1]
// now we will check if sum[a1..ak-1] > ak or not
// if yes then return the sum else remove ak from the sum
// continue this till the side is atleast 3
private static void type2() {
int[] nums = {1, 12, 1, 2, 5, 50, 3};
long ans = largestPerimeter2(nums);
Expand All @@ -65,20 +43,29 @@ private static long largestPerimeter2(int[] nums) {
heap.offer(num);
}
while (heap.size() >= 3) {
int max = heap.poll();
long diff = sum - max;
if (diff > max) {
int side = heap.poll();
long remSum = sum - side;
if (remSum > side) {
return sum;
}
sum = diff;
sum = remSum;
}
return -1;
}

// todo explain this in the interview
// this is a greedy approach
// here we will sort the array first

// so there are 2 conditions
// A polygon is a closed plane figure that has at least 3 sides
// a1 + a2 + a3 + ... + ak-1 > ak
// here we will sort the array first, so lower sides will come first
// so if we just use a loop and iterate over the array and get the side total
// then we can just check sum(a1..ak-1) > a[k] or not
// if yes we can just include the a[k] to our polygon
// and we don't need to check for other combination as a[k] is highest among others
// if sum(a1..ak-1) > a[k] satisfies then sum(a1..ak) > a[i] will be satisfied
// so if total is greater than num then we will think that [a1..ak] will be our current largest polygon
// we will update the total in every iteration
private static void type1() {
int[] nums = {1, 12, 1, 2, 5, 50, 3};
long ans = largestPerimeter1(nums);
Expand Down
4 changes: 3 additions & 1 deletion src/com/problems/dp/DeleteAndEarn.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ private static int deleteAndEarn4(int[] nums) {
return prev;
}

//
// iterative approach
// same as type2, here we will go from 0 to n
// and collect value for (i-1) and (i-2) and compute the value with the same condition
private static void type3() {
int[] nums = {1, 2, 3, 15, 16, 17, 18};
int ans = deleteAndEarn3(nums);
Expand Down

0 comments on commit 239668a

Please sign in to comment.