[Leetcode Series] [183] algorithm week tournament games

table of Contents

 

Topic one: non-increasing order of the smallest sub-sequence

Problem-solving ideas:

Code:

Title II: The binary representation of the number of steps is reduced to 1

Problem-solving ideas:

method one:

Method Two:

Code:

method one:

Method Two:

Topic three: Happy longest string

Problem-solving ideas:

Code:

Topic Four: stone Game III

Problem-solving ideas:

Code:


Topic one: non-increasing order of the smallest sub-sequence

Topic links:  https://leetcode-cn.com/problems/minimum-subsequence-in-non-increasing-order/

 

Problem-solving ideas:

Sorting, from the forward to numbers, each take a judgment is greater than half the sum

If so, the result is returned

 

Code:

class Solution:
    def minSubsequence(self, nums: List[int]) -> List[int]:
        nums.sort()
        total = sum(nums)
        res = []
        for index in range(len(nums) - 1, -1, -1):
            res.append(nums[index])
            if sum(res) > total / 2:
                break
                
        return res

 

Title II: The binary representation of the number of steps is reduced to 1

Topic links:  https://leetcode-cn.com/problems/number-of-steps-to-reduce-a-number-in-binary-representation-to-one/

 

Problem-solving ideas:

method one:

The binary string converted to int variables, in accordance with rules each simulation step, and the number of superimposed

Method Two:

Traversal from back to front each, add a carry identification. Carry flag when traversing together, there will be the following situations:

  1. 0: Description is even, the number of steps 1 +
  2. 1: Description is odd, the current need for digital addition After 1 + 2, in order to eliminate this bit, steps 2 +, carry = 1
  3. 2: Description originally an odd number, but after adding the carry becomes even number, step number + 1, carry = 1

 

Code:

method one:

class Solution:
    def numSteps(self, s: str) -> int:
        num = int(s, 2)
        count = 0
        while num > 1:
            if num % 2 == 0:
                num //= 2
            else:
                num += 1
                
            count += 1
            
        return count

 

Method Two:

class Solution:
    def numSteps(self, s: str) -> int:
        count = 0
        add_num = 0
        for index in range(len(s) - 1, -1, -1):
            num = int(s[index]) + add_num
            add_num = 0
            if index == 0 and add_num == 0 and num == 1:
                break

            if 0 == num:
                count += 1
            elif 1 == num:
                count += 2
                add_num = 1
            else:
                count += 1
                add_num = 1
                
        return count

 

Topic three: Happy longest string

Topic links:  https://leetcode-cn.com/problems/longest-happy-string/

 

Problem-solving ideas:

The remaining stitching may be sorted according to the current number of characters, in descending determines whether the current character splice

If splicing, the splicing of a single character, updates the remaining number

If not spliced, get next character in sequence

Until all the characters are all characters can not be spliced ​​or run out

 

Code:

class Solution:
    def longestDiverseString(self, a: int, b: int, c: int) -> str:
        num_lst = {'a' : a, 'b' : b, 'c' : c}
        res = ''
        while sum(num_lst.values()) > 0:
            stop = False
            for a in sorted(num_lst.items(), key = lambda item: item[1], reverse = True):
                if res[-2:] == a[0] * 2 or a[1] <= 0:
                    stop = True
                    continue
                    
                res += a[0]
                num_lst[a[0]] -= 1
                stop = False
                break

            if stop:
                break
                    
        return res

 

Topic Four: stone Game III

Topic links:  https://leetcode-cn.com/problems/stone-game-iii/

 

Problem-solving ideas:

Game theory type of problem, dynamic programming, recursion from back to front

Dynamic Programming array dp first index position saved, if during the remaining pile of stones from the index to last, be able to get a maximum score is the number

When the rest of the last time, because you must handle a so last dp array, the original array is equal to the last bit

After the forward recursion, add the sum to the current position from the last time recursion, when the first index update bit dynamic array of programming logic is as follows:

variable:

  1. index: The index to the current recurrence
  2. dp: an array of dynamic programming (multi-application ratio stoneValue 3, figures are 0. This can not be special treatment for subscript)
  3. curr_sum: From the last bit of the sum to index
  4. stoneValue: original array, the array is assumed here [1, 2, 3, -1, -2, -3, 7]

Logic (stoneValue traversal from back to front):

  1. 更新curr_sum = curr_sum + stoneValue[index] = 0 + 7 = 7
  2. Initialization dp [index] = float ( '- inf') infinitesimal
  3. Updating logic dp [index] in mind the following table, the update equation of dp [index] = curr_sum - min (dp [index + 1], dp [index + 2], dp [index + 3]), the purpose of It is capable of making their own value as quickly as possible to get the maximum
Your behavior The other began subscript The other side of the maximum value Your maximum
Take a index + 1 dp[index + 1] curr_sum - dp[index + 1]
Take 2 index + 2 dp[index + 2] curr_sum - dp[index + 2]
Take 3 index + 3 dp[index + 3] curr_sum - dp[index + 3]
Continuing the above steps until traversed dp [0]

Because Alice always start first, so Alice can obtain the maximum score of dp [0], Bob can get the maximum score of curr_sum - dp [0]

Then the score is determined to Alice and Bob

 

Code:

class Solution:
    def stoneGameIII(self, stoneValue: List[int]) -> str:
        dp = [0] * (len(stoneValue) + 3)
        curr_sum = 0
        for index in range(len(stoneValue) - 1, -1, -1):
            dp[index] = float('-inf')
            curr_sum += stoneValue[index]
            dp[index] = curr_sum - min([dp[index + 1], dp[index + 2], dp[index + 3]])
        
        alice_score = dp[0]
        bob_score = curr_sum - dp[0]
        if alice_score > bob_score:
            return 'Alice'
        elif alice_score < bob_score:
            return 'Bob'
        
        return 'Tie'

 

Published 100 original articles · won praise 4 · Views 1467

Guess you like

Origin blog.csdn.net/songyuwen0808/article/details/105326992