# preludes # from collections import * classSolution: defmostFrequentEven(self, nums: List[int]) -> int: try: returnmax(Counter([i for i in nums ifnot i % 2]).items(), key=lambda x:(x[1], -x[0]))[0] except: return -1
In this problem, we are trying to get the most frequent even number
in a sequence, if there's multiple most frequent answers, we take the
smallest one. E.g. for a sequence of number ,
the smallest of most frequent numbers is .
We first use a list comprehension to get the even values followed by
putting them on the init values of Counter() class from the
collections module to get a counter for the list of
numbers. Then use .items() method to convert the
Counter object, which is actually a dict, to a
list to tuples that looks like (number, frequency). We then
use a max function we a key argument to get the most
frequent element and the smallest one. What we do here is to create a
tuple for each key, x[1] means we want the most frequent
ones first, -x[0] means we want the smallest number first.
Putting them in a tuple means we want the one that fulfils both
conditions but the first one has a higher priority. We then ask for the
first item in the sorted list and return that value. Finally, we wrap
the whole line with a try and except block
because if there's no even number, the length of the list comprehension
will be zero, and max function will raise an error.
Original Problem:
https://leetcode.com/contest/weekly-contest-311/problems/sum-of-prefix-scores-of-strings/
For a string , we need to find
all its prefixes and all strings with the same prefix. We can achieve
this goal by using a trie tree with a meta tag in each node
to record the time for a specific prefix to happen in all strings.
classSolution: defsumPrefixScores(self, words: List[str]) -> List[int]: tree = {} # build tree for word in words: cur = tree for i in word: if cur.get(i): cur = cur[i] cur['meta'] += 1 else: cur[i] = {} cur = cur[i] cur['meta'] = 1 # calculate ans = [] for word in words: t = 0 cur = tree for i in word: cur = cur[i] t += cur['meta'] ans.append(t) return ans
After getting all of the input, sort the array reversely and
calculate prefix sum of it. For each pair of and , the maximum value of free item would
be .
For character at a index ,
check where . If result of q2 equals
to the unique letters in the range, the letter at must equal to the letter on
position`. We can then use a
binary search to improve this algorithm to decrease the amount of query
for each letter to . If
there is no such result even when , the letter must be a new letter. Use q1 on it.
For number and , we can found that ones in plus ones in , equals to the ones in plus ones in .
E.g. for and , their binary formats are and ,
(3 & 2).bit_count() equals to 1,
(3 | 2).bit_count() equals to 2.
Thus, for this problem, we need to find any pair of numbers where the
sum of their bit_count() is greater or equal than . We can use a binary search algorithm
to achieve this.
class Solution: def countExcellentPairs(self, nums: List[int], k: int) -> int: res = 0 nums = set(nums)
def bitcount(x: int): c = 0 while x: c += x & 1 x >>= 1 return c
length = len(nums) binl = sorted([bitcount(i) for i in nums]) for ii, i in enumerate(binl): pos = bisect.bisect_left(binl, k - i) t = length - pos res += t return res
This is the minimum template that only implements
BufferedReader and BufferedWriter. Unlike
std::io::BufReader, you can simply use
sc.nextInt() to get next int.
git clone https://github.com/spihere/shortlink-py-deta deta new -p shortlink-py-deta cd shortlink-py-deta deta deploy
The deta new command returns a json formatted string
that contains the URL for your instance.
use [your-instance-url]/link to access a short link,
[url]/new?src=[alias]&to=[target] to create a
shortlink, [url]/del?src=[alias]to delete a shortlink.
By adding DBConnections implementation in the db folder, this
application can support other databases. You can do it by creating a new
file in db folder and implements a class that inherits
DBCOnnection abstract class. You can then change the
database variable in main.py to anything you
want.
For each pair of pair and
, if b equals to zero. have to be subtracted at least b times,
but have no limit on how high it goes. If is not zero, but be greater or equal than , also must be equal to the on other pairs where
is not zero.