Question 1 Two Sum

The description of the question is in the following link: https://leetcode.com/problems/two-sum/

//My code:

for i in range(len(nums)-1):
for j in range(i+1,len(nums)):
if (nums[i]+nums[j] == target):
return [i,j]

Explanation:

My idea is to search all the pairs and return the index pair whenever I found the pair. So for the first for loop, I start from the very beginning. For the second for loop, I start from the number due to the symmetry of the sum. It could be modified by returning an empty array if there is no such pair.

// The code from the forum:

    def twoSum(self, num, target):
        map = {}
        for i in range(len(num)):
            if num[i] not in map:
                map[target - num[i]] = i + 1
            else:
                return map[num[i]], i + 1

        return -1, -1

This uses the idea called “Hash Table”. It is a dictionary, however, the key of the Hash Table is a function of the input value, and then we save it in the RAM and we are able to obtain it whenever we want.

Back to this example. For each value num[i], we want to know if it corresponds to a specific value in the hash table we construct. For example, for the index 0, the table is empty, thus we definitely want to save the value (target – num[0]) into the dictionary and gives a value for the key as the index. So if for the first time, the element num[i] is in the dictionary, corresponding to the element num[j],  and the index we stored for the (target -num[j]) in the dictionary is j + 1 (as the starting number in python is 0).

Summary:

  1. Going through all the pairs is the basic idea of finding things, however, the enumeration is not the best and the most efficient way to do it, because it may waste some the computational power and does not make full use of the space capacity.
  2. Ideas for the Hash Table in this case:
    1. We have the relation of two things: e.g. a+b = k, k is a constant. or b = f(a)
    2. We want to output the value a and b.
    3. We store the dictionary element {f(a) = b: a}
    4. If the ith element num[i-1] gives the value b we want, then we may output num[i-1] and a for these two pairs.

These are the main explanations for these two ideas. There is definitely more things for the Hash Table to learn.

LeetCode Introduction

最近打算开始系统化地刷一些Leetcode 的面试题,一方面是为了为自己的quantitative trader / Researcher的职业做准备,另一方面也是为了以后能够在复习的时候有一个更加系统化的索引。

很多时候,自己的思路和能力是很有限的,所以我不仅希望提供自己的思路,我也可能会去读一些别人写的code,去分析他们的思路,并且和自己的思路进行比较,然后可以提升自己的分析code的能力以及一些不一样的思路。

我知道很多事情都是道阻且长,除了自己努力没有别的办法,每天都坚持努力,不管怎么样都应该会变得更好。想到什么事情就要去做。不应该为自己的行为去找任何借口。

陆琮然

01/03/2020

UpDate:

在咨询朋友的建议之后,我决定按照算法得类型来集中练习,不再序号顺序做题。主要还是因为很多算法和数据结构都是需要系统性学习和练习的,集中练习可以比较好地记住这些知识。

15/03/2020

索引:

我打算在这里增加一个索引,对任何有需要的朋友,这个可以更加方便地替你找到你想要的类型的问题。如果有leetcode的小伙伴其实可以直接查看leetcode上的tag然后直接搜索对应的问题即可。

26/03/2020

计划

最近思考了一下自己之后的计划。

首先是在语言方面,一方面是要巩固自己的英语,每天坚持看英语新闻,提升自己英语的语感, 另一方面,我觉得是有必要学习一门外语了。我之前一直在法语和日语之间纠结, 现在看来,我觉得好像是法语在欧洲这一块更加实用一些。我打算自己学习法语。

其次是在学术方面。我现在越来越觉得Q quant已经越来越局限。我觉得 p quant才是未来。所以我打算自己学习一下 machine learning, deep learning and reinforcement learning, 以及相关的数学知识, 包括线性代数,优化和数值计算问题。除此之外, 我还打算认真细致地学习数学统计和金融时间序列分析。

但是话又说回来,对于Q quant的部分,也不应该就此完全放弃。我觉得有一些数学工具还是必要的。

另外除了数学, 我觉得有必要看完整本Hull的《期权,期货和其他衍生品》。看完这个会对自己在衍生品方面的认识有所提高。因为我自己最终的目标也是希望自己能成为一个能够做量化交易的人,所以有些必要的数学工具也是要扎扎实实地去补充提高的。

最后,就是自己生活方面的提升。以前自己并没有经常注意这一点,我觉得这个是我自己的失职以及对自己的不负责任。包括的是个人的卫生以及对自己的周围环境的整洁度的注意。以及我觉得真的要一直坚持健身。

所以现在(无工作,自由状态)大致计划是这样的:

每天早上计划八点起床

