File tree Expand file tree Collapse file tree 3 files changed +113
-0
lines changed
container-with-most-water
longest-increasing-subsequence Expand file tree Collapse file tree 3 files changed +113
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * Runtime: 3ms
3+ * Time Complexity: O(n)
4+ *
5+ * Memory: 77.24MB
6+ * Space Complexity: O(1)
7+ *
8+ * Approach: ์ข์ฐ ๋์ด ์ค ๋ ์์ ๋์ด ๊ธฐ์ค์ผ๋ก ๊ณ์ฐํ๊ธฐ ๋๋ฌธ์ ๊ทธ ๋์ด๋ฅผ ์ต๋ํ ๋์ผ ์ ์๋ ๋ฐฉํฅ์ผ๋ก ํฌ์ธํฐ ์ด๋
9+ * - while๋ฌธ ๋ด์์ ํ์ฌ ํฌ์ธํฐ์ ๋์ด๋ณด๋ค ๊ฐ๊ฑฐ๋ ์์ ๋์ด๋ฅผ ๊ฑด๋๋ฐ๋๋ก ์ต์ ํ
10+ */
11+ class Solution {
12+ public int maxArea (int [] height ) {
13+ int left = 0 ;
14+ int right = height .length -1 ;
15+ int max = 0 ;
16+
17+ while (left < right ) {
18+ int hLeft = height [left ];
19+ int hRight = height [right ];
20+ int currentArea = (right -left ) * Math .min (hLeft , hRight );
21+ if (currentArea > max ) {
22+ max = currentArea ;
23+ }
24+
25+ if (hLeft < hRight ) {
26+ left ++;
27+ while (left < right && height [left ] <= hLeft ) {
28+ left ++;
29+ }
30+ } else {
31+ right --;
32+ while (left < right && height [right ] <= hRight ) {
33+ right --;
34+ }
35+ }
36+ }
37+
38+ return max ;
39+ }
40+ }
Original file line number Diff line number Diff line change 1+ /**
2+ * Runtime: 3ms
3+ * Time Complexity: O(n log n)
4+ * - ๊ฐ ์์๋ง๋ค ์ด๋ถ ํ์
5+ *
6+ * Memory: 45.99MB
7+ * Space Complexity: O(n)
8+ *
9+ * Approach: Patience Sorting, ๊ฐ ์์๋ฅผ ์์๋๋ก ํ์ธํ๋ฉฐ ํ์ฌ๊น์ง ๋ง๋ ๊ฐ์ฅ ์ ๋ฆฌํ ์์ด ๊ณ์ฐ
10+ * - result์ ๋ชจ๋ ์์๋ณด๋ค ํฌ๋ฉด ๋งจ ๋ค์ ์ถ๊ฐ
11+ * - ๊ทธ๋ ์ง ์์ผ๋ฉด, ์ด๋ถ ํ์์ผ๋ก ๋์จ ์์น์ ๊ฐ์ ๋์ฒด
12+ */
13+ class Solution {
14+ public int lengthOfLIS (int [] nums ) {
15+ int [] result = new int [nums .length ];
16+ int resultSize = 0 ;
17+
18+ for (int num : nums ) {
19+ int left = 0 ;
20+ int right = resultSize ;
21+
22+ while (left < right ) {
23+ int mid = left + (right -left ) / 2 ;
24+ if (result [mid ] < num ) {
25+ left = mid +1 ;
26+ } else {
27+ right = mid ;
28+ }
29+ }
30+
31+ result [left ] = num ;
32+
33+ if (left == resultSize ) {
34+ resultSize ++;
35+ }
36+ }
37+
38+ return resultSize ;
39+ }
40+ }
Original file line number Diff line number Diff line change 1+ /**
2+ * Runtime: 4ms
3+ * Time Complexity: O(n)
4+ *
5+ * Memory: 42.94MB
6+ * Space Complexity: O(n)
7+ *
8+ * Approach: ์คํ ์ฌ์ฉ
9+ * - ์ด๋ฆฐ ๊ดํธ๋ฅผ ํธ์ํ๋ฉด์ ๋ซํ ๊ดํธ๊ฐ ๋์ฌ ๋๋ง๋ค ์คํ์์ ํํ์ฌ ์ง์ด ๋ง๋์ง ํ์ธ
10+ */
11+ class Solution {
12+ public boolean isValid (String s ) {
13+ if (s .length ()%2 == 1 ) return false ;
14+
15+ Stack <Character > stack = new Stack <>();
16+ Map <Character , Character > map = new HashMap <>();
17+ map .put ('(' , ')' );
18+ map .put ('{' , '}' );
19+ map .put ('[' , ']' );
20+
21+ for (char ch : s .toCharArray ()) {
22+ if (map .containsKey (ch )) {
23+ stack .push (ch );
24+ } else if (")}]" .indexOf (ch ) != -1 ) {
25+ if (stack .isEmpty () || ch != map .get (stack .pop ())) {
26+ return false ;
27+ }
28+ }
29+ }
30+
31+ return stack .isEmpty ();
32+ }
33+ }
You canโt perform that action at this time.
0 commit comments