File tree Expand file tree Collapse file tree 5 files changed +205
-0
lines changed
best-time-to-buy-and-sell-stock
encode-and-decode-strings
implement-trie-prefix-tree Expand file tree Collapse file tree 5 files changed +205
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ Time complexity: O(n)
3
+ Space complexity: O(1)
4
+
5
+ i <= j 인 두 인덱스 i, j에 대해서, prices[j] - prices[i]를 최대화해야 한다.
6
+
7
+ 1. i = 0부터 시작하여, 오른쪽으로 순회한다.
8
+ 2. 현재 값이 max보다 크다면, max를 갱신하고, min과의 차이를 계산한다.
9
+ 3. 현재 값이 min보다 작다면, min을 갱신하고, max 역시 같은 값으로 갱신한다. (과거로 돌아가서 팔 수는 없으므로)
10
+ */
11
+ class Solution {
12
+ public int maxProfit (int [] prices ) {
13
+ int min = 999999 ;
14
+ int max = 0 ;
15
+ int ans = 0 ;
16
+ for (int i = 0 ; i < prices .length ; i ++) {
17
+ if (prices [i ] > max ) {
18
+ max = prices [i ];
19
+ if (max - min > ans ) {
20
+ ans = max - min ;
21
+ }
22
+ }
23
+ if (prices [i ] < min ) {
24
+ min = max = prices [i ];
25
+ }
26
+ }
27
+
28
+ return ans ;
29
+ }
30
+ }
Original file line number Diff line number Diff line change
1
+ /*
2
+ Time Complexity: O(n)
3
+ Space Complexity: O(1)
4
+
5
+ non-ASCII 유니코드 문자를 사용함
6
+
7
+ To-Do : escaping 방법 학습하기
8
+ */
9
+ public class Codec {
10
+
11
+ // Encodes a list of strings to a single string.
12
+ public String encode (List <String > strs ) {
13
+ StringBuilder sb = new StringBuilder ();
14
+ for (int i = 0 ; i < strs .size (); i ++) {
15
+ if (i > 0 ) {
16
+ sb .append ('\u2764' );
17
+ }
18
+ sb .append (strs .get (i ));
19
+ }
20
+
21
+ return sb .toString ();
22
+ }
23
+
24
+ // Decodes a single string to a list of strings.
25
+ public List <String > decode (String s ) {
26
+ return new ArrayList <>(Arrays .asList (s .split ("\u2764 " )));
27
+ }
28
+ }
29
+
30
+ // Your Codec object will be instantiated and called as such:
31
+ // Codec codec = new Codec();
32
+ // codec.decode(codec.encode(strs));
Original file line number Diff line number Diff line change
1
+ /*
2
+ Time Complexity: O(n)
3
+ Space Complexity: O(n)
4
+ */
5
+ class Solution {
6
+ public List <List <String >> groupAnagrams (String [] strs ) {
7
+ Map <String , List <String >> map = new HashMap <>();
8
+
9
+ for (String str : strs ) {
10
+ int [] cnt = new int [26 ];
11
+ for (int i = 0 ; i < str .length (); i ++) {
12
+ cnt [str .charAt (i ) - 'a' ]++;
13
+ }
14
+ char [] chars = new char [str .length ()];
15
+ int idx = 0 ;
16
+ for (int i = 0 ; i < 26 ; i ++) {
17
+ while (cnt [i ] > 0 ) {
18
+ chars [idx ++] = (char )(i + 'a' );
19
+ cnt [i ]--;
20
+ }
21
+ }
22
+ String sorted = new String (chars );
23
+ if (!map .containsKey (sorted )) {
24
+ map .put (sorted , new ArrayList <>());
25
+ }
26
+ map .get (sorted ).add (str );
27
+ }
28
+
29
+ List <List <String >> ans = new ArrayList <>();
30
+ for (String key : map .keySet ()) {
31
+ ans .add (new ArrayList <>());
32
+ for (String str : map .get (key )) {
33
+ ans .get (ans .size () - 1 ).add (str );
34
+ }
35
+ }
36
+
37
+ return ans ;
38
+ }
39
+ }
Original file line number Diff line number Diff line change
1
+ /*
2
+ Time Complexity (n = length of word/prefix)
3
+ - initialization: O(1)
4
+ - insert: O(n)
5
+ - search: O(n)
6
+ - startsWith: O(n)
7
+
8
+ Space Complexity: O(n * c) (c = calls)
9
+ - 길이 3인 알파벳 소문자 문자열의 가짓수 : 26^3 = 17,576
10
+ - 길이 4인 알파벳 소문자 문자열의 가짓수 : 26^4 = 456,976
11
+ 만약 n이 3 이하였다면, 3만 번의 call 동안 trie가 포화되어 공간 복잡도가 O(26 * n) = O(n) 이었을 것.
12
+ 하지만 n이 2,000으로 충분히 크기 때문에 trie가 포화되지는 않을 것이므로, 공간 복잡도는 O(n * c).
13
+ */
14
+ class Trie {
15
+
16
+ class Node {
17
+ public char val ;
18
+ public boolean ends ;
19
+ public HashMap <Character , Node > children ;
20
+
21
+ Node () {
22
+ this .children = new HashMap <>();
23
+ }
24
+ }
25
+
26
+ public Node root ;
27
+
28
+ public Trie () {
29
+ this .root = new Node ();
30
+ }
31
+
32
+ public void insert (String word ) {
33
+ Node curr = this .root ;
34
+
35
+ for (char ch : word .toCharArray ()) {
36
+ curr = curr .children .computeIfAbsent (ch , c -> new Node ());
37
+ curr .val = ch ;
38
+ }
39
+ curr .ends = true ;
40
+ }
41
+
42
+ public boolean search (String word ) {
43
+ Node curr = this .root ;
44
+
45
+ for (char ch : word .toCharArray ()) {
46
+ curr = curr .children .get (ch );
47
+ if (curr == null )
48
+ return false ;
49
+ }
50
+ return curr .ends ;
51
+ }
52
+
53
+ public boolean startsWith (String prefix ) {
54
+ Node curr = this .root ;
55
+
56
+ for (char ch : prefix .toCharArray ()) {
57
+ curr = curr .children .get (ch );
58
+ if (curr == null )
59
+ return false ;
60
+ }
61
+ return true ;
62
+ }
63
+ }
64
+
65
+ /**
66
+ * Your Trie object will be instantiated and called as such:
67
+ * Trie obj = new Trie();
68
+ * obj.insert(word);
69
+ * boolean param_2 = obj.search(word);
70
+ * boolean param_3 = obj.startsWith(prefix);
71
+ */
Original file line number Diff line number Diff line change
1
+ /*
2
+
3
+ Time Complexity: O(s * L^2)
4
+ - L is max length of wordDict[i]
5
+ Space Complexity: O(s + w * L)
6
+ */
7
+ class Solution {
8
+ public boolean wordBreak (String s , List <String > wordDict ) {
9
+ Set <String > wordSet = new HashSet <>();
10
+ int maxLen = 0 ;
11
+ for (String word : wordDict ) {
12
+ wordSet .add (word );
13
+ maxLen = Math .max (maxLen , word .length ());
14
+ }
15
+
16
+ boolean [] dp = new boolean [s .length () + 1 ];
17
+ dp [0 ] = true ;
18
+
19
+ for (int i = 0 ; i < s .length (); i ++) {
20
+ for (int j = 1 ; j <= maxLen ; j ++) {
21
+ int beginIdx = i - j + 1 ;
22
+ if (beginIdx < 0 )
23
+ continue ;
24
+ if (wordSet .contains (s .substring (beginIdx , i + 1 )) && dp [beginIdx ]) {
25
+ dp [i + 1 ] = true ;
26
+ break ;
27
+ }
28
+ }
29
+ }
30
+
31
+ return dp [s .length ()];
32
+ }
33
+ }
You can’t perform that action at this time.
0 commit comments