File tree Expand file tree Collapse file tree 2 files changed +63
-0
lines changed
best-time-to-buy-and-sell-stock Expand file tree Collapse file tree 2 files changed +63
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+
3+ ์ต๋์ด์ต = ์ต๊ณ ๊ธ์ก - ์ต์๊ธ์ก
4+ */
5+ class Solution {
6+ public int maxProfit (int [] prices ) {
7+ int minPrice = Integer .MAX_VALUE ;
8+ int maximumProfit = 0 ;
9+
10+ for (int price : prices ) {
11+ if (price < minPrice ) {
12+ minPrice = price ;
13+ }
14+ int currentProfit = price - minPrice ;
15+ if (currentProfit > maximumProfit ) {
16+ maximumProfit = currentProfit ;
17+ }
18+ }
19+ return maximumProfit ;
20+ }
21+ }
22+
Original file line number Diff line number Diff line change 1+ /**
2+ 1. ๋ฌธ์ ์ดํด
3+ anagrams๋ ๋จ์ด๊ฐ ์์๋ ๋จ์ด์ ํฌํจ๋ ์ํ๋ฒณ ๋น๋์๊ฐ ๋ชจ๋ ๋์ผํ๊ฒ์ ์๋ฏธํฉ๋๋ค.
4+ group anagrams๋ list์ ๋จ์ด๋ชฉ๋ก์ด ์์๋ anagrams์ด ๋๋ ๋จ์ด๋ค์ ๋ฐฐ์ด๋ก ๊ทธ๋ฃนํํ ๋ฆฌ์คํธ ์
๋๋ค.
5+
6+ 2. naive algorithm๋์ถ
7+
8+ O(N^2) ๋ฐฉ๋ฒ
9+ 2.1.๋จ์ด ํ๋๋ฅผ ์ ํํ๊ณ , ํด๋น ๋จ์ด์ ๋น๋์๋ฅผ ์ ์ฅํ๋ Map์ ์์ฑํฉ๋๋ค.
10+ 2.2ํด๋น ๋จ์ด๋ฅผ ์ ์ธํ๊ณ ๋๋จธ์ง ๋จ์ด๋ค์ ํ์ํ๋ฉด์ ๋์ผํ ๋น๋์๋ฅผ ๊ฐ์ง ๋จ์ด๋ค์ ์๋ก์ด list์ ์ ์ฅํฉ๋๋ค.
11+
12+ ๊ฐ์ ํฌ์ธํธ
13+ ๊ฐ ๋จ์ด๋ฅผ ์ํ๋ฒณ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌ
14+
15+ */
16+ class sangyyypark {
17+ public List <List <String >> groupAnagrams (String [] strs ) {
18+ Map <String ,List <String >> map = new HashMap <>();
19+ for (String str : strs ) {
20+ String key = sortedString (str );
21+ List <String > group = map .get (key );
22+ if (group != null ) {
23+ group .add (str );
24+ }
25+ else {
26+ List <String > newGroup = new ArrayList <>();
27+ newGroup .add (str );
28+ map .put (key , newGroup );
29+ }
30+ }
31+
32+ return new ArrayList <>(map .values ());
33+ }
34+
35+ public String sortedString (String str ) {
36+ char [] charArr = str .toCharArray ();
37+ Arrays .sort (charArr );
38+ return new String (charArr );
39+ }
40+ }
41+
You canโt perform that action at this time.
0 commit comments