Skip to content

Commit 11eac89

Browse files
authored
Merge pull request #1872 from geegong/main
[geegong] WEEK 06 solutions
2 parents 6564843 + 4a85208 commit 11eac89

File tree

5 files changed

+309
-0
lines changed

5 files changed

+309
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import java.util.ArrayList;
2+
import java.util.HashMap;
3+
import java.util.List;
4+
import java.util.Map;
5+
6+
public class Geegong {
7+
8+
/**
9+
* case 1. Letter ๋ผ๋Š” ํด๋ž˜์Šค ์•ˆ์— ๊ฐ char ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ  ํ•˜๋‚˜์”ฉ ์ˆœํšŒํ•˜๋ฉด์„œ search ๋ฅผ ํ•˜๋‹ˆ ์‘๋‹ต์†๋„๊ฐ€ ๋„ˆ๋ฌด ๋А๋ฆผ..
10+
*/
11+
class WordDictionary {
12+
public Map<Integer, List<Letter>> wordsLengthMap;
13+
14+
public WordDictionary() {
15+
this.wordsLengthMap = new HashMap<>();
16+
}
17+
18+
public void addWord(String word) {
19+
char[] wordChars = word.toCharArray();
20+
21+
Letter prev = new Letter(null);
22+
Letter root = new Letter(prev);
23+
for (char wordChar : wordChars) {
24+
Letter curr = new Letter(wordChar, false);
25+
26+
prev.nextLetter = curr;
27+
28+
prev = curr;
29+
}
30+
31+
prev.isEnd = true;
32+
33+
wordsLengthMap.computeIfAbsent(wordChars.length, ArrayList::new).add(root.nextLetter);
34+
}
35+
36+
public boolean search(String word) {
37+
char[] wordChars = word.toCharArray();
38+
int length = wordChars.length;
39+
40+
if (!wordsLengthMap.containsKey(length)) {
41+
return false;
42+
}
43+
44+
List<Letter> letters = wordsLengthMap.get(length);
45+
for (Letter letter : letters) {
46+
if (letter.isMatched(wordChars)) {
47+
return true;
48+
}
49+
}
50+
51+
return false;
52+
}
53+
}
54+
55+
public static class Letter {
56+
public char currentLetterOfAsciiCode; // '?'-'a'
57+
public Letter nextLetter;
58+
public boolean isEnd; // ๋งˆ์ง€๋ง‰ ๊ธ€์ž ์ธ์ง€ ์—ฌ๋ถ€
59+
60+
public Letter(Letter next) {
61+
this.nextLetter = next;
62+
}
63+
64+
public Letter(char wordChar, boolean isEnd) {
65+
this.currentLetterOfAsciiCode = wordChar;
66+
this.isEnd = isEnd;
67+
}
68+
69+
public boolean isMatched(char[] wordChars) {
70+
71+
Letter current = this.nextLetter;
72+
// ๋ณธ์ธ ์‹œ์ ์œผ๋กœ ๋ถ€ํ„ฐ wordChars ๋งˆ์ง€๋ง‰๊นŒ์ง€ ์ฒดํฌ
73+
for (char wordChar : wordChars) {
74+
75+
// . ์ด๋ฉด ๊ทธ๋ƒฅ ํŒจ์Šค
76+
boolean isMatched = wordChar == '.' || wordChar == current.currentLetterOfAsciiCode;
77+
if (isMatched) {
78+
if (current.isEnd) {
79+
return true;
80+
} else {
81+
current = current.nextLetter;
82+
}
83+
84+
} else {
85+
return false;
86+
}
87+
}
88+
89+
return false;
90+
91+
}
92+
}
93+
}
94+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
public class Geegong {
2+
3+
/**
4+
* two pointer ์‚ฌ์šฉ
5+
* time complexity : o(log n)
6+
* space complexity : o(1)
7+
*
8+
* when nums are sorted array, binary search could be used in O(log n)
9+
* (rotating doesn't matter)
10+
* @param nums
11+
* @return
12+
*/
13+
public int findMin(int[] nums) {
14+
int leftIndex = 0;
15+
int rightIndex = nums.length - 1;
16+
17+
while(leftIndex < rightIndex) {
18+
int midIndex = leftIndex + ((rightIndex - leftIndex) / 2);
19+
20+
if (nums[midIndex] > nums[rightIndex]) {
21+
leftIndex = midIndex + 1;
22+
// midIndex ์— +1 ํ•˜๋Š” ์ด์œ ๋Š” midIndex ๊ฐ’์€ ์ด๋ฏธ rightIndex ๋ณด๋‹ค ํฌ๋‹ค๊ณ  ํŒ๋‹จ์ด ๋˜๊ธฐ์— ์ตœ์†Ÿ๊ฐ’์ด ๋  ์ˆ˜ ์—†์–ด
23+
// midIndex ์˜ ๊ฐ’์€ ๋ฐฐ์ œํ•œ๋‹ค.
24+
} else if (nums[midIndex] < nums[rightIndex]) {
25+
rightIndex = midIndex - 1;
26+
} else if (nums[midIndex] == nums[rightIndex]) { // ๋‘๊ฐœ ์ธ๋ฑ์Šค๊ฐ€ ๊ฐ™์•„์งˆ ์ˆ˜๊ฐ€ ์žˆ๋‚˜?
27+
return nums[midIndex];
28+
}
29+
}
30+
31+
return nums[leftIndex];
32+
}
33+
34+
}
35+
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import java.util.Arrays;
2+
3+
public class Geegong {
4+
5+
/**
6+
* case 1. binarySearch ๋ฅผ ์ด์šฉ, ๋ฒ”์œ„๋ฅผ 1๊ฐœ์˜ ์›์†Œ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ ์ ์ฐจ ๋Š˜๋ฆด ์ˆ˜ ์žˆ๋Š” LIS ๋ฅผ ๋ณ„๋„ ๋ณ€์ˆ˜์—์„œ ๊ด€๋ฆฌ
7+
* ๊ทธ๋ฆฌ๊ณ  ์ •๋ ฌ์ด ๋˜์–ด๊ฐ€๋Š” ๋ฐฐ์—ด์„ ๋”ฐ๋กœ ์ €์žฅํ•˜๋Š” ๋ฐฐ์—ด๋„ ํ•„์š”
8+
* Patience Sorting ์•„์ด๋””์–ด (?) -> ์ฐพ์•„๋ณด์ž
9+
* time complexity :
10+
* O(log n) - binary search ์ด๊ณ 
11+
* ์œ„ bs ๋ฅผ ๊ฐ ์›์†Œ๋ณ„๋กœ ์ง„ํ–‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์— o(n log n)
12+
* space complexity : O(n)
13+
*
14+
* case 2. DP
15+
* dp[i] => nums ์˜ i ๋ฒˆ์งธ ์ธ๋ฑ์Šค๊นŒ์ง€์˜ LIS ์ตœ์žฅ ๊ธธ์ด๋ฅผ ์˜๋ฏธ
16+
* time complexity : O(n^2)
17+
* space complexity : O(n)
18+
*
19+
* @param nums
20+
* @return
21+
*/
22+
// public static int lengthOfLIS(int[] nums) {
23+
// int n = nums.length;
24+
// int[] tails = new int[n];
25+
// int size = 0;
26+
//
27+
// for (int x : nums) {
28+
// // 0 ~ size ์•ˆ์—์„œ ์ด๋ถ„ํƒ์ƒ‰์œผ๋กœ ๋„ฃ์„ ์ˆ˜ ์žˆ๋Š” ์ž๋ฆฌ๊ฐ€ ์žˆ์œผ๋ฉด ๊ทธ ์ž๋ฆฌ์˜ index ๋ฆฌํ„ด (๋”ฐ๋ผ์„œ tc ๋Š” o(n log n)
29+
// // ๋„ฃ์„ ์ˆ˜ ์—†์œผ๋ฉด ์Œ์ˆ˜๊ฐ’์„ ๋ฆฌํ„ดํ•˜๋Š”๋ฐ ret = - (insertionPosition - 1)
30+
// // (0~size) ์•ˆ์—์„œ x ๊ฐ€ ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋Š” index๋Š”?=> insertionPosition = - (ret + 1) ์ด๋ ‡๊ฒŒ ํš๋“
31+
// int i = Arrays.binarySearch(tails, 0, size, x);
32+
// if (i < 0) i = -(i + 1); // lower_bound
33+
// tails[i] = x;
34+
// // size ๋Š” nums ์•ˆ์— ์›์†Œ๋“ค์„ ํ•˜๋‚˜์”ฉ ํ›‘์–ด๊ฐ€๋ฉด์„œ ์ง€๊ธˆ๊นŒ์ง€ LIS์˜ ์ตœ์žฅ ๊ธธ์ด
35+
// if (i == size) size++;
36+
// }
37+
// return size;
38+
// }
39+
40+
public static int lengthOfLIS(int[] nums) {
41+
int[] dp = new int[nums.length + 1];
42+
int maxLIS = 0;
43+
44+
for (int index=0; index<nums.length; index++) {
45+
dp[index] = 1; // ์ฒซ๋ฒˆ์จฐ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ LIS ๋Š” 1์ด๋ผ๊ณ  ๋ณด์ž
46+
for (int innerIndex=0; innerIndex<index; innerIndex++) {
47+
// ex) {10,9,2,5,3,7,101,18};
48+
if (nums[innerIndex] < nums[index]) {
49+
//dp[index] = dp[index] + 1; => ๋งจ ์ฒ˜์Œ์—๋Š” ์ด๋ ‡๊ฒŒ ์ƒ๊ฐํ–ˆ์œผ๋‚˜
50+
dp[index] = Math.max(dp[index], dp[innerIndex] + 1);
51+
// dp[innerIndex] + 1 ์—์„œ +1์„ ํ•œ ์ด์œ ๋Š” innerIndex์˜ ๊ฐ’๊ณผ index์˜ ๊ฐ’๊นŒ์ง€๊ฐ€ LIS๊ฐ€ ๋  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋”ํ•ด์ง„ ๊ฒƒ
52+
}
53+
}
54+
55+
maxLIS = Math.max(maxLIS, dp[index]);
56+
}
57+
58+
return maxLIS;
59+
}
60+
61+
62+
}
63+

