[Algorithm] Algorithm question-20231205

1. LCS 01. Download plug-in

Simple
Xiaokou plans to install and use plug-ins for his VS code. In the initial state, the bandwidth can complete the download of 1 plug-in per minute. Assume that one of the following two strategies is chosen every minute:
Use current bandwidth to download plug-ins
Double the bandwidth (and therefore double the number of downloaded plug-ins)< a i=4> Please return to Xiaokou for the minimum number of minutes it takes to download n plug-ins. Note: The actual number of downloaded plug-ins can exceed n

Example 1:
Input: n = 2
Output: 2
Explanation: The following two solutions, Can download 2 plug-ins within 2 minutes
Option 1: Double the bandwidth in the first minute, and the bandwidth can download 2 plug-ins per minute; download 2 plug-ins in the second minute
Option 2: Download 1 plug-in in the first minute and 1 plug-in in the second minute

Example 2:
Input: n = 4
Output: 3
Explanation: It takes at least 3 minutes to complete To complete the download of 4 plug-ins, the following is one of the solutions: the bandwidth is doubled in the first minute, and the bandwidth can download 2 plug-ins per minute; 2 plug-ins are downloaded in the second minute; 2 plug-ins are downloaded in the third minute.

Problem-solving ideas
The logic seems to be this: if it takes two minutes to download, then it will definitely take double + 1 minute to complete the download. If it takes 4 minutes to download, then it will definitely be doubled twice + 1 minute to download. Therefore, doubling is always the optimal solution. So, double

class Solution:
    def leastMinutes(self, n: int) -> int:
        time_count = 0
        width = 1
        while n > width:
            width = width * 2
            time_count = time_count + 1
        time_count = time_count + 1
        return time_count

2. Given a list of numbers, please move all 0s in the list to the right

Given a list of numbers, move all 0s in the list to the right.
For example, move_zeros([1, 0, 1, 2, 0, 1, 3]), the expected return result is: [1, 1, 2, 1, 3, 0, 0]

def test(nums):
    fast = 0
    slow = 0
    while fast < len(nums):
        if nums[fast]:
            nums[slow], nums[fast] = nums[fast], nums[slow]
            slow += 1
        fast += 1
    return nums


nums = [1, 0, 1, 2, 0, 1, 3]
print(test(nums))

3. Implement a trim() function to remove spaces at the beginning and end of the string (the strip() method cannot be used)

Title: Implement a trim() function to remove spaces at the beginning and end of a string (strip() method cannot be used)
Example:
1. If the string is empty, enter trim(' ') and the expected return result is ''
2. If the number of spaces at the beginning and end of the string is greater than 1, enter trim(' a bc ') Expected return result 'a bc'

def trim(super):
    a = 0
    b = 0
    for i in range(len(super)):
        if super[i] == ' ':
            a += 1
        else:
            break
    for i in range(len(super)):
        if super[-(i + 1)] == ' ':
            b += 1
        else:
            break
    print(super[a:-b])


trim('        23 2    1231  123  ')

Insert image description here

Guess you like

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