Skip to content

Commit

Permalink
Merge pull request #907 from forest000014/main
Browse files Browse the repository at this point in the history
[forest000014] Week 6
  • Loading branch information
forest000014 authored Jan 18, 2025
2 parents 22fea49 + 0b38772 commit 015ee61
Show file tree
Hide file tree
Showing 5 changed files with 376 additions and 0 deletions.
115 changes: 115 additions & 0 deletions container-with-most-water/forest000014.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
solution 1. brute force
Time Complexity: O(n^2)
Space Complexity: O(1)
๋ชจ๋“  ์„ ๋ถ„ i์— ๋Œ€ํ•ด, ๊ฐ€์žฅ ๋งŽ์€ ๋ฌผ์„ ๋‹ด์„ ์ˆ˜ ์žˆ๋Š” ๋ฐ˜๋Œ€์ชฝ ์„ ๋ถ„ j๋ฅผ ์ฐพ๋Š”๋‹ค.
solution 2. PQ
Time Complexity: O(nlogn)
- ์ •๋ ฌ : O(nlogn)
- i๋ฒˆ์งธ ์„ ๋ถ„์— ๋Œ€ํ•ด (์ž์‹ ๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์€) ๊ฐ€์žฅ ๋ฉ€๋ฆฌ์žˆ๋Š” ์„ ๋ถ„ ํƒ์ƒ‰(O(logn)) * n = O(nlogn)
Space Complexity: O(n)
(์‚ฌ๊ณ ์˜ ํ๋ฆ„์„ ์ ๊ธฐ ์œ„ํ•ด ํŽธ์˜์ƒ ๋ฐ˜๋ง๋กœ ์ ์—ˆ์Šต๋‹ˆ๋‹ค... ^^)
brute force ์—์„œ ๋ถˆํ•„์š”ํ•œ ํƒ์ƒ‰์„ ์ค„์—ฌ๋ณด์ž.
์ผ๋‹จ ์–ด๋–ค ์„ ๋ถ„ i๊ฐ€ ์ฃผ์–ด์กŒ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์ž. i๋ฅผ ํ•œ์ชฝ ๋ฒฝ์œผ๋กœ ํ•ด์„œ ๊ฐ€์žฅ ๋งŽ์€ ๋ฌผ์„ ๋‹ด๋Š” ๋ฐฉ๋ฒ•์„ ์ฐพ์œผ๋ ค๋ฉด, ๋ฐ˜๋“œ์‹œ ๋‚˜๋จธ์ง€ ๋ชจ๋“  ์„ ๋ถ„์„ ํƒ์ƒ‰ํ•ด์•ผ๋งŒ ํ• ๊นŒ?
(1) ์ž์‹ ๋ณด๋‹ค ์ž‘์€ ์„ ๋ถ„์€ ํƒ์ƒ‰ํ•˜์ง€ ์•Š๋Š”๋‹ค.
์ž์‹ ๋ณด๋‹ค ํฐ ์„ ๋ถ„๋งŒ์„ ํƒ์ƒ‰ํ•ด๋„ ์ถฉ๋ถ„ํ•˜๋‹ค.
์™œ๋ƒํ•˜๋ฉด, ์„ค๋ น ์ž์‹ ๋ณด๋‹ค ๋” ์ž‘์€ ์„ ๋ถ„ ์ค‘์— ์ •๋‹ต์ด ์žˆ์—ˆ๋‹ค๊ณ  ํ•˜๋”๋ผ๋„, ๊ทธ ์„ ๋ถ„์„ ๊ธฐ์ค€์œผ๋กœ ํƒ์ƒ‰ํ•  ๋•Œ ์ •๋‹ต์— ํ•ด๋‹นํ•˜๋Š” ์Œ์„ ํ™•์ธํ•˜๊ฒŒ ๋  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
(2) ์ž์‹ ๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์€ ์„ ๋ถ„๋งŒ์„ ํƒ์ƒ‰ ๋Œ€์ƒ์œผ๋กœ ์‚ผ๋Š”๋‹ค๋ฉด, ๊ฐ€์žฅ ๋ฉ€๋ฆฌ์žˆ๋Š” ์„ ๋ถ„๋งŒ ํ™•์ธํ•˜๋ฉด ๋œ๋‹ค.
ํƒ์ƒ‰ ๋Œ€์ƒ์ด ์ž์‹ ๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์€ ์„ ๋ถ„๋“ค์ด๋ผ๋ฉด, ์–ด์ฐจํ”ผ ๋‹ด์„ ์ˆ˜ ์žˆ๋Š” ๋ฌผ์˜ ๋†’์ด๋Š” ์ž์‹ ์˜ ๋†’์ด๋กœ ์ผ์ •ํ•˜๋‹ค.
๋”ฐ๋ผ์„œ, ๋ฉ€๋ฆฌ์žˆ๋Š” ์„ ๋ถ„์ผ์ˆ˜๋ก ๋” ๋งŽ์€ ๋ฌผ์„ ๋‹ด๊ฒŒ ๋œ๋‹ค.
์ฆ‰, "์ž์‹ ๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด์„œ", "๊ฐ€์žฅ ๋ฉ€๋ฆฌ ์žˆ๋Š”(์˜ค๋ฅธ์ชฝ์ด๋“  ์™ผ์ชฝ์ด๋“ )" ์„ ๋ถ„๋งŒ์„ ํ›„๋ณด๋กœ ์‚ผ์•„ ํ™•์ธํ•˜๋ฉด ์ถฉ๋ถ„ํ•˜๋‹ค.
(๊ทธ ์™ธ์˜ ๋‚˜๋จธ์ง€, ์ฆ‰ ์ž์‹ ๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด์„œ, ๊ฐ€์žฅ ๋ฉ€๋ฆฌ์žˆ์ง€ ์•Š์€ ๋‚˜๋จธ์ง€ ์„ ๋ถ„๋“ค์€, ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ํƒ์ƒ‰ํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ๋‹ค.)
์ •๋ฆฌํ•˜์ž๋ฉด, ์ฃผ์–ด์ง„ ์„ ๋ถ„ i์— ๋Œ€ํ•ด์„œ, ๋‹จ 2๋ฒˆ(์˜ค๋ฅธ์ชฝ, ์™ผ์ชฝ)์˜ ํƒ์ƒ‰๋งŒ ํ•˜๋ฉด ์ถฉ๋ถ„ํ•˜๋‹ค.
(3) ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ๊ณผ 2๊ฐœ์˜ PQ๋ฅผ ์•„๋ž˜์ฒ˜๋Ÿผ ํ™œ์šฉํ•˜๋ฉด, ์œ„ ํƒ์ƒ‰์„ O(logn) ์‹œ๊ฐ„์— ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.
PQ๋Š” ๊ฐ๊ฐ x ์ขŒํ‘œ ๊ธฐ์ค€ max heap(๊ฐ€์žฅ ์˜ค๋ฅธ์ชฝ์˜ ์„ ๋ถ„์„ ์ฐพ๊ธฐ ์œ„ํ•จ), x ์ขŒํ‘œ ๊ธฐ์ค€ min heap(๊ฐ€์žฅ ์™ผ์ชฝ์˜ ์„ ๋ถ„์„ ์ฐพ๊ธฐ ์œ„ํ•จ)์„ ์‚ฌ์šฉํ•œ๋‹ค.
์„ ๋ถ„๋“ค์„ ๊ธธ์ด ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด๋†“๊ณ , ํ•˜๋‚˜์”ฉ ์ˆœํšŒํ•˜๋ฉด์„œ (say, i๋ฒˆ์งธ ์„ ๋ถ„), ์•„๋ž˜ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•œ๋‹ค.
- ์ž์‹ ๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด์„œ ๊ฐ€์žฅ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋ฉ€๋ฆฌ ์žˆ๋Š” ์„ ๋ถ„์˜ ์œ„์น˜๋ฅผ ์ฐพ์•„์„œ(= ํ˜„์žฌ PQ(max heap)์˜ root), ์ตœ๋Œ€ ๋ฌผ์˜ ์–‘์„ ๊ณ„์‚ฐํ•œ๋‹ค.
- ์ž์‹ ๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด์„œ ๊ฐ€์žฅ ์™ผ์ชฝ์œผ๋กœ ๋ฉ€๋ฆฌ ์žˆ๋Š” ์„ ๋ถ„์˜ ์œ„์น˜๋ฅผ ์ฐพ์•„์„œ(= ํ˜„์žฌ PQ(min heap)์˜ root), ์ตœ๋Œ€ ๋ฌผ์˜ ์–‘์„ ๊ณ„์‚ฐํ•œ๋‹ค.
- i๋ฒˆ์งธ ์„ ๋ถ„์„ PQ 2๊ฐœ์— ๊ฐ๊ฐ ๋„ฃ๋Š”๋‹ค.
solution 3. two pointers
(AlgoDale ํ’€์ด๋ฅผ ์ฐธ๊ณ ํ•จ)
Time Complexity: O(n)
Space Complexity: O(1)
2 ํฌ์ธํ„ฐ๋ฅผ ํ™œ์šฉํ•˜๋ฉด, PQ๋„ ์—†์ด ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ O(n)์œผ๋กœ ์ค„์ผ ์ˆ˜ ์žˆ์—ˆ๋‹ค.
๋‹จ์ˆœํžˆ "ํฐ ์ชฝ์„ ์ค„์ด๊ธฐ๋ณด๋‹ค๋Š”, ์ž‘์€ ์ชฝ์„ ์ค„์ด๋Š” ๊ฒŒ ์œ ๋ฆฌํ•˜๊ฒ ์ง€" ์ •๋„์˜ greedyํ•œ ๋…ผ๋ฆฌ๋Š” ์ถฉ๋ถ„ํ•˜์ง€ ์•Š์€ ๊ฒƒ ๊ฐ™๊ณ , ๋” ๋ช…ํ™•ํ•œ ๊ทผ๊ฑฐ๊ฐ€ ์žˆ์„ ๊ฒƒ ๊ฐ™์€๋ฐ ์‹œ๊ฐ„ ๊ด€๊ณ„์ƒ ๊ณ ๋ฏผํ•ด๋ณด์ง€๋Š” ๋ชปํ–ˆ๋‹ค.
To-Do : ํ’€์ด๊ฐ€ ๋Œ€๊ฐ• ์ดํ•ด๋Š” ๋˜์—ˆ์ง€๋งŒ, ์ด๊ฒŒ ์™œ ๋˜๋Š”์ง€, ์—„๋ฐ€ํ•˜๊ฒŒ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด PQ๋ฅผ ์‚ฌ์šฉํ–ˆ๋˜ ๋…ผ๋ฆฌ๋ฅผ ์ข€ ๋” ๋ฐœ์ „์‹œ์ผœ๋ณผ ํ•„์š”๊ฐ€ ์žˆ๋‹ค.
*/
class Solution {
public int maxArea(int[] height) {
int i = 0, j = height.length - 1;

int ans = 0;
while (i < j) {
int area = (j - i) * Math.min(height[i], height[j]);
if (area > ans) {
ans = area;
}

if (height[i] <= height[j]) {
i++;
} else {
j--;
}
}

return ans;
}

////// ์•„๋ž˜๋Š” solution 2
private static class Tuple {
private int x;
private int h;

Tuple(int x, int h) {
this.x = x;
this.h = h;
}
}

public int maxArea2(int[] height) {
List<Tuple> tuples = IntStream.range(0, height.length)
.mapToObj(i -> new Tuple(i, height[i]))
.collect(Collectors.toList());
Collections.sort(tuples, (a, b) -> b.h - a.h);

PriorityQueue<Tuple> minPq = new PriorityQueue<>((a, b) -> a.x - b.x);
PriorityQueue<Tuple> maxPq = new PriorityQueue<>((a, b) -> b.x - a.x);

int ans = 0;
for (int i = 0; i < height.length; i++) {
minPq.add(tuples.get(i));
maxPq.add(tuples.get(i));

Tuple curr = tuples.get(i);

Tuple left = minPq.peek();
int leftArea = (curr.x - left.x) * curr.h;
if (leftArea > ans) {
ans = leftArea;
}

Tuple right = maxPq.peek();
int rightArea = (right.x - curr.x) * curr.h;
if (rightArea > ans) {
ans = rightArea;
}
}

return ans;
}
}
78 changes: 78 additions & 0 deletions design-add-and-search-words-data-structure/forest000014.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
Time Complexity:
- add: O(w)
- search: O(26^2 * w) = O(w)
Space Complexity: O(w)
Trie๋ฅผ ํ™œ์šฉํ•˜๋˜, '.'์˜ ํƒ์ƒ‰์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ์—๋Š” for๋ฌธ์„ ์‚ฌ์šฉํ•œ๋‹ค.
*/
class WordDictionary {
class Node {
public char ch;
public boolean ends;
public Map<Character, Node> children;

Node() {
this.children = new HashMap<>();
}

Node(char ch) {
this.ch = ch;
this.children = new HashMap<>();
}
}

Node root;

public WordDictionary() {
this.root = new Node();
}

public void addWord(String word) {
Node curr = this.root;

for (int i = 0; i < word.length(); i++) {
char ch = word.charAt(i);
if (!curr.children.containsKey(ch)) {
curr.children.put(ch, new Node(ch));
}

curr = curr.children.get(ch);
}

curr.ends = true;
}

public boolean search(String word) {
return searchChar(word, 0, this.root);
}

private boolean searchChar(String word, int idx, Node curr) {
if (curr == null) {
return false;
} else if (idx == word.length()) {
return curr.ends;
}

char ch = word.charAt(idx);

if (ch == '.') {
for (Character key : curr.children.keySet()) {
if (searchChar(word, idx + 1, curr.children.get(key))) {
return true;
}
}
return false;
} else {
return searchChar(word, idx + 1, curr.children.get(ch));
}
}
}

