Skip to content

Commit f231299

Browse files
authored
Merge pull request #732 from minji-go/main
[minji-go] Week 2
2 parents 55c004d + 93d395f commit f231299

File tree

5 files changed

+182
-0
lines changed

5 files changed

+182
-0
lines changed

3sum/minji-go.java

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Problem: https://leetcode.com/problems/3sum/
3+
Description: return all the triplets (i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0)
4+
Concept: Array, Two Pointers, Sorting
5+
Time Complexity: O(N²), Runtime 70ms
6+
Space Complexity: O(N), Memory 51.63MB
7+
*/
8+
class Solution {
9+
public List<List<Integer>> threeSum(int[] nums) {
10+
Map<Integer, Integer> number = new HashMap<>();
11+
for(int i=0; i<nums.length; i++) {
12+
number.put(nums[i], number.getOrDefault(nums[i], 0)+1);
13+
}
14+
15+
Arrays.sort(nums);
16+
Set<List<Integer>> set = new HashSet<>();
17+
List<List<Integer>> triplets = new ArrayList<>();
18+
List<Integer> lastTriplet = null;
19+
for(int i=0; i<nums.length-1; i++) {
20+
if(i>0 && nums[i]==nums[i-1]) continue;
21+
22+
for(int j=i+1; j<nums.length; j++){
23+
if(j>i+1 && nums[j]==nums[j-1]) continue;
24+
25+
int target = -(nums[i]+nums[j]);
26+
if(nums[j]>target) continue;
27+
28+
int count = number.getOrDefault(target,0);
29+
if(nums[i]==target) count--;
30+
if(nums[j]==target) count--;
31+
if(count<=0) continue;
32+
33+
List<Integer> triplet = List.of(nums[i], nums[j], target);
34+
if(triplet.equals(lastTriplet)) continue;
35+
lastTriplet = triplet;
36+
triplets.add(triplet);
37+
}
38+
}
39+
return triplets;
40+
}
41+
}

climbing-stairs/minji-go.java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
Problem: https://leetcode.com/problems/climbing-stairs/
3+
Description: how many distinct ways can you climb to the top, if you can either climb 1 or 2 steps
4+
Concept: Dynamic Programming, Memoization, Recursion, Math, Array, Iterator, Combinatorics ...
5+
Time Complexity: O(n), Runtime: 0ms
6+
Space Complexity: O(1), Memory: 40.63MB
7+
*/
8+
class Solution {
9+
public int climbStairs(int n) {
10+
if(n==1) return 1;
11+
if(n==2) return 2;
12+
13+
int prev=1, cur=2;
14+
for(int i=3; i<=n; i++){
15+
int next=prev+cur;
16+
prev=cur;
17+
cur=next;
18+
}
19+
return cur;
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
Problem: https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/
3+
Description: Given two integer arrays preorder and inorder, construct and return the binary tree.
4+
Concept: Array, Hash Table, Divide and Conquer, Tree, Binary Tree
5+
Time Complexity: O(NM), Runtime 2ms
6+
Space Complexity: O(N), Memory 45.02MB
7+
*/
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
11+
class Solution {
12+
public TreeNode buildTree(int[] preorder, int[] inorder) {
13+
boolean isLeft = true;
14+
Map<Integer, TreeNode> parents = new HashMap<>();
15+
TreeNode rootNode = null, parentNode = null;
16+
17+
for (int pidx=0, iidx=0; pidx<preorder.length; pidx++) {
18+
int pval = preorder[pidx];
19+
int ival = inorder[iidx];
20+
21+
if(pidx==0) {
22+
rootNode = parentNode = new TreeNode(pval);
23+
} else if (isLeft) {
24+
parents.put(pval, parentNode);
25+
parentNode = parentNode.left = new TreeNode(pval);
26+
} else {
27+
isLeft = true;
28+
parents.put(pval, parentNode);
29+
parentNode = parentNode.right = new TreeNode(pval);
30+
}
31+
32+
if(pval==ival) {
33+
isLeft = false;
34+
TreeNode targetNode = parentNode;
35+
while (iidx<inorder.length-1 && parents.get(parentNode.val)!=null) {
36+
if(parentNode.val == inorder[iidx+1]){
37+
iidx++;
38+
targetNode = parentNode;
39+
}
40+
parentNode = parents.get(parentNode.val);
41+
}
42+
if(iidx<inorder.length-1 && parentNode.val != inorder[iidx+1]){
43+
iidx++;
44+
parentNode = targetNode;
45+
} else iidx = iidx+2;
46+
}
47+
}
48+
49+
return rootNode;
50+
}
51+
}
52+
53+
/**
54+
* Definition for a binary tree node.
55+
* public class TreeNode {
56+
* int val;
57+
* TreeNode left;
58+
* TreeNode right;
59+
* TreeNode() {}
60+
* TreeNode(int val) { this.val = val; }
61+
* TreeNode(int val, TreeNode left, TreeNode right) {
62+
* this.val = val;
63+
* this.left = left;
64+
* this.right = right;
65+
* }
66+
* }
67+
*/

decode-ways/minji-go.java

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
Problem: https://leetcode.com/problems/decode-ways/
3+
Description: Given a string s containing only digits, return the number of ways to decode it
4+
Concept: String, Dynamic Programming
5+
Time Complexity: O(N), Runtime 1ms
6+
Space Complexity: O(N), Memory 42.12MB
7+
*/
8+
class Solution {
9+
public int numDecodings(String s) {
10+
int[] dp = new int[s.length()];
11+
if(decode(s.substring(0, 1))) dp[0]=1;
12+
if(s.length()>1 && decode(s.substring(1, 2))) dp[1]+=dp[0];
13+
if(s.length()>1 && decode(s.substring(0, 2))) dp[1]+=dp[0];
14+
15+
for(int i=2; i<s.length(); i++){
16+
if(decode(s.substring(i,i+1))) dp[i]+=dp[i-1];
17+
if(decode(s.substring(i-1,i+1))) dp[i]+=dp[i-2];
18+
}
19+
return dp[s.length()-1];
20+
}
21+
22+
public boolean decode(String s){
23+
int num = Integer.parseInt(s);
24+
int numLength = (int) Math.log10(num) + 1;
25+
if(num<0 || num>26 || numLength != s.length()) return false;
26+
return true;
27+
}
28+
}

valid-anagram/minji-go.java

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
Problem: https://leetcode.com/problems/valid-anagram/
3+
Description: return true if one string is an anagram of the other, one formed by rearranging the letters of the other
4+
Concept:String, Hash Table, Sorting, Array, Counting, String Matching, Ordered Map, Ordered Set, Hash Function ...
5+
Time Complexity: O(n), Runtime: 27ms
6+
Space Complexity: O(n), Memory: 43.05MB
7+
*/
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
11+
class Solution {
12+
public boolean isAnagram(String s, String t) {
13+
if(s.length() != t.length()) return false;
14+
15+
Map<Character, Integer> charCount = new HashMap<>();
16+
for(int i=0; i<s.length(); i++){
17+
charCount.put(s.charAt(i), charCount.getOrDefault(s.charAt(i), 0)+1);
18+
charCount.put(t.charAt(i), charCount.getOrDefault(t.charAt(i), 0)-1);
19+
}
20+
for(Integer count : charCount.values()){
21+
if(count !=0) return false;
22+
}
23+
return true;
24+
}
25+
}

0 commit comments

Comments
 (0)