File tree Expand file tree Collapse file tree 2 files changed +55
-0
lines changed
best-time-to-buy-and-sell-stock Expand file tree Collapse file tree 2 files changed +55
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * Runtime: 1ms
3+ * Time Complexity: O(n)
4+ *
5+ * Memory: 94.08MB
6+ * Space Complexity: O(1)
7+ *
8+ * Approach: ๊ทธ๋ฆฌ๋ ์๊ณ ๋ฆฌ์ฆ
9+ * - ์ฃผ์ด์ง prices ๋ฐฐ์ด์ ์ํํ๋ฉฐ ์ต์ ๊ฐ๊ฒฉ(min)์ ๊ฐฑ์
10+ */
11+ class Solution {
12+ public int maxProfit (int [] prices ) {
13+ int maxProfit = 0 ;
14+ int min = prices [0 ];
15+
16+ for (int i =1 ; i <prices .length ; i ++) {
17+ if (min >= prices [i ]) {
18+ min = prices [i ];
19+ } else {
20+ maxProfit = Math .max (maxProfit , prices [i ]-min );
21+ }
22+ }
23+
24+ return maxProfit ;
25+ }
26+ }
Original file line number Diff line number Diff line change 1+ /**
2+ * Runtime: 6ms
3+ * Time Complexity: O(n x k log k)
4+ * - n: ๋ฌธ์์ด ๊ฐ์
5+ * - k: ๋ฌธ์์ด ์ต๋ ๊ธธ์ด
6+ * - k log k: ๊ฐ ๋ฌธ์์ด์ ์ ๋ ฌํ๋ ์๊ฐ
7+ *
8+ * Memory: 49.30MB
9+ * Space Complexity: O(n x k)
10+ * - ์ต๋ n๊ฐ์ ํค-๊ฐ
11+ *
12+ * Approach: ์ ๋ ฌ๋ ๋ฌธ์์ด์ ํค๋ก ์ฌ์ฉํด์ ๊ฐ์ ์ ๋๊ทธ๋จ๋ผ๋ฆฌ ๊ทธ๋ฃนํ
13+ */
14+ class Solution {
15+ public List <List <String >> groupAnagrams (String [] strs ) {
16+ Map <String , List <String >> anagramMap = new HashMap <>();
17+
18+ for (String str : strs ) {
19+ char [] chs = str .toCharArray ();
20+ Arrays .sort (chs );
21+ String key = new String (chs );
22+
23+ anagramMap .computeIfAbsent (key , k -> new ArrayList <>())
24+ .add (str );
25+ }
26+
27+ return new ArrayList <>(anagramMap .values ());
28+ }
29+ }
You canโt perform that action at this time.
0 commit comments