ARTS 第 41 周

每周完成一个 ARTS:
Algorithm: 每周至少做一个 LeetCode 的算法题
Review: 阅读并点评至少一篇英文技术文章
Tips: 学习至少一个技术技巧
Share: 分享一篇有观点和思考的技术文章


Contents


Algorithm

409. Longest Palindrome

题目:409. Longest Palindrome

难度:Easy

题意:Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.

This is case sensitive, for example “Aa” is not considered a palindrome here.

Note: Assume the length of given string will not exceed 1,010.

示例:

Input:
"abccccdd"

Output:
7

Explanation:
One longest palindrome that can be built is "dccaccd", whose length is 7.

解法:

这道题目其实思路比较直接,但参考答案中有一个 tricky 的地方,容我细细道来。首先,我们肯定要统计字符串中每个字符出现的次数,这个简单,使用字典即可,接下来就是对每个字符进行选择了:如果这个字符出现的次数是偶数,那么可以全部拿来组成回文数,如果出现的次数是奇数,那么只能取 次数 - 1 个组成回文数。这种操作,想一想我们如果使用地板除法再乘 2,就可以完成。举个例子:s = aaaaa,那么 len(s) // 2 * 2 就是 s 中能拿出来组成回文数的字符的个数(不愧是出现在参考答案中的操作😂)。最后,因为我们不管最后组成的回文数是什么样子,只要统计次数,那么如果有出现次数为奇数的字符,则只要最后的答案加上 1 即可,而且算上这个 1 之后,就不能再加 1 了,所以对其进行限制,在判断条件中加上计算结果应为偶数。

代码:

class Solution:
    def longestPalindrome(self, s: str) -> int:
        dict1 = dict()
        res = 0

        for i in s:
            if i in dict1:
                dict1[i] += 1
            else:
                dict1[i] = 1

        for i in dict1:
            res += dict1[i] // 2 * 2
            if dict1[i] % 2 == 1 and res % 2 == 0:
                res += 1

        return res

时间复杂度 O(N),空间复杂度 O(N)。


Review

19 Lessons from 2019

首先声明一下,这周的文章选文不佳,读完以后我感觉这是一篇没有什么营养的鸡汤文(是的,歪果仁也喜欢读鸡汤文),并且作者喜欢用长句和俚语,可能这是作者的写作风格,但对我们来说却很难读,所以这篇翻译也仅供参考。下次尽量不选择这类文章。

这周选文来自 Medium 上的「P.S. I Love You」专栏,《19 Lessons from 2019》。这篇文章讲的是作者自己从 2019 年中学到的事情,关于职场和生活。作者倡导用积极和平和的态度来接纳生活和职场中的不如意,并给出了一些个人的建议。


Tips

关于 stats::arima()forecast::Arima() 的新息方差和 AIC 计算结果的解释

在时间序列学习的过程中,我发现多次在使用 stats::arima() 函数和 forecast::Arima() 函数对同一个时间序列、同样阶次进行建模时,虽然会给出相同的参数计算结果,但最终的 新息方差 $\sigma^2$赤池信息量 AIC 的计算结果是不一样的,于是在 Cross Validated 发帖求助,居然得到了大神 Rob Hydnman 教授的回答。原贴地址可以点击这个链接

Hyndman 教授的解释如下:

对于新息方差 $\sigma^2$:

这样就导致了新息方差的不一致。

关于 AIC:

由 AIC 的定义有:$\text{AIC}(k) = -2 \ln (L) + 2k$,其中 $L$ 为时序 ${x_t}$ 的似然函数,$k$ 为参数个数。

这样 forecast::Arima() 的参数总是比 stats::arima() 要多出一个,forecast::Arima() 的 $k$ 总是比 stats::arima() 的 $k$ 大 1,乘以 2 这个系数之后自然 AIC 就会比 stats::arima() 的 AIC 大 2。

那么哪种方式比较好呢?根据 Burnham and Anderson (Springer, 2002),书中建议要将新息方差作为参数考虑进去,就像 forecast::Arima() 中所执行的。


Share

让预测变得更容易或者更准确的五件重要的事

本周分享一个在一档由 Monash Bussiness School 主持的播客 Thought Capital 中,Rob Ryndman 教授提出的关于预测的的一些观点。可以在这个链接查看播客的 scripts。

下面是 Hydnman 教授的观点(没错我现在是他粉丝了,很难想象他曾经是耶稣弟兄会 Christadelphian 的活跃成员,帮助建立了非常多的教堂,但之后他觉得缺少神存在的确定性证据,于是公开决定退出,还写了本书《Unbelievable》来佐证他的观点,太硬核了 😂人真是复杂的动物)。

I’ve identified five things that are important for something to be easy to forecast, or for forecasts to be good:

  1. The first is you need to have a very good understanding of the factors that contribute to that variable that you’re trying to forecast.
  2. Secondly, there should be lots of data available.
  3. Thirdly, the forecast shouldn’t affect the thing you’re trying to forecast.
  4. Four, they should be a relatively low natural, unexplainable random variation.
  5. And fifth, the future should be somehow similar to the past.

I’ll give you two examples of extremes, so take forecasting the sunrise tomorrow. You probably don’t even think of it as a forecast because it’s so accurate and so predictable that we just take it for granted, but that’s actually something that gets forecast. And it’s forecast very, very well because we have a very good understanding of the factors that contribute to it. There’s lots of data available going back millennia, the forecast can’t affect the thing you’re trying to forecast. My forecast of the sunrise is not going to change what time the sun comes up. There’s very low natural, unexplained, random variation and the future is very similar to the past.

Something that’s quite difficult to forecast, a stock process. We don’t have a good understanding of the factors that contribute to them. There is lots of data available, but sometimes the data is not so relevant to the problem that we’ve got at hand. The forecast can affect the thing you’re trying to forecast. If I forecast the price of Google will rise tomorrow and I’m a well known forecaster, then that can actually affect the forecast. There’s quite a lot of unexplainable random variation and the future could be very different from the past. If the stock that I’m trying to forecast, if something happens in that company, the CEO dies, there’s some unexplained increase in their quarterly earnings, and then the future is very different from the past. So if you get those five things, or at least most of those five things, then the thing that you’re trying to forecast is not too difficult.