Leetcode Biweekly 172 - 3/4 problems solved in 18 minutes

Leetcode Biweekly 172 - solved 𝟑/𝟒 problems. I started 𝟐𝟎 𝐦𝐢𝐧𝐮𝐭𝐞𝐬 𝐥𝐚𝐭𝐞 due to the server and network error, still manage to solve 3/4 problems 𝐰𝐢𝐭𝐡𝐢𝐧 𝟑𝟖 𝐦𝐢𝐧𝐮𝐭𝐞𝐬 after start. Here is my solution: 1️⃣ Create a map to count the frequency of each element and create a multiset to store the current highest frequency element. Now we will traverse our vector, since we have to remove the first 3 elements so at every i % 3 == 0, we will check if the highest frequency == 1 means all elements are distinct and we can break else increment ans and we will erase the current element's frequency from multiset, decrement it in the map, and reinsert its updated frequency into the multiset. 2️⃣ A cakewalk number theory problem for a CF solver. As we know, the sum of three elements will be divisible by 3 if sum of their modulo with 3 is divisible by 3. So, we need to create three vectors which store elements with modulo 0, 1, and 2 respectively and sort them in descending order, and try these combinations - three 0s, three 1s, three 2s, and one 0, one 1, and one 2 modulo element and take max of them as ans. 3️⃣ The main observation is that we can't move '1' towards the right and can only move it towards the left, so it will be optimal if '1' will be in front of the current largest number of the vector towards the left side. To keep track of it, we will use a priority queue(pq). We will traverse the string and push corresponding vector elements into the pq, and whenever we have '1' in the string, we will add the top element of the pq to ans and pop it. Although my yesterday Codeforces round was not gone well, but 𝐭𝐡𝐞 𝐛𝐞𝐬𝐭 𝐩𝐚𝐫𝐭 𝐚𝐛𝐨𝐮𝐭 𝐜𝐨𝐦𝐩𝐞𝐭𝐢𝐭𝐢𝐯𝐞 𝐩𝐫𝐨𝐠𝐫𝐚𝐦𝐦𝐢𝐧𝐠 𝐢𝐬 𝐭𝐡𝐚𝐭 𝐞𝐯𝐞𝐫𝐲 𝐜𝐨𝐧𝐭𝐞𝐬𝐭 𝐠𝐢𝐯𝐞𝐬 𝐲𝐨𝐮 𝐚 𝐟𝐫𝐞𝐬𝐡 𝐬𝐭𝐚𝐫𝐭 𝐚𝐧𝐝 𝐚 𝐧𝐞𝐰 𝐜𝐡𝐚𝐧𝐜𝐞 𝐭𝐨 𝐜𝐨𝐦𝐞 𝐛𝐚𝐜𝐤. #coding #leetcode #competitiveprogramming

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories