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+ 시간복잡도: O(n²)
3+ 공간복잡도: O(1)
4+ class Solution {
5+ public int maxProfit(int[] prices) {
6+ int maxStock = 0;
7+ for(int i = 0; i < prices.length; i++) {
8+ for (int j = 0; j < prices.length; j++) {
9+ if (i <= j) break;
10+ if (prices[i] - prices[j] > maxStock) {
11+ maxStock = prices[i] - prices[j];
12+ }
13+ }
14+ }
15+ return maxStock;
16+ }
17+ }
18+ */
19+ // 시간복잡도: O(n)
20+ // 공간복잡도: O(1)
21+ class Solution {
22+ public int maxProfit (int [] prices ) {
23+ int maxStock = 0 ;
24+ int minPrice = prices [0 ];
25+ for (int price : prices ) {
26+ if (price < minPrice ) {
27+ minPrice = price ;
28+ } else {
29+ maxStock = Math .max (price - minPrice , maxStock );
30+ }
31+ }
32+ return maxStock ;
33+ }
34+ }
Original file line number Diff line number Diff line change 1+ /*
2+ 시간복잡도: O(k log k)
3+ 공간복잡도: O(n * k)
4+ */
5+ class Solution {
6+ public List <List <String >> groupAnagrams (String [] strs ) {
7+ Map <String , List <String >> map = new HashMap <>();
8+
9+ for (String str : strs ) {
10+ char [] chars = str .toCharArray ();
11+ Arrays .sort (chars );
12+ String key = new String (chars );
13+
14+ if (!map .containsKey (key )) {
15+ map .put (key , new ArrayList <>());
16+ }
17+ map .get (key ).add (str );
18+ }
19+ return new ArrayList <>(map .values ());
20+ }
21+ }
You can’t perform that action at this time.
0 commit comments