【leetcode】Best Time to Buy and Sell Stock IV

地址

https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/

题目

Say you have an array for which the i-th element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete at most k transactions.

Note: You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

Example 1:

1
2
3
Input: [2,4,1], k = 2
Output: 2
Explanation: Buy on day 1 (price = 2) and sell on day 2 (price = 4), profit = 4-2 = 2.

Example 2:

1
2
3
4
Input: [3,2,6,5,0,3], k = 2
Output: 7
Explanation: Buy on day 2 (price = 2) and sell on day 3 (price = 6), profit = 6-2 = 4.
Then buy on day 5 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.
阅读全文

【leetcode】 Maximum Gap

地址

https://leetcode.com/problems/maximum-gap/

题目

Given an unsorted array, find the maximum difference between the successive elements in its sorted form.

Return 0 if the array contains less than 2 elements.

Example 1:

1
2
3
4
Input: [3,6,9,1]
Output: 3
Explanation: The sorted form of the array is [1,3,6,9], either
(3,6) or (6,9) has the maximum difference 3.

Example 2:

1
2
3
Input: [10]
Output: 0
Explanation: The array contains less than 2 elements, therefore return 0.

Note:

  • You may assume all elements in the array are non-negative integers and fit in the 32-bit signed integer range.
  • Try to solve it in linear time/space.
阅读全文

【leetcode】Find Minimum in Rotated Sorted Array II

地址

https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/

题目

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.

(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).

Find the minimum element.

The array may contain duplicates.

Example 1:

1
2
Input: [1,3,5]
Output: 1

Example 2:

1
2
Input: [2,2,2,0,1]
Output: 0

Note:

阅读全文

【leetcode】Max Points on a Line

地址

https://leetcode.com/problems/max-points-on-a-line/

题目

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.

Example 1:

1
2
3
4
5
6
7
8
9
10
Input: [[1,1],[2,2],[3,3]]
Output: 3
Explanation:
^
|
| o
| o
| o
+------------->
0 1 2 3 4

Example 2:

1
2
3
4
5
6
7
8
9
10
11
Input: [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
Output: 4
Explanation:
^
|
| o
| o o
| o
| o o
+------------------->
0 1 2 3 4 5 6

NOTE: input types have been changed on April 15, 2019. Please reset to default code definition to get new method signature.

阅读全文

【leetcode】Dungeon Game

地址

https://leetcode.com/problems/dungeon-game/

题目

The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M x N rooms laid out in a 2D grid. Our valiant knight (K) was initially positioned in the top-left room and must fight his way through the dungeon to rescue the princess.

The knight has an initial health point represented by a positive integer. If at any point his health point drops to 0 or below, he dies immediately.

Some of the rooms are guarded by demons, so the knight loses health (negative integers) upon entering these rooms; other rooms are either empty (0's) or contain magic orbs that increase the knight's health (positive integers).

In order to reach the princess as quickly as possible, the knight decides to move only rightward or downward in each step.

Write a function to determine the knight's minimum initial health so that he is able to rescue the princess.

For example, given the dungeon below, the initial health of the knight must be at least 7 if he follows the optimal path RIGHT-> RIGHT -> DOWN -> DOWN.

-2 (K) -3 3
-5 -10 1
10 30 -5 (P)

Note:

  • The knight's health has no upper bound.
  • Any room can contain threats or power-ups, even the first room the knight enters and the bottom-right room where the princess is imprisoned.
阅读全文

vps常用脚本或网站

收集的部分VPS常用脚本或网站

  • 测ICMP && 测TCP

    1
    2
    3
    4
    # 测ICMP
    bash <(curl -sL mcnb.top/besttrace.sh)
    # 测TCP
    bash <(curl -sL mcnb.top/besttcp.sh)
  • BBR N合1脚本,包含bbr原版,bbr puls,锐速,bbr2等

    1
    wget -N --no-check-certificate "https://github.com/ylx2016/Linux-NetSpeed/releases/download/sh/tcp.sh" && chmod +x tcp.sh && ./tcp.sh
阅读全文

Linux非root用户编译安装zsh和oh-my-zsh

闲话

在服务器上想用oh-my-zsh,但没有root权限,用惯了oh-my-zsh没有很难受。只能把zsh安装在本地。之前在公司实习的时候也遇到弄过一次,但这次安装还是得google一下,网上相关博客还是很多年以前的,问题不少。所以今天打算自己记录一下。

阅读全文

Ubuntu离线安装pytorch

pytorch安装问题

今天在服务器上安装pytorch时,加了清华镜像也跳到了默认外国下载地址,然后下载速度奇慢无比,而且中间还会报错然后停止运行。百度下找到个离线下载的方法,故记录一下:

阅读全文

Ubuntu命令行安装Anaconda3

  • 运行以下命令下载sh文件:

    其他版本的文件可以上 https://repo.continuum.io/archive/ 上查找,然后wget即可

    1
    wget https://repo.continuum.io/archive/Anaconda3-2018.12-Linux-x86_64.sh
  • 安装Anaconda3

    一路回车或yes即可,有需要的话安装时可以自行更改一些设置

    1
    bash Anaconda3-2018.12-Linux-x86_64.sh
阅读全文