File tree Expand file tree Collapse file tree 3 files changed +86
-0
lines changed
best-time-to-buy-and-sell-stock
encode-and-decode-strings Expand file tree Collapse file tree 3 files changed +86
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ /**
3+ 시간복잡도: O(n)
4+ 공간복잡도: O(1)
5+ */
6+ public int maxProfit (int [] prices ) {
7+ int buyPrice = prices [0 ];
8+ int maxProfit = 0 ;
9+
10+ for (int i = 0 ; i < prices .length ; i ++) {
11+ buyPrice = Math .min (buyPrice , prices [i ]);
12+ maxProfit = Math .max (maxProfit , prices [i ] - buyPrice );
13+ }
14+
15+ return maxProfit ;
16+ }
17+ }
Original file line number Diff line number Diff line change 1+ import java .util .ArrayList ;
2+ import java .util .List ;
3+
4+ public class Codec {
5+ String SPLIT = ":" ;
6+
7+ // Encodes a list of strings to a single string.
8+ public String encode (List <String > strs ) {
9+ StringBuilder encoded = new StringBuilder ();
10+
11+ for (String str : strs ) {
12+ encoded .append (str .length ()).append (SPLIT ).append (str );
13+ }
14+
15+ return encoded .toString ();
16+ }
17+
18+ // Decodes a single string to a list of strings.
19+ public List <String > decode (String s ) {
20+ List <String > decoded = new ArrayList <>();
21+
22+ int pointer = 0 ;
23+ while (pointer < s .length ()) {
24+ int index = s .indexOf (SPLIT , pointer );
25+ int length = Integer .parseInt (s .substring (pointer , index ));
26+
27+ String str = s .substring (index + 1 , index + 1 + length );
28+ decoded .add (str );
29+
30+ pointer = index + 1 + length ;
31+ }
32+
33+ return decoded ;
34+ }
35+ }
36+
37+ // Your Codec object will be instantiated and called as such:
38+ // Codec codec = new Codec();
39+ // codec.decode(codec.encode(strs));
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+ class Solution {
8+ /**
9+ 시간복잡도: O(n)
10+ 공간복잡도: O(n)
11+ */
12+ public List <List <String >> groupAnagrams (String [] strs ) {
13+ Map <String , List <String >> anagramMap = new HashMap <>();
14+
15+ for (String str : strs ) {
16+ char [] charStr = str .toCharArray ();
17+ Arrays .sort (charStr );
18+ String sortedStr = String .valueOf (charStr );
19+
20+ if (!anagramMap .containsKey (sortedStr )) {
21+ anagramMap .put (sortedStr , new ArrayList <>());
22+
23+ }
24+
25+ anagramMap .get (sortedStr ).add (str );
26+ }
27+
28+ return new ArrayList <>(anagramMap .values ());
29+ }
30+ }
You can’t perform that action at this time.
0 commit comments