July 24, 2025 - Daily Drill¶
🎯 Daily Goals¶
- Review Anki Deck
- Lumosity training
- Leetcode
- Developing laplace
- Langchain Learning
📝 What I learned:¶
Leetcode Problem¶
Thinking Process¶
The key to the problem is to figure out what does rotate k
steps mean. Take an example, given a linked list with length of n
rotate k
would mean making the n - (k % n)
th (0 based index) node as the new head and n - (k % n) - 1
th node the new tail. Now the question can be broken into two parts. First closed the linked list and get the n
. Then move to the n - (k % n) - 1
th node, break the link and return the new head.
Complexity Analysis¶
Time Complexity: O(n) Space Complexity: O(1)
Key Takeaways¶
- question like this is all about translation. Rotate
k
-> findn - (k % n) - 1
th node
3623. Count Number of Trapezoids I
Thinking Process¶
First we need to think of how we can get a unique horizontal trapezoid. There are many points on the plane some of them fall on the same horizontal line. We need to pick two different horizontal lines and pick two different points on that line. Four points picked like this would form a unique horizontal trapezoid. Thus the information that is useful to us are the following: the number of points on each horizontal line, the number of combination of picking two different points from each horizontal line, and finally the combination of four points from two different lines. We are given that there are a points lying on the line y = 1, which form x unique pairs. Similarly, there are b points on the line y = 3 forming y pairs, and c points on y = 5 forming z pairs. Then the total number of combination would be xy + xz + yz. If we calculate this using nested for loops, in the worst case, we need O(n2) time. To avoid this we can calculate the (x + y + z)2 and subtract it with x2 + y2 + z2 then divide it by 2. This would only cost liner time in worst case.
Complexity Analysis¶
Time Complexity: O(n) Space Complexity: O(n)
Langchain¶
Runnable¶
Runnable interface is one of the most important interface in langchain since defines the foundation of how to work with LangChain components. Language model, output parser, retriever and compiled LangGraph all implements this interface.
A Runnable component can be
- Invoked: A single input is transformed into an output.
- Batched: Multiple inputs are efficiently transformed into outputs.
- Streamed: Outputs are streamed as they are produced.
- Inspected: Schematic information about Runnable's input, output, and configuration can be accessed.
- Composed: Multiple Runnables can be composed to work together using the LangChain Expression Language (LCEL) to create complex pipelines.
`` note "batch mode"
The batch mode of Runnable is achieved through
threading` module thus it is not actually concurrent due to the GIL. However, this is still useful since the calls to LLM are I/O bound, and the thread would be put on hold if it hits an I/O operation.
LCEL¶
To understand runnable we need to first understand how langchain expression language work. It is a new language designed to better manage Runnables. It takes an declarative approach to building new Runnable from existing ones.
Here is a cheatsheet. From it you can tell what are some common interaction with runnables using LCEL.
When to use LCEL? Here is the guideline:
- If you are making a single LLM call, you don't need LCEL; instead call the underlying chat model directly.
- If you have a simple chain (e.g., prompt + llm + parser, simple retrieval set up etc.), LCEL is a reasonable fit, - if you're taking advantage of the LCEL benefits.
- If you're building a complex chain (e.g., with branching, cycles, multiple agents, etc.) use LangGraph instead. Remember that you can always use LCEL within individual nodes in LangGraph.
Composition Syntax:
The two primitive of LCEL is RunnableSequence
and RunnableParallel
. From the name you can tell that one specifies the runnable to run sequentially and the other in parallel.
You can create them using class constructor or use their shorthand.
chain = runnable1 | runnable2
is equivalent to chain = RunnableSequence([runnable1, runnable2])
Inside an LCEL expression, a dictionary is automatically converted to a RunnableParallel
.
The above is converted to
chain = RunnableSequence([RunnableParallel(mapping), runnable3])
Inside an LCEL expression, a function is automatically converted to a RunnableLambda
.
The above is converted to
chain = RunnableSequence([RunnableLambda(some_func), runnable1])