File tree Expand file tree Collapse file tree 2 files changed +58
-0
lines changed
best-time-to-buy-and-sell-stock Expand file tree Collapse file tree 2 files changed +58
-0
lines changed Original file line number Diff line number Diff line change
1
+ // NOTE: tc --> O(n)
2
+ class Solution {
3
+ public int maxProfit (int [] prices ) {
4
+
5
+ int curMax = 0 ;
6
+ int gMax = 0 ;
7
+
8
+ if (prices .length == 0 ) return 0 ;
9
+
10
+ int sell = prices [0 ];
11
+ for (int i = 1 ; i < prices .length ; i ++) {
12
+ curMax = Math .max (0 , prices [i ] - sell );
13
+
14
+ // NOTE: 새롭게 시작하는게 더 좋은경우
15
+ if (curMax == 0 ) {
16
+ sell = prices [i ];
17
+ }
18
+
19
+ gMax = Math .max (curMax , gMax );
20
+ }
21
+
22
+ return gMax ;
23
+ }
24
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .ArrayList ;
2
+ import java .util .Arrays ;
3
+ import java .util .HashMap ;
4
+ import java .util .List ;
5
+ import java .util .Map ;
6
+
7
+ // NOTE: tc -> O(n)
8
+ class Solution {
9
+ public List <List <String >> groupAnagrams (String [] strs ) {
10
+
11
+ List <List <String >> result = new ArrayList <>();
12
+ Map <String , List <String >> sMap = new HashMap <>();
13
+
14
+ for (int i = 0 ; i < strs .length ; i ++) {
15
+ char [] cArr = strs [i ].toCharArray ();
16
+ Arrays .sort (cArr );
17
+ String sorted = new String (cArr );
18
+
19
+ if (sMap .containsKey (sorted )) {
20
+ sMap .get (sorted ).add (strs [i ]);
21
+ } else {
22
+ List <String > temp = new ArrayList <>();
23
+ temp .add (strs [i ]);
24
+ sMap .put (sorted , temp );
25
+ }
26
+ }
27
+
28
+ for (List <String > arr : sMap .values ()) {
29
+ result .add (arr );
30
+ }
31
+
32
+ return result ;
33
+ }
34
+ }
You can’t perform that action at this time.
0 commit comments