3086. Minimum Moves to Pick K Ones
You are given a 0-indexed binary array
nums
of lengthn
, a positive integerk
and a non-negative integermaxChanges
.Dylan Smith plays a game, where the goal is for Dylan to pick up
k
ones fromnums
using the minimum number of moves. When the game starts, Dylan picks up any indexdylanIndex
in the range[0, n - 1]
and stands there. Ifnums[dylanIndex] == 1
, Dylan picks up the one andnums[dylanIndex]
becomes0
(this does not count as a move). After this, Dylan can make any number of moves (including zero) where in each move Dylan must perform exactly one of the following actions:
- Select any index
j != dylanIndex
such thatnums[j] == 0
and setnums[j] = 1
. This action can be performed at mostmaxChanges
times.- Select any two adjacent indices
x
andy
(|x - y| == 1
) such thatnums[x] == 1
,nums[y] == 0
, then swap their values (setnums[y] = 1
andnums[x] = 0
). Ify == dylanIndex
, Dylan picks up the one after this move andnums[y]
becomes0
.Return the minimum number of moves required by Dylan to pick exactly
k
ones.
3085. Minimum Deletions to Make String K-Special
You are given a string
word
and an integerk
.We consider
word
to be k-special if|freq(word[i]) - freq(word[j])| <= k
for all indicesi
andj
in the string.Here,
freq(x)
denotes the frequency of the characterx
inword
, and|y|
denotes the absolute value ofy
.Return the minimum number of characters you need to delete to make
word
k-special\.
3084. Count Substrings Starting and Ending with Given Character
You are given a string
s
and a characterc
. Return the total number of substrings ofs
that start and end withc
.
3083. Existence of a Substring in a String and Its Reverse
Given a string
s
, find any substring of length2
which is also present in the reverse ofs
.Return
true
if such a substring exists, andfalse
otherwise.
3082. Find the Sum of the Power of All Subsequences
- User Accepted:924
- User Tried:1995
- Total Accepted:987
- Total Submissions:4186
- Difficulty:**Hard**
You are given an integer array
nums
of lengthn
and a positive integerk
.The power of an array of integers is defined as the number of subsequences with their sum equal to
k
.Return the sum of power of all subsequences of
nums
.Since the answer may be very large, return it modulo
109 + 7
.
3081. Replace Question Marks in String to Minimize Its Value
You are given a string
s
.s[i]
is either a lowercase English letter or'?'
.For a string
t
having lengthm
containing only lowercase English letters, we define the functioncost(i)
for an indexi
as the number of characters equal tot[i]
that appeared before it, i.e. in the range[0, i - 1]
.The value of
t
is the sum ofcost(i)
for all indicesi
.For example, for the string
t = "aab"
:
cost(0) = 0
cost(1) = 1
cost(2) = 0
- Hence, the value of
"aab"
is0 + 1 + 0 = 1
.Your task is to replace all occurrences of
'?'
ins
with any lowercase English letter so that the value ofs
is minimized.Return a string denoting the modified string with replaced occurrences of
'?'
. If there are multiple strings resulting in the minimum value, return the lexicographically smallest one.
3080. Mark Elements on Array by Performing Queries
You are given a 0-indexed array
nums
of sizen
consisting of positive integers.You are also given a 2D array
queries
of sizem
wherequeries[i] = [indexi, ki]
.Initially all elements of the array are unmarked.
You need to apply
m
queries on the array in order, where on theith
query you do the following:
- Mark the element at index
indexi
if it is not already marked.- Then mark
ki
unmarked elements in the array with the smallest values. If multiple such elements exist, mark the ones with the smallest indices. And if less thanki
unmarked elements exist, then mark all of them.Return an array answer of size
m
whereanswer[i]
is the sum of unmarked elements in the array after theith
query.