Skip to content

Commit 4b251c4

Browse files
authored
Merge pull request #2194 from YuuuuuuYu/main
[YuuuuuuYu] WEEK 06 solutions
2 parents 06415a3 + 0e4baa6 commit 4b251c4

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
}

0 commit comments

Comments
ย (0)