欢迎光临散文网 会员登陆 & 注册

LeetCode 2601. Prime Subtraction Operation

2023-03-26 14:31 作者:您是打尖儿还是住店呢  | 我要投稿

You are given a 0-indexed integer array nums of length n.

You can perform the following operation as many times as you want:

  • Pick an index i that you haven’t picked before, and pick a prime p strictly less than nums[i], then subtract p from nums[i].

Return true if you can make nums a strictly increasing array using the above operation and false otherwise.

A strictly increasing array is an array whose each element is strictly greater than its preceding element.

 

Example 1:

Input: nums = [4,9,6,10]

Output: true

Explanation: 

In the first operation: Pick i = 0 and p = 3, and then subtract 3 from nums[0], 

so that nums becomes [1,9,6,10]. 

In the second operation: i = 1, p = 7, subtract 7 from nums[1], 

so nums becomes equal to [1,2,6,10]. 

After the second operation, nums is sorted in strictly increasing order, so the answer is true.

Example 2:

Input: nums = [6,8,11,12]

Output: true

Explanation: Initially nums is sorted in strictly increasing order, so we don't need to make any operations.

Example 3:

Input: nums = [5,8,3]

Output: false

Explanation: It can be proven that there is no way to perform operations to make nums sorted in strictly increasing order, so the answer is false.

 

Constraints:

  • 1 <= nums.length <= 1000

  • 1 <= nums[i] <= 1000

  • nums.length == n

好尴尬啊,基本上每个函数都写错过,isprime函数漏写了1的情况,1不是质数,不是质数,不是质数。。。。

first_numSubtract()这个函数是将第一个元素先降低到最小值,

然后剩下的元素去每次判断是否存在num[i]-num[i-1]的质数,有的话,就将这个num[i]减去对应的质数,

还有个checksort的函数,判断是否是严格递增的,没错是严格递增,我以为>=就行了,不行,不行,不行,要>才行啊。。。

综上就是题意没理解透彻,另外就是基础不扎实,还需要多多锻炼,汗颜啊。。。

Runtime: 6 ms, faster than 90.91% of Java online submissions for Prime Subtraction Operation.

Memory Usage: 42.6 MB, less than 63.64% of Java online submissions for Prime Subtraction Operation.


LeetCode 2601. Prime Subtraction Operation的评论 (共 条)

分享到微博请遵守国家法律