your..

Stay button brush title

Binary search

Binary search method, also known as binary search.

Advantages: relatively small number of fast search speed, good average performance;

Disadvantages: Requirements to be ordered lookup table, delete and insert difficult.

Therefore, the binary search method is applicable to not change often and find frequently ordered list.

First, assume that the table element is in ascending order, the key intermediate position table search key and record comparison

If they are equal, then find success

Otherwise, before using the recording table into an intermediate position, if the two sub-tables record key intermediate position is greater than a search key

The further search before a child table

Otherwise, a further look after the child table

Repeat the process until the record satisfies the condition is found, the search is successful, or until the child table does not exist, at which time search is unsuccessful

Given an ordered array contains only lowercase letters lettersand a target letter target, to find an ordered array which is larger than the minimum target letter alphabet.

In alphabetical order of the array is the loop. For example, if the target letter target = 'z'and ordered array was letters = ['a', 'b'], the answer returned 'a'.

Example:

输入:
letters = ["c", "f", "j"]
target = "a"
输出: "c"

输入:
letters = ["c", "f", "j"]
target = "c"
输出: "f"

输入:
letters = ["c", "f", "j"]
target = "d"
输出: "f"

输入:
letters = ["c", "f", "j"]
target = "g"
输出: "j"

输入:
letters = ["c", "f", "j"]
target = "j"
输出: "c"

输入:
letters = ["c", "f", "j"]
target = "k"
输出: "c"
注:

letters长度范围在[2, 10000]区间内。
letters 仅由小写字母组成,最少包含两个不同的字母。
目标字母target 是一个小写字母。

Buckle in force implemented in the code: *

class Solution:
    def nextGreatestLetter(self, letters: List[str], target: str) -> str:
        return letters[bisect.bisect(letters, target) - len(letters)]
    
class Solution:
    def nextGreatestLetter(self, letters: List[str], target: str) -> str:
        return letters[bisect.bisect(letters, target) % len(letters)]

operation result:

输入
["c","f","j"]
"a"
输出
"c"
预期结果
"c"

The shortest complete word

If a word list of words (words) contains license (licensePlate) all the letters, then we call it a complete word. In all a complete word, we call it the shortest shortest word complete word.

Not case-sensitive when matching license plate letters, such as the license plate "P" can still match the word "p" in the letter.

We guarantee a minimum there must be a complete word. When there are multiple words match the criteria, the shortest term of a complete list of words to take the most forward.

License may contain the same character more, for example: For the license "PP", the word "pair" can not match, but the "supper" can match.

Example 1:

Input: licensePlate = "1s3 PSt", words = [ "step", "steps", "stripe", "stepple"]
Output: "steps"
Description: The minimum complete data word should comprise "s", "p", "s "and" t ". For the "step" it contains only one "s" so it does not qualify. Meanwhile in the matching process, we ignore the case of the license.

Example 2:

Input: licensePlate = "1s3 456", words = [ "looks", "pest", "stew", "show"]
Output: "pest"
Description: There are three complete word contains the letter "s" and has the shortest length but we return to complete the word first appears.

note:

In the region of the length [1, 7] License (licensePlate) a.
License (licensePlate) will contain numbers, spaces, or letters (uppercase and lowercase).
List of words (words) in length of the interval [10, 1000] in.
Each word words [i] are in lowercase, and a length in the interval [1, 15].

class Solution:
    def shortestCompletingWord(self, licensePlate: str, words: List[str]) -> str:
        words.sort(key=len)
        licensePlate=licensePlate.lower()
        d=collections.defaultdict(int)
        for c in licensePlate:
            if c.isalpha():
                d[c]+=1
        for s in words:
            t=d.copy()
            for c in s:
                if c in t:
                    t[c]-=1
                    if t[c]==0:
                        t.pop(c)
            if not t:
                return s
nums = [1,6,1,0]
r = max(nums)
index=0
for i in range(len(nums)):
    if r!=nums[i] and r<2*nums[i]:
        print(-1)
    if r==nums[i]:
        index=i
print(index)

Find at least twice the maximum number of other digital

In a given array nums in, there is always a maximum element.

Find an array of whether the largest element in the array is at least twice every other number.

If so, the index of the largest element is returned, otherwise -1.

Example 1:

Input: nums = [3, 6, 1, 0]
Output: 1
Explanation: 6 is the greatest integer, for other integer array,
6 times larger than the other elements in the array. 6 index is 1, so we returned.

Example 2:

Input: nums = [1, 2, 3, 4]
Output: -1
explanation: 4 No more than three large double, so we return -1.

prompt:

nums length in the range [1, 50].
Each nums [I] is an integer in the range [0, 100].

Stay button code implementation

class Solution:
    def dominantIndex(self, nums: List[int]) -> int:
        r = max(nums)
        index=0
        for i in range(len(nums)):
            if r!=nums[i] and r<2*nums[i]:
                return -1
            if r==nums[i]:
                index=i
        return index

Run Results:
input
[0,0,0,1]
Output
3
Expected Result
3

pycharm code to achieve the effect:

nums = [1, 2, 3, 4]
r = max(nums)
index=0
for i in range(len(nums)):
    if r!=nums[i] and r<2*nums[i]:#判断比较元素
        print(-1)#返回值
    if r==nums[i]:
        index=i
print(index)# 最大元素索引位置

operation result:

-1

3

Use minimum cost stairs

Each index of the array as a ladder, the i-th step corresponding to a physical non-negative cost value cost i .

Every time you climb a ladder you have to take physical cost value corresponding to, and then you can choose to continue to climb a ladder or climbing two steps.

You need to find the lowest cost at the top floor. In the beginning, you can choose from index 0 or 1 as the initial elements of the ladder.

Example 1:

Input: cost = [10, 15, 20]
Output: 15
Explanation: minimum cost from cost [1] Start, then take a step to the top in two steps, spent a total of 15.
Example 2:

Input: cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1]
Output: 6
Explanation: from the lowest cost approach is cost [0] starts, one by one after that, skips cost [ 3], spent a total of 6.
note:

in length will cost [2, 1000].
Each cost [i] is an Integer type will, in the range [0, 999].

Stay button code to run

class Solution:
    def minCostClimbingStairs(self, cost: List[int]) -> int:
        f = [cost[0], cost[1]]
        for i in range(2, len(cost)):
            f.append(cost[i] + min(f[i-1], f[i-2]))
        return min(f[-1], f[-2])

operation result:

输入
[0,0,0,0]
输出
0
预期结果
0

Guess you like

Origin www.cnblogs.com/gfhh/p/11564062.html