Press "Enter" to skip to content

Posts published in September 2018

花花酱 LeetCode 668. Kth Smallest Number in Multiplication Table

Problem

Nearly every one have used the Multiplication Table. But could you find out the k-th smallest number quickly from the multiplication table?

Given the height m and the length n of a m * n Multiplication Table, and a positive integer k, you need to return the k-th smallest number in this table.

Example 1:

Input: m = 3, n = 3, k = 5
Output: 
Explanation: 
The Multiplication Table:
1	2	3
2	4	6
3	6	9

The 5-th smallest number is 3 (1, 2, 2, 3, 3).

Example 2:

Input: m = 2, n = 3, k = 6
Output: 
Explanation: 
The Multiplication Table:
1	2	3
2	4	6

The 6-th smallest number is 6 (1, 2, 2, 3, 4, 6).

Note:

  1. The m and n will be in the range [1, 30000].
  2. The k will be in the range [1, m * n]

 

Solution 1: Nth element (MLE)

Time complexity: O(mn)

Space complexity: O(mn)

C++

Solution2 : Binary Search

Find first x such that there are k elements less or equal to x in the table.

Time complexity: O(m*log(m*n))

Space complexity: O(1)

C++

Java

Python3

Related Problems

[反面教材] 我是如何快速搭建一个公众号机器人的

声明:本文可以作为快速开发的经典反面教材,请勿学习,不然后果自负。

说来惭愧,公众号虽然开了有一年多,但没怎么发过东西。由于众所不知的原因,文章中不能有第三方视频/链接。要插入视频的话必须上传到腾讯视频,那真叫一个蛋腾,所以就放弃治疗了。真的对不住大家了!

直到昨天晚上,在手工编辑关键字自动回复的时候,无意中发现自动回复居然可以支持任意URL的超链接。这一下子激动得我就像发现了新大陆一样!赶紧加了几个按照leetcode题目编号自动回复视频/博客链接的关键字。我靠,这得手工编辑到什么时候啊?我怎么说也是一个堂堂正正的程序员好吗,怎么能干手工编辑这种事情呢(虽然我一直在干)?

自动回复,自动回复,自动回复!按照题目编号回复视频链接!说干就干!我路子野起来,连我的MacBook Pro都害怕。

其实流程还是比较清楚的,如下图:

自动回复流程图

数据源:我有一张Google Spreadsheet记录了所有的视频,网站链接。(就是上文提到的手工编辑的)【DONE】

数据源:http://t.cn/RTcVHnY

数据处理:这对于常年参加Kaggle比赛,总是1000名开外的我来说自然也不是什么难事。Google Spreadsheet发布成csv格式,这样处理起来方便些。写个python脚本从数据源生成一个JSON文件,key就是题目编号,value就是回复正文。【DONE】

数据处理

配置服务器:

首先你得有一台服务器。我博客的虚拟主机正好可以来拿当炮灰。【DONE】

官文的例子是用Python开发的,需要80/443端口,已经被Apache占用了【放弃治疗】

看来只能改用PHP了,我好歹也写过几百行PHP好吗?PHP:精通。一直保持在 echo “Hello” . ” World!”; 的巅峰水平,从未被超越过。【DONE】

我不是有WordPress在跑这么,直接用它不就完了么?找了几个插件,不是PHP版本/环境要求太高,就是2年多没更新了。还是自己写吧,但怎么插入自己的PHP代码呢?好像没那么简单。如果用SSH/FTP上传倒是可以,就是太麻烦了【放弃治疗】。

对了,其实我可以借壳上市。找一个不用的插件,把它的代码替换了不就行了么。再把readme.txt替换成之前生成的JSON文件。直接在WordPress中编辑就行了。【DONE】

服务器设定,指向那个炮灰插件。

实现业务逻辑:

官方的开发文档实在是无力吐槽,盲人摸象吧。接口测试功能倒是不错。

每次收到消息,读取readme.txt(此处应该有缓存),看看content是不是key,如果是的话就返回value。就是这么简单。【DONE】

服务器代码

夜深了,是时候和自己写的机器人进行促膝长谈了。【DONE】

最后的效果

打赏专用二维码

后记:

  1. 坑太多了,前前后后一共花了3个多小时。
  2. 数据源还有很多NaN正在手工编辑中。
  3. 大家千万不要学习我这种野路子风格,数据更新什么的还是需要手动,一点都不程序员。
  4. 准备加入更多功能,比如特辑/播放列表的关键字回复。
  5. 对我的机器人温柔一点。

花花酱 LeetCode 898. Bitwise ORs of Subarrays

Problem

We have an array A of non-negative integers.

For every (contiguous) subarray B = [A[i], A[i+1], ..., A[j]] (with i <= j), we take the bitwise OR of all the elements in B, obtaining a result A[i] | A[i+1] | ... | A[j].

Return the number of possible results.  (Results that occur more than once are only counted once in the final answer.)

Example 1:

Input: [0]
Output: 1
Explanation: 
There is only one possible result: 0.

Example 2:

Input: [1,1,2]
Output: 3
Explanation: 
The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2].
These yield the results 1, 1, 2, 1, 3, 3.
There are 3 unique values, so the answer is 3.

Example 3:

Input: [1,2,4]
Output: 6
Explanation: 
The possible results are 1, 2, 3, 4, 6, and 7.

Note:

  1. 1 <= A.length <= 50000
  2. 0 <= A[i] <= 10^9



Solution 1: DP (TLE)

dp[i][j] := A[i] | A[i + 1] | … | A[j]

dp[i][j] = dp[i][j – 1] | A[j]

ans = len(set(dp))

Time complexity: O(n^2)

Space complexity: O(n^2) -> O(n)

C++ SC O(n^2)

C++ SC O(n)

Solution 2: DP opted

dp[i] := {A[i], A[i] | A[i – 1], A[i] | A[i – 1] | A[i – 2], … , A[i] | A[i – 1] | … | A[0]}, bitwise ors of all subarrays end with A[i].

|dp[i]| <= 32

Proof: all the elements (in the order of above sequence) in dp[i] are monotonically increasing by flipping 0 bits to 1 from A[i].

There are at most 32 0s in A[i]. Thus the size of the set is <= 32.

证明: dp[i] = {A[i], A[i] | A[i – 1], A[i] | A[i – 1] | A[i – 2], … , A[i] | A[i – 1] | … | A[0]},这个序列单调递增,通过把A[i]中的0变成1。A[i]最多有32个0。所以这个集合的大小 <= 32。

e.g. 举例:Worst Case 最坏情况 A = [8, 4, 2, 1, 0] A[i] = 2^(n-i)。

A[5] = 0,dp[5] = {0, 0 | 1, 0 | 1 | 2, 0 | 1 | 2 | 4, 0 | 1 | 2 | 4 | 8} = {0, 1, 3, 7, 15}.

Time complexity: O(n*log(max(A))) < O(32n)

Space complexity: O(n*log(max(A)) < O(32n)

C++

Java

Python3