LeetCode Weekly Contest 56 解题报告

来源:互联网 发布:网络考试平台 编辑:程序博客网 时间:2024/06/07 07:01

LeetCode Weekly Contest 56 解题报告

对应No.717,718,719443 的题解。

如前面几篇博客中说的那样, 我想换些形式来写这东西。(毕竟有一定的任务性质, 还是有点枯燥的) 这种以Contest的形式早就想过了, 只不过之前Contest的题没有完整做出过 Orz….. 总算这次的题略水一点。就先做一次合集吧。

T717以及 T443

由于这两题都是easy, 所以放一起作为一个板块写。

T717题目描述:

We have two special characters. The first character can be represented by one bit 0. The second character can be represented by two bits (10 or 11).

Now given a string represented by several bits. Return whether the last character must be a one-bit character or not. The given string will always end with a zero.

Note:
1 <= len(bits) <= 1000.
bits[i] is always 0 or 1.

这个编码是一个长度为2的哈夫曼编码。所以可以从头开始直接匹配, 不存在有多种匹配的可能性。
所以很容易得到时间复杂度为 O(n)的算法:依次扫描,如果是0,则匹配为 0, 如果是1,则一次匹配两个字符。

AC代码如下:

class Solution {public:    bool isOneBitCharacter(vector<int>& bits) {        int i = 0;        bool isOne;        while (i < bits.size()) {            if (bits[i] == 1) {                isOne = false;                i+= 2;            }            else {                isOne = true;                i++;            }        }        return isOne;    }};

T443 题目描述:

Given an array of characters, compress it in-place.

The length after compression must always be smaller than or equal to the original array.

Every element of the array should be a character (not int) of length 1.

After you are done modifying the input array in-place, return the new length of the array.

题目还附了许多样例, 建议直接去看一遍题:https://leetcode.com/problems/string-compression/description/

题目大意就是说把一个字符串进行压缩, 压缩的主体是连续出现的字符。 把连续出现的字符用字符和出现的次数来表示。

这题是一道基础题吧。 就是简单的编程实现功能即可。 并不需要什么算法。需要注意的是:
1. 出现的次数可能是多位数, 不局限于个位数, 因此需要实现一个itoa()函数。
2. 最后一次要注意判断压缩以及进行压缩。

AC代码如下:

class Solution {public:    string itoa(int n) {        if (n == 0) return "0";        string ans = "";        while (n != 0) {            ans.insert(ans.begin(), '0' + n % 10);            n/= 10;        }        return ans;    }    void Compress(vector<char> &chars, int &j, char x, int nums) {        string times = itoa(nums);        chars[j] = x; ++j;        for (int i = 0; i < times.size(); ++i) {            chars[j] = times[i];            ++j;        }    }    int compress(vector<char>& chars) {        int count = 1, j = 0;        for (int i = 1; i < chars.size(); ++i) {            while (i < chars.size() && chars[i] == chars[i - 1]) {                count++;                i++;            }            if (i >= chars.size()) break;            if (count > 1) {                 Compress(chars, j, chars[i - 1], count);            }            else {                chars[j] = chars[i - 1];                j++;            }            count = 1;        }        if (count > 1) {             Compress(chars, j, chars[chars.size() - 1], count);        }        else {            chars[j] = chars[chars.size() - 1];            j++;        }        return j;    }};

T718

Given two integer arrays A and B, return the maximum length of an subarray that appears in both arrays.
Example 1:
Input:
A: [1,2,3,2,1]
B: [3,2,1,4,7]
Output: 3
Explanation:
The repeated subarray with maximum length is [3, 2, 1].

Note:
1. 1 <= len(A), len(B) <= 1000
2. 0 <= A[i], B[i] < 100

题目大意

这道题是找出两个串的最长公共子串。从题目描述也能看出, 这题和著名的DP LCS 十分相似。事实上分析下来, 就是用LCS 的方法就可以解。状态转移方程如下:

Count[i][j] = A[i] == B[j] ? Conut[i - 1][j - 1] + 1 : 0

