Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .idea/2025-software-dev-problem-set.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions solutions/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions solutions/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions solutions/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions solutions/.idea/solutions.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions solutions/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions solutions/ContainerWMostWater.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
public int maxArea(int[] height) {
int l = 0;
int r = height.length - 1;
int res = 0;

while(l < r){
int area = Math.min(height[l], height[r]) * (r - l);
res = Math.max(res, area);
if (height[l] <= height[r]) {
l++;
}else{
r--;
}
}
return res;
}
}
17 changes: 17 additions & 0 deletions solutions/DailyTemps.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import java.util.Stack;

public class DailyTemps {
int[] res = new int[temperatures.length];
Stack<int[]> stack = new Stack<>();

for(int i = 0; i < temperatures.length; i++){
int t = temperatures[i];

while(!stack.isEmpty() && t > stack.peek()[0]){
int[] pair = stack.pop();
res[pair[1]] = i - pair[i];
}
stack.push(new int[]{t, i});
}
return res;
}
26 changes: 26 additions & 0 deletions solutions/Eval_Rev_Polish_Notation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import java.util.Stack;

class Solution {
public int evalRPN(String[] tokens) {
Stack<Integer> stack = new Stack<>();

for(String c : tokens){
if(c.equals("+")){
stack.push(stack.pop() + stack.pop());
} else if(c.equals("-")){
int a = stack.pop();
int b = stack.pop();
stack.push(b - a);
} else if(c.equals("*")){
stack.push(stack.pop() * stack.pop());
} else if(c.equals("/")){
int a = stack.pop();
int b = stack.pop();
stack.push(b / a);
} else {
stack.push(Integer.parseInt(c));
}
}
return stack.pop();
}
}
30 changes: 30 additions & 0 deletions solutions/Gen_Parentheses.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import java.util.ArrayList;
import java.util.List;

class Solution {
private void backtrack(int openN, int closedN, int n, List<String> res, StringBuilder stack) {
if(openN == closedN && openN == n){
res.add(stack.toString());
return;
}

if(openN < closedN){
stack.append('(');
backtrack(openN + 1, closedN, n, res, stack);
stack.deleteCharAt(stack.length() - 1);
}

if(closedN < openN){
stack.append(')');
backtrack(openN, closedN + 1, n, res, stack);
stack.deleteCharAt(stack.length() - 1);
}

public List<String> generateParenthesis(int n){
List<String> res = new ArrayList<>();
StringBuilder stack = new StringBuilder();
backtrack(0, 0, n, res, stack);
return res;
}
}
}
19 changes: 19 additions & 0 deletions solutions/Group_Anagram.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import java.util.*;

public class Group_Anagram {
public List<List<String>> groupAnagrams(String[] strs) {
Map<String, List<String>> res = new HashMap<>;

for (String s : strs) {
int[] count = new int[26];
for (char c : s.toCharArray()) {
count[c - 'a']++;
}

String key = Arrays.toString(count);
res.putIfAbsent(key, new ArrayList());
res.get(key).add(s);
}
return new ArrayList<>(res.values());
}
}
19 changes: 19 additions & 0 deletions solutions/LConsSeq.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import java.util.HashMap;
import java.util.Map;

class Solution {
public int longestConsecutive(int[] nums) {
Map<Integer, Integer> mp = new HashMap<>();
int res = 0;

for(int num : nums){
if (!mp.containsKey(num)){
mp.put(num, mp.getOrDefault(num - 1, 0) + mp.getOrDefault(num + 1, 0) + 1);
mp.put(num - mp.getOrDefault(num - 1, 0), mp.get(num));
mp.put(num + mp.getOrDefault(num + 1, 0), mp.get(num));
res = Math.max(res, mp.get(num));
}
}
return res;
}
}
45 changes: 45 additions & 0 deletions solutions/Min_Stack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import java.util.Stack;