/**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary obj = new WordDictionary();
* obj.addWord(word);
* boolean param_2 = obj.search(word);
*/
111 changes: 111 additions & 0 deletions longest-increasing-subsequence/forest000014.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
# Time Complexity: O(nlogn)
- tuples ArrayList ์ •๋ ฌ: O(nlogn)
- ์ธ๋ฑ์Šค ํŠธ๋ฆฌ ์‚ฝ์ž…: O(logn) * n times = O(nlogn)
- ์ธ๋ฑ์Šค ํŠธ๋ฆฌ ์กฐํšŒ: O(logn) * n times = O(nlogn)
# Space Complexity: O(nlogn)
- tuples ArrayList: O(n)
- ์ธ๋ฑ์Šค ํŠธ๋ฆฌ: O(nlogn)
# solution
์ด ๋ฌธ์ œ๋Š” DP๋กœ ์ ‘๊ทผํ–ˆ์Šต๋‹ˆ๋‹ค.
LIS(1, x)๋ฅผ ๋ฒ”์œ„ [1, x] ๋‚ด์˜ LIS(๋‹จ, nums[x]๋ฅผ ๋ฐ˜๋“œ์‹œ ํฌํ•จ)์˜ ๊ธธ์ด๋ผ๊ณ  ์ •์˜ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. - (1)
1 <= j < i ์ธ ๋ชจ๋“  j์— ํ•œ LIS(1, j)๋ฅผ ์•Œ๊ณ  ์žˆ๋‹ค๋ฉด, LIS(1, i)๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
LIS(1, i) = max(LIS(1, j)) (๋‹จ, j๋Š” 1 <= j < i ์ด๊ณ , nums[j] < nums[i]) - (2)
max(LIS(1, j))๋ฅผ ๊ตฌํ•  ๋•Œ, ๋ชจ๋“  j์— ๋Œ€ํ•ด ํƒ์ƒ‰ํ•œ๋‹ค๋ฉด, ์ „์ฒด ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(n^2)๊ฐ€ ๋˜๊ธฐ ๋•Œ๋ฌธ์—, ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ์ค„์ผ ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
์ด ํƒ์ƒ‰ ๊ณผ์ •์„ ์ค„์ด๊ธฐ ์œ„ํ•ด, ์•„๋ž˜์˜ ์‚ฌ๊ณ  ๊ณผ์ •์„ ๊ฑฐ์ณค์Šต๋‹ˆ๋‹ค.
์–ด๋–ค ๋ฒ”์œ„ ๋‚ด์˜ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ O(logn) ์‹œ๊ฐ„์— ๊ตฌํ•˜๊ธฐ ์œ„ํ•œ ์ž๋ฃŒ๊ตฌ์กฐ๋กœ, ์ธ๋ฑ์Šค ํŠธ๋ฆฌ(ํ˜น์€ ์„ธ๊ทธ๋จผํŠธ ํŠธ๋ฆฌ)๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
(์ด ์ธ๋ฑ์Šค ํŠธ๋ฆฌ์˜ x๋ฒˆ์งธ leaf ๋…ธ๋“œ์—๋Š” LIS(1, x) ๊ฐ’์„ ์ €์žฅํ•˜๊ณ , internal ๋…ธ๋“œ์—๋Š” ์ž์‹ ๋…ธ๋“œ๋“ค ์ค‘ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.)
๋‹ค๋งŒ, ๋‹จ์ˆœํžˆ ํ•ด๋‹น ๋ฒ”์œ„ ๋‚ด์˜ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ๊ตฌํ•˜๋Š” ๊ฒƒ๋งŒ์œผ๋กœ๋Š” ๋ถ€์กฑํ•˜๊ณ , nums[j] < nums[i]์ธ j๋งŒ์„ ํ›„๋ณด๋กœ ์‚ผ์•„์•ผ ํ•  ํ…๋ฐ์š”,
๊ทธ๋Ÿฌ๊ธฐ ์œ„ํ•ด์„œ, ์ธ๋ฑ์Šค ํŠธ๋ฆฌ์— ๋ชจ๋“  leaf ๋…ธ๋“œ๋ฅผ ๋ฏธ๋ฆฌ ์‚ฝ์ž…ํ•ด๋‘๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์•„๋ž˜์ฒ˜๋Ÿผ ์ˆœ์ฐจ์ ์œผ๋กœ max(LIS(1, i))์˜ ๊ณ„์‚ฐ๊ณผ ์‚ฝ์ž…์„ ๋ฒˆ๊ฐˆ์•„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
nums[i]์˜ ํฌ๊ธฐ๊ฐ€ ์ž‘์€ ๊ฒƒ๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ, "max(LIS(1, j))๋ฅผ ๊ณ„์‚ฐํ•˜๊ณ , leaf๋ฅผ ํ•˜๋‚˜ ์‚ฝ์ž…"ํ•˜๋Š” ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.
nums[i]๋ณด๋‹ค ๋” ํฐ ๊ฐ’์€ ์•„์ง ์ธ๋ฑ์Šค ํŠธ๋ฆฌ์— ์‚ฝ์ž…๋˜์ง€ ์•Š์€ ์ƒํƒœ์ด๊ธฐ ๋•Œ๋ฌธ์—, ์ธ๋ฑ์Šค ํŠธ๋ฆฌ์—์„œ ๊ตฌ๊ฐ„ [1, i-1]์˜ ์ตœ๋Œ€๊ฐ’์„ ์กฐํšŒํ•˜๋ฉด nums[j] < num[i]์ธ j์— ๋Œ€ํ•ด์„œ๋งŒ ์ตœ๋Œ€๊ฐ’์„ ์ฐพ๊ฒŒ ๋˜๋ฏ€๋กœ,
(2)๋ฒˆ ๊ณผ์ •์„ O(logn) ์‹œ๊ฐ„์— ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
๋”ฐ๋ผ์„œ ์ „์ฒด ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(nlogn)์ด ๋ฉ๋‹ˆ๋‹ค.
*/
class Solution {

int[] tree;
int L = 1;

public int lengthOfLIS(int[] nums) {
init(nums);
ArrayList<Tuple> tuples = new ArrayList<>();

for (int i = 0; i < nums.length; i++) {
tuples.add(new Tuple(i, nums[i]));
}

Collections.sort(tuples, (a, b) -> {
if (a.val == b.val) {
return b.ref - a.ref; // 2์ˆœ์œ„ : ref ๋‚ด๋ฆผ์ฐจ์ˆœ
} else {
return a.val - b.val; // 1์ˆœ์œ„ : val ์˜ค๋ฆ„์ฐจ์ˆœ
}
});

int ans = 0;
for (int i = 0; i < nums.length; i++) {
int curr = getMax(0, tuples.get(i).ref - 1) + 1;
ans = Math.max(ans, curr);
insert(tuples.get(i).ref, curr);
}

return ans;
}

public class Tuple {
public int ref;
public int val;

public Tuple(int ref, int val) {
this.ref = ref;
this.val = val;
}
}

public void init(int[] nums) {
while (L < nums.length) {
L *= 2;
}

tree = new int[L * 2];

for (int i = 1; i < L * 2; i++) {
tree[i] = 0;
}
}

public void insert(int idx, int v) {
int i = idx + L;
tree[i] = v;
i /= 2;
while (i >= 1) {
tree[i] = Math.max(tree[i * 2], tree[i * 2 + 1]);
i /= 2;
}
}

public int getMax(int l, int r) {
int i = l + L;
int j = r + L;
int ret = 0;
while (i <= j) {
if (i % 2 == 1) {
ret = Math.max(ret, tree[i++]);
}
if (j % 2 == 0) {
ret = Math.max(ret, tree[j--]);
}
i /= 2;
j /= 2;
}

return ret;
}
}
35 changes: 35 additions & 0 deletions spiral-matrix/forest000014.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Time Complexity: O(m * n)
Space Complexity: O(1)
ํ˜„์žฌ ์œ„์น˜๋ฅผ r, c๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋‚˜ํƒ€๋‚ด๊ณ , ์ด r, c๋ฅผ ์ง์ ‘ ์ œ์–ดํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ–‰๋ ฌ์„ ์ˆœํšŒํ•œ๋‹ค.
*/
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> ans = new ArrayList<>();

