字节开启新一轮期权回购,价格又涨了(含算法原题)

字节期权

近日,字节跳动开启新一轮期权回购,价格微涨至 170 美元。

之前我们就写过 文章,分享历年来字节跳动的期权变化情况,这里再贴一下:

  • 18年:10+
  • 19年:30+
  • 20年:60-70
  • 21年:126
  • 22年4月:140
  • 22年10月:155
  • 23年4月:155
  • 23年10月:160

本次字节期权回购价格来到 170 美元,但仍还是从美国先开始。

字节期权仍在涨,主因是字节在 2023 年第三季度收入上涨 43%(达 309 亿美元),在广告和电商板块的增长尤其明显。

以最新数据来看,目前字节的增长速度是 Meta(前身 Facebook)的两倍。

...

以最新的汇率来计算,当时那位手握价值 900w 人民币字节期权的同学,又"少少地"涨了一点身价:

8400 股,每股涨 10 美元,以最新汇率为 7.1985 来计算。

<math xmlns="http://www.w3.org/1998/Math/MathML"> 8400 × 10 × 7.1985 ≈ 8400 \times 10 \times 7.1985 \approx </math>8400×10×7.1985≈ 60W,少少涨了 60W。

...

回归主线。

来一道「字节跳动」一面算法原题。

题目描述

平台:LeetCode

题号:790

有两种形状的瓷砖:一种是 2 x 1 的多米诺形,另一种是形如 "L" 的托米诺形,两种形状都可以旋转。

给定整数 n ,返回可以平铺 2 x n 的面板的方法的数量,返回对 <math xmlns="http://www.w3.org/1998/Math/MathML"> 1 0 9 + 7 10^9 + 7 </math>109 +7 取模 的值。

平铺指的是每个正方形都必须有瓷砖覆盖。

两个平铺不同,当且仅当面板上有四个方向上的相邻单元中的两个,使得恰好有一个平铺有一个瓷砖占据两个正方形。

示例 1:

makefile 复制代码
输入: n = 3

输出: 5

解释: 五种不同的方法如上所示。

示例 2:

ini 复制代码
输入: n = 1

输出: 1

提示:

  • <math xmlns="http://www.w3.org/1998/Math/MathML"> 1 < = n < = 1000 1 <= n <= 1000 </math>1<=n<=1000

状态机 DP

定义 <math xmlns="http://www.w3.org/1998/Math/MathML"> f [ i ] [ j ] f[i][j] </math>f[i][j] 为无须考虑前 <math xmlns="http://www.w3.org/1998/Math/MathML"> i − 1 i - 1 </math>i−1 列(含义为前 <math xmlns="http://www.w3.org/1998/Math/MathML"> i − 1 i - 1 </math>i−1 列已铺满),当前第 <math xmlns="http://www.w3.org/1998/Math/MathML"> i i </math>i 列状态为 <math xmlns="http://www.w3.org/1998/Math/MathML"> j j </math>j 时的方案数。

其中 <math xmlns="http://www.w3.org/1998/Math/MathML"> j j </math>j 取值范围为 <math xmlns="http://www.w3.org/1998/Math/MathML"> [ 0 , 4 ) [0, 4) </math>[0,4) 分别对应了当前列的填充情况:

为了方便,我们人为规定列数从 <math xmlns="http://www.w3.org/1998/Math/MathML"> 1 1 </math>1 开始。

由于骨牌只能在 <math xmlns="http://www.w3.org/1998/Math/MathML"> 2 × n 2 \times n </math>2×n 的棋盘内填充(不能延伸出棋盘两端),因此我们有显而易见的初始化状态:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> f [ 1 ] [ 0 ] = f [ 1 ] [ 1 ] = 1 f[1][0] = f[1][1] = 1 </math>f[1][0]=f[1][1]=1

分别对应「第一列不放置任何骨牌」和「第一列竖着放一块 <math xmlns="http://www.w3.org/1998/Math/MathML"> 1 × 2 1 \times 2 </math>1×2 骨牌」合法方案。

