Saturday, September 28, 2019

Leetcode solution 1176: Diet Plan Performance

Problem Statement 

A dieter consumes calories[i] calories on the i-th day. 
Given an integer k, for every consecutive sequence of k days (calories[i], calories[i+1], ..., calories[i+k-1] for all 0 <= i <= n-k), they look at T, the total calories consumed during that sequence of k days (calories[i] + calories[i+1] + ... + calories[i+k-1]):
  • If T < lower, they performed poorly on their diet and lose 1 point; 
  • If T > upper, they performed well on their diet and gain 1 point;
  • Otherwise, they performed normally and there is no change in points.
Initially, the dieter has zero points. Return the total number of points the dieter has after dieting for calories.length days.
Note that the total points can be negative.

Example 1:
Input: calories = [1,2,3,4,5], k = 1, lower = 3, upper = 3
Output: 0
Explanation: Since k = 1, we consider each element of the array separately and compare it to lower and upper.
calories[0] and calories[1] are less than lower so 2 points are lost.
calories[3] and calories[4] are greater than upper so 2 points are gained.
Example 2:
Input: calories = [3,2], k = 2, lower = 0, upper = 1
Output: 1
Explanation: Since k = 2, we consider subarrays of length 2.
calories[0] + calories[1] > upper so 1 point is gained.
Example 3:
Input: calories = [6,5,0,0], k = 2, lower = 1, upper = 5
Output: 0
Explanation:
calories[0] + calories[1] > upper so 1 point is gained.
lower <= calories[1] + calories[2] <= upper so no change in points.
calories[2] + calories[3] < lower so 1 point is lost.

Constraints:
  • 1 <= k <= calories.length <= 10^5
  • 0 <= calories[i] <= 20000
  • 0 <= lower <= upper
Problem link

 

Video Tutorial

You can find the detailed video tutorial here

Thought Process

This is an easy problem but might be a bit hard to code it up correctly in one pass. We can use a sliding window to keep a rolling sum and compare it with upper and lower bound.

A few caveats in implementation:
  • We can simplify using two pointers start and end by keeping an index i (start) and i - k (end)
  • We can only have one loop instead of having two separate loops
sliding window

Solutions

Sliding window


Time Complexity: O(N) visited the array once
Space Complexity: O(1) No extra space is needed

References

  • None

Saturday, September 21, 2019

Leetcode solution 1169: Invalid Transactions

Problem Statement 

A transaction is possibly invalid if:
  • the amount exceeds $1000, or;
  • if it occurs within (and including) 60 minutes of another transaction with the same name in a different city.
Each transaction string transactions[i] consists of comma separated values representing the name, time (in minutes), amount, and city of the transaction.
Given a list of transactions, return a list of transactions that are possibly invalid.  You may return the answer in any order.

Example 1:
Input: transactions = ["alice,20,800,mtv","alice,50,100,beijing"]
Output: ["alice,20,800,mtv","alice,50,100,beijing"]
Explanation: The first transaction is invalid because the second transaction occurs within a difference of 60 minutes, have the same name and is in a different city. Similarly the second one is invalid too.
Example 2:
Input: transactions = ["alice,20,800,mtv","alice,50,1200,mtv"]
Output: ["alice,50,1200,mtv"]
Example 3:
Input: transactions = ["alice,20,800,mtv","bob,50,1200,mtv"]
Output: ["bob,50,1200,mtv"]

Constraints:
  • transactions.length <= 1000
  • Each transactions[i] takes the form "{name},{time},{amount},{city}"
  • Each {name} and {city} consist of lowercase English letters, and have lengths between 1 and 10.
  • Each {time} consist of digits, and represent an integer between 0 and 1000.
  • Each {amount} consist of digits, and represent an integer between 0 and 2000.

Problem link

 

Video Tutorial

You can find the detailed video tutorial here

Thought Process

It's an implementation problem and it's up to you how fast you can type to determine whether you want to flex your muscles on object oriented design. It's actually quite a small moev, whether you want to convert the comma separated line into a Transaction object or not.

To meet the two conditions, we can simply loop over all the transactions while at the same time, keep a map to store all the transactions under the same name. Then the problem is compare the city for all the transactions under the same name that time is less or equal than 60 mins. At first I was trying to sort the collection based on the time in ascending order. However, that does not really help reduce the O(N^2) complexity since worst case is still all transactions are within the 60 mins bound, and one "bad" transaction can essentially cause all the previous "good" transactions to become bad, so we have to go back and revisit the previous ones. (as shown in below graph)

Example on one transaction make all previous ones invalid


Caveats
  • It's better to use a Set for de-duplication than a list since we might add duplicate transactions to the collection (e.g., a transaction > 1000 amount could also be invalid due to same name with different city under time < 60min)


Solutions


Time Complexity: O(N^2),we have to loop through the transactions in a nested way for worst case all transactions are under the same name and all within 60min.
Space Complexity: O(N),the result list and set and the extra hashmap we used

References

  • None