Posts

Showing posts from January, 2023

Sorting, and why the numbers are distinct!

Image
The description of problems always brings important information - it is rare that an information inside the problem description doesn't add any value. For instance, in this problem all the scores are unique. This is done so that you can create a unique mapping score --> row which can be used to reconstruct the matrix after the sorting is completed. Hence you can complete the task in O(row*col + row*log(row))-time and O(row*col)-space. Code is down below, cheers, ACC. Sort the Students by Their Kth Score - LeetCode There is a class with  m  students and  n  exams. You are given a  0-indexed   m x n  integer matrix  score , where each row represents one student and  score[i][j]  denotes the score the  i th  student got in the  j th  exam. The matrix  score  contains  distinct  integers only. You are also given an integer  k . Sort the students (i.e., the rows of the matrix) by their scores in...

Standard Priority Queue

Image
A problem requiring a standard priority queue solution which can be solved in nlogn. Code is down below, cheers, ACC. Maximal Score After Applying K Operations - LeetCode 2530. Maximal Score After Applying K Operations Medium 223 5 Add to List Share You are given a  0-indexed  integer array  nums  and an integer  k . You have a  starting score  of  0 . In one  operation : choose an index  i  such that  0 <= i < nums.length , increase your  score  by  nums[i] , and replace  nums[i]  with  ceil(nums[i] / 3) . Return  the maximum possible  score  you can attain after applying  exactly   k   operations . The ceiling function  ceil(val)  is the least integer greater than or equal to  val .   Example 1: Input: nums = [10,10,10,10,10], k = 5 Output: 50 Explanation: Apply the operation to each array element exactly once. The final score is 10 ...

Queue using List II

Image
Another problem where you can implement a queue-like structure using a simple LinkedList. In this example, we keep track of the last K elements in a queue-implemented-as-linkedList, and a count of the instances of value. Code is down below, cheers, ACC. Find Consecutive Integers from a Data Stream - LeetCode 2526. Find Consecutive Integers from a Data Stream Medium 40 0 Add to List Share For a stream of integers, implement a data structure that checks if the last  k  integers parsed in the stream are  equal  to  value . Implement the  DataStream  class: DataStream(int value, int k)  Initializes the object with an empty integer stream and the two integers  value  and  k . boolean consec(int num)  Adds  num  to the stream of integers. Returns  true  if the last  k  integers are equal to  value , and  false  otherwise. If there are less than  k  integers, the condition do...