Contents

Best Album (with.Java)

   Jun 4, 2024     4 min read

This article looks into the “Best Album (with.Java)” issue.

As I solve coding test problems, I look back on the problems I solved and look into different solution methods to learn more.

Let’s look at the problem first.

problem

The streaming site is planning to release a best album by collecting the two most played songs from each genre.

Songs are classified by unique numbers, and the criteria for including songs are as follows.

  • Genres with the most played songs are listed first.
  • The most played songs within the genre are listed first.
  • Among songs with the same number of plays within a genre, songs with lower unique numbers are included first.
  • Given the string array genres representing the genre of the song and the integer array plays representing the number of plays for each song, complete the solution function to return the unique numbers of the songs in the best album in order.

Restrictions

  • genres[i] is the genre of the song with unique number i.
  • plays[i] is the number of times the song with unique number i was played.
  • Genres and plays have the same length, which must be between 1 and 10,000.
  • There are less than 100 genres.
  • If there is only one song in the genre, select only one song.
  • Every genre has a different number of plays.

Input/Output Example

genresplaysreturn
[“classic”, “pop”, “classic”, “classic”, “pop”][500, 600, 150, 800, 2500][4, 1, 3, 0]

My solution to the problem

import java.util.*;

class Solution {
 public int[] solution(String[] genres, int[] plays) {
 HashMap<String, Integer> map = new HashMap<>();
 for(int i = 0; i < genres.length; i++){
 map.put(genres[i], map.getOrDefault(genres[i], 0) + plays[i]);
 }

 ArrayList<String> arrStringList = new ArrayList<>();
 for(String key : map.keySet()){
 arrStringList.add(key);
 }
 Collections.sort(arrStringList, (key_1, key_2) -> map.get(key_2) - map.get(key_1));

 ArrayList<Integer> arrIntegerList = new ArrayList<>();
 for(int i = 0; i < arrStringList.size(); i++){
 String str = arrStringList.get(i);
 int max = 0;
 int idx_0 = -1;
 for(int j = 0; j < genres.length; j++){
 if(str.equals(genres[j]) && max < plays[j]){
 max = plays[j];
 idx_0 = j;
 }
 }

 max = 0;
 int idx_1 = -1;
 for(int j = 0; j < genres.length; j++){
 if(str.equals(genres[j]) && max < plays[j] && idx_0 != j){
 max = plays[j];
 idx_1 = j;
 }
 }

 arrIntegerList.add(idx_0);
 if(idx_1 > -1){
 arrIntegerList.add(idx_1);
 }
 }

 int[] answer = new int[arrIntegerList.size()];
 for(int i = 0; i < arrIntegerList.size(); i++){
 answer[i] = arrIntegerList.get(i);
 }
 return answer;
 }
}

Solved review

The solution method takes as input two arrays: genres and plays.

The first for loop uses a HashMap<String, Integer> object map to calculate the total number of plays for each genre. Here, key represents the genre and value represents the total number of plays of that genre.

In the second for loop, we add the genres stored in the map to the ArrayList to sort them in descending order according to the number of plays.

For descending sorting, use Collections.sort() and set the sorting criteria using a lambda expression.

The third for loop finds the indices of the most played song and the next most played song for each genre. To do this, we find the index of the most played song and the next most played song for each genre.

Add the index of the most played song for each genre to ArrayList, and also add the index of the next most played song.

Finally, we convert ArrayList to an array and return the result. This code solves the problem of finding and returning the indices of the most played song and the next most played song for each genre.