2130. Maximum Twin Sum of a Linked List
Leetcode Link
This problem can be approached using the reverse linked list technique.If you haven’t solved Leetcode 206 before, I recommend doing that first to get familiar with how to reverse a singly linked list.
Input: head = [4,2,2,3]Output: 7
Since the twin sum is defined as summing the i-th node from the start with the i-th node from the end (for a list of even length), we can reverse the linked list to simulate accessing nodes from the back.
Original: [4, 2, 2, 3]Reversed: [3, 2, 2 ...
739. Daily Temperatures
Leetcode Link
A brute-force solution would use two nested loops to compare each day’s temperature with the upcoming days, resulting in O(n²) time complexity, which is inefficient for large inputs.
To optimize this, we can use a monotonic stack to store temperature information, reducing the time complexity to O(n).
Input: temperatures = [73,74,75,71,69,72,76,73]Output: [1,1,4,2,1,1,0,0]
Step-by-step Stack Simulation:We first initialize a result array of the same length, filled with 0s:
res ...
Leetcode 714. Best Time to Buy and Sell Stock with Transaction Fee
Leetcode Link
ConceptEvery day, we have two choices: either hold a stock or sell a stock.
Holding a stock means we either bought it on a previous day or purchased it today.Selling a stock means we either still have cash from previous transactions or we sold a stock today.To solve this problem efficiently, we can use dynamic programming.
Understanding the ProblemGiven an array prices where prices[i] represents the stock price on the ith day, and an integer fee representing the transaction fee, ...
Leetcode 216 Combination Sum III
Leetcode Link
AnalysisIf we start with ( k = 3 ) and ( n = 9 ), we need to find all possible combinations of three distinct numbers from ( 1 ) to ( 9 ) that sum up to ( 9 ).
For example, the valid combinations are:
([1, 2, 6]) → sum = 9 ✅
([1, 3, 5]) → sum = 9 ✅
([2, 3, 4]) → sum = 9 ✅
This problem is similar to the subsets problem, where we explore different combinations by picking each number one at a time. The key difference is that we need to ensure the s ...
Leetcode 62 Unique Paths
Leetcode Link
To understand this problem, let’s begin by considering a simple example: a 2x2 maze. In this maze, you can only move right or down. To reach the bottom-right corner ([1, 1]), there are two possible paths:
Right → Down
Down → Right
Thus, we can conclude that for a 2x2 maze, there are 2 unique paths to reach the bottom-right corner.
Extending to Larger GridsLet’s consider a larger grid, such as a 3x7 maze. We can construct a matrix to represent the number of ways to reach each ...
Leetcode 735 Asteroid Collision
Leetcode Link
We can use a stack to solve this problem.
From the problem description, the planets move at the same speed, so we only need to consider their direction and size.Whether two planets will collide and the result of the collision can be divided into three cases:
asteroids = [5, 10] or asteroids = [-5, -10]
Both planets move in the same direction, so no collision occurs.
asteroids = [-5, 10]
The two planets move in different directions but will not collide.
asteroids = ...
Cloud Summit 2024 Participation Experience
OverviewAttending Cloud Summit for the first time allowed me to gain a deeper understanding of cloud technologies. I am quite happy to see that the industry has many new tools and technologies that simplify complex tasks, continuously pushing the capabilities of Taiwan’s software industry.
Of course, a speaker who discussed the double-edged nature of the Cloud, highlighting both its advantages and disadvantages. In this era of AI, technological iterations are happening rapidly, but the unchang ...
Two Ways to Set Up a Firewall in Linux
Firewall ConfigurationOn Ubuntu Linux, you can use ufw or iptables for firewall configuration. Here are the commands and some pitfalls I encountered along the way. 🤣
UFW
View firewall settings sudo ufw status
Add firewall rules # allow sshsudo ufw allow ssh# allow specific ip to specific portsudo ufw allow from 123.123.123.123 to any port 22/tcp
List firewall rules with numbers sudo ufw status numbered
Example root@localhost:~# sudo ufw status numberedStatus: active To ...
The maps below show an industrial area in the town of Norbiton, and planned future development of the site.
The two maps illustrate the variation in Norbiton town, showing the current industrial usage and the proposed future refurbishment of the area.
Overall, it is evident that there will be more accommodations and numerous facilities replacing the factories in the town.
Upon closer examination of the maps, many factories are situated along the routes, and there are still significant unused spaces. These factories will be replaced by a plethora of new apartments or flats to accommodate the rising p ...
It is important for people to take risks, both in their professional lives and their personal lives.
Do you think the advantages of taking risks outweigh the disadvantages?
Risk evaluation is a crucial aspect of decision-making, as uncertainty about the future often leads to feelings of unease and confusion. While some people argue taking risks could have adverse effects on one’s life, others contend that the benefits outweigh the drawbacks. In this essay, I am favor of risk-taking and I will illustrate its pros outweigh its drawbacks.
To begin with, taking risks can significantly impact one’ ...