File tree Expand file tree Collapse file tree 5 files changed +209
-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 +209
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ public int maxProfit (int [] prices ) {
3+ /**
4+ 1. ํ๋ฃจ์ ํ์์ ๊ฐ์ฅ ์ต๋ ์ด์ต์ ๊ตฌํ๋๋กํ๋ max price return
5+ 2. ์กฐ๊ฑด
6+ - ๋ฏธ๋ ๋ค๋ฅธ๋ ์ง์ ํ๋งค (์ด์ ๋ ์ง์ ํ๋งค x)
7+ - choosing a single day
8+ - ๋ฐฐ์ด ๊ธธ์ด min = 1, max = 10^5
9+ - ์์๊ฐ : min = 0, max = 10^4
10+ 3. ํ์ด
11+ - 1)brtueforce: time complexity O(n^2), space: O(1)
12+ - 2)ํ์ฌ ๊ฐ - ์ด์ ๊ฐ ์ค ๊ฐ์ฅ ์ต์๊ฐ -> maxProfit , ์ฆ min๊ฐ์ ๊ณ์ ๊ธฐ์ตํ๋ค๊ฐ ํ์ฌ ๊ฐ๊ณผ์ ์ฐจ์ด ์ค ๊ฐ์ฅ ํฐ ๊ฐ์ ๊ตฌํ๋ฉด๋๋ค.
13+ - time: O(n)
14+ - space: O(1)
15+ */
16+
17+ int maxProfit = 0 ;
18+ int minStock = Integer .MAX_VALUE ;
19+ int n = prices .length ;
20+
21+ for (int i = 0 ; i <n ; i ++) {
22+ if (prices [i ] < minStock ) {
23+ minStock = prices [i ];
24+ }
25+ if (i > 0 && prices [i ] - minStock > maxProfit ) {
26+ maxProfit = Math .max (maxProfit , prices [i ] - minStock );
27+ }
28+ }
29+ return maxProfit ;
30+
31+
32+ // for(int i = 0; i < n; i++) {
33+ // int curStock = prices[i];
34+ // for(int j= i + 1; j < n; j++) {
35+ // if(curStock < prices[j]) {
36+ // int curProfit = prices[j] - curStock;
37+ // maxProfit = Math.max(maxProfit, curProfit);
38+ // }
39+ // }
40+ // }
41+ // return maxProfit;
42+ }
43+ }
Original file line number Diff line number Diff line change 1+ class Solution :
2+ """
3+ @param: strs: a list of strings
4+ @return: encodes a list of strings to a single string.
5+ """
6+ def encode (self , strs : List [str ]):
7+ text = ""
8+ for str in strs :
9+ text += f"{ len (str )} :{ str } "
10+ return text
11+
12+ """
13+ @param: str: A string
14+ @return: decodes a single string to a list of strings
15+ """
16+ def decode (self , s : str ):
17+ output = []
18+ start = 0
19+ while start < len (s ):
20+ mid = s .find (":" , start )
21+ length = int (s [start :mid ])
22+ output .append (s [mid + 1 : mid + 1 + length ])
23+ start = mid + 1 + length
24+ return output
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public List <List <String >> groupAnagrams (String [] strs ) {
3+ /**
4+ 1.anagram ๋ผ๋ฆฌ ๊ทธ๋ฃนํํด์ return
5+ 2.์กฐ๊ฑด
6+ - answer in any order
7+ - strs ๊ธธ์ด ์ต์ = 1, ์ต๋ = 10^4
8+ - ์์ ํ๋๋น ๊ธธ์ด ์ต์ = 0, ์ต๋ = 100
9+ - ๋ชจ๋ ์๋ฌธ์๋ก ๊ตฌ์ฑ
10+ 3.ํ์ด
11+ - 1) i๋ฒ์งธ ์ดํ ๋จ์ด๋ฅผ ๋น๊ตํด๊ฐ๋ฉด์ anagram check, time: O(n^2)
12+ - 2) HashMap: string element ๋ฅผ ์ ๋ ฌํด์ key ๋ก ์ฌ์ฉ, ์ค๋ณต๋๋ Key ์์ผ๋ฉด anagram, time: O(n)
13+ */
14+
15+ int n = strs .length ;
16+ //anagram ์ฒดํฌํ Map
17+ Map <String , List <String >> map = new HashMap <>();
18+
19+ for (int i = 0 ; i < n ; i ++) {
20+ String curStr = strs [i ];
21+ char [] tmp = curStr .toCharArray ();
22+ Arrays .sort (tmp );
23+ String key = new String (tmp );
24+ // System.out.println("curStr:" + curStr + ", key: " + key);
25+ //Map ์ ์ ์ฅ
26+ map .putIfAbsent (key , new ArrayList <>());
27+ map .get (key ).add (curStr );
28+ // if(!map.containsKey(key)) {
29+ // List<String> words = new ArrayList<>();
30+ // words.add(curStr);
31+ // map.put(key, words);
32+ // } else {
33+ // List<String> words = map.get(key);
34+ // words.add(curStr);
35+ // map.put(key, words);
36+ // }
37+ }
38+
39+
40+ return new ArrayList <>(map .values ());
41+ }
42+ }
Original file line number Diff line number Diff line change 1+ class Trie {
2+ class TrieNode {
3+ TrieNode [] children ;
4+ boolean isEnd ;
5+
6+ public TrieNode () {
7+ children = new TrieNode [26 ]; // a ~ z
8+ isEnd = false ;
9+ }
10+ }
11+
12+ private TrieNode root ;
13+
14+ public Trie () {
15+ root = new TrieNode ();
16+ }
17+
18+ public void insert (String word ) {
19+ TrieNode node = root ;
20+
21+ for (char c : word .toCharArray ()) {
22+ int index = c - 'a' ;
23+ //์ฒ์ ๋ค์ด์ค๋ ๋จ์ด๋ node ์์ฑ
24+ if (node .children [index ] == null ) {
25+ node .children [index ] = new TrieNode ();
26+ }
27+ //๋ค์ ๋
ธ๋๋ก ์ด๋
28+ node = node .children [index ];
29+ }
30+ //๋จ์ด์ ๋ ํ์
31+ node .isEnd = true ;
32+ }
33+
34+ public boolean search (String word ) {
35+ TrieNode node = root ;
36+ for (char c : word .toCharArray ()) {
37+ int index = c - 'a' ;
38+ //์กด์ฌํ์ง ์์ผ๋ฉด false return
39+ if (node .children [index ] == null ) {
40+ return false ;
41+ }
42+ //๋ค์ ๋
ธ๋๋ก ์ด๋
43+ node = node .children [index ];
44+ }
45+ return node .isEnd ;
46+ }
47+
48+ public boolean startsWith (String prefix ) {
49+ //์ค๊ฐ์ null์ ์๋ง๋๋ฉด true ๋ฐํ
50+ TrieNode node = root ;
51+ for (char c : prefix .toCharArray ()) {
52+ int index = c - 'a' ;
53+ if (node .children [index ] == null ) {
54+ return false ;
55+ }
56+ node = node .children [index ];
57+ }
58+ return true ;
59+ }
60+ }
61+
62+ /**
63+ * Your Trie object will be instantiated and called as such:
64+ * Trie obj = new Trie();
65+ * obj.insert(word);
66+ * boolean param_2 = obj.search(word);
67+ * boolean param_3 = obj.startsWith(prefix);
68+ */
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public boolean wordBreak (String s , List <String > wordDict ) {
3+ /**
4+ 1.๋ฌธ์ : s ๊ฐ wordDict ๋ก ์ชผ๊ฐ์ง ์ ์๋์ง true/false return
5+ 2.์กฐ๊ฑด
6+ - s ์๋ wordDict ๊ฐ ์ฌ๋ฌ๋ฒ ๋์ฌ ์ ์์.
7+ - s.length() ์ต์ = 1, ์ต๋ 300
8+ - wordDict.size() ์ต์ 1, ์ต๋ 1000
9+ - ํ ๋จ์ด ๊ธธ์ด ์ต์=1, ์ต๋ = 20
10+ - ๋ชจ๋ ์๋ฌธ์. ๋ชจ๋ wordDict ๊ฐ์ unique
11+ 3.ํ์ด
12+ - i๋ฒ์งธ๊น์ง ๋ฌธ์์ด์ ๋ง๋ค ์ ์๋์ง check
13+ */
14+
15+ int n = s .length ();
16+ boolean [] dp = new boolean [n +1 ];
17+
18+ dp [0 ] = true ; //๋น ๋ฌธ์์ด์ธ ๊ฒฝ์ฐ
19+
20+ for (int i = 1 ; i < n +1 ; i ++) {
21+ for (int j = 0 ; j < i ; j ++) {
22+ String subStr = s .substring (j , i );
23+ //์๋จ๊ณ ๋ชจ๋ ๋ง๋ค ์ ์๊ณ && wordDict์ ์กด์ฌํ๋์ง ์ฒดํฌ
24+ if (dp [j ] && wordDict .contains (subStr )) {
25+ dp [i ] = true ;
26+ break ;
27+ }
28+ }
29+ }
30+ return dp [n ];
31+ }
32+ }
You canโt perform that action at this time.
0 commit comments