Skip to content

Instantly share code, notes, and snippets.

View ritik-agrawal's full-sized avatar

Ritik Agrawal ritik-agrawal

View GitHub Profile
class RecentCounter {
List<Integer> queue;
int head;
int tail;
public RecentCounter() {
this.queue = new ArrayList<Integer>();
this.head = -1;
this.tail = -1;
class Solution {
public String decodeString(String s) {
var retVal = new StringBuilder();
var len = s.length();
var i = 0;
while(i < len){
var chr = s.charAt(i);
if (Character.isDigit(chr)){
i = valueOf((i) , s, retVal);
} else {
class Solution {
public int[] asteroidCollision(int[] asteroids) {
var len = asteroids.length;
var stk = new ArrayList<Integer>();
for (int cur : asteroids){
if (Objects.nonNull(peek(stk)) && isPositiveNegative(peek(stk), cur)){
if ((peek(stk) + cur) == 0){
pop(stk);
} else if ((peek(stk) + cur) < 0){
while(
class Solution {
public String removeStars(String s) {
var stk = new ArrayList<Character>();
var len = s.length();
for (int i = 0; i < len; i++){
var ch = s.charAt(i);
if (Objects.equals(ch, '*')){
pop(stk);
} else {
stk.add(ch);
class Solution {
public boolean uniqueOccurrences(int[] arr) {
Arrays.sort(arr);
var seen = new HashMap<Integer, List<Integer>>();
var s = 0;
var e = 0;
var len = arr.length;
while (e < len){
while ( e < len && arr[s] == arr[e]){
@ritik-agrawal
ritik-agrawal / findPivotIndex.java
Created September 16, 2023 11:15
Find the pivot index. Index where left sum = right sum
class Solution {
public int pivotIndex(int[] nums) {
var len = nums.length;
var l = new int[len];
var r = new int[len];
var ls = 0;
var rs = 0;
var s = 0;
var e = len-1;
for (int i = 0; i < len; i++){
@ritik-agrawal
ritik-agrawal / MaxVowel.java
Created August 12, 2023 08:55
LeetCode: Max count of vowel is a given string in a given interval.
class Solution {
public int maxVowels(String s, int k) {
int slen = s.length();
int[] dp = new int[slen];
int vcc = 0;
for (int i = 0; i < slen; i++){
if (isVowel(s.charAt(i))){
dp[i] = ++vcc;
} else {
dp[i] = vcc;
@ritik-agrawal
ritik-agrawal / maxKsum.java
Created August 10, 2023 08:48
LeetCode: Max number of k-sums in a given integer array
class Solution {
public int maxOperations(int[] nums, int k) {
var ret = 0;
Arrays.sort(nums);
var j = nums.length;
var i = 0;
while (i < j){
if (nums[i]+nums[j] == k){
ret++;
i++;
@ritik-agrawal
ritik-agrawal / isSubsequence.java
Created August 7, 2023 05:11
LeetCode: isSubsequence, given with two strings return true if `s` is subsequence of `t` .
class Solution {
public boolean isSubsequence(String s, String t) {
var slen = s.length();
var tlen = t.length();
if (slen == 0){
return true;
}
if (tlen < slen){
return false;
}
@ritik-agrawal
ritik-agrawal / moveZeros.java
Created August 6, 2023 08:38
Leet Code: Move the zeros to the last of the given int[] while maintaining the relative order of the non zero numbers.
class Solution {
public void moveZeroes(int[] nums) {
var len = nums.length;
var mark = 0;
for (int i = 0; i < len; i++){
if (nums[i] != 0){
nums[mark] = nums[i];
mark++;
}
}