Redtongue

人生苦短,我用Python


  • 首页

  • 归档

  • 分类

  • 标签

  • 关于

  • 搜索

957.Prison Cells After Days(N 天后的牢房)

发表于 2018-12-19 | 更新于: 2018-12-19 | 分类于 leetcode

Description

There are 8 prison cells in a row, and each cell is either occupied or vacant.

Each day, whether the cell is occupied or vacant changes according to the following rules:

  • If a cell has two adjacent neighbors that are both occupied or both vacant, then the cell becomes occupied.
  • Otherwise, it becomes vacant.

(Note that because the prison is a row, the first and the last cells in the row can’t have two adjacent neighbors.)

We describe the current state of the prison in the following way:
cells[i] == 1 if the i-th cell is occupied, else cells[i] == 0.

Given the initial state of the prison, return the state of the prison after N days (and N such changes described above.)


8 间牢房排成一排,每间牢房不是有人住就是空着。

每天,无论牢房是被占用或空置,都会根据以下规则进行更改:

如果一间牢房的两个相邻的房间都被占用或都是空的,那么该牢房就会被占用。
否则,它就会被空置。
(请注意,由于监狱中的牢房排成一行,所以行中的第一个和最后一个房间无法有两个相邻的房间。)

我们用以下方式描述监狱的当前状态:如果第 i 间牢房被占用,则 cell[i]==1,否则 cell[i]==0。

根据监狱的初始状态,在 N 天后返回监狱的状况(和上述 N 种变化)。

题目链接:https://leetcode.com/problems/prison-cells-after-n-days/

Difficulty: medium

阅读全文 »

956.Tallest Billboard

发表于 2018-12-09 | 更新于: 2018-12-11 | 分类于 leetcode

Description

You are installing a billboard and want it to have the largest height. The billboard will have two steel supports, one on each side. Each steel support must be an equal height.

You have a collection of rods which can be welded together. For example, if you have rods of lengths 1, 2, and 3, you can weld them together to make a support of length 6.

Return the largest possible height of your billboard installation. If you cannot support the billboard, return 0.


你正在安装一个广告牌,并希望它高度最大。这块广告牌将有两个钢制支架,两边各一个。每个钢支架的高度必须相等。

你有一堆可以焊接在一起的钢筋 rods。举个例子,如果钢筋的长度为 1、2 和 3,则可以将它们焊接在一起形成长度为 6 的支架。

返回广告牌的最大可能安装高度。如果没法安装广告牌,请返回 0。

题目链接:https://leetcode.com/problems/tallest-billboard/

Difficulty: hard

阅读全文 »

955.Delete Columns to Make Sorted II(删列造序 ||)

发表于 2018-12-09 | 更新于: 2018-12-11 | 分类于 leetcode

Description

We are given an array A of N lowercase letter strings, all of the same length.

Now, we may choose any set of deletion indices, and for each string, we delete all the characters in those indices.

For example, if we have an array A = ["abcdef","uvwxyz"] and deletion indices {0, 2, 3}, then the final array after deletions is ["bef","vyz"].

Suppose we chose a set of deletion indices D such that after deletions, the final array has its elements in lexicographic order (A[0] <= A[1] <= A[2] ... <= A[A.length - 1]).

Return the minimum possible value of D.length.


给定由 N 个小写字母字符串组成的数组 A,其中每个字符串长度相等。

选取一个删除索引序列,对于 A 中的每个字符串,删除对应每个索引处的字符。

比如,有 A = ["abcdef", "uvwxyz"],删除索引序列 {0, 2, 3},删除后 A 为["bef", "vyz"]。

假设,我们选择了一组删除索引 D,那么在执行删除操作之后,最终得到的数组的元素是按 字典序(A[0] <= A[1] <= A[2] ... <= A[A.length - 1])排列的,然后请你返回 D.length 的最小可能值。

题目链接:https://leetcode.com/problems/delete-columns-to-make-sorted-ii/

Difficulty: medium

阅读全文 »

954.Array of Doubled Pairs(二倍数对数组)

发表于 2018-12-09 | 更新于: 2018-12-11 | 分类于 leetcode

Description

Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.


给定一个长度为偶数的整数数组 A,只有对 A 进行重组后可以满足 “对于每个 0 <= i < len(A) / 2,都有 A[2 * i + 1] = 2 * A[2 * i]” 时,返回 true;否则,返回 false。

题目链接:https://leetcode.com/problems/array-of-doubled-pairs/

Difficulty: medium

阅读全文 »

953.Verifying an Alien Dictionary(验证外星语词典)

发表于 2018-12-09 | 更新于: 2018-12-11 | 分类于 leetcode

Description

In an alien language, surprisingly they also use english lowercase letters, but possibly in a different order. The order of the alphabet is some permutation of lowercase letters.

Given a sequence of words written in the alien language, and the order of the alphabet, return true if and only if the given words are sorted lexicographicaly in this alien language.


某种外星语也使用英文小写字母,但可能顺序 order 不同。字母表的顺序(order)是一些小写字母的排列。

给定一组用外星语书写的单词 words,以及其字母表的顺序 order,只有当给定的单词在这种外星语中按字典序排列时,返回 true;否则,返回 false。

题目链接:https://leetcode.com/problems/verifying-an-alien-dictionary/

Difficulty: easy

阅读全文 »

952.Largest Component Size by Common Factor(按公因数计算最大组件大小)

发表于 2018-12-02 | 更新于: 2018-12-11 | 分类于 leetcode