class MinStack {

private Stack<Integer> stack = new Stack<>();

public MinStack() {
stack = new Stack<>;
}

public void push(int val) {
stack.push(val);
}

public void pop() {
stack.pop();
}

public int top() {
return stack.peek();
}

public int getMin() {
Stack<Integer> tmp = new Stack<>;
int mini = stack.peek();

while(!stack.isEmpty()){
mini = Math.min(mini, stack.peek());
tmp.push(stack.pop());
}
while(!tmp.isEmpty()){
stack.push(tmp.pop());
}
return mini;
}
}

/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(val);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/
17 changes: 17 additions & 0 deletions solutions/ProductofArrayXItself.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
public class ProductofArrayXItself {

int n = nums.length;
int[] res = new int[n];

int res[0] = 1;
for(int i = 1; i < n; i++){
res[i] = res[i - 1] * nums[i - 1];
}

int postfix = 1;
for(int i = n - 1; i >= 0; i--){
res[i] *= postfix;
postfix *= nums[i];
}
return res;
}
32 changes: 32 additions & 0 deletions solutions/Three_Sum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

class Solution {
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> res = new ArrayList<>();
for(int i = 0; i < nums.length; i++){
if(nums[i] > 0) break;
if(i > 0 && nums[i] == nums[i - 1]) continue;

int l = i + 1, r = nums.length - 1;
while(l < r){
int sum = nums[i] + nums[l] + nums[r];
if(sum > 0){
r--;
} else if (sum < 0){
l++;
} else {
res.add(Arrays.asList(nums[i], nums[l], nums[r]));
l++;
r--;
while(l < r && (nums[l] == nums[l - 1]){
l++;
}
}
}
}
return res;
}
}
35 changes: 35 additions & 0 deletions solutions/TopKFreq.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

class Solution {
public int[] topKFrequent(int[] nums, int k){
Map<Integer, Integer> count = new HashMap<>();

for(int n : nums){
count.put(n, count.getOrDefault(n, 0) + 1);
}

List<Integer>[] freq = new List[nums.length + 1];
for(int i = 0; i < freq.length; i++){
freq[i] = new ArrayList<>;
}

for(Map.Entry<Integer, Integer> entry : count.entrySet()){
freq[entry.getValue()].add(entry.getKey());
}

int[] res = new int[k];
int index = 0;
for(int i = freq.length - 1; i > 0 && index < k; i--){
for(int n : freq[i]) {
res[index++] = n;
if(index == k){
return res;
}
}
}
return res;
}
}
18 changes: 18 additions & 0 deletions solutions/TwoSum2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
public int[] twoSum(int[] numbers, int target) {
int l = 0, r = numbers.length - 1;

while(l < r){
int curSum = numbers[l] + numbers[r];

if(curSum > target){
r--;
} else if (curSum < target){
l++;
} else {
return new int[] = {l + 1, r + 1};
}
}
return new int[0];
}
}
20 changes: 20 additions & 0 deletions solutions/Two_Sum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import java.util.HashMap;

public class Two_Sum {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> prevMap = new HashMap<>();

for (int i = 0; i < nums.length; i++) {
int num = nums[i];
int diff = target - num;


if (prevMap.containsKey(diff)) {
return new int[]{prevMap.get(diff), i};
}
prevMap.put(num, i);
}
return new int[]{};
}
}

25 changes: 25 additions & 0 deletions solutions/ValidPalindrome.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution {
public boolean isPalindrome(String s) {
int l = 0, r = s.length() - 1;

while(l < r){
while(l < r && !alphaNum(s.charAt(l))){
l++;
}
while(r > l && !alphaNum(s.charAt(r))){
r--;
}
if(Character.toLowerCase(s.charAt(l)) != Character.toLowerCase(s.charAt(r))){
return false;
}
l++; r--;
}
return true;
}

public boolean alphaNum(char c){
return (c >= 'A' && c <= 'Z' ||
c >= 'a' && c <= 'z' ||
c >= '0' && c <= '9');
}
}
Loading