LEETCODE 1216. Valid Palindrome III 解题思路分析

题目大意:

验证回文字符串 III

给出一个字符串 s 和一个整数 k,请你帮忙判断这个字符串是不是一个 K 回文。

如果可以通过从字符串中删去最多 k 个字符将其转换为回文,该字符串是一个 K 回文。

继续阅读
发表在 leetcode | 标签为 , , , , | 留下评论

LEETCODE 1228. Missing Number In Arithmetic Progression 解题思路分析

题目大意:

等差数列中缺失的数字

数组 arr 中的值符合等差数列的数值规律:在 0 <= i < arr.length – 1 的前提下,arr[i+1] – arr[i] 的值都相等。

然后,我们从数组中删去一个 既不是第一个也不是最后一个的值。

给你一个缺失值的数组,请你帮忙找出那个被删去的数字。

继续阅读
发表在 leetcode | 标签为 , , | 留下评论

LEETCODE 1229. Meeting Scheduler 解题思路分析

题目大意:

安排会议日程

你是一名行政助理,手里有两位客户的空闲时间表:slots1 和 slots2,以及会议的预计持续时间 duration,请你为他们安排合适的会议时间。

「会议时间」是两位客户都有空参加,并且持续时间能够满足预计时间 duration 的 最早的时间间隔。

如果没有满足要求的会议时间,就请返回一个 空数组。

「空闲时间」的格式是 [start, end],由开始时间 start 和结束时间 end 组成,表示从 start 开始,到 end 结束。

题目保证数据有效:同一个人的空闲时间不会出现交叠的情况,也就是说,对于同一个人的两个空闲时间 [start1, end1] 和 [start2, end2],要么 start1 > end2,要么 start2 > end1。

继续阅读
发表在 leetcode | 标签为 , , , | 留下评论

LEETCODE 1230. Toss Strange Coins 解题思路分析

题目大意:

抛掷硬币

有一些不规则的硬币。在这些硬币中,prob[i] 表示第 i 枚硬币正面朝上的概率。

请对每一枚硬币抛掷 一次,然后返回正面朝上的硬币数等于 target 的概率。

继续阅读
发表在 leetcode | 标签为 , , , | 留下评论

LEETCODE 1231. Divide Chocolate 解题思路分析

题目大意:

你有一大块巧克力,它由一些甜度不完全相同的小块组成。我们用数组 sweetness 来表示每一小块的甜度。

你打算和 K 名朋友一起分享这块巧克力,所以你需要将切割 K 次才能得到 K+1 块,每一块都由一些 连续 的小块组成。

为了表现出你的慷慨,你将会吃掉 总甜度最小 的一块,并将其余几块分给你的朋友们。

请找出一个最佳的切割策略,使得你所分得的巧克力 总甜度最大,并返回这个 最大总甜度。

继续阅读
发表在 leetcode | 标签为 , , | 留下评论

LEETCODE 1236. Web Crawler 解题思路分析

题目大意:

网页爬虫

Given a url startUrl and an interface HtmlParser, implement a web crawler to crawl all links that are under the same hostname as startUrl

Return all urls obtained by your web crawler in any order.

Your crawler should:

  • Start from the page: startUrl
  • Call HtmlParser.getUrls(url) to get all urls from a webpage of given url.
  • Do not crawl the same link twice.
  • Explore only the links that are under the same hostname as startUrl.

As shown in the example url above, the hostname is example.org. For simplicity sake, you may assume all urls use http protocol without any port specified. For example, the urls http://leetcode.com/problems and http://leetcode.com/contest are under the same hostname, while urls http://example.org/test and http://example.com/abc are not under the same hostname.

The HtmlParser interface is defined as such: 

interface HtmlParser {
  // Return a list of all urls from a webpage of given url.
  public List<String> getUrls(String url);
}