语言:

  1. 每天早上起来看一篇彭博的文章,然后写下自己的一些思考(用英语),发在网上。
  2. 现阶段,每天一课的你好法语。每天花一些时间背诵一些法语单词。不需要很多,每天先来5个,即使只有5个我觉得坚持下来就会有收获。
  3. 对于法语的话,我觉得一个小时听和读,一个小时写和说会好一些。如果有什么不懂的地方,可以去请教法国的朋友。教材暂时打算用你好法语。

数学:

  1. 每天的重头戏是机器学习,深度学习和增强学习这一块。首先是每天一到两个section的ESL。这个是最基础的机器学习。然后如果有代数方面的问题不会的, 再抽出时间去翻看资料。对于deep learning,这一块的话可以看Ian goodfellow的deep learning. 再然后就是 reinforcement learning,这一块的话,可以详见Sutton 的 reinforcement learning: an introduction.
  2. 其次有一点,就是可能要再强化一下对于线性代数的学习。本科的时候并没有学tensor(张量),但是我认为这个是非常重要的。因为在整个机器学习里面很多高维度的运算都会用到张量这个概念。
  3. 然后就是计算数学和优化问题。这方面比较复杂。我觉得对于整个优化的问题,我应该去看斯坦福的那本书。优化问题应该做到每天都坚持了解学习,因为这个对机器学习来说是一个比较重要的问题。但是数值计算的话,我觉得可以有一个大概的了解,所以我现在的想法就是认真看优化,但是不要太过于深究数字计算。
  4. 其次就是重头戏:金融时间序列分析。这一块的话非常重要,尤其是对想要了解p quant的人来说。
  5. 剩下的话还有一些量化交易的内容,比如quantitative portfolio management之类的书,对于我以后的目标还是有帮助的。这些可能得视我的进度耳边

生活:

  1. 坚持做到每天起床和睡觉前都做好护肤
  2. 每周两次用面膜清洁脸部。
  3. 每两周一次更换床单和被单。
  4. 自己手洗内衣裤和袜子
  5. 坚持每天健身,唯一的休息日只有周三
  6. 每两周用消毒湿巾擦拭卫生间以及自己房间里的柜子等等
  7. 坚持清淡饮食,偶尔可以出门去吃好吃的东西,但是要节制。
  8. 洗干净的衣服和外面回来换洗的脏衣服,分开来悬挂。
  9. 洗衣服的时候要用金纺和滴露的消毒液,要注意卫生。
  10. 偶尔也应该逛街,去买自己喜欢的衣服和裤子。

其他:

  1. 既然自己选择了想要学习摄影,那我觉得我应该争取抽出时间来思考摄影的内涵,以及怎么提高自己。我希望这个能成为我的一个很不错的爱好。
  2. 游戏的话不是不能打,但是我也知道,自己对于游戏已经越来越没有以前的热情了,我不知道现在的自己是不是还会和以前一样沉迷游戏,我估计我是不会了。
  3. 健身是一方面,但是我觉得我现在已经没有时间去提高自己的篮球水平了,一方面是由于时间限制,另一方面也是因为场地限制。所以我现在只希望自己能够提高自己的桌球水平,这样至少在别人问起来的时候,我也能和他们说我桌球打得很好。
  4. 文学方面。我觉得自己应该看一些非学术类的书籍,但是我还没决定好到底要看哪些东西。不过我必须要承认的是文学带给人的是精神上的慰藉与鼓舞。可能先从东野圭吾的《解忧杂货店》开始看起吧。
  5. 我觉得应该不断告诉自己不能急于求成,我希望自己能够安下心来,踏踏实实提高自己。希望自己能够摆脱那份浮躁,做一个更加成熟的人。

 

 

 

这些大致就是我近期的目标和计划。有的时候只有走出学校了,沉下心来看待自己,才会发现自己有那么多的不足之处,以及需要提高的地方,不论是生活上还是学习上。曾经的我是一个习惯被生活拽着走的人,但是现在开始我想要做一个能够掌控生活的人。

希望,也相信自己能够坚持,也希望自己能够做到不忘初心,成为更好的自己。我知道很多时候,自己离做得好还差得很远,但是这个不是借口。种一棵树,最好的时间是十年前,其次是现在;想成为一个更好的人,最好的开始时间是十年前,其次也是现在。的确只有跳出现在生活的循环与桎梏,我才能看清自己到底想要的是什么,到底怎么才能提高。

随笔

想起来尼采曾经说过的一句话,英语大概是这个样子的:

To live is to suffer, and to survive is to find meaning in the suffering.

我承认我从没有找到过真正的自我,一切都似乎是虚无的,即使是外部的支撑,也总是在不经意间分崩离析。寻找意义真的是一件非常困难的事情, 我有时候甚至怀疑自己是不是需要依靠不切实际的信仰来重新找到内在的平静。

活着就是痛苦,是一种找不到解药的痛苦。

随笔

这集vlog我们拍了十年,致最美好的青春

深夜,一如既往得睡不着,熟练得刷着视频网站,想要在这安静的世界里稍微招募一些喧嚣。偶然一刷就看到了这个视频(本文开头链接)。

视频的大致内容是这样的:两个人恩爱了十年,从高中毕业开始,一直到结婚的那一刻。两个人为对方做出的努力和牺牲。视频的最后,他们在蜜月旅行的途中收到了许许多多的陌生人的祝福。听着那一声声殷切热烈的congratulations,即使是平时很少流泪的我也不禁湿润了眼眶。我觉得视频中流露出的点点真情是表达匮乏的我所无法用言语轻易描述的,所以建议点开链接自己细细品味一下个中滋味。

爱情真是一个奇特的东西,因为你不知道它从何而来,也不知道它将往何去。我一直抱持着这样一个观点:真正的爱情绝大多数情况下都在校园中诞生。那时候的人们青涩,懵懂,单纯的喜欢在感情中那个占有绝对的统治力。我并不敢断言除了校园,就没有了真爱;只是当我们解除了社会,不免沾染一些风尘,很快便风干了那份稚嫩。房,车,钱等各种各样的客观因素将我们的视线从纯粹的理想拉回现实。

我个人一直不欣赏这种充满着铜臭味的婚姻。这种纯粹的物质婚姻和野蛮的原始文明中的交合没有任何区别。以前的所谓婚姻,不过是女性提供生育权,换取男性的物质支持以及保护。这是婚姻的雏形。但是现代婚姻之所以建立在自由恋爱之上,不就是为了能够让我们在粗暴原始的婚姻中留下一丝丝文明的烙印么?倘若我们把爱情的那一片给活生生扯去,在我眼里那就是一种背弃文明的恶劣行径。我这里想要强调的是,我并不是说婚姻一定要和物质撇清关系,而是婚姻的目的不能是单纯地为了物质和生育。

诚然,我相信会有人大声斥责,说你这种居庙堂之高的人,怎么可能理解底层人民的想法。我承认我不可能会了解所有人的想法,但是倘若一个人连最基本的物质都无法满足,那ta婚姻,也只是野蛮的,原始的婚姻,而非现代的婚姻。这个是我所不愿意所看见的。但是我没有办法干涉逼人的生活,也只能在这里埋怨几句罢了。

但是话又说转回来,这种真正的爱情,天下又有多少人可以遇见呢?这就好比打德州扑克,总是能看到别人的皇家同花顺,但是自己却从来没有遇见过。真正的爱情也只存在于别人的世界中,仿佛自己与真爱绝缘。每次想到这种事情,我的内心便会有一种淡淡的落寞。

真正的爱情到底会不会来呢?没有人知道。那也无妨,就让我们静静欣赏别人的爱情,嘴角微微上扬就好了。

BABA IS YOU LEVEL: Solitary Island

  1. Stage 00: Poem
    • Push down the line is twice
    • Push violet into the block next to it
    • Push the rose underneath the work baba
    • Push up the word rose
    • Use rose to touch baba to get the win
  2. Stage 01: Float
    • Make the rule” Rock is you “
    • Use rock to touch the flag
  3. Stage 02:Warm River
    • Make the rule” River is hot and float”
    • Go through everything that is floating to touch the flag.
  4. Stage 03: Bridge Building:
    • Push rock to sink one block of water
    • Make rule ” rock is you ” to move the rock
    • Make the rule “rock is push ” again to sink one block of water again
  5. Stage 04: Bridge Building?
    • Use Push to sink one block of water
    • Make the rule “Rock is You”
    • Sink another block with the word”sink” from the sentence “flag is win”
    • Push word “flag” and ” win” to the very right of the place
    • Push is from the sentence “rock is you ” to make the rule “flag is win”
  6. Stage 05:Victory Spring
    • Use two cog to sink the water next to the word”win”
    • push the line  text is float next to the win and push the word”win” back
    • Deactivate the rule” text is float” to clear the paths
    • Make the rule“ baba is w.in”
  7. Stage 06:Research Facility
    • Push the bolt to the line where keke and the word”skull” is.
    • Push the bolt one grid right to make sure it moves rightward.
    • Push the word ” defeat”  leftward to take the place of the word “hot”
    • Then keep tapping space to let the bolt push away the word “skull”
    • Go downward to push out the word “win” and the only thing needs to be noticed is that you should keep waiting until the bolt comes out.
    • Then make the rule “bolt is win”
  8. Stage 07: Assembly Team
    • Push the word “baba” to the most left. Then use the word to make two robots standing on two different lines.
    • Sink one block of water with one robot.
    • Make the rule: baba is you
  9. Stage 08: Tiny Pond
    • Make the rule: key is flag
    • Make the rule: flag is you and open. As one key has already been transferred into a flag. Use on flag to sink the water and use the other flag to push the word “win” out
    • Make the sentence “flag is win ” and “baba is you “
  10. Stage 09:Catch the thief
    • Push down the rule “baba is you”
    • Make rule “robot is baba”
    • Push word “win ” out
    • Make rule “baba is win”
  11. Stage 10:

