Skip to content

Commit 3854db0

Browse files
authored
Merge pull request #707 from GangBean/main
[GangBean] Week 2
2 parents 36cfe67 + 1654d1d commit 3854db0

File tree

5 files changed

+210
-0
lines changed

5 files changed

+210
-0
lines changed

โ€Ž3sum/GangBean.java

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class Solution {
2+
public List<List<Integer>> threeSum(int[] nums) {
3+
/**
4+
1. understanding
5+
- integer array nums, find the whole combination of 3 nums, and the sum of the 3 nums equal to 0. And don't allow reusing same indiced number(but can duplicate in value)
6+
2. solve strategy
7+
- brute force
8+
- in every combination, validate sum of the nums equal to 0
9+
- but it can take O(N^3) times where N is the length of input array, and given that the N can be 3000 at most(3 * 10^3), time can be 27 * 10^9, which takes too long...
10+
- sort and two pointers
11+
- sort nums in ascending order, so move the left pointer to right means the sum of window is getting bigger.
12+
- and mid pointer set to left + 1 index
13+
- if sum of pointers is less than 0, then move mid pointer to right, until the sum is bigger than 0, and while processing them, if the sum of pointers is 0, then add the combination to the return list.
14+
- [-4, -1, -1, 0, 1, 2]:
15+
16+
3. complexity
17+
- time: O(N^2) -> each left pointer, you can search at most N-1, and left pointer's range is [0, N-1), so the max length is N-1 for left index pointer.
18+
- space: O(1) -> no extra space is needed
19+
*/
20+
// 0. assign return variable Set
21+
Set<List<Integer>> answer = new HashSet<>();
22+
23+
// 1. sort the num array in ascending order
24+
Arrays.sort(nums); // O(NlogN)
25+
// Arrays.stream(nums).forEach(System.out::println);
26+
27+
// 3. move the mid pointer from left to right to find the combination of which's sum is 0, and if the sum is over 0, and then move right pointer to the left. else if the sum is under 0, then move left pointer to right direction.
28+
for (int left = 0; left < nums.length - 1; left++) {
29+
int mid = left + 1;
30+
int right = nums.length - 1;
31+
while (mid < right) {
32+
// System.out.println(String.format("%d,%d,%d", nums[left], nums[mid], nums[right]));
33+
int sum = nums[left] + nums[mid] + nums[right];
34+
if (sum > 0) {
35+
right--;
36+
} else if (sum == 0) {
37+
answer.add(List.of(nums[left], nums[mid], nums[right]));
38+
right--;
39+
} else {
40+
mid++;
41+
}
42+
}
43+
}
44+
45+
return new ArrayList<>(answer);
46+
}
47+
}
48+

โ€Žclimbing-stairs/GangBean.java

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public int climbStairs(int n) {
3+
/**
4+
1. Understanding
5+
- return the count of ways to climb up to top
6+
- way means the sequence of step count
7+
- each state, there can be 2 ways. first climb 1 step, second climb 2 step
8+
2. solve strategy
9+
- assume the count of ways to climb up to K th stairs is C[K].
10+
- then, C[0] = 1, C[1] = 1, C[2] = 2(because, you can up to 2nd stair from 0th stair and also from 1st stair.)
11+
- and C[3] = C[2] + C[1], C[4] = C[3] + C[2], ... etc...
12+
- so we can fomulate C[k] = C[k-1] + C[k-2]
13+
- iterate over 0 to n, you can caculate C[k].
14+
- and finally return C[n] is the answer.
15+
16+
3. complexity
17+
- I answer to this part, along with coding upon each line description.
18+
*/
19+
20+
// 1. create array to demonstrate each stairs way count to reach that position.
21+
// the maximun step count is 45, so maybe there is over 2^32(approximately 2 billion; so i worry about the overflow), I assign long type array. Oh.. but i catch that return type of this method is integer, so i can assume that maximum value is under integer range. So, assign as integer.
22+
int[] c = new int[n + 1]; // the extra plus 1 means 0th stair state
23+
// space complexity: O(n)
24+
for (int stair = 0; stair <= n; stair++) { // time complexity O(n)
25+
if (stair <= 1) {
26+
c[stair] = 1; // O(1)
27+
continue;
28+
}
29+
c[stair] = c[stair-1] + c[stair-2]; // O(1)
30+
}
31+
32+
return c[n];
33+
}
34+
}
35+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
public TreeNode buildTree(int[] preorder, int[] inorder) {
18+
/**
19+
1. understanding
20+
- preorder: mid -> left -> right
21+
- inorder: left -> mid -> right
22+
- so, first element of the preorder array is always mid node.
23+
- if the idx of inorder's 1st depth mid node is k, then inorder[0:k-1] is the left tree part array. And also, preorder[k:] is the right tree part.
24+
2. strategy
25+
- find the inorder's mid node idx, and then split left tree part and right part, buildTree with each preorder and inorder part.
26+
27+
3. complexity
28+
- time: O(N^2)
29+
- space: O(N^2)
30+
*/
31+
if (preorder.length == 0) return null;
32+
if (preorder.length == 1) return new TreeNode(preorder[0]);
33+
int i = 0;
34+
List<Integer> leftPreorder = new ArrayList<>(); // O(N)
35+
List<Integer> leftInorder = new ArrayList<>(); // O(N)
36+
List<Integer> rightPreorder = new ArrayList<>(); // O(N)
37+
List<Integer> rightInorder = new ArrayList<>(); // O(N)
38+
for (; i < inorder.length; i++) { // O(N)
39+
if (inorder[i] == preorder[0]) break;
40+
leftPreorder.add(preorder[i+1]);
41+
leftInorder.add(inorder[i]);
42+
}
43+
for (int idx = i+1; idx < inorder.length; idx++) { // O(N)
44+
rightPreorder.add(preorder[idx]);
45+
rightInorder.add(inorder[idx]);
46+
}
47+
48+
return new TreeNode(preorder[0], buildTree(leftPreorder.stream().mapToInt(Integer::intValue).toArray(), leftInorder.stream().mapToInt(Integer::intValue).toArray()), buildTree(rightPreorder.stream().mapToInt(Integer::intValue).toArray(), rightInorder.stream().mapToInt(Integer::intValue).toArray()));
49+
}
50+
}
51+

