Skip to content

[iam-edwin] WEEK 02 solutions #1416

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 11, 2025
Merged
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
40 changes: 40 additions & 0 deletions 3sum/iam-edwin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> result = new ArrayList<>();

Map<Integer, Integer> counter = new HashMap<>();
for (int num : nums) {
int count = counter.getOrDefault(num, 0);
counter.put(num, count + 1);
}

Set<Integer> keySet = new HashSet<>(counter.keySet());
for (int num1 : keySet) {
int num1Count = counter.get(num1);
if (num1Count > 1) {
counter.put(num1, num1Count - 1);
} else {
counter.remove(num1);
}

for (int num2 : counter.keySet()) {
int num3 = -num1 - num2;
int count = counter.getOrDefault(num3, 0);
if (((num2 == num3 && count >= 2) || (num2 != num3 && count >= 1)) && num3 >= num2) {
result.add(List.of(num1, num2, num3));
}
}

counter.remove(num1);
}

return result;
}
}
13 changes: 13 additions & 0 deletions climbing-stairs/iam-edwin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution {
public int climbStairs(int n) {
int[] result = new int[n + 1];
result[0] = 1;
result[1] = 1;

for (int i = 2; i < n + 1; i++) {
result[i] = result[i - 1] + result[i - 2];
}

return result[n];
}
}
39 changes: 39 additions & 0 deletions product-of-array-except-self/iam-edwin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class Solution {
public int[] productExceptSelf(int[] nums) {
int[] left = new int[nums.length + 1];
int[] right = new int[nums.length + 1];
left[0] = 1;
right[nums.length] = 1;

for (int i = 0; i < nums.length; i++) {
left[i + 1] = left[i] * nums[i];
right[nums.length - i - 1] = right[nums.length - i] * nums[nums.length - i - 1];
}

int[] result = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
result[i] = left[i] * right[i + 1];
}
return result;
}

public int[] productExceptSelfV2(int[] nums) {
int[] result = new int[nums.length];

int curr = 1;
result[0] = curr;
for (int i = 1; i < nums.length; i++) {
curr *= nums[i - 1];
result[i] = curr;
}

curr = 1;
result[nums.length - 1] *= curr;
for (int i = nums.length - 2; i >= 0; i--) {
curr *= nums[i + 1];
result[i] *= curr;
}

return result;
}
}
27 changes: 27 additions & 0 deletions valid-anagram/iam-edwin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import java.util.HashMap;
import java.util.Map;

class Solution {
public boolean isAnagram(String s, String t) {
Map<Character, Integer> sMap = new HashMap<>();
for (char c : s.toCharArray()) {
Integer count = sMap.getOrDefault(c, 0);
sMap.put(c, count + 1);
}

Map<Character, Integer> tMap = new HashMap<>();
for (char c : t.toCharArray()) {
Integer count = tMap.getOrDefault(c, 0);
tMap.put(c, count + 1);
}

for (char key : sMap.keySet()) {
if (!sMap.get(key).equals(tMap.get(key))) {
return false;
}
tMap.remove(key);
}

return tMap.isEmpty();
}
}
52 changes: 52 additions & 0 deletions validate-binary-search-tree/iam-edwin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
class Solution {
public boolean isValidBST(TreeNode root) {
return (root.left == null || isValidBSTMax(root.left, root.val))
&& (root.right == null || isValidBSTMin(root.right, root.val));
}

private boolean isValidBSTMax(TreeNode root, int max) {
if (root.val >= max) {
return false;
}

return (root.left == null || isValidBSTMax(root.left, root.val))
&& (root.right == null || isValidBSTMinMax(root.right, root.val, max));
}

private boolean isValidBSTMin(TreeNode root, int min) {
if (root.val <= min) {
return false;
}

return (root.left == null || isValidBSTMinMax(root.left, min, root.val))
&& (root.right == null || isValidBSTMin(root.right, root.val));
}

private boolean isValidBSTMinMax(TreeNode root, int min, int max) {
if (root.val >= max || root.val <= min) {
return false;
}

return (root.left == null || isValidBSTMinMax(root.left, min, root.val))
&& (root.right == null || isValidBSTMinMax(root.right, root.val, max));
}

public class TreeNode {
int val;
TreeNode left;
TreeNode right;

TreeNode() {
}

TreeNode(int val) {
this.val = val;
}

TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
}