Below are two examples explaining the functionality of the problem, for custom testing purposes you’ll have three variables urlsedges and startUrl. Notice that you will only have access to startUrl in your code, while urls and edges are not directly accessible to you in code.

继续阅读
发表在 leetcode | 标签为 , , , | 3条评论

LEETCODE 1242. Web Crawler Multithreaded 解题思路分析

题目大意:

多线程网页爬虫

Given a url startUrl and an interface HtmlParser, implement a Multi-threaded web crawler to crawl all links that are under the same hostname as startUrl

Return all urls obtained by your web crawler in any order.

Your crawler should:

  • Start from the page: startUrl
  • Call HtmlParser.getUrls(url) to get all urls from a webpage of given url.
  • Do not crawl the same link twice.
  • Explore only the links that are under the same hostname as startUrl.

As shown in the example url above, the hostname is example.org. For simplicity sake, you may assume all urls use http protocol without any port specified. For example, the urls http://leetcode.com/problems and http://leetcode.com/contest are under the same hostname, while urls http://example.org/test and http://example.com/abc are not under the same hostname.

The HtmlParser interface is defined as such: 

interface HtmlParser {
  // Return a list of all urls from a webpage of given url.
  // This is a blocking call, that means it will do HTTP request and return when this request is finished.
  public List<String> getUrls(String url);
}

Note that getUrls(String url) simulates performing a HTTP request. You can treat it as a blocking function call which waits for a HTTP request to finish. It is guaranteed that getUrls(String url) will return the urls within 15ms.  Single-threaded solutions will exceed the time limit so, can your multi-threaded web crawler do better?

Below are two examples explaining the functionality of the problem, for custom testing purposes you’ll have three variables urlsedges and startUrl. Notice that you will only have access to startUrl in your code, while urls and edges are not directly accessible to you in code.

Follow up:

  1. Assume we have 10,000 nodes and 1 billion URLs to crawl. We will deploy the same software onto each node. The software can know about all the nodes. We have to minimize communication between machines and make sure each node does equal amount of work. How would your web crawler design change?
  2. What if one node fails or does not work?
  3. How do you know when the crawler is done?
继续阅读
发表在 leetcode | 标签为 , , , | 4条评论

LEETCODE 1243. Array Transformation 解题思路分析

题目大意:

数组变换

首先,给你一个初始数组 arr。然后,每天你都要根据前一天的数组生成一个新的数组。

第 i 天所生成的数组,是由你对第 i-1 天的数组进行如下操作所得的:

  • 假如一个元素小于它的左右邻居,那么该元素自增 1。
  • 假如一个元素大于它的左右邻居,那么该元素自减 1。
  • 首、尾元素永不改变。

过些时日,你会发现数组将会不再发生变化,请返回最终所得到的数组。

继续阅读
发表在 leetcode | 标签为 , , | 留下评论

LEETCODE 1244. Design A Leaderboard 解题思路分析

题目大意:

设计排行榜

请你帮忙来设计这个 Leaderboard 类,使得它有如下 3 个函数:

addScore(playerId, score):
假如参赛者已经在排行榜上,就给他的当前得分增加 score 点分值并更新排行。
假如该参赛者不在排行榜上,就把他添加到榜单上,并且将分数设置为 score。

top(K):返回前 K 名参赛者的得分总和。

reset(playerId):将指定参赛者的成绩清零。题目保证在调用此函数前,该参赛者已有成绩,并且在榜单上。

初始状态下,排行榜是空的。

继续阅读
发表在 leetcode | 标签为 , , , , | 留下评论

LEETCODE 1245. Tree Diameter 解题思路分析

题目大意:

树的直径

给定一棵无向树,返回它的直径:树中最长路径的 边 的数量。

树用一个数组给出,数组为 edges[i] = [u, v],每个元素代表一条双向边连接结点 u 和 v。每个结点的编号为 {0, 1, …, edges.length}。

继续阅读
发表在 leetcode | 标签为 , , | 8条评论