Skip to content

Commit 7e3a732

Browse files
committed
[20200711] Solve problems
1 parent 0e09c24 commit 7e3a732

File tree

5 files changed

+210
-0
lines changed

5 files changed

+210
-0
lines changed

โ€ŽREADME.md

+2
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@
210210
| [1014](https://leetcode.com/problems/best-sightseeing-pair/) | Best Sightseeing Pair | medium | [python](๐Ÿค”medium/1014.Best-Sightseeing-Pair/best_sightseeing_pair.py) | |
211211
| [1019](https://leetcode.com/problems/next-greater-node-in-linked-list/) | Next Greater Node In Linked List | medium | [python](๐Ÿค”medium/1019.Next-Greater-Node-In-Linked-List/next_greater_node_in_linked_list.py) | |
212212
| [1025](https://leetcode.com/problems/divisor-game/) | Divisor Game | easy | [python](๐Ÿ™‚easy/1025.Divisor-Game/divisor_game.py) | |
213+
| [1027](https://leetcode.com/problems/longest-arithmetic-sequence/) | Longest Arithmetic Sequence | meidum | [python](๐Ÿค”medium/1027.Longest-Arithmetic0Sequence/longest_arithmetic_sequence.py) | |
213214
| [1114](https://leetcode.com/problems/print-in-order/) | Print in Order | easy | [python](๐Ÿ™‚easy/1114.Print-in-Order/print_in_order.py) | |
214215
| [1122](https://leetcode.com/problems/relative-sort-array/) | Relative Sort Array | easy | [python](๐Ÿ™‚easy/1122.Relative-Sort-Array/relative_sort_array.py) | |
215216
| [1128](https://leetcode.com/problems/number-of-equivalent-domino-pairs/) | Number of Equivalent Domino Pairs | easy | [python](๐Ÿ™‚easy/1128.Number-of-Equivalent-Domino-Pairs/number_of_equivalent_domino_pairs.py) | |
@@ -229,3 +230,4 @@
229230
| [Hamming Distance](https://leetcode.com/explore/challenge/card/july-leetcoding-challenge/544/week-1-july-1st-july-7th/3381/) | [python](challenge/2020/july/Hamming_Distance.py) | [solutions](challenge/2020/july/Hamming_Distance.md)|
230231
| [Plus One](https://leetcode.com/explore/challenge/card/july-leetcoding-challenge/544/week-1-july-1st-july-7th/3382/) | [python](challenge/2020/july/Plus_One.py) | [solutions](challenge/2020/july/Plus_One.md)|
231232
| [Island Perimeter](https://leetcode.com/explore/challenge/card/july-leetcoding-challenge/544/week-1-july-1st-july-7th/3383/) | [python](challenge/2020/july/Island_Perimeter.py) | [solutions](challenge/2020/july/Island_Perimeter.md)|
233+
| [Flatten a Multilevel Doubly Linked List](https://leetcode.com/explore/challenge/card/july-leetcoding-challenge/545/week-2-july-8th-july-14th/3386/) | [python](challenge/2020/july/Flatten_a_Multilevel_Doubly_Linked_List.py) | [solutions](challenge/2020/july/Flatten_a_Multilevel_Doubly_Linked_List.md)|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
## ๋ฌธ์ œ
2+
3+
doubly linked list ๋ฅผ flatten ํ•˜๊ฒŒ ๋งŒ๋“œ๋Š” ๋ฌธ์ œ๋‹ค.
4+
๋‹ค๋งŒ ์ผ๋งŒ linked list ๋ž‘ ๋‹ค๋ฅด๊ฒŒ child ๊ฐ€ ์žˆ๊ณ , ์ด child ์˜ ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋” ๋†’์•„์•ผ ํ•œ๋‹ค(?) ๋Š” ๊ฒƒ์ด๋‹ค.
5+
6+
### ์†”๋ฃจ์…˜
7+
child ๊ฐ€ ๋จผ์ € ์„ ํ–‰ ๋˜์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— depth ๋ฅผ ๋‘๊ณ , queue ๋ฅผ heap ์œผ๋กœ ์จ์„œ(child ๋ฅผ queue ์—์„œ ๋จผ์ € ๊บผ๋‚ด๋ ค๊ณ ) bfs ๋กœ ํ’€์—ˆ๋‹ค.
8+
9+
์ฝ”๋“œ ์ฐธ๊ณ 
10+
```python3
11+
class Solution:
12+
def flatten(self, head: 'Node') -> 'Node':
13+
14+
if head is None:
15+
return None
16+
17+
heap = []
18+
19+
item = (0, head)
20+
heappush(heap, item)
21+
22+
dummy = Node(0, None, None, None)
23+
result = dummy
24+
25+
while heap:
26+
depth, node = heappop(heap)
27+
28+
dummy.next = node
29+
node.prev = dummy
30+
31+
if node.child is not None:
32+
heappush(heap, (depth - 1, node.child))
33+
if node.next is not None:
34+
heappush(heap, (depth, node.next))
35+
36+
dummy = dummy.next
37+
dummy.next = None
38+
dummy.child = None
39+
40+
ret = result.next
41+
ret.prev = None
42+
return ret
43+
```
44+
45+
๊ทผ๋ฐ ๋ง‰์ƒ ํ’€๊ณ ๋‚˜๋‹ˆ๊นŒ stack ์œผ๋กœ ํ•  ์ˆ˜ ์žˆ๋”๋ผ. next ๋ฅผ ๋จผ์ € ๋„ฃ๊ณ  child ๋ฅผ ๋‚˜์ค‘์— ๋„ฃ๋Š” ์‹์œผ๋กœ ํ•˜๋ฉด LIFO ์ด๊ธฐ ๋•Œ๋ฌธ์— child ๊ฐ’๋งŒ ๋จผ์ € ํ™•์ธ ๊ฐ€๋Šฅํ•˜๋‹ค(!)
46+
47+
### ์ฃผ์ €๋ฆฌ
48+
๊ฒฐ๊ณผ ๊ฐ’์ด doubly linked list ๊ฐ€ ์•„๋‹ˆ๋ž˜์„œ ํ•œ์ฐธ ํ—ค๋งธ๋Š”๋ฐ... ์•Œ๊ณ ๋ณด๋‹ˆ dummy ๋กœ ์ค€ ์ฒซ๋ฒˆ์งธ dummy head ๊ฐ€ ๊ฒฐ๊ณผ ๊ฐ’์— ํฌํ•จ ๋˜์—ˆ์—ˆ๊ธฐ ๋•Œ๋ฌธ์ด์—ˆ๋‹ค.
49+
50+
๊ทธ๋ฆฌ๊ณ  ํ•œ๋ฒˆ ํ‹€๋ ธ๋Š”๋ฐ (ใ… ใ… ) None ์ด input ์œผ๋กœ ๋“ค์–ด์˜ค๋Š” ๊ฒƒ์„ ํ™•์ธ ๋ชปํ–ˆ๋‹ค. ์ œ๋ฐœ... ๊ฒ€ํ† ๋ฅผ ํ™•์‹คํžˆ ํ•˜์ž!!
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# 2020.07.11
2+
# Flatten a Multilevel Doubly Linked List
3+
4+
from heapq import heappop, heappush
5+
6+
7+
# Definition for a Node.
8+
class Node:
9+
def __init__(self, val, prev=None, next=None, child=None):
10+
self.val = val
11+
self.prev = prev
12+
self.next = next
13+
self.child = child
14+
15+
16+
class Solution:
17+
def flatten(self, head: 'Node') -> 'Node':
18+
19+
if head is None:
20+
return None
21+
22+
heap = []
23+
24+
item = (0, head)
25+
heappush(heap, item)
26+
27+
dummy = Node(0, None, None, None)
28+
result = dummy
29+
30+
while heap:
31+
depth, node = heappop(heap)
32+
33+
dummy.next = node
34+
node.prev = dummy
35+
36+
if node.child is not None:
37+
heappush(heap, (depth - 1, node.child))
38+
if node.next is not None:
39+
heappush(heap, (depth, node.next))
40+
41+
dummy = dummy.next
42+
dummy.next = None
43+
dummy.child = None
44+
45+
ret = result.next
46+
ret.prev = None
47+
return ret
48+
49+
50+
if __name__ == '__main__':
51+
node = Node(1)
52+
node2 = Node(2)
53+
54+
node.next = node2
55+
node2.prev = node
56+
57+
node.child = Node(3)
58+
59+
sol = Solution()
60+
result = sol.flatten(node)
61+
62+
while result is not None:
63+
print(result.val)
64+
result = result.next
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# 1027. Longest Arithmetic Sequence
2+
# https://leetcode.com/problems/longest-arithmetic-sequence/
3+
4+
from typing import List
5+
from collections import defaultdict
6+
7+
8+
class Solution:
9+
def longestArithSeqLength(self, A: List[int]) -> int:
10+
dp = defaultdict(int)
11+
result = 0
12+
13+
for i in range(len(A) - 1):
14+
for j in range(i + 1, len(A)):
15+
diff = A[i] - A[j]
16+
if (i, diff) in dp:
17+
dp[(j, diff)] = dp[(i, diff)] + 1
18+
else:
19+
dp[(j, diff)] = 2
20+
result = max(result, dp[(j, diff)])
21+
22+
return result
23+
24+
25+
if __name__ == '__main__':
26+
sol = Solution()
27+
28+
assert sol.longestArithSeqLength([20, 1, 15, 3, 10, 5, 8]) == 4
29+
assert sol.longestArithSeqLength([9, 4, 7, 2, 10]) == 3
30+
assert sol.longestArithSeqLength([3, 6, 9, 12]) == 4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
## ๋ฌธ์ œ
2+
๋ฐฐ์—ด์—์„œ ๊ฐ€์žฅ ๊ธด arithmetic sequence ๋ฅผ ์ฐพ๋Š” ๋ฌธ์ œ๋‹ค.
3+
arithmetic sequence ๋ž€ ๋ฐฐ์—ด A[i], A[i+1], A[i+2] ... ๊ฐ€ ์žˆ์„ ๋•Œ ๊ฐ ์›์†Œ์˜ ๊ฐ’์˜ ์ฐจ์ด๊ฐ€ ๋˜‘๊ฐ™์€ ์‹œํ€€์Šค ์˜ˆ๋ฅผ ๋“ค๋ฉด
4+
5+
- `[3, 6, 9, 12, 13]` ์ด๋ž€ ๋ฐฐ์—ด์ด ์žˆ์„ ๋•Œ `[3, 6, 9, 12]` ๋Š” +3 ์˜ ์ฐจ์ด๋กœ ์ด๋ฃจ์–ด์ง„ ๊ฐ€์žฅ ๊ธด ๋ฐฐ์—ด์ด ๋œ๋‹ค.
6+
- `[9, 4, 7, 2, 10]` ์ผ ๊ฒฝ์šฐ์—” [`4, 7, 10]`์ด +3 ์œผ๋กœ ๊ฐ€์žฅ ๊ธธ๊ฒŒ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ๋ฐฐ์—ด์ด ๋œ๋‹ค.
7+
8+
## ์†”๋ฃจ์…˜
9+
์›์†Œ๋“ค์˜ ์ฐจ์ด๋ฅผ ์ด์šฉํ•œ dp ๋กœ ํ’€ ์ˆ˜ ์žˆ๋‹ค.
10+
11+
๊ทธ๋ฆผ์€ ์•„๋ž˜๋ฅผ ์ฐธ๊ณ ํ•˜๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค.
12+
[์ฐธ๊ณ ](https://medium.com/algorithm-and-datastructure/longest-arithmetic-sequence-567cc4eea949)
13+
14+
`[9, 4, 7, 2, 10]` ์˜ ๊ฒฝ์šฐ ๊ฐ€์žฅ ๊ธด ๋ฐฐ์—ด์€ `[4, 7, 10]` ์ด๋‹ค.
15+
์ด๋ฅผ ๋‚˜๋ˆ ์„œ ์ƒ๊ฐํ•ด๋ณด๋ฉด ๋ฐฐ์—ด์˜ ๊ฐ’์€ `[4, 7]` ๊ณผ `[7, 10]` ์˜ ์กฐํ•ฉ์ด๋‹ค. ์ฆ‰ ์–ด๋–ค arithmetic sequence ๋ฅผ ์ด๋ฃจ๋ ค๋ฉด
16+
์ž„์˜์˜ ๋ฐฐ์—ด A[i,j] ์™€ ๋ฐฐ์—ด A[j,k] ์˜ ์กฐํ•ฉ๋“ค๋กœ ์ด๋ฃจ์–ด์ ธ์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ๊ฐ๊ฐ์˜ ๋ฐฐ์—ด์—์„œ A[j] - A[i]์™€ A[k] - A[j]๋Š” ๊ฐ™์•„์•ผ ํ•œ๋‹ค.
17+
18+
19+
dp[(i, diff)] ๋Š” i ๋ฒˆ์งธ ์ธ๋ฑ์Šค์™€ i ๋ฒˆ์งธ ์ธ๋ฑ์Šค์—์„œ i+1 ~ k ์ธ๋ฑ์Šค์™€์˜ ๋น„๊ต๊ฐ’ diff ์˜ count ๋ฅผ ์˜๋ฏธํ•œ๋‹ค.
20+
21+
์˜ˆ๋กœ ๋ณด๋ฉด dp[(1, 3)] ์€ A[1] ์ธ 4์—์„œ +3 ์ด ๋‚˜์˜จ ํšŸ์ˆ˜์ด๋‹ค. (๊ทธ๋ฆฌ๊ณ  ๊ทธ ๊ฐ’์€ A[2] ์ธ 7์—์„œ count ๋˜์—ˆ์„ ๊ฒƒ์ด๋‹ค.)
22+
๊ทธ๋ฆฌ๊ณ  ๋‹ค์Œ ๋ฃจํ”„์—์„œ dp[(2, 3)] ์ด๋ž€ ๊ฐ’์ด ๋‚˜์˜ฌ ํ…๋ฐ, ์—ฌ๊ธฐ์„œ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ A[2] ์™€ A[4] ์˜ ์ฐจ์ด +3 ์ด ๋‚˜์˜จ count ๋‹ค.
23+
24+
> ์ฝ”๋“œ์—์„œ๋Š” 4-7 ์„ ํ•ด์„œ ์‚ฌ์‹ค์€ -3 ์ด๋ž€ ๊ฐ’์ด ๋“ค์–ด๊ฐ€์ง€๋งŒ ์„ค๋ช…์—์„  ํ—ท๊ฐˆ๋ฆฌ๋ฏ€๋กœ +3 ์ด๋ผ๊ณ  ํ•œ๋‹ค. ์–ด์ฐจํ”ผ abs ํ•˜์ง€ ์•Š๋Š” ์ด์ƒ diff ๋Š” ์ƒ๊ด€ ์—†๋‹ค.
25+
26+
์ด๊ฑธ ์ฝ”๋“œ๋กœ ๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค
27+
28+
```python3
29+
dp[(j, diff)] = dp[(i, diff)] + 1
30+
```
31+
32+
i ๋ฒˆ์งธ์—์„œ ๋ฐœ์ƒํ•œ diff ์นด์šดํŒ… ํ•œ ๊ฐ’์— 1์„ ๋”ํ•˜๋ฉด j ๋ฒˆ์งธ ๋ฐœ์ƒํ•œ diff ์นด์šดํŒ…์ด ๋œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. diff ๋Š” ๊ฐ™์•„์•ผ ํ•˜๊ณ  (arithmetic sequence!)
33+
๋ถ€๋ถ„ ๋ฐฐ์—ด์˜ ์กฐํ•ฉ์„ ์ƒ๊ฐํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. (์˜ˆ๋ฅผ ๋“ค๋ฉด `[4, 7, 11, 14]` ์—์„œ 4์™€ 7์€ +3, 11๊ณผ 14๋Š” +3 ์ด์ง€๋งŒ 4๊ฐœ๋ฅผ ๊ฐ™์ด ๋ดค์„ ๋•Œ +3 ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ์‹œํ€€์Šค๋Š” ์•„๋‹ˆ๋‹ค!)
34+
35+
36+
์˜ˆ์ œ์—์„œ ๋‚˜์™”๋˜ `[9, 4, 7, 2, 10]` ์˜ ๊ฒฐ๊ณผ ๋ฐฐ์—ด `[4, 7, 10]` ์˜ ๊ธธ์ด ๊ฐ’์€
37+
38+
1. dp[(2, 3)] ์นด์šดํŒ… (์ฝ”๋“œ์—์„  2๋ฅผ ๋„ฃ์–ด์คŒ)
39+
2. dp[(4, 3)] = dp[(2, 3)] + 1
40+
41+
์ฒ˜๋Ÿผ ๋œ๋‹ค.
42+
43+
44+
### ์ฝ”๋“œ
45+
```python3
46+
class Solution:
47+
def longestArithSeqLength(self, A: List[int]) -> int:
48+
dp = defaultdict(int)
49+
result = 0
50+
51+
for i in range(len(A) - 1):
52+
for j in range(i + 1, len(A)):
53+
diff = A[i] - A[j]
54+
if (i, diff) in dp:
55+
dp[(j, diff)] = dp[(i, diff)] + 1
56+
else:
57+
dp[(j, diff)] = 2
58+
result = max(result, dp[(j, diff)])
59+
60+
return result
61+
```
62+
63+
i ๋ฒˆ์งธ์—์„œ diff ๋ž€ ๊ฐ’์ด ์—†์„ ๋•Œ 2๋ฅผ ๋„ฃ์–ด์ค€ ์ด์œ ๋Š” ๋ฐฐ์—ด์ด [4, 10] ๋งŒ ์žˆ๋‹ค๊ณ  ๋ณด๋ฉด +5 ๋กœ ๊ฒฐ๊ณผ ๊ฐ’์ด 2๊ฐ€ ๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
64+
์ฆ‰ ๋ฐฐ์—ด์˜ ์‚ฌ์ด์ฆˆ๊ฐ€ 0 ๋˜๋Š” 1์ด ์•„๋‹Œ ์ด์ƒ ๋ฌด์กฐ๊ฑด ์ตœ์†Œ ๊ฐ’์€ 2๊ฐ€ ๋œ๋‹ค.

0 commit comments

Comments
ย (0)