Description

Given a non-empty array of unique positive integers A, consider the following graph:

  • There are A.length nodes, labelled A[0] to A[A.length - 1];
  • There is an edge between A[i] and A[j] if and only if A[i] and A[j] share a common factor greater than 1.

Return the size of the largest connected component in the graph.


给定一个由不同正整数的组成的非空数组 A,考虑下面的图:

  • 有 A.length 个节点,按从 A[0] 到 A[A.length - 1] 标记;
  • 只有当 A[i] 和 A[j] 共用一个大于 1 的公因数时,A[i] 和 A[j] 之间才有一条边。

返回图中最大连通组件的大小。

题目链接:https://leetcode.com/problems/largest-component-size-by-common-factor/

Difficulty: hard

阅读全文 »

951.Flip Equivalent Binary Trees(翻转等价二叉树)

发表于 2018-12-02 | 更新于: 2018-12-11 | 分类于 leetcode

Description

For a binary tree T, we can define a flip operation as follows: choose any node, and swap the left and right child subtrees.

A binary tree X is flip equivalent to a binary tree Y if and only if we can make X equal to Y after some number of flip operations.

Write a function that determines whether two binary trees are flip equivalent. The trees are given by root nodes root1 and root2.


我们可以为二叉树 T 定义一个翻转操作,如下所示:选择任意节点,然后交换它的左子树和右子树。

只要经过一定次数的翻转操作后,能使 X 等于 Y,我们就称二叉树 X 翻转等价于二叉树 Y。

编写一个判断两个二叉树是否是翻转等价的函数。这些树由根节点 root1 和 root2 给出。

题目链接:https://leetcode.com/problems/flip-equivalent-binary-trees/

Difficulty: medium

阅读全文 »

950.Reveal Cards In Increasing Order(按递增顺序显示卡牌)

发表于 2018-12-02 | 更新于: 2018-12-11 | 分类于 leetcode

Description

In a deck of cards, every card has a unique integer. You can order the deck in any order you want.

Initially, all the cards start face down (unrevealed) in one deck.

Now, you do the following steps repeatedly, until all cards are revealed:

  • Take the top card of the deck, reveal it, and take it out of the deck.
  • If there are still cards in the deck, put the next top card of the deck at the bottom of the deck.
  • If there are still unrevealed cards, go back to step 1. Otherwise, stop.

Return an ordering of the deck that would reveal the cards in increasing order.

The first entry in the answer is considered to be the top of the deck.


牌组中的每张卡牌都对应有一个唯一的整数。你可以按你想要的顺序对这套卡片进行排序。

最初,这些卡牌在牌组里是正面朝下的(即,未显示状态)。

现在,重复执行以下步骤,直到显示所有卡牌为止:

  • 从牌组顶部抽一张牌,显示它,然后将其从牌组中移出。
  • 如果牌组中仍有牌,则将下一张处于牌组顶部的牌放在牌组的底部。
  • 如果仍有未显示的牌,那么返回步骤 1。否则,停止行动。

返回能以递增顺序显示卡牌的牌组顺序。

答案中的第一张牌被认为处于牌堆顶部。

题目链接:https://leetcode.com/problems/reveal-cards-in-increasing-order/

Difficulty: medium

阅读全文 »

949.Largest Time for Given Digits(给定数字能组成的最大时间)

发表于 2018-12-02 | 更新于: 2018-12-11 | 分类于 leetcode

Description

Given an array of 4 digits, return the largest 24 hour time that can be made.

The smallest 24 hour time is 00:00, and the largest is 23:59. Starting from 00:00, a time is larger if more time has elapsed since midnight.

Return the answer as a string of length 5. If no valid time can be made, return an empty string.


给定一个由 4 位数字组成的数组,返回可以设置的符合 24 小时制的最大时间。

最小的 24 小时制时间是 00:00,而最大的是 23:59。从 00:00 (午夜)开始算起,过得越久,时间越大。

以长度为 5 的字符串返回答案。如果不能确定有效时间,则返回空字符串。

题目链接:https://leetcode.com/problems/largest-time-for-given-digits/

Difficulty: easy

阅读全文 »

948.Bag of Tokens(令牌放置)

发表于 2018-11-25 | 更新于: 2018-12-11 | 分类于 leetcode

Description

You have an initial power P, an initial score of 0 points, and a bag of tokens.

Each token can be used at most once, has a value token[i], and has potentially two ways to use it.

  • If we have at least token[i] power, we may play the token face up, losing token[i] power, and gaining 1 point.
  • If we have at least 1 point, we may play the token face down, gaining token[i] power, and losing 1 point.

Return the largest number of points we can have after playing any number of tokens.


你的初始能量为 P,初始分数为 0,只有一包令牌。

令牌的值为 token[i],每个令牌最多只能使用一次,可能的两种使用方法如下:

  • 如果你至少有 token[i] 点能量,可以将令牌置为正面朝上,失去 token[i] 点能量,并得到 1 分。
  • 如果我们至少有 1 分,可以将令牌置为反面朝上,获得 token[i] 点能量,并失去 1 分。

在使用任意数量的令牌后,返回我们可以得到的最大分数。

题目链接:https://leetcode.com/problems/bag-of-tokens/

Difficulty: medium

阅读全文 »
1…567…21
yunxiang wang

yunxiang wang

记录点点

202 日志
4 分类
41 标签
GitHub E-Mail
© 2016 — 2020 yunxiang wang