π» A2SV prep: Sliding window problem
β The following questions should be done sequentially. Level 1 and Level 2
π’ 3206. Alternating Groups I Easy
πLink: Click here!
π A more optimized version
π‘ 3208. Alternating Groups II ~ 1800 ( Hard medium )
π Link: Click here!
ππ @AceCoding Presents! ππ
β The following questions should be done sequentially. Level 1 and Level 2
π’ 3206. Alternating Groups I Easy
πLink: Click here!
python
class Solution:
def numberOfAlternatingGroups(self, colors: List[int]) -> int:
count = 0
# check the end colors
if len(colors) > 2:
if colors[0] != colors[1] and colors[1] == colors[-1]:
count += 1
if colors[-2] != colors[-1] and colors[-2] == colors[0]:
count += 1
l, r = 0, 0
k = 3
for r in range(len(colors)):
if r - l + 1 == 3:
l += 1
if r < len(colors) - 1 and colors[l] == colors[r+1] and colors[l] != colors[r]:
count += 1
return count
π A more optimized version
class Solution:
def numberOfAlternatingGroups(self, colors: List[int]) -> int:
n = len(colors) # keep the orginal length for latter
count = 0
# extend the colors array to simulate a circular pattern
colors.extend(colors[:2])
for r in range(n):
if colors[r] == colors[r+2] and colors[r] != colors[r+1]:
count += 1
return count
π‘ 3208. Alternating Groups II ~ 1800 ( Hard medium )
π Link: Click here!
python
class Solution:
def numberOfAlternatingGroups(self, colors: List[int], k: int) -> int:
count = 0
colors.extend(colors[:k-1]) # if k = 3 we add 2 numbers like the above question
print(colors)
L = 0
for R in range(1, len(colors)):
if colors[R] == colors[R-1]:
L = R
if R - L + 1 == k:
count += 1
L += 1
return count
ππ @AceCoding Presents! ππ
LeetCode
Alternating Groups I - LeetCode
Can you solve this real interview question? Alternating Groups I - There is a circle of red and blue tiles. You are given an array of integers colors. The color of tile i is represented by colors[i]:
* colors[i] == 0 means that tile i is red.
* colors[i]β¦
* colors[i] == 0 means that tile i is red.
* colors[i]β¦
π» A2SV prep: Sliding window problem
π‘ 3191. Minimum Operations to Make Binary Array Elements Equal to One I
π Link: Click here
ππ @AceCoding Presents! ππ
π‘ 3191. Minimum Operations to Make Binary Array Elements Equal to One I
π Link: Click here
class Solution:
def minOperations(self, nums: List[int]) -> int:
n = len(nums)
count = L =0
for R in range(n - 2):
if nums[R] == 0:
nums[R] = 1 - nums[R]
nums[R + 1] = 1 - nums[R + 1]
nums[R + 2] = 1 - nums[R + 2]
count += 1
if nums[L] == 1: L+= 1
for i in range(len(nums)):
if nums[i] == 0:
return -1
return count
ππ @AceCoding Presents! ππ
LeetCode
Minimum Operations to Make Binary Array Elements Equal to One I - LeetCode
Can you solve this real interview question? Minimum Operations to Make Binary Array Elements Equal to One I - You are given a binary array nums.
You can do the following operation on the array any number of times (possibly zero):
* Choose any 3 consecutiveβ¦
You can do the following operation on the array any number of times (possibly zero):
* Choose any 3 consecutiveβ¦
π₯ Gear Up for Java Exam! QUIZ Time π₯
Get ready for a detailed and comprehensive Java prep with tricky questions.
π‘ Donβt miss outβshare with friends and tackle it together! π
#javaexam #oopexam #oopquiz #javaquiz #oopmidexam #javamidexam
ππ @AceCoding Presents! ππ
Get ready for a detailed and comprehensive Java prep with tricky questions.
π‘ Donβt miss outβshare with friends and tackle it together! π
#javaexam #oopexam #oopquiz #javaquiz #oopmidexam #javamidexam
ππ @AceCoding Presents! ππ
Java provides automatic memory management using garbage collection.
Anonymous Quiz
90%
True
10%
False
What is the correct file extension for compiled Java files?
Anonymous Quiz
61%
a) .java
30%
b) .class
4%
c) .jar
4%
d) .exe
Which keyword is used to inherit a class in Java?
Anonymous Quiz
31%
a) extend
57%
b) extends
4%
c) implement
8%
d) inherits
What is the default value of a boolean in Java?
Anonymous Quiz
20%
a) true
63%
b) false
10%
c) null
8%
d) 0
Which package is imported by default in Java?
Anonymous Quiz
35%
a) java.util
38%
b) java.lang
22%
c) java.io
5%
d) java.net
What is Math.PI in Java?
Anonymous Quiz
31%
a) A method
47%
b) A constant
2%
c) A variable
20%
d) A class
What is the purpose of the final keyword?
Anonymous Quiz
54%
a) Define constants
6%
b) Prevent inheritance
13%
c) Prevent method overriding
27%
d) All of the above
Which of these is NOT a valid Java keyword?
Anonymous Quiz
2%
a) static
0%
b) void
10%
c) null
88%
d) include
Java programs are compiled into ______.
Anonymous Quiz
72%
a) Bytecode
17%
b) Machine code
6%
c) Source code
6%
d) Executables
The default access modifier in Java is ______.
Anonymous Quiz
31%
a) public
6%
b) private
4%
c) protected
11%
d) package-private
49%
e) Default
The main method in Java must return ______.
Anonymous Quiz
4%
a) int
79%
b) void
13%
c) String
4%
d) boolean
The Scanner class is located in the ______ package.
Anonymous Quiz
4%
a) java.io
94%
b) java.util
2%
c) java.lang
0%
d) java.net
Why is the main method static in Java?
Anonymous Quiz
68%
a) To allow it to be called without creating an object
15%
b) To ensure it can access instance variables
0%
c) To make it faster
18%
d) To ensure it cannot be overridden
π1