ARTS Week 17

KeepNewbie_yan
3 min readDec 18, 2021

Algorithm

This week’s LeetCode problem is 121. Best Time to Buy and Sell Stock

You are given an array prices where prices[i] is the price of a given stock on the ith day. You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.

Input: prices = [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.

To achieve the maximum profit, then we have to make the buy price as low as possible and the sell price as high as possible, so when the loop traverses the array, if the current price is lower than the previous minimum buy price, then update the minimum buy price, and then compare if the current profit (current price — previous minimum price) is greater than the maximum profit, then update the maximum profit.

class Solution {
public int maxProfit(int[] prices) {
int maxProfit = 0;
int minPrice = Integer.MAX_VALUE;
for (int i = 0; i < prices.length; i++) {
if (prices[i] < minPrice) {
minPrice = prices[i];
}
if (maxProfit < prices[i] - minPrice) {
maxProfit = prices[i] - minPrice;
}
}
return maxProfit;
}
}

Review

This week’s Review is for the following article: “Open source” is not broken

Many people know what happened recently in log4j. No one contributed to the original author. Whose fault is this? Authors If FOSS (Free and Open Source) is destroyed, then the Internet as we know it today will no longer exist; FOSS is a beautiful system that does not distinguish between national boundaries, races, beliefs and economic backgrounds, and gives everyone the same power .

FOSS was destroyed because it was abused by some users, and FOSS was the victim rather than the culprit in the process. There are several main reasons. The first problem is to maximize profit. To maximize profit, the cost must be minimized. Therefore, the company will not pay for it when adopting FOSS. Although it is unethical, this It’s not illegal. But then again, how should a FOSS be priced? Should a simple CSS library, log library, or even a database use the same pricing or different pricing?

FOSS is not black-and-white. So why are so many people involved in the development of FOSS? The author takes himself as an example, because he likes it, he does not expect to get a monetary return, and seeing people use the software he writes makes him feel Happy. This is the beauty of FOSS.

Tip

The method of using gettimeofday() function and struct timeval to count time in Linux is as follows:

#include <sys/time.h>struct timeval start_timeval;
struct timeval end_timeval;
// Record start time
gettimeofday(&start_timeval, NULL);
// some code you want to get its running time
// Record end time
gettimeofday(&end_timeval, NULL);
// Calculate the time difference, the time unit obtained is ms
float elapsed = (end_timeval.tv_sec - start_timeval.tv_sec) * 1000000.0 + end_timeval.tv_usec-start_timeval.tv_usec;

Share

This week I want to share a useful website: Programming Language and compiler Benchmarks

As the title indicates, this website records and counts the compilation and running time of different languages under same programs. The mainstream languages, including C, C++, Java, JavaScript, Python and so on.

For example, C language, it also compares the performance of the same program under different compilers, including gcc, clang, and so on.

--

--

KeepNewbie_yan

A programmer. Share knowledge of programming, operating system, Linux kernel, and reading, thinking etc. Let us maintain a beginner mend and grow together!