leetcode (402 && 406) two greedy

Both codes are entirely their own independent thought out code pretty simple ... refueling ~ mt

leetcode 402

Given a non-negative integer num as a string, removing the k-bit digital numbers such that the minimum remaining figures.

note:

num is less than 10002 and a length of ≥ k.
num does not contain any leading zeros.
Example 1:

Input: num = "1432219", k = 3
Output: "1219"
explanation: the number 4 is removed out of three, 3, 2, and form a new minimum number 1219.
Example 2:

Input: num = "10200", k = 1
Output: "200"
Explanation: the first definite displacement of the remaining output number 1 to 200. Note that there can be no leading zeros.
Example 3:

Input: num = "10", k = 2
Output: "0"
explanation: remove all numbers from the original figures, the remaining blank is 0.

Core: Conversion Problems - select sequences of length len (nums) -k is the lexicographically smallest sequence

 1 class Solution:
 2     def removeKdigits(self, num: str, k: int) -> str:
 3         ans = []
 4         lenth = len(num)-k
 5         for it in num:
 6             while k and ans and ans[-1]>it:
 7                 ans.pop()
 8                 k -= 1
 9             ans.append(it)
10         ans = ans[:lenth]
11         pos = 0
12         whilepos <len (years) and years [pos] == ' 0 ' :
 13              pos + = 1 
14          years = years [pos]
 15          return  "" .join (years) if len (years) else  ' 0 '

leetcode  406

Suppose scrambled group of people standing in a queue. Each person is represented by a pair of integers (h, k), where h is the person's height, k is the row in front of the person and the height h is greater than or equal to the number. Write an algorithm to reconstruct the queue.

Note:
The total number fewer than 1,100 people.

Examples

Input:
[[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]

Output:
[[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]]

class Solution:
    def reconstructQueue(self, people):
        people.sort(key=lambda x: (-x[0], x[1]))
        ans = []
        for it in people:
            ans.insert(it[1], it)
        return ans

 

Guess you like

Origin www.cnblogs.com/xidian-mao/p/11420327.html