这是典型的DP。 思路就不多解释了。 如果不理解请先去了解LCS的解法。
这里需要注意到, 根据这个状态转移方程, 我们可以对状态进行压缩, 以节省空间复杂度。可以看到, 每一层的迭代只和其上一层有关。因此我们最多只需保留两层的状态, 反复迭代即可。 关键在于, 如果我们调整j的搜索顺序, 改为从后向前搜索, 那么可以完美的压缩一维, 即只用保留当前这一层的状态。

(剧透: 这题不状态压缩会MLE, 不然我也懒得写。。。

以下是AC代码, 基本就是上面状态转移方程的代码实现, 重点是循环的顺序

class Solution {public:    int findLength(vector<int>& A, vector<int>& B) {        vector<int> count(B.size(), 0);        int maximum = 0;        for (int i = 0; i < A.size(); ++i) {            for (int j = B.size() - 1; j >= 0; --j) {                if (A[i] != B[j]) {                    count[j] = 0;                }                else {                    if (i == 0 || j == 0) count[j] = 1;                    else count[j] = count[j - 1] + 1;                }                if (count[j] > maximum) maximum = count[j];            }        }        return maximum;    }};

T719

题目描述

Given an integer array, return the k-th smallest distance among all the pairs. The distance of a pair (A, B) is defined as the absolute difference between A and B.
Input:
nums = [1,3,1]
k = 1
Output: 0
Explanation:
Here are all the pairs:
(1,3) -> 2
(1,1) -> 0
(3,1) -> 2
Then the 1st smallest distance pair is (1,1), and its distance is 0.

Note:
1. 2 <= len(nums) <= 10000.
2. 0 <= nums[i] < 1000000.
3. 1 <= k <= len(nums) * (len(nums) - 1) / 2.

题目分析

emmm,这题的数据范围我觉得比较有意思。 由于是[1,10000],一般而言, 用O(n2)的方式对于这种数据量是勉强够的。 但是可能会受到具体实现的时候写法的制约。(就是有可能一个小优化没有考虑到就会超时的那种)

再来看题意。 首先这种数对的数量是O(n2)数量级的。也就是光完全构造就要 O(N2). emm考虑到我暂时没想到如何不完全构造就能实现,所以我打算先用O(n2)的方式实现下试试看。

既然用了O(n2), 就必须在其他地方尽量节约时间。 事实上除了构造部分, 就是挑选第 k 大的数。显然直接去排序 不是一个明智的选择(排序的复杂度是n2log(n),这想要过是很困难的)。这时候 note的作用就很明显了:其中1,3是常规的规定数据范围。 用于控制复杂度。 而2条件是开始没有被用到的。而且这种无端给整数范围的条件一般也就一种可能: 桶排序。
桶的范围是[1, 1000000], 小于复杂度 O(n2). 因此总复杂度还是O(n2)

(交上去是过了,不过耗时肯定在正态分布之外。。)

以下是AC代码。 整个代码非常短, 只有10来行。 完全看不出这是一道Hard

class Solution {public:    int smallestDistancePair(vector<int>& nums, int k) {        vector<int> count(1000000, 0);        for (int i = 0; i < nums.size() - 1; ++i) {            for (int j = i + 1; j < nums.size(); ++j) {                count[abs(nums[i] - nums[j])]++;            }        }        int sum = 0;        for(int i = 0; i < 10000000; ++i) {            sum+= count[i];            if (sum >= k) return i;        }    }};

Contest 总结

这次的Contest我觉得是我做过的最简单的一次。题目分布是 2Easy, 1Medium, 1Hard. 容易在于, Hard的数据量卡的不严, 一个比较trival的解法也能蹭过;而Medium是一个朴素DP, 只是考察了一个状态压缩。从时间上来说, 我觉得每道题的分配时间理想情况下应大概为: 10min, 15min, 15min, 10min 中间两题时间略多是因为一个是要对字符串进行处理, 稍不留神可能会错; 另一个是考虑到要推出状态转移, 并且要进行状态压缩。

当然实际做的时候哪有那么理想。。。我觉得大概90min做完就差不多吧。

(残念: 这次因为睡晚了只在Contest最后半小时开始, 到最后只A了前两题Orz。。。)

The End.