File tree Expand file tree Collapse file tree 2 files changed +62
-0
lines changed
container-with-most-water Expand file tree Collapse file tree 2 files changed +62
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * ๊ตฌํ๋ ค๋ ๊ฒ์ ์ต๋ ์์ญ๊ฐ์ด๊ธฐ ๋๋ฌธ์ x,y์ถ์ ๊ณ์ฐํ ํ์ฌ ์์ญ๊ณผ ๊ธฐ์กด ์์ญ์ ๋น๊ตํด max๊ฐ์ ๋ฐํํ๋ฉด ๋ฉ๋๋ค.
3+ * ํฌ์ธํฐ๋ฅผ ์ด๋ป๊ฒ ์ขํ๊น ์๊ฐํ ์ ์๋๋ฐ ์ฝ๊ฒ ๋งํด์ start์ end ์ค ๋ ์์ ์ชฝ์ ์ ๋๋ ๋ค๋ก ์ด๋ํ๋ฉด ๋ฉ๋๋ค.
4+ * ์์ญ์ ๋ ๋ง๋๊ฐ ๋ชจ๋ ์ถฉ์กฑ๊ฐ๋ฅํ ๊ธธ์ด๊ฐ ๋์ด์ผ ํ๋ฏ๋ก Math.min()์ผ๋ก ์ค์ ํฉ๋๋ค.
5+ */
6+ class Solution {
7+ public int maxArea (int [] height ) {
8+ int start = 0 ;
9+ int end = height .length - 1 ;
10+ int area = 0 ;
11+
12+ while (start < end ) {
13+ int y = Math .min (height [start ], height [end ]); // y์ถ์ ๋ ์์ ๊ฐ์ผ๋ก ์ค์
14+ int x = Math .abs (start - end );
15+ int calculatedArea = x * y ;
16+ area = Math .max (area , calculatedArea );
17+
18+ // [์ค์] ํฌ์ธํฐ ์ด๋ ๋ก์ง
19+ if (height [start ] <= height [end ])
20+ start ++;
21+ else
22+ end --;
23+ }
24+ return area ;
25+ }
26+ }
Original file line number Diff line number Diff line change 1+ import java .util .Stack ;
2+
3+ class Solution {
4+ /**
5+ * ์ฃผ์ด์ง ๋ฌธ์์ด s์ ๊ดํธ๊ฐ ์ ๋๋ก ๋ซํ์๋์ง ์ฌ๋ถ๋ฅผ ํ๋จํ๋ ๋ฌธ์ ์
๋๋ค.
6+ * ๋์ฌ ์ ์๋ ๊ดํธ์ ์ข
๋ฅ๋ 3๊ฐ์ด๋ฉฐ ์ง๋ง์ถ๊ธฐ์ ์ ๋ฆฌํ ์๋ฃ๊ตฌ์กฐ์ธ ์คํ์ ์ฌ์ฉํฉ๋๋ค.
7+ * {, [, ( ๊ฐ์ ์ผ์ชฝ ๊ดํธ๋ ๋ฌด์กฐ๊ฑด stack์ ์ถ๊ฐ๋ฉ๋๋ค.
8+ * }, ], ) ๊ฐ์ ์ค๋ฅธ์ชฝ ๊ดํธ๋ฅผ ๋ง๋๋ฉด ๋งค์นญ๋๋ ์ผ์ชฝ ๊ดํธ๋ฅผ ๋ง๋ ๋๊น์ง pop()์ ํตํด stack์์ ๊ดํธ๋ค์ ๊บผ๋ด์ผ ํฉ๋๋ค.
9+ */
10+ public boolean isValid (String s ) {
11+ Stack <Character > stack = new Stack <>();
12+
13+ for (char c : s .toCharArray ()) {
14+ if (c == '(' || c == '[' || c == '{' ) {
15+ stack .push (c );
16+ } else {
17+ /**
18+ * [์ค์] ๋ฌธ์์ด์ด ์ค๋ฅธ์ชฝ ๊ดํธ๋ก๋ง ๊ตฌ์ฑ๋๋ ๊ฒฝ์ฐ๋ ์์ผ๋ฏ๋ก for๋ฌธ์ด ๋ฐ๋ณต๋๋๋ฐ ์คํ์ด ๋น์๋ค๋ฉด
19+ * ๊ทธ ์ฆ์ false๋ฅผ ๋ฐํํ๊ณ breakํด์ผ๋ง stack Exception์ ๋ง์ ์ ์์ต๋๋ค.
20+ * ๋ฌธ์ ์ผ์ด์ค: "]"
21+ */
22+ if (stack .isEmpty ()) {
23+ return false ;
24+ }
25+ if (stack .peek () == '(' && c == ')' ) {
26+ stack .pop ();
27+ } else if (stack .peek () == '[' && c == ']' ) {
28+ stack .pop ();
29+ } else if (stack .peek () == '{' && c == '}' ) {
30+ stack .pop ();
31+ }
32+ }
33+ }
34+ return stack .isEmpty (); // ์คํ์ด ๋น์๋ค๋ฉด ๊ดํธ๋ ์ง์ด ๋ง๋๋ค๋ ์๋ฏธ์
๋๋ค.
35+ }
36+ }
You canโt perform that action at this time.
0 commit comments