int m = matrix.length, n = matrix[0].length;
int r = 0, c = 0;
int[] dr = {0, 1, 0, -1};
int[] dc = {1, 0, -1, 0};
int d = 0;
final int VISITED = -999;

while (ans.size() < m * n) {
ans.add(matrix[r][c]);
matrix[r][c] = VISITED;

int nr = r + dr[d];
int nc = c + dc[d];
if (nr < 0 || nr >= m || nc < 0 || nc >= n || matrix[nr][nc] == VISITED) {
d = (d + 1) % 4;
nr = r + dr[d];
nc = c + dc[d];
}
r = nr;
c = nc;
}

return ans;
}
}
37 changes: 37 additions & 0 deletions valid-parentheses/forest000014.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
Time Complexity: O(n)
Space Complexity: O(n)
์œ ํšจํ•œ ๋ฌธ์ž์—ด์ด๋ผ๋ฉด, ์ธ์ ‘ํ•œ ์—ด๊ณ  ๋‹ซ๋Š” ๊ด„ํ˜ธ ์Œ์„ ํ•˜๋‚˜์”ฉ ์บ”์Šฌ์‹œ์ผฐ์„ ๋•Œ, ๋นˆ ๋ฌธ์ž์—ด๋งŒ ๋‚จ๊ฒŒ ๋œ๋‹ค.
๋งค์น˜๋˜๋Š” ์Œ์ด ์„œ๋กœ ๋–จ์–ด์ ธ์žˆ์„ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ๊ทธ ์•ˆ์˜ ์œ ํšจํ•œ ์Œ๋“ค์„ ๋ฏธ๋ฆฌ ๋ชจ๋‘ ์บ”์Šฌ์‹œํ‚จ ๋’ค์— ํŒ๋‹จํ•ด์•ผ ๋งค์นญ ์—ฌ๋ถ€๋ฅผ ์‰ฝ๊ฒŒ ํŒ๋‹จํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ด ๊ณผ์ •์„ ์Šคํƒ์„ ์ด์šฉํ•ด ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.
*/
class Solution {
public boolean isValid(String s) {
Stack<Character> st = new Stack<>();

if (s.length() % 2 == 1) {
return false;
}

for (char ch : s.toCharArray()) {
if (ch == '(' || ch == '{' || ch == '[') {
st.push(ch);
} else {
if (st.empty())
return false;
if (ch == ')') {
if (st.peek() != '(')
return false;
} else if (ch == '}') {
if (st.peek() != '{')
return false;
} else {
if (st.peek() != '[')
return false;
}
st.pop();
}
}
return st.empty();
}
}

0 comments on commit 015ee61

Please sign in to comment.