diff --git a/.idea/2025-software-dev-problem-set.iml b/.idea/2025-software-dev-problem-set.iml new file mode 100644 index 0000000..1335759 --- /dev/null +++ b/.idea/2025-software-dev-problem-set.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..db51b41 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..3cbc8d9 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..9c228bd --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,71 @@ + + + + + + + + + + + + + + + + + { + "lastFilter": { + "state": "OPEN", + "assignee": "StaceyO21" + } +} + { + "selectedUrlAndAccountId": { + "url": "https://github.com/StaceyO21/2025-software-dev-problem-set.git", + "accountId": "e5bbf594-16de-4cd6-b07a-fa67bb5a872f" + } +} + { + "associatedIndex": 5 +} + + + + + + + + + 1764623963310 + + + + \ No newline at end of file diff --git a/solutions/.idea/.gitignore b/solutions/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/solutions/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/solutions/.idea/misc.xml b/solutions/.idea/misc.xml new file mode 100644 index 0000000..31e1ebc --- /dev/null +++ b/solutions/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/solutions/.idea/modules.xml b/solutions/.idea/modules.xml new file mode 100644 index 0000000..7d282b8 --- /dev/null +++ b/solutions/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/solutions/.idea/solutions.iml b/solutions/.idea/solutions.iml new file mode 100644 index 0000000..b107a2d --- /dev/null +++ b/solutions/.idea/solutions.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/solutions/.idea/vcs.xml b/solutions/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/solutions/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/solutions/ContainerWMostWater.java b/solutions/ContainerWMostWater.java new file mode 100644 index 0000000..373dee9 --- /dev/null +++ b/solutions/ContainerWMostWater.java @@ -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; + } +} \ No newline at end of file diff --git a/solutions/ContainsDuplicate_Java.js b/solutions/ContainsDuplicate_Java.js new file mode 100644 index 0000000..e69de29 diff --git a/solutions/DailyTemps.java b/solutions/DailyTemps.java new file mode 100644 index 0000000..dae1ce8 --- /dev/null +++ b/solutions/DailyTemps.java @@ -0,0 +1,17 @@ +import java.util.Stack; + +public class DailyTemps { + int[] res = new int[temperatures.length]; + Stack 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; +} diff --git a/solutions/Eval_Rev_Polish_Notation.java b/solutions/Eval_Rev_Polish_Notation.java new file mode 100644 index 0000000..4bcfd9a --- /dev/null +++ b/solutions/Eval_Rev_Polish_Notation.java @@ -0,0 +1,26 @@ +import java.util.Stack; + +class Solution { + public int evalRPN(String[] tokens) { + Stack 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(); + } +} diff --git a/solutions/FindMinSortedArray.java b/solutions/FindMinSortedArray.java new file mode 100644 index 0000000..717b430 --- /dev/null +++ b/solutions/FindMinSortedArray.java @@ -0,0 +1,16 @@ +public class FindMinSortedArray { + public int findMin(int[] nums){ + int l = 0; + int r = nums.length - 1; + + while(l < r){ + int m = l + (r - l) / 2; + if (nums[m] < nums[r]) { + r = m; + } else { + l = m + 1; + } + } + return nums[l]; + } +} diff --git a/solutions/Gen_Parentheses.java b/solutions/Gen_Parentheses.java new file mode 100644 index 0000000..72a32c1 --- /dev/null +++ b/solutions/Gen_Parentheses.java @@ -0,0 +1,30 @@ +import java.util.ArrayList; +import java.util.List; + +class Solution { + private void backtrack(int openN, int closedN, int n, List 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 generateParenthesis(int n){ + List res = new ArrayList<>(); + StringBuilder stack = new StringBuilder(); + backtrack(0, 0, n, res, stack); + return res; + } + } +} \ No newline at end of file diff --git a/solutions/Group Anagrams.js b/solutions/Group Anagrams.js new file mode 100644 index 0000000..e0ff2c8 --- /dev/null +++ b/solutions/Group Anagrams.js @@ -0,0 +1,24 @@ +class Solution { + /** + * @param {string[]} strs + * @return {string[][]} + */ + groupAnagrams(strs) { + res = {}; + + for(let s in strs){ + let count = Array(26).fill(0); + + for(let letter in s){ + count[letter.charCodeAt(0) - 'a'.charCodeAt(0)] += 1; + } + const key = count.join(','); + + if(!res[key]){ + res[key] = []; + } + res[key].push(s); + } + return Object.values(res); +} +} \ No newline at end of file diff --git a/solutions/Group_Anagram.java b/solutions/Group_Anagram.java new file mode 100644 index 0000000..05fac4b --- /dev/null +++ b/solutions/Group_Anagram.java @@ -0,0 +1,19 @@ +import java.util.*; + +public class Group_Anagram { + public List> groupAnagrams(String[] strs) { + Map> 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()); + } +} diff --git a/solutions/KokoBananas.java b/solutions/KokoBananas.java new file mode 100644 index 0000000..944cda5 --- /dev/null +++ b/solutions/KokoBananas.java @@ -0,0 +1,25 @@ +import java.util.Arrays; + +public class KokoBananas { + public int minEatingSpeed(int[] piles, int h){ + int l = 1; + int r = Arrays.stream(piles).max().getAsInt(); + int res = r; + + while(l <= r){ + int k = (l + r) / 2; + + int totalTime = 0; + for(int p : piles){ + totalTime += Math.ceil((double) p / k); + } + if(totalTime <= h){ + res = k; + r = k - 1; + } else{ + l = k + 1; + } + } + return res; + } +} diff --git a/solutions/LConsSeq.java b/solutions/LConsSeq.java new file mode 100644 index 0000000..b386149 --- /dev/null +++ b/solutions/LConsSeq.java @@ -0,0 +1,19 @@ +import java.util.HashMap; +import java.util.Map; + +class Solution { + public int longestConsecutive(int[] nums) { + Map 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; + } +} \ No newline at end of file diff --git a/solutions/Min_Stack.java b/solutions/Min_Stack.java new file mode 100644 index 0000000..abc2e95 --- /dev/null +++ b/solutions/Min_Stack.java @@ -0,0 +1,45 @@ +import java.util.Stack; + +class MinStack { + + private Stack 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 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(); + */ \ No newline at end of file diff --git a/solutions/ProductofArrayXItself.java b/solutions/ProductofArrayXItself.java new file mode 100644 index 0000000..7345fa9 --- /dev/null +++ b/solutions/ProductofArrayXItself.java @@ -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; +} diff --git a/solutions/SearchNRotatedSortedArray.java b/solutions/SearchNRotatedSortedArray.java new file mode 100644 index 0000000..0129e25 --- /dev/null +++ b/solutions/SearchNRotatedSortedArray.java @@ -0,0 +1,26 @@ +public class SearchNRotatedSortedArray { + public int search(int nums, int target){ + int l = 0; + int r = nums.length - 1; + + while(l <= r){ + int mid = (l + r) / 2; + if(nums[mid] == target){ + return mid; + if(nums[l] <= nums[mid]) { + if (target > nums[mid] || target < nums[l]) { + l = mid + 1; + } else{ + r = mid - 1; + } + } else { + if(target < nums[mid] || target > nums[r]){ + r = mid - 1; + } else { + l = mid + 1; + } + } + } + return -1; + } +} diff --git a/solutions/Three_Sum.java b/solutions/Three_Sum.java new file mode 100644 index 0000000..4a6946f --- /dev/null +++ b/solutions/Three_Sum.java @@ -0,0 +1,32 @@ +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +class Solution { + public List> threeSum(int[] nums) { + Arrays.sort(nums); + List> 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; + } +} \ No newline at end of file diff --git a/solutions/TopKFreq.java b/solutions/TopKFreq.java new file mode 100644 index 0000000..5ff6092 --- /dev/null +++ b/solutions/TopKFreq.java @@ -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 count = new HashMap<>(); + + for(int n : nums){ + count.put(n, count.getOrDefault(n, 0) + 1); + } + + List[] freq = new List[nums.length + 1]; + for(int i = 0; i < freq.length; i++){ + freq[i] = new ArrayList<>; + } + + for(Map.Entry 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; + } +} diff --git a/solutions/TwoSum2.java b/solutions/TwoSum2.java new file mode 100644 index 0000000..01c635a --- /dev/null +++ b/solutions/TwoSum2.java @@ -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]; + } +} \ No newline at end of file diff --git a/solutions/Two_Sum.java b/solutions/Two_Sum.java new file mode 100644 index 0000000..bce825d --- /dev/null +++ b/solutions/Two_Sum.java @@ -0,0 +1,20 @@ +import java.util.HashMap; + +public class Two_Sum { + public int[] twoSum(int[] nums, int target) { + HashMap 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[]{}; + } + } + diff --git a/solutions/Two_Sum.js b/solutions/Two_Sum.js new file mode 100644 index 0000000..864b949 --- /dev/null +++ b/solutions/Two_Sum.js @@ -0,0 +1,13 @@ + +var twoSum = function(nums, target) { + + for (let i = 0; i < nums.length; i++){ + for(let j = i + 1; j < nums.length; j++){ + if(nums[i] + nums[j] == target){ + return[i, j]; + } + } + + } + return null; +}; \ No newline at end of file diff --git a/solutions/Valid Anagram.js b/solutions/Valid Anagram.js new file mode 100644 index 0000000..a68c1bc --- /dev/null +++ b/solutions/Valid Anagram.js @@ -0,0 +1,19 @@ +/** + * @param {string} s + * @param {string} t + * @return {boolean} + */ +var isAnagram = function(s, t) { + + if(s.length !== t.length){ + return false + } + + let counter = Array(26).fill(0); + for(let i = 0; i < s.length; i++) { + counter[s.charCodeAt(i) - 97]++; + counter[t.charCodeAt(i) - 97]--; + } + + return counter.every(count => count === 0); +} \ No newline at end of file diff --git a/solutions/ValidAnagrams_Java.js b/solutions/ValidAnagrams_Java.js new file mode 100644 index 0000000..e69de29 diff --git a/solutions/ValidPalindrome.java b/solutions/ValidPalindrome.java new file mode 100644 index 0000000..7efa143 --- /dev/null +++ b/solutions/ValidPalindrome.java @@ -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'); + } + } diff --git a/solutions/ValidSydoku.java b/solutions/ValidSydoku.java new file mode 100644 index 0000000..69a3add --- /dev/null +++ b/solutions/ValidSydoku.java @@ -0,0 +1,39 @@ +import java.util.HashSet; +import java.util.Set; + +class Solution { + public boolean isValidSudoku(char[][] board) { + + for (int row = 0; row < 9; row++) { + Set seen = new HashSet<>(); + for (int i = 0; i < 9; i++) { + if (board[row][i] == '.') continue; + if (seen.contains(board[row][i])) return false; + seen.add(board[row][i]); + } + } + + for (int col = 0; col < 9; col++) { + Set seen = new HashSet<>(); + for (int i = 0; i < 9; i++) { + if (board[i][col] == '.') continue; + if (seen.contains(board[i][col])) return false; + seen.add(board[i][col]); + } + } + + for (int square = 0; square < 9; square++) { + Set seen = new HashSet<>(); + for (int i = 0; i < 9; i++) { + for (int j = 0; j < 9; j++) { + int row = (square / 3) * 3 + i; + int col = (square % 3) * 3 + j; + if (board[row][col] == '.') continue; + if (seen.contains(board[row][col])) return false; + seen.add(board[row][col]); + } + } + } + return true; + } +} \ No newline at end of file diff --git a/solutions/Valid_Parentheses.java b/solutions/Valid_Parentheses.java new file mode 100644 index 0000000..735fcf4 --- /dev/null +++ b/solutions/Valid_Parentheses.java @@ -0,0 +1,24 @@ +import java.util.Stack; + +class Solution { + public boolean isValid(String s) { + Stack stack = new Stack<>(); + java.util.Map closeToOpen = new java.util.HashMap<>(); + closeToOpen.put(')', '('); + closeToOpen.put(']', '['); + closeToOpen.put('}', '{'); + + for(Character c : s.toCharArray()){ + if(closeToOpen.containsKey(c)){ + if(!stack.isEmpty() && stack.peek() == closeToOpen.get(c)) { + stack.pop(); + } else { + return false; + } + } else { + stack.push(c); + } + } + return stack.isEmpty(); + } + } diff --git a/solutions/exercise solutions.js b/solutions/exercise solutions.js new file mode 100644 index 0000000..48202a1 --- /dev/null +++ b/solutions/exercise solutions.js @@ -0,0 +1,22 @@ +/** + * @param {number[]} nums + * @return {boolean} + */ +var containsDuplicate = function(nums) { + + for (i = 0; i < nums.length; i++) { + let value = nums[i]; + + for (j = i + 1; j < nums.length; j++){ + let dupliValue = nums[j]; + + if(value === dupliValue){ + return true; + } + + + } + } + return false; + } +