[Algorithm] Algorithm question-20231128

1. 55. Jumping Game

Given you an array nums of non-negative integers, you are initially at the first index of the array. Each element in the array represents the maximum length you can jump at that position.
Determine whether you can reach the last subscript, if so, return true; otherwise, return false

Example 1:
Input: nums = [2,3,1,1,4]
Output: true
Explanation: You can jump 1 step first, from index 0 to index 1, and then jump 3 steps from index 1 to the last index.

Example 2:
Input: nums = [3,2,1,0,4]
Output: false
Explanation: No matter what, the position with index 3 will always be reached. But the maximum jump length of this index is 0, so the last index can never be reached.

Idea: The basic idea of ​​​​solving the problem is that we initialize a variable max_i, which represents the farthest position that can currently be jumped to,
and then traverse the array. For each position, we Check whether it can jump to a farther position from the current position,
If it can jump to a farther position, then we update max_i.

class Solution:
    def canJump(self, nums):
        max_i = 0
        for i, jump in enumerate(nums):
            if max_i >= i and i + jump > max_i:
                max_i = i + jump
        return max_i >= i


res = Solution()
nums = [3, 2, 1, 0, 4]
print(res.canJump(nums))

2. 274. H index

Tips
Medium
425
Related companies
Give you an integer Array citations, where citations[i] represents the number of times the researcher's i-th paper has been cited. Calculate and return the h-index for this researcher.

According to the definition of h-index on Wikipedia: h stands for "high citation count". A researcher's h-index means that he or she has published at least h papers, and each paper has been cited at least h times. If h has multiple possible values, the h index is the largest of them.

Example 1:
Input: citations = [3,0,6,1,5]
Output: 3
Explanation: The given array indicates that the researcher has a total of 5 papers, and each paper has been cited 3, 0, 6, 1, and 5 times.
Since the researcher has 3 papers that have been cited at least 3 times each, and the remaining two papers have been cited no more than 3 times, her h-index is 3.

Example 2:
Import: citations = [1,3,1]
Export: 1

class Solution(object):
    def hIndex(self, citations):
        citations.sort(reverse=True)
        for i, v in enumerate(citations):
            if v <= i:
                return i
        return i + 1


res = Solution()
citations = [1, 3, 1]
print(res.hIndex(citations))

3. 125. Verify palindrome string

Simple
712
Related Companies
If you convert all uppercase characters to lowercase characters and remove After all non-alphanumeric characters, the phrase reads the same upright or backwards. Then the phrase can be considered to be a palindrome string.

Letters and numbers are alphanumeric characters.

Given a string s, if it is a palindrome, return true; otherwise, return false.

Example 1:

Input: s = “A man, a plan, a canal: Panama”
Output: true
Explanation: “amanaplanacanalpanama” is a palindrome string.
Example 2:

Input: s = “race a car”
Output: false
Explanation: “raceacar” is not a palindrome string.
Example 3:

Input: s = " "
Output: true
Explanation: s is a null character after removing non-alphanumeric characters string"" .
Since the empty string reads the same forward and backward, it is a palindrome string.

def test5(s):
    s=s.lower()
    left = 0
    right = len(s) - 1
    while left < right:
        while not s[left].isalpha():
            left+=1
        while not s[right].isalpha():
            right-=1

        if s[left]!=s[right]:
            return False
        else:
            left+=1
            right-=1
    return True


s = " "
print(test5(s))

Insert image description here

Guess you like

Origin blog.csdn.net/YZL40514131/article/details/134658599