class Solution:
def averageOfLevels(self, root: Optional[TreeNode]) -> List[float]:
def helper(qu):
if not qu:
return
i = len(qu)
leng = len(qu)
total = 0
while i > 0:
poped = qu.popleft()
total += poped.val
if poped.left:
qu.append(poped.left)
if poped.right:
qu.append(poped.right)
i -= 1
ans.append(total/leng)
helper(qu)
qu = deque()
qu.append(root)
ans = []
helper(qu)
return ans
👍2✍1
Forwarded from Codeforces Official
Codeforces Round 1011 (Div. 2) will take place on the 22nd of March at 14:35 UTC.
Please, join by the link https://codeforces.com/contests/2085?locale=en
Please, join by the link https://codeforces.com/contests/2085?locale=en
Codeforces
Codeforces Round 1011 (Div. 2) - Codeforces
Codeforces. Programming competitions and contests, programming community
Forwarded from Codeforces Official
Codeforces Round #1012 (Div. 1, Div. 2) will take place on the 23rd of March at 05:35 UTC.
Please, join by the link https://codeforces.com/contests/2089,2090?locale=en
Please, join by the link https://codeforces.com/contests/2089,2090?locale=en
Codeforces
Codeforces Round 1012 - Codeforces
Codeforces. Programming competitions and contests, programming community
▎2169. Count Operations to Obtain Zero
▎Problem
You are given two non-negative integers,
• If
• If
Repeat this until either
▎Examples
▎Example 1
Input:
Output:
▎Example 2
Input:
Output:
▎Constraints
• 0 ≤ num1, num2 ≤ 10⁵
▎Problem
You are given two non-negative integers,
num1 and num2. In one operation, do the following:• If
num1 >= num2, subtract num2 from num1.• If
num1 < num2, subtract num1 from num2.Repeat this until either
num1 or num2 becomes zero. Return the total number of operations performed.▎Examples
▎Example 1
Input:
num1 = 2, num2 = 3 Output:
3 ▎Example 2
Input:
num1 = 10, num2 = 10 Output:
1 ▎Constraints
• 0 ≤ num1, num2 ≤ 10⁵
LeetCode
Count Operations to Obtain Zero - LeetCode
Can you solve this real interview question? Count Operations to Obtain Zero - You are given two non-negative integers num1 and num2.
In one operation, if num1 >= num2, you must subtract num2 from num1, otherwise subtract num1 from num2.
* For example,…
In one operation, if num1 >= num2, you must subtract num2 from num1, otherwise subtract num1 from num2.
* For example,…
Leetcode with dani
▎2169. Count Operations to Obtain Zero ▎Problem You are given two non-negative integers, num1 and num2. In one operation, do the following: • If num1 >= num2, subtract num2 from num1. • If num1 < num2, subtract num1 from num2. Repeat this until either…
class Solution:
def countOperations(self, num1: int, num2: int) -> int:
count = 0
while num1 and num2:
if num1>=num2:
count += num1//num2
num1 = num1%num2
else:
count += num2//num1
num2 = num2%num1
return count
🔵 1823. Find the Winner of the Circular Game
📌 Problem:
There are n friends sitting in a circle, numbered 1 to n.
Starting from friend 1, count k friends clockwise (including the current one).
The k-th friend leaves the circle.
Repeat the process, starting from the next friend.
The last remaining friend is the winner.
📌 Input:
n → Number of friends.
k → Step count for elimination.
📌 Output:
The winner's number.
📌 Example 1:
🔹 Input: n = 5, k = 2
🔹 Output: 3
🔹 Explanation:
Friends leave in this order: 2 → 4 → 1 → 5 → (Winner: 3)
📌 Example 2:
🔹 Input: n = 6, k = 5
🔹 Output: 1
🔹 Explanation:
Friends leave in this order: 5 → 4 → 6 → 2 → 3 → (Winner: 1)
🔎 Can you find the last friend standing? 😃
📌 Problem:
There are n friends sitting in a circle, numbered 1 to n.
Starting from friend 1, count k friends clockwise (including the current one).
The k-th friend leaves the circle.
Repeat the process, starting from the next friend.
The last remaining friend is the winner.
📌 Input:
n → Number of friends.
k → Step count for elimination.
📌 Output:
The winner's number.
📌 Example 1:
🔹 Input: n = 5, k = 2
🔹 Output: 3
🔹 Explanation:
Friends leave in this order: 2 → 4 → 1 → 5 → (Winner: 3)
📌 Example 2:
🔹 Input: n = 6, k = 5
🔹 Output: 1
🔹 Explanation:
Friends leave in this order: 5 → 4 → 6 → 2 → 3 → (Winner: 1)
🔎 Can you find the last friend standing? 😃
Leetcode with dani
🔵 1823. Find the Winner of the Circular Game 📌 Problem: There are n friends sitting in a circle, numbered 1 to n. Starting from friend 1, count k friends clockwise (including the current one). The k-th friend leaves the circle. Repeat the process, starting…
LeetCode
Find the Winner of the Circular Game - LeetCode
Can you solve this real interview question? Find the Winner of the Circular Game - There are n friends that are playing a game. The friends are sitting in a circle and are numbered from 1 to n in clockwise order. More formally, moving clockwise from the ith…
Forwarded from Codeforces Official
Codeforces Round 1013 (Div. 3) will take place on the 25th of March at 14:35 UTC.
Please, join by the link https://codeforces.com/contests/2091?locale=en
Please, join by the link https://codeforces.com/contests/2091?locale=en
Codeforces
Codeforces Round 1013 (Div. 3) - Codeforces
Codeforces. Programming competitions and contests, programming community
Forwarded from A2SV | Africa to Silicon Valley
In-person Conversion Registration is Open! 🚀
We’re excited to welcome students for the A2SV in-person conversion! This is your chance to take your learning to the next level and be part of a vibrant tech community.
Open to current students from Addis Ababa University (AAU), Addis Ababa Science and Technology University (AASTU), and Adama Science and Technology University (ASTU).
📅 Registration Dates: March 24 - March 28
✅ Requirements:
- At least 150 solved problems
- At least 30 active days
📚 Topics to Cover: Two Pointers, Sorting, Sliding Window, Stack, Queue, Monotonicity, Linked List, Recursion.
Don’t miss this opportunity—apply now!
🔗 Apply here: link
#A2SV #TechEducation #CodingJourney #LevelUp
We’re excited to welcome students for the A2SV in-person conversion! This is your chance to take your learning to the next level and be part of a vibrant tech community.
Open to current students from Addis Ababa University (AAU), Addis Ababa Science and Technology University (AASTU), and Adama Science and Technology University (ASTU).
📅 Registration Dates: March 24 - March 28
✅ Requirements:
- At least 150 solved problems
- At least 30 active days
📚 Topics to Cover: Two Pointers, Sorting, Sliding Window, Stack, Queue, Monotonicity, Linked List, Recursion.
Don’t miss this opportunity—apply now!
🔗 Apply here: link
#A2SV #TechEducation #CodingJourney #LevelUp
👍5
Forwarded from Codeforces Official
Codeforces Round 1014 (Div. 2) will take place on the 29th of March at 14:35 UTC.
Please, join by the link https://codeforces.com/contests/2092?locale=en
Please, join by the link https://codeforces.com/contests/2092?locale=en
Codeforces
Codeforces Round 1014 (Div. 2) - Codeforces
Codeforces. Programming competitions and contests, programming community
Forwarded from Codeforces Official
🤪April Fools Day Contest 2025🤪 will take place on the 1st of April at 14:35 UTC.
Please, join by the link https://codeforces.com/contests/2095
Please, join by the link https://codeforces.com/contests/2095
Codeforces
April Fools Day Contest 2025 - Codeforces
Codeforces. Programming competitions and contests, programming community
If anyone has questions or is interviewed by A2SV today, please share the questions with me. I'll post them in this channel!
@zprogramming_bot
@zprogramming_bot
👍5❤2
Forwarded from Codeforces Official
Educational Codeforces Round 177
(rated for Div. 2) starts on the 3rd of April at 14:35 UTC.
Please, join by the link https://codeforces.com/contests/2086
(rated for Div. 2) starts on the 3rd of April at 14:35 UTC.
Please, join by the link https://codeforces.com/contests/2086
Codeforces
Educational Codeforces Round 177 (Rated for Div. 2) - Codeforces
Codeforces. Programming competitions and contests, programming community
Question Denoscription
You are given a 0-indexed string s consisting of only lowercase English letters. Return the number of substrings in s that begin and end with the same character.
A substring is a contiguous non-empty sequence of characters within a string.
Example 1:
Input: s = "abcba" #"a", "b", "c", "b","a",abcba","bcb" a:2 b:2 c : 1
Output: 7
Explanation:
The substrings of length 1 that start and end with the same letter are: "a", "b", "c", "b", and "a".
The substring of length 3 that starts and ends with the same letter is: "bcb".
The substring of length 5 that starts and ends with the same letter is: "abcba".
Example 2:
Input: s = "abacad"
Output: 9
Explanation:
The substrings of length 1 that start and end with the same letter are: "a", "b", "a", "c", "a", and "d".
The substrings of length 3 that start and end with the same letter are: "aba" and "aca".
The substring of length 5 that starts and ends with the same letter is: "abaca".
Example 3:
Input: s = "a"
Output: 1
Explanation:
The substring of length 1 that starts and ends with the same letter is: "a".
Constraints:
1 <= s.length <= 105
s consists only of lowercase English letters
You are given a 0-indexed string s consisting of only lowercase English letters. Return the number of substrings in s that begin and end with the same character.
A substring is a contiguous non-empty sequence of characters within a string.
Example 1:
Input: s = "abcba" #"a", "b", "c", "b","a",abcba","bcb" a:2 b:2 c : 1
Output: 7
Explanation:
The substrings of length 1 that start and end with the same letter are: "a", "b", "c", "b", and "a".
The substring of length 3 that starts and ends with the same letter is: "bcb".
The substring of length 5 that starts and ends with the same letter is: "abcba".
Example 2:
Input: s = "abacad"
Output: 9
Explanation:
The substrings of length 1 that start and end with the same letter are: "a", "b", "a", "c", "a", and "d".
The substrings of length 3 that start and end with the same letter are: "aba" and "aca".
The substring of length 5 that starts and ends with the same letter is: "abaca".
Example 3:
Input: s = "a"
Output: 1
Explanation:
The substring of length 1 that starts and ends with the same letter is: "a".
Constraints:
1 <= s.length <= 105
s consists only of lowercase English letters
Forwarded from Ibnu-Nezif Abasambi
I HAVE INTERVIEWED ON A2SV ON THURSDAY AND THE QUESTION WAS
/*
Question Denoscription
Given a string s and an integer k, return the number of substrings in s of length k with no repeated characters.
Example 1:
Input: s = "havefunonleetcode", k = 5
Output: 6
Explanation: There are 6 substrings they are: 'havef','avefu','vefun','efuno','etcod','tcode'.
Example 2:
Input: s = "home", k = 5
Output: 0
Explanation: Notice k can be larger than the length of s. In this case, it is not possible to find any substring.
Constraints:
1 <= s.length <= 104
s consists of lowercase English letters.
1 <= k <= 104
*/
FINALLY I HAVE SOLVED THE QUESTION LIKE THIS IN C++ MAY BE IT IS USEFUL
/*
Question Denoscription
Given a string s and an integer k, return the number of substrings in s of length k with no repeated characters.
Example 1:
Input: s = "havefunonleetcode", k = 5
Output: 6
Explanation: There are 6 substrings they are: 'havef','avefu','vefun','efuno','etcod','tcode'.
Example 2:
Input: s = "home", k = 5
Output: 0
Explanation: Notice k can be larger than the length of s. In this case, it is not possible to find any substring.
Constraints:
1 <= s.length <= 104
s consists of lowercase English letters.
1 <= k <= 104
*/
FINALLY I HAVE SOLVED THE QUESTION LIKE THIS IN C++ MAY BE IT IS USEFUL
#include <bits/stdc++.h>
using namespace std;
class Solution {
public :
int solve (string s,int k){
vector <int> freq(26);
int l=0;int r=0;
int offset=(int)'a';
int ans=0;
while (r<s.size()){
if (r-l<k-1){
freq[s[r]-offset]++;
r++;
}else if (r-l==k-1){
freq[s[r]-offset]++;
r++;
bool t=true;
for (int x:freq){
if (x>1){
t=false;
}
}
t?ans+=1:ans=ans;
}else if (r-l>k-1){
freq[s[l]-offset]--;
l++;
}
}
return ans;
}
};
int main () {
string s="abcdefhijklmnopqrstuvwxyz";
int k=5;
Solution* sol=new Solution();
cout<<sol->solve(s,k);
}
❤4⚡1