File tree Expand file tree Collapse file tree 2 files changed +71
-0
lines changed
best-time-to-buy-and-sell-stock Expand file tree Collapse file tree 2 files changed +71
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+
3+
4+
5+
6+
7+ //O(n)에 증가 한 경우 profitMax를 갱신시키며 판단 -> time limit
8+ public int maxProfit (int [] prices ) {
9+ int profitMax = 0 ;
10+ /*
11+ for(int i=0; i< prices.length - 1; i++){
12+ int buy= prices[i];
13+
14+
15+ for(int d =i+1; d < prices.length; d++){
16+ int sellCandid = prices[d];
17+ if (sellCandid - buy > profitMax){
18+ profitMax = sellCandid - buy;
19+ }
20+
21+ }
22+ }//end of for
23+
24+ */
25+
26+ int priceMin = Integer .MAX_VALUE ;
27+ for (int i =0 ; i < prices .length ; i ++){
28+ if (prices [i ] < priceMin ){
29+ priceMin = prices [i ];
30+ }
31+ else {
32+ profitMax = Math .max (profitMax , prices [i ] - priceMin );
33+ }
34+ }
35+
36+ return profitMax ;
37+
38+ }
39+
40+ }
41+
Original file line number Diff line number Diff line change 1+ import java .util .*;
2+
3+ class Solution {
4+ //static HashMap<
5+ public List <List <String >> groupAnagrams (String [] strs ) {
6+ HashMap <String , List <String >> map = new HashMap <>();
7+
8+ for (int w = 0 ;w < strs .length ; w ++){
9+ String word = strs [w ];
10+ char [] arr = word .toCharArray ();
11+
12+ //정렬한거
13+ Arrays .sort (arr );
14+ String newWord = new String (arr );
15+
16+ map .putIfAbsent (newWord , new ArrayList <>());
17+ map .get (newWord ).add (word );
18+
19+ }
20+
21+
22+ List <List <String >> list = new ArrayList <>();
23+ for (List <String > group : map .values ()){
24+ list .add (group );
25+ }
26+
27+ return list ;
28+ }
29+ }
30+
You can’t perform that action at this time.
0 commit comments