而 <math xmlns="http://www.w3.org/1998/Math/MathML"> f [ 1 ] [ 2 ] f[1][2] </math>f[1][2] 和 <math xmlns="http://www.w3.org/1998/Math/MathML"> f [ 1 ] [ 3 ] f[1][3] </math>f[1][3] 由于没法在棋盘左侧以外的位置放置骨牌,不存在合法方案,其值均为 <math xmlns="http://www.w3.org/1998/Math/MathML"> 0 0 </math>0。

同时可知 <math xmlns="http://www.w3.org/1998/Math/MathML"> f [ n ] [ 1 ] f[n][1] </math>f[n][1] 为我们最终答案,含义为所有列都恰好铺完,不溢出棋盘右侧位置。

不失一般性考虑 <math xmlns="http://www.w3.org/1998/Math/MathML"> f [ i ] [ j ] f[i][j] </math>f[i][j] 该如何计算,其实就是一个简单的状态机转移分情况讨论:

  • <math xmlns="http://www.w3.org/1998/Math/MathML"> f [ i ] [ 0 ] f[i][0] </math>f[i][0] : 需要前 <math xmlns="http://www.w3.org/1998/Math/MathML"> i − 1 i - 1 </math>i−1 列铺满,同时第 <math xmlns="http://www.w3.org/1998/Math/MathML"> i i </math>i 列没有被铺,只能由 <math xmlns="http://www.w3.org/1998/Math/MathML"> f [ i − 1 ] [ 1 ] f[i - 1][1] </math>f[i−1][1] 转移而来,即有 <math xmlns="http://www.w3.org/1998/Math/MathML"> f [ i ] [ 0 ] = f [ i − 1 ] [ 1 ] f[i][0] = f[i - 1][1] </math>f[i][0]=f[i−1][1]

    这里需要尤其注意:虽然我们能够在上一步留空第 <math xmlns="http://www.w3.org/1998/Math/MathML"> i − 1 i - 1 </math>i−1 列,然后在 <math xmlns="http://www.w3.org/1998/Math/MathML"> i − 1 i - 1 </math>i−1 列竖放一块 <math xmlns="http://www.w3.org/1998/Math/MathML"> 1 × 2 1 \times 2 </math>1×2 的骨牌(如下图)

    但我们不能从 <math xmlns="http://www.w3.org/1998/Math/MathML"> f [ i − 1 ] [ 0 ] f[i - 1][0] </math>f[i−1][0] 转移到 <math xmlns="http://www.w3.org/1998/Math/MathML"> f [ i ] [ 0 ] f[i][0] </math>f[i][0],因为此时放置的骨牌,仅对第 <math xmlns="http://www.w3.org/1998/Math/MathML"> i − 1 i - 1 </math>i−1 列产生影响,不会对第 <math xmlns="http://www.w3.org/1998/Math/MathML"> i i </math>i 列产生影响,该决策所产生的方案数,已在 <math xmlns="http://www.w3.org/1998/Math/MathML"> f [ i − 1 ] [ X ] f[i - 1][X] </math>f[i−1][X] 时被统计

  • <math xmlns="http://www.w3.org/1998/Math/MathML"> f [ i ] [ 1 ] f[i][1] </math>f[i][1] : 可由 <math xmlns="http://www.w3.org/1998/Math/MathML"> f [ i − 1 ] [ j ] f[i - 1][j] </math>f[i−1][j] 转移而来(见下图),其中 <math xmlns="http://www.w3.org/1998/Math/MathML"> j ∈ [ 0 , 4 ) j \in [0, 4) </math>j∈[0,4),即有 <math xmlns="http://www.w3.org/1998/Math/MathML"> f [ i ] [ 1 ] = ∑ j = 0 3 f [ i − 1 ] [ j ] f[i][1] = \sum_{j = 0}^{3} f[i - 1][j] </math>f[i][1]=∑j=03f[i−1][j]

  • <math xmlns="http://www.w3.org/1998/Math/MathML"> f [ i ] [ 2 ] f[i][2] </math>f[i][2] : 可由 <math xmlns="http://www.w3.org/1998/Math/MathML"> f [ i − 1 ] [ 0 ] f[i - 1][0] </math>f[i−1][0] 和 <math xmlns="http://www.w3.org/1998/Math/MathML"> f [ i − 1 ] [ 3 ] f[i - 1][3] </math>f[i−1][3] 转移而来

  • <math xmlns="http://www.w3.org/1998/Math/MathML"> f [ i ] [ 3 ] f[i][3] </math>f[i][3] : 可由 <math xmlns="http://www.w3.org/1998/Math/MathML"> f [ i − 1 ] [ 0 ] f[i - 1][0] </math>f[i−1][0] 和 <math xmlns="http://www.w3.org/1998/Math/MathML"> f [ i − 1 ] [ 2 ] f[i - 1][2] </math>f[i−1][2] 转移而来