BABA IS YOU Level : Lake

  1. Stage 01: ICY Water:
    • Push away the word “sink”.
    • Push the word “wall” onto the jelly underneath the flag.
    • 推走单词“sink”
    • 将wall这个单词推到jelly上。这样就可以碰到flag了。
  2. Stage 02:Turns
    • Make the sentence”star is sink and push”. Then push the star onto the skull to eliminate the skull.
    • Push the word “crab” up one grid, push the word”is” under it and push them together to make the sentence”crab is baba is you”. Then the crab outside the wall will become baba and you can control it to touch the flag.
    • 首先组成句子“star is sink and push”。然后把star推到skull上使得skull消失。
    • 其次组成句子:crab is baba is you 。这样墙外的crab变成baba从而可以操纵它去碰旗子。
  3. Stage 03:Affection:
    • Make the sentence “Love is move.” Then wait until the heart is coming out of the grass.
    • 组成句子“love is move ”然后让爱心走出草丛即可。
  4. Stage 04:Pillar Yard
    • Push the pillars to make a queue horizontally next to the star wall. Then push the pillar line into the wall. After that, push the word”pillar” to make the sentence “pillar is you”.
    • 将柱子拍成一排,和旗子在同一水平线。之后将一整串柱子推进去。再然后用”pillar”去组成句子”Pillar is you “
  5. Stage 05:Brick Wall
    • Directly make the sentence: baba is win.
    • 直接组成句子“baba is win”
  6. Stage 06:Lock
    • Push two keys to open two horizontally aligned doors.
    • Use rock to make two sentences: Rock is push and Rock is open.(vertically and horizontally)
    • Push the stone to open the door.
    • 先用钥匙打开两扇门。
    • 用Rock 组成两句句子:Rock is push; Rock is open.
    • 用石头开门。
  7. Stage 07:Novice Locksmith
    • Make” key is push” vertically and ” key is open”horizontally with same “is”.
    • Push the key to open the door. Them make the sentence “door is push” to push the door away to get to the flag.
    • 先用一个“is”组成“key is push ” 和“key is open ”两句话。第一句竖直第二句水平。
    • 然后用钥匙开门再组成一个句子“door is push”。之后再把门推开。
  8. Stage 08:Locked in
    • Push the work “jelly” to make the sentence” wall is jelly”
    • Make the sentence “baba is win”
    • 先推单词“jelly”组成句子“wall is jelly”。
    • 然后组成句子“ baba is win”.
  9. Stage 09:changeless
    • Deactivate the sentence” Rock is rock”(This makes the rock unchangeable).
    • Then make the sentence “Rock is flag”
    • 首先打乱句子”Rock is rock”
    • 然后组成句子“Rock is flag”
  10. Stage 10: Two Doors
    • Make the sentence “Door is you”. Then push the remaining part of the sentence right next to the corresponding part of the sentence” door is you” (is by is and you by push).place Keke one grid left to the word”door”.
    • Push two “is” to the left first and then push the word “push” and ” you “to the left.
    • Then use keke to push the word keke and door to the left. Repeat the procedure with the door as the sentence changes to “Door is you”
    • Stop until “you ” is two blocks away from the word”key”
    • Use keke again to push the word”keke” and “door ” to the left again.
    • This time push the word “is ” of the right sentence to the bottom of the sentence “Door is you ” Then push up one grid and push the is next to word”key”.
    • push sentence “door is you ” one grid down and  push the word you one grid left. This time you become the key.
    • control the key to make the sentence flag is win and touch the flag.
  11. Stage 11: Jelly Throne
    • Generate the sentence “Jelly is baba” with the is from sentence “flag is push”
    • Make the sentence “Flag is win””
    •  组成句子” Jelly is baba”
    • 用墙内的baba 去组成句子“flag is win”
  12. Stage 12:Crab Storage
    • First make the sentence “flag is open” vertically. Then open the door
    • make the sentence “baba is baba”
    • Push the word “crab” to make the sentence”crab is win”
    • 首先组成句子flag is open去用旗子开门
    • 然后组成句子 baba is baba
    • 最后将crab推至顶部组成 crab is win
  13. Stage 13: burglary
    • Make rules” star is open” and ” wall is shut”
    • Use star to open the wall
    • Deactivate the rule “key is defeat”
    • Make the sentence ” key is win”
    • 首先组成句子“star is open” 和“wall is out”
    • 推走单词key 使得“key is defeat”失效
    • 组成句子“key is win”
  14. Extra 1: Submerged ruins
    • Push the rock left to the word”crab”
    • Use the word “rock” and “is ” to make the sentence “Rock is you”
    • Control the Rock to walk into the place guarded by crabs while keep baba outside.
    • Control baba to push the word “crab” in to complete sentences.
    • control rock to touch the flag.
    • 首先将单词Rock推至crab 左边。
    • 组成句子rock is you
    • 操控Rock 和baba使得Rock 走到flag边上,保持baba在外
    • 用在外面的baba把右上角的句子推完整
    • 用rock去触碰旗子
  15. Extra 2:Sunken Temple
    • Push rock and jelly in to finish the sentence.
    • Push the word”rock” to make the sentence rock is you (not vertically, push away baba instead)
    • Make the sentence ” rock is push” and “baba is you ” together with one “is”
    • push rock next to the crab and is next to the rock
    • Push is and rock together to let rock in.
    • make the sentence “rock is you ” again. Then use rock to touch the flag.

 

 

