0%

Ranking: 1197 / 15151 ๐๐๐ญ

# Q1. 1518. Water Bottles

Given numBottles full water bottles, you can exchange numExchange empty water bottles for one full water bottle.

The operation of drinking a full water bottle turns it into an empty bottle.

Return the maximum number of water bottles you can drink

Ranking: 606 / 8175 ๐๐๐ญ

# Q1. 5177. Reformat Date

Given a date string in the form Day Month Year, where:

Day is in the set {โ1stโ, โ2ndโ, โ3rdโ, โ4thโ, โฆ, โ30thโ, โ31stโ}.
Month is in the set {โJanโ, โFebโ, โMarโ, โAprโ, โMayโ, โJunโ, โJulโ, โAugโ, โSepโ, โOctโ, โNovโ, โDecโ}.
Year is in the range [1900, 2100].
Convert the date string to the format YYYY-MM-DD, where:

YYYY denotes the 4 digit year.
MM denotes the 2 digit month.
DD denotes the 2 digit day.

## Note

Very straight forward. Just do the string conversion.

Ranking: 133 / 14301 ๐๐๐

# Q1. 1502. Can Make Arithmetic Progression From Sequence

Given an array of numbers arr. A sequence of numbers is called an arithmetic progression if the difference between any two consecutive elements is the same.

Return true if the array can be rearranged to form an arithmetic progression, otherwise, return false.

## Note

Was not clear about the description at the very beginning.
The question is easy after fully understand the description.

# Pre

Before we try to convert any recursive algo into non-recursive, we should be familiar to recursive and non-recursive version of binary tree traversal.
Read binary tree traversal to recap.

# Key points

1. The idea is use `stack` in heap to mimic the behavior of function call stack.
2. For post-order traversal, since we need to update parentโs state from child node, we will need to push state of parent along with child node into stack. We will explain this later with example.

# Post order

## Code skeleton

1. Lowest Common Ancestor of a Binary Tree
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.

According to the definition of LCA on Wikipedia: โThe lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).โ

Given the following binary tree: root = [3,5,1,6,2,0,8,null,null,7,4]

# Leetcode

Ranking: 429 / 13808 ๐

# Q1. 1486. XOR Operation in an Array

Given an integer n and an integer start.
Define an array nums where nums[i] = start + 2*i (0-indexed) and n == nums.length.
Return the bitwise XOR of all elements of nums.

Very straight forward.

Ranking: 678 / 8571

# Q1. 1475. Final Prices With a Special Discount in a Shop

Given the array prices where prices[i] is the price of the ith item in a shop. There is a special discount for items in the shop, if you buy the ith item, then you will receive a discount equivalent to prices[j] where j is the minimum index such that j > i and prices[j] <= prices[i], otherwise, you will not receive any discount at all.
Return an array where the ith element is the final price you will pay for the ith item of the shop considering the special discount.

Solved it by using brute force in contest. O(N^2)

## Intuition

1. Subtract each value by next smaller number
2. Equivalent to subtract each value from all its previous greater number
3. Monotonic stack !!!

Ranking: 2148 / 13794 ๐ญ

# Q1. 5453. Running Sum of 1d Array

Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]โฆnums[i]).
Return the running sum of nums.

Equivalent to compute prefix-sum.

# What can Kafka do?

• Kafka as a Messaging System
• Combination of two traditional queuing systems
• `queuing`
Scalable but not extendable. Data is gone after consumed
• `publish-subscribe`
Not scalable since every subscriber will receive a copy of new messages
• Provide string order guarantee in a partition
YES, no global ordering across multiple partitions in a topic
• Kafka as a Storage System
• Data is written on disk and replicated for fault-tolerance
• Provide write on acknowledgement
• Perform the same whether the data is 50KB or 50TB
• Kafka for a Stream Processing
• Handling out-of-order data
• Use `event time` rather than `processing time` to provide a deterministic result
• Performing stateful computations
• uses Kafka for stateful storage
• Reprocessing input as code changes

The following syntax looks very handy in many case.

I can understand what does those code try to do easily. Yeah, this is the magic of Python. :-)
But I had a hard time to find the semantic explanation for the syntax.

I was thinking about

1. `Arbitrary Argument Lists`
But this is not function signature. And `Arbitrary Argument Lists` will construct `*args` as a `tuple` rather than a `list`.

2. `Unpacking Argument Lists`
As the name suggested, this syntax unpacking a list or tuple rather than create a new list.