Day 26 of LeetCode Challenge

Cloud and DevOps Engineer with hands-on expertise in AWS, CI/CD pipelines, Docker, Kubernetes, and Monitoring tools. Adept at building and automating scalable, fault-tolerant cloud infrastructures, and consistently improving system performance, security, and reliability in dynamic environments.
Problem 1: Counting Bits
Link to the problem: leetcode.com/problems/counting-bits/

class Solution {
public int[] countBits(int n) {
int[] ans = new int[n+1];
ans[0] = 0;
int num=1;
for(int i=1; i<ans.length; i++){
if(num*2==i)
num = num*2;
ans[i] = ans[i-num]+1;
}
return ans;
}
}
Problem 2: Flood Fill
Link to the problem: https://leetcode.com/problems/flood-fill/

class Solution {
public int[][] floodFill(int[][] image, int sr, int sc, int color) {
int temp=image[sr][sc];
image[sr][sc]=color;
int m=image.length;
int n=image[0].length;
if (temp == color) return image;
if(sc+1<n && image[sr][sc+1]==temp){
floodFill(image,sr,sc+1,color);
}
if(sc-1>=0 && image[sr][sc-1]==temp){
floodFill(image,sr,sc-1,color);
}
if(sr-1>=0 && image[sr-1][sc]==temp){
floodFill(image,sr-1,sc,color);
}
if(sr+1<m && image[sr+1][sc]==temp){
floodFill(image,sr+1,sc,color);
}
return image;
}
}
Problem 3: Roman to Integer
Link to the problem: https://leetcode.com/problems/roman-to-integer/

class Solution {
public int romanToInt(String s) {
int ans = 0;
for(int i=0; i<s.length(); i++){
if(s.charAt(i)=='M')
ans+=1000;
else if(s.charAt(i)=='D')
ans+=500;
else if(s.charAt(i)=='C'){
if(i==s.length()-1)
ans+=100;
else{
if(s.charAt(i+1)=='D'){
ans+=400;
i++;
}else if(s.charAt(i+1)=='M'){
ans+=900;
i++;
}else
ans+=100;
}
}else if(s.charAt(i)=='L')
ans+=50;
else if(s.charAt(i)=='X'){
if(i==s.length()-1)
ans+=10;
else{
if(s.charAt(i+1)=='L'){
ans+=40;
i++;
}else if(s.charAt(i+1)=='C'){
ans+=90;
i++;
}else
ans+=10;
}
}else if(s.charAt(i)=='V')
ans+=5;
else{
if(i==s.length()-1)
ans+=1;
else{
if(s.charAt(i+1)=='V'){
ans+=4;
i++;
}else if(s.charAt(i+1)=='X'){
ans+=9;
i++;
}else
ans+=1;
}
}
}
return ans;
}
}
Problem 4: Single Number
Link to the problem: https://leetcode.com/problems/single-number/

class Solution {
public int singleNumber(int[] nums) {
int xor = 0;
for(int num :nums){
xor ^= num;
}
return xor;
}
}
Problem 5: Missing Number
Link to the problem: https://leetcode.com/problems/missing-number/

class Solution {
public int missingNumber(int[] nums) {
int sum = (nums.length*(nums.length+1))/2;
int num = 0;
for(int i:nums)
num+=i;
return sum-num;
}
}