Java 代码:

Java 复制代码
class Solution {
    int MOD = (int)1e9+7;
    public int numTilings(int n) {
        int[][] f = new int[n + 10][4];
        f[1][0] = f[1][1] = 1;
        for (int i = 2; i <= n; i++) {
            f[i][0] = f[i - 1][1];
            int cur = 0;
            for (int j = 0; j < 4; j++) cur = (cur + f[i - 1][j]) % MOD;
            f[i][1] = cur;
            f[i][2] = (f[i - 1][0] + f[i - 1][3]) % MOD;
            f[i][3] = (f[i - 1][0] + f[i - 1][2]) % MOD;
        }
        return f[n][1];
    }
}

C++ 代码:

C++ 复制代码
class Solution {
    const int MOD = 1e9 + 7;
public:
    int numTilings(int n){
        vector<vector<int>> f(n + 10, vector<int>(4));
        f[1][0] = f[1][1] = 1;
        for (int i = 2; i <= n; ++i){
            f[i][0] = f[i - 1][1];
            int cur = 0;
            for (int j = 0; j < 4; ++j) cur = (cur + f[i - 1][j]) % MOD;
            f[i][1] = cur;
            f[i][2] = (f[i - 1][0] + f[i - 1][3]) % MOD;
            f[i][3] = (f[i - 1][0] + f[i - 1][2]) % MOD;
        }
        return f[n][1];
    }
};

Python3 代码:

Python 复制代码
class Solution:
    def numTilings(self, n: int) -> int:
        f = [[0] * 4 for _ in range(n + 10)]
        f[1][0] = f[1][1] = 1
        for i in range(2, n + 1):
            f[i][0] = f[i - 1][1]
            f[i][1] = sum([f[i - 1][j] for j in range(4)])
            f[i][2] = f[i - 1][0] + f[i - 1][3]
            f[i][3] = f[i - 1][0] + f[i - 1][2]
        return f[n][1] % 1000000007

TypeScript 代码:

TypeScript 复制代码
function numTilings(n: number): number {
    const MOD = 1e9+7
    const f = new Array<Array<number>>()
    for (let i = 0; i <= n; i++) f[i] = new Array<number>(4).fill(0)
    f[1][0] = f[1][1] = 1
    for (let i = 2; i <= n; i++) {
        f[i][0] = f[i - 1][1]
        let cur = 0
        for (let j = 0; j < 4; j++) cur = (cur + f[i - 1][j]) % MOD
        f[i][1] = cur
        f[i][2] = (f[i - 1][0] + f[i - 1][3]) % MOD
        f[i][3] = (f[i - 1][0] + f[i - 1][2]) % MOD
    }
    return f[n][1]
}
  • 时间复杂度: <math xmlns="http://www.w3.org/1998/Math/MathML"> O ( n ) O(n) </math>O(n)
  • 空间复杂度: <math xmlns="http://www.w3.org/1998/Math/MathML"> O ( n ) O(n) </math>O(n)

滚动数组优化

利用 <math xmlns="http://www.w3.org/1998/Math/MathML"> f [ i ] [ X ] f[i][X] </math>f[i][X] 仅依赖于 <math xmlns="http://www.w3.org/1998/Math/MathML"> f [ i − 1 ] [ X ] f[i - 1][X] </math>f[i−1][X],我们可以采用「滚动数组」方式将其空间优化至 <math xmlns="http://www.w3.org/1998/Math/MathML"> O ( 1 ) O(1) </math>O(1)。

Java 代码:

Java 复制代码
class Solution {
    int MOD = (int)1e9+7;
    public int numTilings(int n) {
        int[][] f = new int[2][4];
        f[1][0] = f[1][1] = 1;
        for (int i = 2; i <= n; i++) {
            int a = i & 1, b = (i - 1) & 1;
            f[a][0] = f[b][1];
            int cur = 0;
            for (int j = 0; j < 4; j++) cur = (cur + f[b][j]) % MOD;
            f[a][1] = cur;
            f[a][2] = (f[b][0] + f[b][3]) % MOD;
            f[a][3] = (f[b][0] + f[b][2]) % MOD;
        }
        return f[n & 1][1];
    }
}

C++ 代码:

C++ 复制代码
class Solution {
    const int MOD = 1e9 + 7;
public:
    int numTilings(int n){
        vector<vector<int>> f(2, vector<int>(4));
        f[1][0] = f[1][1] = 1;
        for (int i = 2; i <= n; ++i){
            int a = i & 1, b = (i - 1) & 1;
            f[a][0] = f[b][1];
            int cur = 0;
            for (int j = 0; j < 4; ++j) cur = (cur + f[b][j]) % MOD;
            f[a][1] = cur;
            f[a][2] = (f[b][0] + f[b][3]) % MOD;
            f[a][3] = (f[b][0] + f[b][2]) % MOD;
        }
        return f[n & 1][1];
    }
};

Python3 代码:

Python 复制代码
class Solution:
    def numTilings(self, n: int) -> int:
        f = [[0] * 4 for _ in range(2)]
        f[1][0] = f[1][1] = 1
        for i in range(2, n + 1):
            a, b = i & 1, (i - 1) & 1
            f[a][0] = f[b][1]
            f[a][1] = sum([f[b][j] for j in range(4)])
            f[a][2] = f[b][0] + f[b][3]
            f[a][3] = f[b][0] + f[b][2]
        return f[n & 1][1] % 1000000007

TypeScript 代码:

TypeScript 复制代码
function numTilings(n: number): number {
    const MOD = 1e9+7
    const f = new Array<Array<number>>()
    for (let i = 0; i <= 1; i++) f[i] = new Array<number>(4).fill(0)
    f[1][0] = f[1][1] = 1
    for (let i = 2; i <= n; i++) {
        const a = i & 1, b = (i - 1) & 1
        f[a][0] = f[b][1]
        let cur = 0
        for (let j = 0; j < 4; j++) cur = (cur + f[b][j]) % MOD
        f[a][1] = cur
        f[a][2] = (f[b][0] + f[b][3]) % MOD
        f[a][3] = (f[b][0] + f[b][2]) % MOD
    }
    return f[n & 1][1]
}
  • 时间复杂度: <math xmlns="http://www.w3.org/1998/Math/MathML"> O ( n ) O(n) </math>O(n)
  • 空间复杂度: <math xmlns="http://www.w3.org/1998/Math/MathML"> O ( 1 ) O(1) </math>O(1)

我是宫水三叶,每天都会分享算法知识,并和大家聊聊近期的所见所闻。

欢迎关注,明天见。

更多更全更热门的「笔试/面试」相关资料可访问排版精美的 合集新基地 🎉🎉

相关推荐
浮华似水14 分钟前
简洁之道 - React Hook Form
前端
2401_857622662 小时前
SpringBoot框架下校园资料库的构建与优化
spring boot·后端·php
正小安2 小时前
如何在微信小程序中实现分包加载和预下载
前端·微信小程序·小程序
2402_857589362 小时前
“衣依”服装销售平台:Spring Boot框架的设计与实现
java·spring boot·后端
哎呦没3 小时前
大学生就业招聘:Spring Boot系统的架构分析
java·spring boot·后端
小飞猪Jay4 小时前
C++面试速通宝典——13
jvm·c++·面试
_.Switch4 小时前
Python Web 应用中的 API 网关集成与优化
开发语言·前端·后端·python·架构·log4j
一路向前的月光4 小时前
Vue2中的监听和计算属性的区别
前端·javascript·vue.js
长路 ㅤ   4 小时前
vite学习教程06、vite.config.js配置
前端·vite配置·端口设置·本地开发
长路 ㅤ   4 小时前
vue-live2d看板娘集成方案设计使用教程
前端·javascript·vue.js·live2d