There are n people in a line queuing to buy tickets, where the 0<sup>th</sup> person is at the front of the line and the (n - 1)<sup>th</sup> person is at the back of the line.
You are given a 0-indexed integer array tickets of length n where the number of tickets that the i<sup>th</sup> person would like to buy is tickets[i].
Each person takes exactly 1 second to buy a ticket. A person can only buy 1 ticket at a time and has to go back to the end of the line (which happens instantaneously) in order to buy more tickets. If a person does not have any tickets left to buy, the person will leave the line.
Return the time taken for the person at positionk(0-indexed)to finish buying tickets.
Example 1:
Input: tickets = [2,3,2], k = 2 Output: 6 Explanation: - In the first pass, everyone in the line buys a ticket and the line becomes [1, 2, 1]. - In the second pass, everyone in the line buys a ticket and the line becomes [0, 1, 0]. The person at position 2 has successfully bought 2 tickets and it took 3 + 3 = 6 seconds.
Example 2:
Input: tickets = [5,1,1,1], k = 0 Output: 8 Explanation: - In the first pass, everyone in the line buys a ticket and the line becomes [4, 0, 0, 0]. - In the next 4 passes, only the person in position 0 is buying tickets. The person at position 0 has successfully bought 5 tickets and it took 4 + 1 + 1 + 1 + 1 = 8 seconds.
Constraints:
n == tickets.length1 <= n <= 100 1 <= tickets[i] <= 1000 <= k < n
deftimeRequiredToBuy(self, tickets: List[int], k: int) -> int: sum, ppl, min_tickets = 0, len(tickets), 0 while tickets[k] > 0: min_tickets = min(t for t in tickets if t > 0) sum += min_tickets * ppl tickets = [t - min_tickets for t in tickets] ppl -= tickets.count(0)
after_k = [i for i inrange(k + 1, len(tickets)) if tickets[i] >= 0] returnsum - len(after_k)
Given an array of integers citations where citations[i] is the number of citations a researcher received for their i<sup>th</sup> paper and citations is sorted in an ascending order, return compute the researcher’s h-index.
According to the definition of h-index on Wikipedia: A scientist has an index h if h of their n papers have at least h citations each, and the other n − h papers have no more than h citations each.
If there are several possible values for h, the maximum one is taken as the h-index.
You must write an algorithm that runs in logarithmic time.
Example 1:
**Input:** citations = [0,1,3,5,6]
**Output:** 3
**Explanation:** [0,1,3,5,6] means the researcher has 5 papers in total and each of them had received 0, 1, 3, 5, 6 citations respectively.
Since the researcher has 3 papers with at least 3 citations each and the remaining two with no more than 3 citations each, their h-index is 3.
Given a signed 32-bit integer x, return xwith its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-2<sup>31</sup>, 2<sup>31</sup> - 1], then return 0.
Assume the environment does not allow you to store 64-bit integers (signed or unsigned).
Example 1:
**Input:** x = 123
**Output:** 321
Example 2:
**Input:** x = -123
**Output:** -321
Example 3:
**Input:** x = 120
**Output:** 21
Example 4:
**Input:** x = 0
**Output:** 0
Constraints:
-2<sup>31</sup> <= x <= 2<sup>31</sup> - 1
题目大意:
反转整数中的数字。
数学法解题思路:
用数学方法每位取余,余数左移。另一种方法是转成字符串然后用字符串反转的方法。
与Java的区别:
不需要定义long,因为Python3所有int默认都是long
反转str一行完成,非常简洁
注意事项:
负值
溢出
Python代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
defreverse(self, x: int) -> int: res, is_negative = 0, False if x < 0: is_negative = True x = -x while x > 0: digit = x % 10 res = res * 10 + digit x //= 10 if res > pow(2, 31) - 1: return0 if is_negative: res = -res return res
字符串法解题思路:
转为字符串,然后反转。
1 2 3 4 5 6 7 8 9
defreverse(self, x: int) -> int: res, is_negative = 0, False if x < 0: is_negative = True x = -x res = int(str(x)[::-1]) if res > pow(2, 31) - 1: return0 return -res if is_negative else res
Give a dictionary of words and a sentence with all whitespace removed, return the number of sentences you can form by inserting whitespaces to the sentence so that each word can be found in the dictionary.
Example 1:
Input: “CatMat” [“Cat”, “Mat”, “Ca”, “tM”, “at”, “C”, “Dog”, “og”, “Do”] Output: 3 Explanation: we can form 3 sentences, as follows: “CatMat” = “Cat” + “Mat” “CatMat” = “Ca” + “tM” + “at” “CatMat” = “C” + “at” + “Mat”
Example 2:
Input: “a” [] Output: 0
题目大意:
一个字符串s,被“字典集合”(wordDict)中的单词拼接而成的可能性种数。
解题思路:
这是经典题。如果知道s[0:n-1)很容易知道s[0:n)是否有解,既然和子问题有关,就用DP。
定义dp[i]为字符串s[0,i)是合法分解种数。
判断一个字符串是否可以合法分解,方案是尝试在每一位进行分解,若其中一个可分解,即有解,加入到dp[i]中。 递归式为dp[i] += dp[k] * isWord(s[k:i)), 0 <= k < i.