File tree Expand file tree Collapse file tree 2 files changed +44
-0
lines changed
best-time-to-buy-and-sell-stock Expand file tree Collapse file tree 2 files changed +44
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * 121. Best Time to Buy and Sell Stock
3+ * 반복문 돌면서 가장 작은 값을 기억해둡니다
4+ * 매번 최소값과의 차이를 비교합니다
5+ */
6+ class Solution {
7+ public int maxProfit (int [] prices ) {
8+ int maxProfit = 0 ;
9+ int minPrice = Integer .MAX_VALUE ;
10+
11+ for (int price : prices ) {
12+ if (price < minPrice ) { // 최소 값 찾기
13+ minPrice = price ;
14+ }
15+ if (price - minPrice > maxProfit ) {
16+ maxProfit = price - minPrice ;
17+ }
18+ }
19+ return maxProfit ;
20+ }
21+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public List <List <String >> groupAnagrams (String [] strs ) {
3+ List <List <String >> answer = new ArrayList <>();
4+ // <정렬한 문자, 원본 문자 리스트>
5+ HashMap <String , List <String >> map = new HashMap <>();
6+
7+ for (String str : strs ) {
8+ char [] arr = str .toCharArray ();
9+ Arrays .sort (arr );
10+ String strKey = String .valueOf (arr );
11+
12+ if (!map .containsKey (strKey )) {
13+ ArrayList <String > list = new ArrayList <>();
14+ list .add (str );
15+ map .put (strKey , list );
16+ } else {
17+ map .get (strKey ).add (str );
18+ }
19+ }
20+ answer .addAll (map .values ());
21+ return answer ;
22+ }
23+ }
You can’t perform that action at this time.
0 commit comments