Skip to content

Commit 6a88b83

Browse files
authored
Merge pull request #1428 from minji-go/week06
[minji-go] week 06 solutions
2 parents 05d427a + 91ce568 commit 6a88b83

File tree

9 files changed

+302
-54
lines changed

9 files changed

+302
-54
lines changed

combination-sum/minji-go.java

+18-18
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
/*
2-
Problem: https://leetcode.com/problems/combination-sum/
3-
Description: return a list of all unique combinations of candidates where the chosen numbers sum to target
4-
Concept: Array, Backtracking
5-
Time Complexity: O(Nᵀ), Runtime 2ms
6-
Space Complexity: O(T), Memory 44.88MB
7-
*/
8-
class Solution {
9-
public List<List<Integer>> answer = new ArrayList<>();
1+
/**
2+
* <a href="https://leetcode.com/problems/combination-sum/">week03-3.combination-sum</a>
3+
* <li>Description: return a list of all unique combinations of candidates where the chosen numbers sum to target </li>
4+
* <li>Topics: Array, Backtracking </li>
5+
* <li>Time Complexity: O(K^T), Runtime 2ms </li>
6+
* <li>Space Complexity: O(T), Memory 44.9MB </li>
7+
*/
108

9+
class Solution {
1110
public List<List<Integer>> combinationSum(int[] candidates, int target) {
11+
List<List<Integer>> combinations = new ArrayList<>();
1212
Arrays.sort(candidates);
13-
findCombination(candidates, target, new ArrayList<>(), 0);
14-
return answer;
13+
dfs(candidates, target, 0, new ArrayList<>(), combinations);
14+
return combinations;
1515
}
1616

17-
public void findCombination(int[] candidates, int target, List<Integer> combination, int idx) {
18-
if(target == 0) {
19-
answer.add(new ArrayList<>(combination));
17+
public void dfs(int[] candidates, int target, int index, List<Integer> combination, List<List<Integer>> combinations) {
18+
if (target == 0) {
19+
combinations.add(new ArrayList<>(combination));
2020
return;
2121
}
2222

23-
for(int i=idx; i<candidates.length; i++) {
24-
if(candidates[i] > target) break;
23+
for (int i = index; i < candidates.length; i++) {
24+
if (target - candidates[i] < 0) break;
2525

2626
combination.add(candidates[i]);
27-
findCombination(candidates, target-candidates[i], combination, i);
28-
combination.remove(combination.size()-1);
27+
dfs(candidates, target - candidates[i], i, combination, combinations);
28+
combination.remove(combination.size() - 1);
2929
}
3030
}
3131
}
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/container-with-most-water/">week06-2.container-with-most-water</a>
3+
* <li>Description: Return the maximum amount of water a container can store</li>
4+
* <li>Topics: Array, Two Pointers, Greedy </li>
5+
* <li>Time Complexity: O(N), Runtime 5ms </li>
6+
* <li>Space Complexity: O(1), Memory 57.42MB </li>
7+
*/
8+
class Solution {
9+
public int maxArea(int[] height) {
10+
int maxArea = 0;
11+
int left = 0;
12+
int right = height.length - 1;
13+
14+
while (left < right) {
15+
int width = right - left;
16+
int minHeight = Math.min(height[left], height[right]);
17+
maxArea = Math.max(maxArea, width * minHeight);
18+
19+
if (height[left] < height[right]) {
20+
left++;
21+
} else {
22+
right--;
23+
}
24+
}
25+
26+
return maxArea;
27+
}
28+
}

decode-ways/minji-go.java

+27-21
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
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-
*/
1+
/**
2+
* <a href="https://leetcode.com/problems/decode-ways/">week03-5.decode-ways</a>
3+
* <li>Description: return the number of ways to decode it </li>
4+
* <li>Topics: String, Dynamic Programming </li>
5+
* <li>Time Complexity: O(N), Runtime 1ms </li>
6+
* <li>Space Complexity: O(1), Memory 41.8MB</li>
7+
*/
88
class Solution {
99
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];
10+
if (s.charAt(0) == '0') {
11+
return 0;
12+
}
13+
14+
int last2 = 1;
15+
int last1 = 1;
16+
17+
for (int i = 1; i < s.length(); i++) {
18+
int curr = 0;
19+
if (s.charAt(i) != '0') {
20+
curr += last1;
21+
}
1422

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];
23+
int num = Integer.parseInt(s.substring(i - 1, i + 1));
24+
if (num >= 10 && num <= 26) {
25+
curr += last2;
26+
}
27+
28+
last2 = last1;
29+
last1 = curr;
1830
}
19-
return dp[s.length()-1];
20-
}
2131

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;
32+
return last1;
2733
}
2834
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/design-add-and-search-words-data-structure/">week06-3.design-add-and-search-words-data-structure</a>
3+
* <li>Description: Design a data structure that supports adding new words and finding if a string matches any previously added string</li>
4+
* <li>Topics: String, Depth-First Search, Design, Trie </li>
5+
* <li>Time Complexity: O(N), Runtime 274ms </li>
6+
* <li>Space Complexity: O(N), Memory 118.44MB </li>
7+
*/
8+
class WordDictionary {
9+
private Trie dictionary;
10+
11+
public WordDictionary() {
12+
dictionary = new Trie();
13+
}
14+
15+
public void addWord(String word) {
16+
dictionary.add(word);
17+
}
18+
19+
public boolean search(String word) {
20+
return dictionary.contains(word);
21+
}
22+
23+
}
24+
25+
public class Trie {
26+
private boolean isEnd;
27+
private Map<Character, Trie> next;
28+
29+
Trie() {
30+
next = new HashMap<>();
31+
}
32+
33+
public void add(String word) {
34+
Trie trie = this;
35+
for(char c : word.toCharArray()){
36+
trie = trie.next.computeIfAbsent(c, k -> new Trie());
37+
}
38+
trie.isEnd = true;
39+
}
40+
41+
public boolean contains(String word) {
42+
Trie trie = this;
43+
for(int i=0; i<word.length(); i++){
44+
char c = word.charAt(i);
45+
if(c == '.') {
46+
for(Trie newTrie : trie.next.values()) {
47+
if(newTrie.contains(word.substring(i+1))){
48+
return true;
49+
}
50+
}
51+
return false;
52+
} else {
53+
trie = trie.next.get(c);
54+
if(trie == null) {
55+
return false;
56+
}
57+
}
58+
}
59+
60+
return trie.isEnd;
61+
}
62+
}
63+
64+
/**
65+
* Your WordDictionary object will be instantiated and called as such:
66+
* WordDictionary obj = new WordDictionary();
67+
* obj.addWord(word);
68+
* boolean param_2 = obj.search(word);
69+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/longest-increasing-subsequence/">week06-4.longest-increasing-subsequence</a>
3+
* <li>Description: return the length of the longest strictly increasing subsequence</li>
4+
* <li>Topics: Array, Binary Search, Dynamic Programming </li>
5+
* <li>Time Complexity: O(NLogN), Runtime 6ms </li>
6+
* <li>Space Complexity: O(N), Memory 44.3MB </li>
7+
*/
8+
class Solution {
9+
public int lengthOfLIS(int[] nums) {
10+
List<Integer> lisTails = new ArrayList<>();
11+
12+
for(int num : nums){
13+
int idx = Collections.binarySearch(lisTails, num);
14+
15+
if(idx < 0) {
16+
idx = -idx -1;
17+
}
18+
19+
if(idx == lisTails.size()) {
20+
lisTails.add(num);
21+
} else {
22+
lisTails.set(idx, num);
23+
}
24+
}
25+
26+
return lisTails.size();
27+
}
28+
}

number-of-1-bits/minji-go.java

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/number-of-1-bits/">week03-2.number-of-1-bits</a>
3+
* <li>Description: returns the number of set bits in its binary representation</li>
4+
* <li>Topics: Divide and Conquer, Bit Manipulation </li>
5+
* <li>Time Complexity: O(logN), Runtime 0ms </li>
6+
* <li>Space Complexity: O(1), Memory 41.95MB </li>
7+
*/
8+
class Solution {
9+
public int hammingWeight(int n) {
10+
int count = 0;
11+
while(n != 0) {
12+
n &= (n-1);
13+
count++;
14+
}
15+
return count;
16+
}
17+
}

spiral-matrix/minji-go.java

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/spiral-matrix/">week06-5.spiral-matrix</a>
3+
* <li>Description: return all elements of the matrix in spiral order</li>
4+
* <li>Topics: Array, Matrix, Simulation </li>
5+
* <li>Time Complexity: O(N*M), Runtime 0ms </li>
6+
* <li>Space Complexity: O(1), Memory 41.95MB </li>
7+
*/
8+
class Solution {
9+
public List<Integer> spiralOrder(int[][] matrix) {
10+
List<Integer> answer = new ArrayList<>();
11+
int lr = 0;
12+
int hr = matrix.length - 1;
13+
int lc = 0;
14+
int hc = matrix[0].length - 1;
15+
16+
while (lr <= hr && lc <= hc) {
17+
for (int c = lc; c <= hc; c++) {
18+
answer.add(matrix[lr][c]);
19+
}
20+
lr++;
21+
22+
for (int r = lr; r <= hr; r++) {
23+
answer.add(matrix[r][hc]);
24+
}
25+
hc--;
26+
27+
if (lr <= hr) {
28+
for (int c = hc; c >= lc; c--) {
29+
answer.add(matrix[hr][c]);
30+
}
31+
hr--;
32+
}
33+
34+
if (lc <= hc) {
35+
for (int r = hr; r >= lr; r--) {
36+
answer.add(matrix[r][lc]);
37+
}
38+
lc++;
39+
}
40+
}
41+
return answer;
42+
}
43+
}

valid-palindrome/minji-go.java

+36-15
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,43 @@
1-
/*
2-
Problem: https://leetcode.com/problems/valid-palindrome/
3-
Description: return true if it is a palindrome, alphanumeric characters(letters and numbers) reads the same forward and backward
4-
Concept: Two Pointers, String
5-
Time Complexity: O(n), Runtime: 10ms
6-
Space Complexity: O(n), Memory: 58.6MB
7-
*/
1+
/**
2+
* <a href="https://leetcode.com/problems/valid-palindrome/">week03-1.valid-palindrome</a>
3+
* <li>Description: return true if it is a palindrome </li>
4+
* <li>Topics: Two Pointers, String </li>
5+
* <li>Time Complexity: O(N), Runtime 2ms </li>
6+
* <li>Space Complexity: O(1), Memory 42.75MB </li>
7+
*/
88
class Solution {
99
public boolean isPalindrome(String s) {
10-
String regex ="[^A-Za-z0-9]";
11-
String palindrome = s.replaceAll(regex,"").toLowerCase(); //replaceAll(), toLowerCase(): O(n)
10+
int left = 0;
11+
int right = s.length() - 1;
1212

13-
boolean answer = true;
14-
for(int i=0; i<palindrome.length()/2; i++){
15-
if(palindrome.charAt(i) != palindrome.charAt(palindrome.length()-1-i)) {
16-
answer = false;
17-
break;
13+
while (left < right) {
14+
char charLeft = s.charAt(left);
15+
char charRight = s.charAt(right);
16+
if (isNotValidCharacter(charLeft)) {
17+
left++;
18+
continue;
1819
}
20+
if (isNotValidCharacter(charRight)) {
21+
right--;
22+
continue;
23+
}
24+
25+
if (Character.toLowerCase(charLeft) != Character.toLowerCase(charRight)) {
26+
return false;
27+
}
28+
left++;
29+
right--;
30+
}
31+
return true;
32+
33+
}
34+
35+
private boolean isNotValidCharacter(char c) {
36+
if ((c >= '0' && c <= '9')
37+
|| (c >= 'a' && c <= 'z')
38+
|| (c >= 'A' && c <= 'Z')) {
39+
return false;
1940
}
20-
return answer;
41+
return true;
2142
}
2243
}

valid-parentheses/minji-go.java

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/valid-parentheses/">week06-1.valid-parentheses</a>
3+
* <li>Description: determine if the input string is valid </li>
4+
* <li>Topics: String, Stack </li>
5+
* <li>Time Complexity: O(N), Runtime 2ms </li>
6+
* <li>Space Complexity: O(N), Memory 41.98MB </li>
7+
*/
8+
9+
class Solution {
10+
public boolean isValid(String s) {
11+
Deque<Character> stack = new ArrayDeque<>();
12+
for(char c : s.toCharArray()) {
13+
if(isOpenBracket(c)){
14+
stack.push(c);
15+
continue;
16+
}
17+
18+
if(stack.isEmpty() || isNoneMatchBracket(stack.pop(), c)) {
19+
return false;
20+
}
21+
}
22+
23+
return stack.isEmpty();
24+
}
25+
26+
private boolean isOpenBracket(char open) {
27+
return open == '(' || open == '{' || open == '[';
28+
}
29+
30+
private boolean isNoneMatchBracket(char open, char close) {
31+
if(open == '(') return close != ')';
32+
if(open == '{') return close != '}';
33+
if(open == '[') return close != ']';
34+
return true;
35+
}
36+
}

0 commit comments

Comments
 (0)