BABA IS YOU Level 0.

第一大关是游戏的最初关,因此也比较简单。

It’s the first stage of the game, hence it is less difficult compared with later stages.

  1.  Stage 01: where do I go ?
    • The key is to push the word “is” or “wall” or “stop” away from the sentence. Then you are not limited by the rule. that you cannot go through the wall. Then go through the wall and make the sentence “flag is win”.
    • 重点在于使得句子:wall is stop失效。然后就可以走过墙去组成句子flag is win.
  2. Stage 02: Now what is this?
    • Similar with the first stage,  the only difference is that you are a wall not a rabbit(?) anymore. Just deactivate the sentence:flag is top. Then walk through the flags and make the sentence “flag is win”.
    • 和第一关类似,使得句子flag is stop失效。之后走过flag组成的墙去拼成句子flag is win即可。
  3. Stage 03:Out of Reach.
    • Use first stone to sink the water and make a path to the right lower corner. Then push the word” rock” downward to make the sentence” rock is win”. Then go back and touch the rock.
    • 首先用一块石头让一片有水的区域消失。 其次走到右下角将单词”rock”往下推组成句子”Rock is win,再回头触碰石头获胜即可。
  4. Stage 04:Out of Reach Again
    • Push up the rock. Then line the rock in the same horizontal line where the word “skull” is. Then push the rock to the word to deactivate the rule “skull is defeat”. Then you can walk through the skull and win easily.
    • 首先将岩石往上推, 之后排成水平的一条线正对着单词”skull”. 之后朝单词方向推石头使得句子失效。之后就可以无障碍地穿过”skull”,获得胜利。
  5. Stage 05:
    • First way to solve it:
      • Push the sentence “baba is you” away horizontally(otherwise you will be dead).Then push the rock out. After that, make the sentence “lava is push”. Then push away the lava to get to the flag.
    • Second way to solve it:
      • Make the sentence ” lava is baba” instead of  “lava is push”. Then you can control all the babas to touch the flag.
    • 第一种解法:
      • 组成句子“lava is push”.
    • 第二种解法:
      • 组成句”lava is baba”
  6. Stage 6: Off limits
    • Firstly, deactivate the rule “wall is stop”. Then push the word ” wall ” away to make the sentence “wall is you “. You can control wall after that, and you can touch the flag with wall to get the win.
    • 首先将句子:“wall is stop”推掉使其失效。然后将单词“wall”推出去组成句子“wall is you”。然后操纵wall取得胜利即可。
  7. Stage 7: Grass Yard.
    • Notice that you are not blocked by the wall, hence you can push out the word flag and win. Use the word “is” twice in the sentence by combing the word”flag”,”is”,”win” vertically.
    • 因为没有被墙给挡住,所以可以将单词“flag”,“win”推出墙外。其次就是利用“is”两次,将句子“flag is win”竖直组成即可。