Day 29 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: Find Lucky Integer in an Array
Link to the problem: https://leetcode.com/problems/find-lucky-integer-in-an-array/

class Solution {
public int findLucky(int[] arr) {
Map<Integer, Integer> map = new HashMap<>();
for(int i:arr){
if(map.containsKey(i))
map.put(i, map.get(i)+1);
else
map.put(i, 1);
}
int max = -1;
for(Map.Entry e:map.entrySet()){
if(((Integer)e.getValue()).intValue()==((Integer)e.getKey()).intValue())
max = max>((Integer)e.getValue()).intValue()?max:((Integer)e.getValue()).intValue();
}
return max;
}
}
Problem 2: Find the Kth Character in String Game II
Link to the problem: https://leetcode.com/problems/find-the-k-th-character-in-string-game-ii/

class Solution {
public char kthCharacter(long k, int[] operations) {
int shift = 0;
List<Long> lengths = new ArrayList<>();
long len = 1;
for (int op : operations) {
len *= 2;
lengths.add(len);
if (len >= k) break;
}
for (int i = lengths.size() - 1; i >= 0; i--) {
long half = lengths.get(i) / 2;
int op = operations[i];
if (k > half) {
k -= half;
if (op == 1) shift++;
}
}
return (char) ((('a' - 'a' + shift) % 26) + 'a');
}
}
Problem 3: Longest Consecutive Sequence
Link to the problem: https://leetcode.com/problems/longest-consecutive-sequence/

class Solution {
public int longestConsecutive(int[] nums) {
Map<Integer, Integer> map = new HashMap<>();
for(int i:nums)
map.put(i, 1);
TreeMap<Integer, Integer> sortedMap = new TreeMap<>(map);
System.out.println(sortedMap);
int num = Integer.MIN_VALUE;
int count = 0;
int ans = Integer.MIN_VALUE;
for(Map.Entry e:sortedMap.entrySet()){
int n = ((Integer) e.getKey()).intValue();
if(num==Integer.MIN_VALUE){
num = n;
count++;
}else{
if(n==num+1){
num = n;
count++;
}else{
num=n;
ans = ans>count?ans:count;
count=1;
}
}
}
return ans>count?ans:count;
}
}
Problem 4: Rotate Array
Link to the problem: https://leetcode.com/problems/rotate-array/

class Solution {
public void rotate(int[] nums, int k) {
k=k%nums.length;
if(nums.length<2)
return;
reverseArray(nums, 0, nums.length-k-1);
reverseArray(nums, nums.length-k, nums.length-1);
reverseArray(nums, 0, nums.length-1);
}
private void reverseArray(int[] nums, int start, int last){
while(start<last){
int temp = nums[start];
nums[start++] = nums[last];
nums[last--] = temp;
}
}
}
Problem 5: Gas Station
Link to the problem: https://leetcode.com/problems/gas-station/

class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
for(int i=0; i<gas.length; i++){
int tank = gas[i];
if(gas[i]<cost[i] || gas[i]<=1)
continue;
int j = (i==gas.length-1)?0:i+1;
boolean hasGas = true;
while(j!=i && hasGas){
int costIndex = (j==0)?gas.length-1:j-1;
if(tank-cost[costIndex]<0)
hasGas=false;
else{
tank -= cost[costIndex];
tank += gas[j++];
}
if(j==gas.length)
j=0;
}
if(!hasGas)
continue;
int index = i==0?gas.length-1:i-1;
if(tank>=cost[index])
return i;
}
return -1;
}
}