โ€Žspiral-matrix/Geegong.javaโ€Ž

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
public class Geegong {
5+
6+
/**
7+
* direction vector ์™€ vistited ๋ฐฐ์—ด์„ ์ด์šฉํ•ด ๋ฉ”๋ชจ์ด์ œ์ด์…˜์„ ํ™œ์šฉํ•˜์—ฌ ํ’€์–ด๋ณด๊ธฐ
8+
* time complexity : o(n)
9+
* space complexity : o(n) - result ์šฉ
10+
* @param matrix
11+
* @return
12+
*/
13+
// move Colum direction , move Row Direction
14+
public int[][] directionVectors = {{0,1}, {1,0}, {0,-1}, {-1, 0}};
15+
public List<Integer> spiralOrder(int[][] matrix) {
16+
17+
List<Integer> result = new ArrayList<>(matrix.length * matrix[0].length);
18+
19+
// for memoization, ๋ฐฉ๋ฌธํ•œ matrix์˜ ์ธ๋ฑ์Šค๋“ค์„ ์ €์žฅ
20+
// int ๋ฐฐ์—ด์€ ๊ธฐ๋ณธ์œผ๋กœ 0 ์ด ์ดˆ๊ธฐํ™”
21+
int[][] visited = new int[matrix.length][matrix[0].length];
22+
23+
makeSpiralMatrix(matrix, 0,0, 0, result, visited);
24+
25+
return result;
26+
}
27+
28+
public void makeSpiralMatrix(int[][] matrix, int startRowIdx, int startColIdx, int directionOrder, List<Integer> result, int[][] visited) {
29+
if (startRowIdx < 0 || startRowIdx >= matrix.length || startColIdx < 0 || startColIdx >= matrix[0].length) {
30+
return;
31+
}
32+
33+
// ๋ฐฉ๋ฌธํ–ˆ๋˜ ๋…€์„์„ ๋‹ค์‹œ ์ฐพ์•„์˜จ๊ฑฐ๋ผ๋ฉด ๋‹ค ๋Œ์•˜๋‹ค๊ณ  ํŒ๋‹จํ•˜๊ณ  recursive ์ข…๋ฃŒ
34+
if (visited[startRowIdx][startColIdx] == 1) {
35+
return;
36+
}
37+
38+
// ์ด๋ฒˆ ์ˆœ์„œ์˜ vector ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค
39+
int[] direction = directionVectors[directionOrder];
40+
41+
// row, col ๊ฐ๊ฐ ์–ด๋–ค ๋ฐฉํ–ฅ์œผ๋กœ ์›€์ง์ผ์ง€ ์ง€์ •
42+
int moveRowPos = direction[0];
43+
int moveColPos = direction[1];
44+
45+
int count = 0;
46+
47+
int rowIndex = startRowIdx + (moveRowPos * count);
48+
int colIndex = startColIdx + (moveColPos * count);
49+
50+
do {
51+
int currentVal = matrix[rowIndex][colIndex];
52+
result.add(currentVal);
53+
visited[rowIndex][colIndex] = 1;
54+
55+
count++;
56+
rowIndex = startRowIdx + (moveRowPos * count);
57+
colIndex = startColIdx + (moveColPos * count);
58+
if (colIndex < 0 || colIndex >= matrix[0].length || rowIndex < 0 || rowIndex >= matrix.length) {
59+
break;
60+
}
61+
62+
} while(visited[rowIndex][colIndex] != 1);
63+
64+
// vector ๋ฆฌ์ŠคํŠธ๋“ค์€ 0๋ฒˆ์งธ, 1๋ฒˆ์งธ, 2๋ฒˆ์งธ, 3๋ฒˆ์งธ, 0๋ฒˆ์งธ, 1๋ฒˆ์งธ .. ์ด ์ˆœ์„œ๋Œ€๋กœ ๊บผ๋‚ด์„œ ์‚ฌ์šฉํ•œ๋‹ค
65+
int nextDirectionOrder = directionOrder + 1 >= directionVectors.length ? 0 : directionOrder + 1;
66+
// ๋‹ค์Œ ๋ฐฉํ–ฅ์„ ๋ฏธ๋ฆฌ ์ง€์ •
67+
int[] nextDirection = directionVectors[nextDirectionOrder];
68+
69+
// ๋‹ค์Œ ๋ฐฉํ–ฅ์˜ ์‹œ์ž‘์  ๋ฏธ๋ฆฌ ์ง€์ •
70+
count--;
71+
int nextStartRowIdx = startRowIdx + (moveRowPos * count) + nextDirection[0];
72+
int nextStartColIdx = startColIdx + (moveColPos * count) + nextDirection[1];
73+
74+
makeSpiralMatrix(matrix, nextStartRowIdx, nextStartColIdx, nextDirectionOrder, result, visited);
75+
}
76+
}
77+
78+
79+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import java.util.HashMap;
2+
import java.util.Stack;
3+
4+
public class Geegong {
5+
6+
/**
7+
* stack ์ž๋ฃŒ๊ตฌ์กฐ ์‚ฌ์šฉ
8+
* ๋‹ซ๋Š” ๋ถ€๋ถ„๋งŒ hashMap์˜ key ๊ฐ’์œผ๋กœ ์„œ๋กœ ํŽ˜์–ด๊ฐ€ ๋˜๋Š” ๊ฐ’์„ value ๋กœ ํ•œ๋‹ค.
9+
* stack ์ž๋ฃŒ๊ตฌ์กฐ๋Š” ๋‹ซ๋Š” bracket์ด ์•„๋‹ˆ๋ฉด push ํ•ด์„œ ๋„ฃ๊ณ  ๋‹ซ๋Š” bracket ๋งŒ ์ฒดํฌ๋ฅผ ํ•˜๋Š”๋ฐ
10+
* stack ์—์„œ pop ์„ ํ•˜๋Š”๋ฐ ์ด๋–„ map์˜ value์™€ ๋‹ค๋ฅด๋ฉด false
11+
*
12+
* time complexity : o(n)
13+
* space complexity : o(1)
14+
* @param s
15+
* @return
16+
*/
17+
public boolean isValid(String s) {
18+
HashMap<Character, Character> map = new HashMap<Character, Character>();
19+
map.put(')', '(');
20+
map.put(']', '[');
21+
map.put('}', '{');
22+
23+
Stack<Character> stack = new Stack<Character>();
24+
for (int i=0; i<s.length(); i++) {
25+
Character ch = s.charAt(i);
26+
if (map.containsKey(ch)) {
27+
if (stack.isEmpty() || map.get(ch) != stack.pop()) {
28+
return false;
29+
}
30+
} else {
31+
stack.push(ch);
32+
}
33+
}
34+
35+
return stack.isEmpty();
36+
}
37+
}
38+

0 commit comments

Comments
ย (0)