Skip to content

Commit bbb850a

Browse files
committed
solved(python): programmers
- 연습문제 / 뒤에 있는 큰 수 찾기
1 parent 97ed0c7 commit bbb850a

4 files changed

Lines changed: 83 additions & 0 deletions

File tree

programmers/python/연습문제/뒤에_있는_큰_수_찾기/__init__.py

Whitespace-only changes.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from typing import List
2+
3+
4+
def solution(numbers: List[int]) -> List[int]:
5+
n = len(numbers)
6+
stack, results = [], [-1] * n
7+
8+
for idx in range(n - 1, -1, -1):
9+
while stack and stack[-1] <= numbers[idx]:
10+
stack.pop()
11+
12+
if stack:
13+
results[idx] = stack[-1]
14+
15+
stack.append(numbers[idx])
16+
17+
return results
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
[
2+
{
3+
"params": [
4+
[
5+
2,
6+
3,
7+
3,
8+
5
9+
]
10+
],
11+
"expected": [
12+
3,
13+
5,
14+
5,
15+
-1
16+
]
17+
},
18+
{
19+
"params": [
20+
[
21+
9,
22+
1,
23+
5,
24+
3,
25+
6,
26+
2
27+
]
28+
],
29+
"expected": [
30+
-1,
31+
5,
32+
6,
33+
6,
34+
-1,
35+
-1
36+
]
37+
}
38+
]
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import json
2+
import os
3+
import unittest
4+
5+
from parameterized import parameterized
6+
7+
from .main import solution
8+
9+
10+
def load_sample(filename: str):
11+
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), filename)
12+
13+
with open(path, "r") as file:
14+
return [(case["params"], case["expected"]) for case in json.load(file)]
15+
16+
17+
class TestCase(unittest.TestCase):
18+
@parameterized.expand(load_sample("sample.json"))
19+
def test_case(self, params: list, expected: any):
20+
# When
21+
result = solution(*params)
22+
23+
# Then
24+
self.assertEqual(expected, result)
25+
26+
27+
if __name__ == "__main__":
28+
unittest.main()

0 commit comments

Comments
 (0)