โ€Ždecode-ways/GangBean.java

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class Solution {
2+
public int numDecodings(String s) {
3+
/**
4+
1. understanding
5+
- number to upper case alphabet mapping code: 1 -> A, ... 26 -> Z
6+
- many ways to decode each input string
7+
- also there can be no way to decode input string.
8+
- answer fits in 32-bit integer.
9+
10+
2. example
11+
- 12: (1, 2), (12)
12+
- 226: (2, 2, 6), (2, 26), (22, 6)
13+
- 06: (0, x)
14+
15+
3. strategy
16+
- iterate in reverse order,
17+
- at index k, dp[k] means the count of decode ways till that index.
18+
- dp[k-1] = 0 if num[k] == 0
19+
- dp[k-1] = dp[k] + dp[k+1] if 1<= nums[k-1:k] < 27
20+
- dp[k-1] = dp[k]
21+
- dp[n] = 1 -> assume that first empty input can be decoded in 1 way.
22+
23+
4. complexity
24+
- time: O(N)
25+
- space: O(1)
26+
*/
27+
int prev = 1;
28+
int current = 1;
29+
for (int i = s.length()-1; i >= 0; i--) { // O(1)
30+
if (s.charAt(i) == '0') {
31+
int tmp = current;
32+
current = 0;
33+
prev = tmp;
34+
} else if ( (i+1) < s.length() && Integer.parseInt(s.substring(i, i+2)) < 27) {
35+
int tmp = current;
36+
current = prev + current;
37+
prev = tmp;
38+
} else {
39+
prev = current;
40+
}
41+
}
42+
return current;
43+
}
44+
}
45+

โ€Žvalid-anagram/GangBean.java

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public boolean isAnagram(String s, String t) {
3+
/**
4+
1. ๋ฌธ์ œ ์ดํ•ด.
5+
- ์•„๋‚˜๊ทธ๋žจ: ๋ฌธ์ž์˜ ์ˆœ์„œ๋งŒ ๋ฐ”๋€Œ์—ˆ์„ ๋•Œ ๋™์ผํ•œ ๊ธ€์ž
6+
- ๊ตฌ์„ฑ ๋ฌธ์ž์™€ ์ˆซ์ž๋งŒ ๋™์ผํ•˜๋ฉด ์•„๋‚˜๊ทธ๋žจ
7+
2. ํ’€์ด ๋ฐฉ์‹
8+
- comapre s.length() with t.length()
9+
- loop over s and t, count each word's character and it's count, and then save it as map(key-value pair structure)
10+
- compare map of s and t
11+
3. Complexity
12+
- time complexity: O(N)
13+
- space complexity: O(N)
14+
*/
15+
16+
if (s.length() != s.length()) return false;
17+
18+
Map<Character, Integer> sMap = new HashMap<>();
19+
Map<Character, Integer> tMap = new HashMap<>();
20+
21+
for (char c: s.toCharArray()) {
22+
sMap.put(c, sMap.getOrDefault(c, 0) + 1);
23+
}
24+
for (char c: t.toCharArray()) {
25+
tMap.put(c, tMap.getOrDefault(c, 0) + 1);
26+
}
27+
28+
return Objects.equals(sMap, tMap);
29+
}
30+
}
31+

0 commit comments

Comments
ย (0)