2024 June 7 Problems

56 views
Skip to first unread message

daryl...@gmail.com

unread,
Jun 7, 2025, 12:41:50 PMJun 7
to leetcode-meetup
Feel free to work on any of the problems you want; we'll have people present their solutions at 11:30.

Please post your solutions to this thread so others can use this as a reference.



3337. Total Characters in String After Transformations II Hard 58.3%


Please download and import the following iCalendar (.ics) files to your calendar system.



FloM

unread,
Jun 7, 2025, 1:00:32 PMJun 7
to leetcode-meetup
Time: O(n) Memory: O(1)
class Solution {
public:
vector<int> getSneakyNumbers(vector<int>& nums) {
unordered_set<int> seen{};
vector<int> twice{};
for(int ii=0; ii<nums.size(); ++ii)
{
if(seen.find(nums[ii]) != seen.end())
{
twice.push_back(nums[ii]);
}
seen.insert(nums[ii]);
}

return twice;
}
};

Anuj Patnaik

unread,
Jun 7, 2025, 1:25:26 PMJun 7
to leetcode-meetup
class Solution:
def getSneakyNumbers(self, nums: List[int]) -> List[int]:
n = len(nums) - 2
expected_sum = n*(n-1)//2
expected_sos_sum = n*(2*n-1)*(n-1)//6
actual_sum = sum(nums)
actual_sos_sum = 0
for i in nums:
actual_sos_sum += i*i
d1 = actual_sum - expected_sum
d2 = actual_sos_sum - expected_sos_sum
#Solving the equation (t - x)(t - y) = 0
xy = (d1*d1 - d2)//2
discriminant = int(sqrt(d1*d1 - 4*xy))
x = (d1 + discriminant) // 2
y= (d1 - discriminant) // 2
return [x,y]

FloM

unread,
Jun 7, 2025, 2:23:05 PMJun 7
to leetcode-meetup
Had to look at the Editorial Answer. Time: O(t) Mem: O(1)
class Solution {
public:
int lengthAfterTransformations(string s, int t) {
int modulo = 1000000007;

vector<int> charCount(26, 0);

for(const char& x : s)
{
++charCount[x - 'a'];
}

for(int ii=0; ii<t; ++ii)
{
int origNumOfA = charCount[0];
int origNumOfB = charCount[1];
int origNumOfZ = charCount[25];

// charCount for A, B, and C
charCount[0] = origNumOfZ % modulo;
charCount[1] = (origNumOfA + origNumOfZ) % modulo;
for(int ii=25; ii>=3; --ii)
{
charCount[ii] = charCount[ii-1] % modulo;
}

charCount[2] = origNumOfB;
}

int count = 0;

for(int x : charCount)
{
count = (count + x) % modulo;
}

return count;
}
};

Sharad Bagri

unread,
Jun 7, 2025, 2:28:04 PMJun 7
to leetcode-meetup
class Solution:
def getSneakyNumbers(self, nums: List[int]) -> List[int]:
sneaky = []
seen = set()
for num in nums:
if num in seen:
sneaky.append(num)
else:
seen.add(num)
return sneaky
from collections import Counter
import copy

class Solution:
def lengthAfterTransformations(self, s: str, t: int) -> int:
cnt = Counter(s)
a_to_y = list('abdcefghijklmnopqrstuvwxy')
for i in range(t):
new_cnt = Counter()
for a_char in a_to_y:
if a_char in cnt:
new_cnt[chr(ord(a_char) + 1)] = cnt[a_char]
if 'z' in cnt:
new_cnt['a'] += cnt['z']
new_cnt['b'] += cnt['z']
cnt = new_cnt
return cnt.total() % ((10**9) + 7)

Reply all
Reply to author
Forward
0 new messages