Skip to content

Commit 015ee61

Browse files
authored
Merge pull request #907 from forest000014/main
[forest000014] Week 6
2 parents 22fea49 + 0b38772 commit 015ee61

File tree

5 files changed

+376
-0
lines changed

5 files changed

+376
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
solution 1. brute force
3+
Time Complexity: O(n^2)
4+
Space Complexity: O(1)
5+
๋ชจ๋“  ์„ ๋ถ„ i์— ๋Œ€ํ•ด, ๊ฐ€์žฅ ๋งŽ์€ ๋ฌผ์„ ๋‹ด์„ ์ˆ˜ ์žˆ๋Š” ๋ฐ˜๋Œ€์ชฝ ์„ ๋ถ„ j๋ฅผ ์ฐพ๋Š”๋‹ค.
6+
7+
8+
solution 2. PQ
9+
Time Complexity: O(nlogn)
10+
- ์ •๋ ฌ : O(nlogn)
11+
- i๋ฒˆ์งธ ์„ ๋ถ„์— ๋Œ€ํ•ด (์ž์‹ ๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์€) ๊ฐ€์žฅ ๋ฉ€๋ฆฌ์žˆ๋Š” ์„ ๋ถ„ ํƒ์ƒ‰(O(logn)) * n = O(nlogn)
12+
Space Complexity: O(n)
13+
14+
(์‚ฌ๊ณ ์˜ ํ๋ฆ„์„ ์ ๊ธฐ ์œ„ํ•ด ํŽธ์˜์ƒ ๋ฐ˜๋ง๋กœ ์ ์—ˆ์Šต๋‹ˆ๋‹ค... ^^)
15+
brute force ์—์„œ ๋ถˆํ•„์š”ํ•œ ํƒ์ƒ‰์„ ์ค„์—ฌ๋ณด์ž.
16+
์ผ๋‹จ ์–ด๋–ค ์„ ๋ถ„ i๊ฐ€ ์ฃผ์–ด์กŒ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์ž. i๋ฅผ ํ•œ์ชฝ ๋ฒฝ์œผ๋กœ ํ•ด์„œ ๊ฐ€์žฅ ๋งŽ์€ ๋ฌผ์„ ๋‹ด๋Š” ๋ฐฉ๋ฒ•์„ ์ฐพ์œผ๋ ค๋ฉด, ๋ฐ˜๋“œ์‹œ ๋‚˜๋จธ์ง€ ๋ชจ๋“  ์„ ๋ถ„์„ ํƒ์ƒ‰ํ•ด์•ผ๋งŒ ํ• ๊นŒ?
17+
18+
(1) ์ž์‹ ๋ณด๋‹ค ์ž‘์€ ์„ ๋ถ„์€ ํƒ์ƒ‰ํ•˜์ง€ ์•Š๋Š”๋‹ค.
19+
20+
์ž์‹ ๋ณด๋‹ค ํฐ ์„ ๋ถ„๋งŒ์„ ํƒ์ƒ‰ํ•ด๋„ ์ถฉ๋ถ„ํ•˜๋‹ค.
21+
์™œ๋ƒํ•˜๋ฉด, ์„ค๋ น ์ž์‹ ๋ณด๋‹ค ๋” ์ž‘์€ ์„ ๋ถ„ ์ค‘์— ์ •๋‹ต์ด ์žˆ์—ˆ๋‹ค๊ณ  ํ•˜๋”๋ผ๋„, ๊ทธ ์„ ๋ถ„์„ ๊ธฐ์ค€์œผ๋กœ ํƒ์ƒ‰ํ•  ๋•Œ ์ •๋‹ต์— ํ•ด๋‹นํ•˜๋Š” ์Œ์„ ํ™•์ธํ•˜๊ฒŒ ๋  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
22+
23+
(2) ์ž์‹ ๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์€ ์„ ๋ถ„๋งŒ์„ ํƒ์ƒ‰ ๋Œ€์ƒ์œผ๋กœ ์‚ผ๋Š”๋‹ค๋ฉด, ๊ฐ€์žฅ ๋ฉ€๋ฆฌ์žˆ๋Š” ์„ ๋ถ„๋งŒ ํ™•์ธํ•˜๋ฉด ๋œ๋‹ค.
24+
25+
ํƒ์ƒ‰ ๋Œ€์ƒ์ด ์ž์‹ ๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์€ ์„ ๋ถ„๋“ค์ด๋ผ๋ฉด, ์–ด์ฐจํ”ผ ๋‹ด์„ ์ˆ˜ ์žˆ๋Š” ๋ฌผ์˜ ๋†’์ด๋Š” ์ž์‹ ์˜ ๋†’์ด๋กœ ์ผ์ •ํ•˜๋‹ค.
26+
๋”ฐ๋ผ์„œ, ๋ฉ€๋ฆฌ์žˆ๋Š” ์„ ๋ถ„์ผ์ˆ˜๋ก ๋” ๋งŽ์€ ๋ฌผ์„ ๋‹ด๊ฒŒ ๋œ๋‹ค.
27+
์ฆ‰, "์ž์‹ ๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด์„œ", "๊ฐ€์žฅ ๋ฉ€๋ฆฌ ์žˆ๋Š”(์˜ค๋ฅธ์ชฝ์ด๋“  ์™ผ์ชฝ์ด๋“ )" ์„ ๋ถ„๋งŒ์„ ํ›„๋ณด๋กœ ์‚ผ์•„ ํ™•์ธํ•˜๋ฉด ์ถฉ๋ถ„ํ•˜๋‹ค.
28+
(๊ทธ ์™ธ์˜ ๋‚˜๋จธ์ง€, ์ฆ‰ ์ž์‹ ๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด์„œ, ๊ฐ€์žฅ ๋ฉ€๋ฆฌ์žˆ์ง€ ์•Š์€ ๋‚˜๋จธ์ง€ ์„ ๋ถ„๋“ค์€, ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ํƒ์ƒ‰ํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ๋‹ค.)
29+
30+
์ •๋ฆฌํ•˜์ž๋ฉด, ์ฃผ์–ด์ง„ ์„ ๋ถ„ i์— ๋Œ€ํ•ด์„œ, ๋‹จ 2๋ฒˆ(์˜ค๋ฅธ์ชฝ, ์™ผ์ชฝ)์˜ ํƒ์ƒ‰๋งŒ ํ•˜๋ฉด ์ถฉ๋ถ„ํ•˜๋‹ค.
31+
32+
(3) ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ๊ณผ 2๊ฐœ์˜ PQ๋ฅผ ์•„๋ž˜์ฒ˜๋Ÿผ ํ™œ์šฉํ•˜๋ฉด, ์œ„ ํƒ์ƒ‰์„ O(logn) ์‹œ๊ฐ„์— ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.
33+
PQ๋Š” ๊ฐ๊ฐ x ์ขŒํ‘œ ๊ธฐ์ค€ max heap(๊ฐ€์žฅ ์˜ค๋ฅธ์ชฝ์˜ ์„ ๋ถ„์„ ์ฐพ๊ธฐ ์œ„ํ•จ), x ์ขŒํ‘œ ๊ธฐ์ค€ min heap(๊ฐ€์žฅ ์™ผ์ชฝ์˜ ์„ ๋ถ„์„ ์ฐพ๊ธฐ ์œ„ํ•จ)์„ ์‚ฌ์šฉํ•œ๋‹ค.
34+
์„ ๋ถ„๋“ค์„ ๊ธธ์ด ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด๋†“๊ณ , ํ•˜๋‚˜์”ฉ ์ˆœํšŒํ•˜๋ฉด์„œ (say, i๋ฒˆ์งธ ์„ ๋ถ„), ์•„๋ž˜ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•œ๋‹ค.
35+
- ์ž์‹ ๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด์„œ ๊ฐ€์žฅ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋ฉ€๋ฆฌ ์žˆ๋Š” ์„ ๋ถ„์˜ ์œ„์น˜๋ฅผ ์ฐพ์•„์„œ(= ํ˜„์žฌ PQ(max heap)์˜ root), ์ตœ๋Œ€ ๋ฌผ์˜ ์–‘์„ ๊ณ„์‚ฐํ•œ๋‹ค.
36+
- ์ž์‹ ๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด์„œ ๊ฐ€์žฅ ์™ผ์ชฝ์œผ๋กœ ๋ฉ€๋ฆฌ ์žˆ๋Š” ์„ ๋ถ„์˜ ์œ„์น˜๋ฅผ ์ฐพ์•„์„œ(= ํ˜„์žฌ PQ(min heap)์˜ root), ์ตœ๋Œ€ ๋ฌผ์˜ ์–‘์„ ๊ณ„์‚ฐํ•œ๋‹ค.
37+
- i๋ฒˆ์งธ ์„ ๋ถ„์„ PQ 2๊ฐœ์— ๊ฐ๊ฐ ๋„ฃ๋Š”๋‹ค.
38+
39+
40+
solution 3. two pointers
41+
(AlgoDale ํ’€์ด๋ฅผ ์ฐธ๊ณ ํ•จ)
42+
43+
Time Complexity: O(n)
44+
Space Complexity: O(1)
45+
46+
2 ํฌ์ธํ„ฐ๋ฅผ ํ™œ์šฉํ•˜๋ฉด, PQ๋„ ์—†์ด ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ O(n)์œผ๋กœ ์ค„์ผ ์ˆ˜ ์žˆ์—ˆ๋‹ค.
47+
๋‹จ์ˆœํžˆ "ํฐ ์ชฝ์„ ์ค„์ด๊ธฐ๋ณด๋‹ค๋Š”, ์ž‘์€ ์ชฝ์„ ์ค„์ด๋Š” ๊ฒŒ ์œ ๋ฆฌํ•˜๊ฒ ์ง€" ์ •๋„์˜ greedyํ•œ ๋…ผ๋ฆฌ๋Š” ์ถฉ๋ถ„ํ•˜์ง€ ์•Š์€ ๊ฒƒ ๊ฐ™๊ณ , ๋” ๋ช…ํ™•ํ•œ ๊ทผ๊ฑฐ๊ฐ€ ์žˆ์„ ๊ฒƒ ๊ฐ™์€๋ฐ ์‹œ๊ฐ„ ๊ด€๊ณ„์ƒ ๊ณ ๋ฏผํ•ด๋ณด์ง€๋Š” ๋ชปํ–ˆ๋‹ค.
48+
49+
To-Do : ํ’€์ด๊ฐ€ ๋Œ€๊ฐ• ์ดํ•ด๋Š” ๋˜์—ˆ์ง€๋งŒ, ์ด๊ฒŒ ์™œ ๋˜๋Š”์ง€, ์—„๋ฐ€ํ•˜๊ฒŒ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด PQ๋ฅผ ์‚ฌ์šฉํ–ˆ๋˜ ๋…ผ๋ฆฌ๋ฅผ ์ข€ ๋” ๋ฐœ์ „์‹œ์ผœ๋ณผ ํ•„์š”๊ฐ€ ์žˆ๋‹ค.
50+
51+
*/
52+
class Solution {
53+
public int maxArea(int[] height) {
54+
int i = 0, j = height.length - 1;
55+
56+
int ans = 0;
57+
while (i < j) {
58+
int area = (j - i) * Math.min(height[i], height[j]);
59+
if (area > ans) {
60+
ans = area;
61+
}
62+
63+
if (height[i] <= height[j]) {
64+
i++;
65+
} else {
66+
j--;
67+
}
68+
}
69+
70+
return ans;
71+
}
72+
73+
////// ์•„๋ž˜๋Š” solution 2
74+
private static class Tuple {
75+
private int x;
76+
private int h;
77+
78+
Tuple(int x, int h) {
79+
this.x = x;
80+
this.h = h;
81+
}
82+
}
83+
84+
public int maxArea2(int[] height) {
85+
List<Tuple> tuples = IntStream.range(0, height.length)
86+
.mapToObj(i -> new Tuple(i, height[i]))
87+
.collect(Collectors.toList());
88+
Collections.sort(tuples, (a, b) -> b.h - a.h);
89+
90+
PriorityQueue<Tuple> minPq = new PriorityQueue<>((a, b) -> a.x - b.x);
91+
PriorityQueue<Tuple> maxPq = new PriorityQueue<>((a, b) -> b.x - a.x);
92+
93+
int ans = 0;
94+
for (int i = 0; i < height.length; i++) {
95+
minPq.add(tuples.get(i));
96+
maxPq.add(tuples.get(i));
97+
98+
Tuple curr = tuples.get(i);
99+
100+
Tuple left = minPq.peek();
101+
int leftArea = (curr.x - left.x) * curr.h;
102+
if (leftArea > ans) {
103+
ans = leftArea;
104+
}
105+
106+
Tuple right = maxPq.peek();
107+
int rightArea = (right.x - curr.x) * curr.h;
108+
if (rightArea > ans) {
109+
ans = rightArea;
110+
}
111+
}
112+
113+
return ans;
114+
}
115+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
Time Complexity:
3+
- add: O(w)
4+
- search: O(26^2 * w) = O(w)
5+
Space Complexity: O(w)
6+
7+
Trie๋ฅผ ํ™œ์šฉํ•˜๋˜, '.'์˜ ํƒ์ƒ‰์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ์—๋Š” for๋ฌธ์„ ์‚ฌ์šฉํ•œ๋‹ค.
8+
9+
*/
10+
class WordDictionary {
11+
class Node {
12+
public char ch;
13+
public boolean ends;
14+
public Map<Character, Node> children;
15+
16+
Node() {
17+
this.children = new HashMap<>();
18+
}
19+
20+
Node(char ch) {
21+
this.ch = ch;
22+
this.children = new HashMap<>();
23+
}
24+
}
25+
26+
Node root;
27+
28+
public WordDictionary() {
29+
this.root = new Node();
30+
}
31+
32+
public void addWord(String word) {
33+
Node curr = this.root;
34+
35+
for (int i = 0; i < word.length(); i++) {
36+
char ch = word.charAt(i);
37+
if (!curr.children.containsKey(ch)) {
38+
curr.children.put(ch, new Node(ch));
39+
}
40+
41+
curr = curr.children.get(ch);
42+
}
43+
44+
curr.ends = true;
45+
}
46+
47+
public boolean search(String word) {
48+
return searchChar(word, 0, this.root);
49+
}
50+
51+
private boolean searchChar(String word, int idx, Node curr) {
52+
if (curr == null) {
53+
return false;
54+
} else if (idx == word.length()) {
55+
return curr.ends;
56+
}
57+
58+
char ch = word.charAt(idx);
59+
60+
if (ch == '.') {
61+
for (Character key : curr.children.keySet()) {
62+
if (searchChar(word, idx + 1, curr.children.get(key))) {
63+
return true;
64+
}
65+
}
66+
return false;
67+
} else {
68+
return searchChar(word, idx + 1, curr.children.get(ch));
69+
}
70+
}
71+
}
72+
73+
/**
74+
* Your WordDictionary object will be instantiated and called as such:
75+
* WordDictionary obj = new WordDictionary();
76+
* obj.addWord(word);
77+
* boolean param_2 = obj.search(word);
78+
*/
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
# Time Complexity: O(nlogn)
3+
- tuples ArrayList ์ •๋ ฌ: O(nlogn)
4+
- ์ธ๋ฑ์Šค ํŠธ๋ฆฌ ์‚ฝ์ž…: O(logn) * n times = O(nlogn)
5+
- ์ธ๋ฑ์Šค ํŠธ๋ฆฌ ์กฐํšŒ: O(logn) * n times = O(nlogn)
6+
7+
# Space Complexity: O(nlogn)
8+
- tuples ArrayList: O(n)
9+
- ์ธ๋ฑ์Šค ํŠธ๋ฆฌ: O(nlogn)
10+
11+
# solution
12+
์ด ๋ฌธ์ œ๋Š” DP๋กœ ์ ‘๊ทผํ–ˆ์Šต๋‹ˆ๋‹ค.
13+
14+
LIS(1, x)๋ฅผ ๋ฒ”์œ„ [1, x] ๋‚ด์˜ LIS(๋‹จ, nums[x]๋ฅผ ๋ฐ˜๋“œ์‹œ ํฌํ•จ)์˜ ๊ธธ์ด๋ผ๊ณ  ์ •์˜ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. - (1)
15+
1 <= j < i ์ธ ๋ชจ๋“  j์— ํ•œ LIS(1, j)๋ฅผ ์•Œ๊ณ  ์žˆ๋‹ค๋ฉด, LIS(1, i)๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
16+
LIS(1, i) = max(LIS(1, j)) (๋‹จ, j๋Š” 1 <= j < i ์ด๊ณ , nums[j] < nums[i]) - (2)
17+
18+
max(LIS(1, j))๋ฅผ ๊ตฌํ•  ๋•Œ, ๋ชจ๋“  j์— ๋Œ€ํ•ด ํƒ์ƒ‰ํ•œ๋‹ค๋ฉด, ์ „์ฒด ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(n^2)๊ฐ€ ๋˜๊ธฐ ๋•Œ๋ฌธ์—, ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ์ค„์ผ ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
19+
์ด ํƒ์ƒ‰ ๊ณผ์ •์„ ์ค„์ด๊ธฐ ์œ„ํ•ด, ์•„๋ž˜์˜ ์‚ฌ๊ณ  ๊ณผ์ •์„ ๊ฑฐ์ณค์Šต๋‹ˆ๋‹ค.
20+
21+
์–ด๋–ค ๋ฒ”์œ„ ๋‚ด์˜ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ O(logn) ์‹œ๊ฐ„์— ๊ตฌํ•˜๊ธฐ ์œ„ํ•œ ์ž๋ฃŒ๊ตฌ์กฐ๋กœ, ์ธ๋ฑ์Šค ํŠธ๋ฆฌ(ํ˜น์€ ์„ธ๊ทธ๋จผํŠธ ํŠธ๋ฆฌ)๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
22+
(์ด ์ธ๋ฑ์Šค ํŠธ๋ฆฌ์˜ x๋ฒˆ์งธ leaf ๋…ธ๋“œ์—๋Š” LIS(1, x) ๊ฐ’์„ ์ €์žฅํ•˜๊ณ , internal ๋…ธ๋“œ์—๋Š” ์ž์‹ ๋…ธ๋“œ๋“ค ์ค‘ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.)
23+
24+
๋‹ค๋งŒ, ๋‹จ์ˆœํžˆ ํ•ด๋‹น ๋ฒ”์œ„ ๋‚ด์˜ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ๊ตฌํ•˜๋Š” ๊ฒƒ๋งŒ์œผ๋กœ๋Š” ๋ถ€์กฑํ•˜๊ณ , nums[j] < nums[i]์ธ j๋งŒ์„ ํ›„๋ณด๋กœ ์‚ผ์•„์•ผ ํ•  ํ…๋ฐ์š”,
25+
๊ทธ๋Ÿฌ๊ธฐ ์œ„ํ•ด์„œ, ์ธ๋ฑ์Šค ํŠธ๋ฆฌ์— ๋ชจ๋“  leaf ๋…ธ๋“œ๋ฅผ ๋ฏธ๋ฆฌ ์‚ฝ์ž…ํ•ด๋‘๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์•„๋ž˜์ฒ˜๋Ÿผ ์ˆœ์ฐจ์ ์œผ๋กœ max(LIS(1, i))์˜ ๊ณ„์‚ฐ๊ณผ ์‚ฝ์ž…์„ ๋ฒˆ๊ฐˆ์•„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
26+
nums[i]์˜ ํฌ๊ธฐ๊ฐ€ ์ž‘์€ ๊ฒƒ๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ, "max(LIS(1, j))๋ฅผ ๊ณ„์‚ฐํ•˜๊ณ , leaf๋ฅผ ํ•˜๋‚˜ ์‚ฝ์ž…"ํ•˜๋Š” ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.
27+
nums[i]๋ณด๋‹ค ๋” ํฐ ๊ฐ’์€ ์•„์ง ์ธ๋ฑ์Šค ํŠธ๋ฆฌ์— ์‚ฝ์ž…๋˜์ง€ ์•Š์€ ์ƒํƒœ์ด๊ธฐ ๋•Œ๋ฌธ์—, ์ธ๋ฑ์Šค ํŠธ๋ฆฌ์—์„œ ๊ตฌ๊ฐ„ [1, i-1]์˜ ์ตœ๋Œ€๊ฐ’์„ ์กฐํšŒํ•˜๋ฉด nums[j] < num[i]์ธ j์— ๋Œ€ํ•ด์„œ๋งŒ ์ตœ๋Œ€๊ฐ’์„ ์ฐพ๊ฒŒ ๋˜๋ฏ€๋กœ,
28+
(2)๋ฒˆ ๊ณผ์ •์„ O(logn) ์‹œ๊ฐ„์— ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
29+
๋”ฐ๋ผ์„œ ์ „์ฒด ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(nlogn)์ด ๋ฉ๋‹ˆ๋‹ค.
30+
*/
31+
class Solution {
32+
33+
int[] tree;
34+
int L = 1;
35+
36+
public int lengthOfLIS(int[] nums) {
37+
init(nums);
38+
ArrayList<Tuple> tuples = new ArrayList<>();
39+
40+
for (int i = 0; i < nums.length; i++) {
41+
tuples.add(new Tuple(i, nums[i]));
42+
}
43+
44+
Collections.sort(tuples, (a, b) -> {
45+
if (a.val == b.val) {
46+
return b.ref - a.ref; // 2์ˆœ์œ„ : ref ๋‚ด๋ฆผ์ฐจ์ˆœ
47+
} else {
48+
return a.val - b.val; // 1์ˆœ์œ„ : val ์˜ค๋ฆ„์ฐจ์ˆœ
49+
}
50+
});
51+
52+
int ans = 0;
53+
for (int i = 0; i < nums.length; i++) {
54+
int curr = getMax(0, tuples.get(i).ref - 1) + 1;
55+
ans = Math.max(ans, curr);
56+
insert(tuples.get(i).ref, curr);
57+
}
58+
59+
return ans;
60+
}
61+
62+
public class Tuple {
63+
public int ref;
64+
public int val;
65+
66+
public Tuple(int ref, int val) {
67+
this.ref = ref;
68+
this.val = val;
69+
}
70+
}
71+
72+
public void init(int[] nums) {
73+
while (L < nums.length) {
74+
L *= 2;
75+
}
76+
77+
tree = new int[L * 2];
78+
79+
for (int i = 1; i < L * 2; i++) {
80+
tree[i] = 0;
81+
}
82+
}
83+
84+
public void insert(int idx, int v) {
85+
int i = idx + L;
86+
tree[i] = v;
87+
i /= 2;
88+
while (i >= 1) {
89+
tree[i] = Math.max(tree[i * 2], tree[i * 2 + 1]);
90+
i /= 2;
91+
}
92+
}
93+
94+
public int getMax(int l, int r) {
95+
int i = l + L;
96+
int j = r + L;
97+
int ret = 0;
98+
while (i <= j) {
99+
if (i % 2 == 1) {
100+
ret = Math.max(ret, tree[i++]);
101+
}
102+
if (j % 2 == 0) {
103+
ret = Math.max(ret, tree[j--]);
104+
}
105+
i /= 2;
106+
j /= 2;
107+
}
108+
109+
return ret;
110+
}
111+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Time Complexity: O(m * n)
3+
Space Complexity: O(1)
4+
5+
ํ˜„์žฌ ์œ„์น˜๋ฅผ r, c๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋‚˜ํƒ€๋‚ด๊ณ , ์ด r, c๋ฅผ ์ง์ ‘ ์ œ์–ดํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ–‰๋ ฌ์„ ์ˆœํšŒํ•œ๋‹ค.
6+
*/
7+
class Solution {
8+
public List<Integer> spiralOrder(int[][] matrix) {
9+
List<Integer> ans = new ArrayList<>();
10+
11+
int m = matrix.length, n = matrix[0].length;
12+
int r = 0, c = 0;
13+
int[] dr = {0, 1, 0, -1};
14+
int[] dc = {1, 0, -1, 0};
15+
int d = 0;
16+
final int VISITED = -999;
17+
18+
while (ans.size() < m * n) {
19+
ans.add(matrix[r][c]);
20+
matrix[r][c] = VISITED;
21+
22+
int nr = r + dr[d];
23+
int nc = c + dc[d];
24+
if (nr < 0 || nr >= m || nc < 0 || nc >= n || matrix[nr][nc] == VISITED) {
25+
d = (d + 1) % 4;
26+
nr = r + dr[d];
27+
nc = c + dc[d];
28+
}
29+
r = nr;
30+
c = nc;
31+
}
32+
33+
return ans;
34+
}
35+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
Time Complexity: O(n)
3+
Space Complexity: O(n)
4+
5+
์œ ํšจํ•œ ๋ฌธ์ž์—ด์ด๋ผ๋ฉด, ์ธ์ ‘ํ•œ ์—ด๊ณ  ๋‹ซ๋Š” ๊ด„ํ˜ธ ์Œ์„ ํ•˜๋‚˜์”ฉ ์บ”์Šฌ์‹œ์ผฐ์„ ๋•Œ, ๋นˆ ๋ฌธ์ž์—ด๋งŒ ๋‚จ๊ฒŒ ๋œ๋‹ค.
6+
๋งค์น˜๋˜๋Š” ์Œ์ด ์„œ๋กœ ๋–จ์–ด์ ธ์žˆ์„ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ๊ทธ ์•ˆ์˜ ์œ ํšจํ•œ ์Œ๋“ค์„ ๋ฏธ๋ฆฌ ๋ชจ๋‘ ์บ”์Šฌ์‹œํ‚จ ๋’ค์— ํŒ๋‹จํ•ด์•ผ ๋งค์นญ ์—ฌ๋ถ€๋ฅผ ์‰ฝ๊ฒŒ ํŒ๋‹จํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ด ๊ณผ์ •์„ ์Šคํƒ์„ ์ด์šฉํ•ด ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.
7+
*/
8+
class Solution {
9+
public boolean isValid(String s) {
10+
Stack<Character> st = new Stack<>();
11+
12+
if (s.length() % 2 == 1) {
13+
return false;
14+
}
15+
16+
for (char ch : s.toCharArray()) {
17+
if (ch == '(' || ch == '{' || ch == '[') {
18+
st.push(ch);
19+
} else {
20+
if (st.empty())
21+
return false;
22+
if (ch == ')') {
23+
if (st.peek() != '(')
24+
return false;
25+
} else if (ch == '}') {
26+
if (st.peek() != '{')
27+
return false;
28+
} else {
29+
if (st.peek() != '[')
30+
return false;
31+
}
32+
st.pop();
33+
}
34+
}
35+
return st.empty();
36+
}
37+
}

0 commit comments

Comments
ย (0)