Skip to content

Commit 31e28bb

Browse files
committed
1475. Final Prices With a Special Discount in a Shop.
- Language: Python. - Time complexity: O(n * n). - Space complexity: O(1). - Method: Brute Force.
1 parent e12b13c commit 31e28bb

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

python_solutions/lc_1475_final_prices_with_a_spec_discount_in_a_shop/__init__.py

Whitespace-only changes.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def finalPrices(self, prices: list[int]) -> list[int]:
3+
# Time complexity: O(n * n). Space complexity: O(1).
4+
for i in range(0, len(prices) - 1):
5+
for j in range(i + 1, len(prices)):
6+
if prices[j] <= prices[i]:
7+
prices[i] = prices[i] - prices[j]
8+
break
9+
return prices
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import pytest
2+
3+
from lc_1475_final_prices_with_a_spec_discount_in_a_shop.lc_1475 import Solution
4+
5+
test_cases = (
6+
# preset cases
7+
([8, 4, 6, 2, 3], [4, 2, 4, 2, 3]),
8+
([1, 2, 3, 4, 5], [1, 2, 3, 4, 5]),
9+
([10, 1, 1, 6], [9, 0, 1, 6]),
10+
([8, 7, 4, 2, 8, 1, 7, 7, 10, 1], [1, 3, 2, 1, 7, 0, 0, 6, 9, 1]),
11+
# common cases
12+
([8, 7, 4, 2, 8, 1, 7, 8, 7, 1], [1, 3, 2, 1, 7, 0, 0, 1, 6, 1]),
13+
([8, 9, 10, 3], [5, 6, 7, 3]),
14+
([5, 4, 3, 1], [1, 1, 2, 1]),
15+
# corner cases
16+
([10], [10]),
17+
([10, 20], [10, 20]),
18+
([10, 10], [0, 10]),
19+
([10, 1], [9, 1]),
20+
([1, 1, 1, 1], [0, 0, 0, 1]),
21+
)
22+
23+
24+
@pytest.mark.parametrize(("lst", "ans"), test_cases)
25+
def test_success_v0(lst: list[int], ans: list[int], solution: Solution):
26+
assert solution.finalPrices(lst) == ans
27+
28+
29+
@pytest.fixture
30+
def solution() -> Solution:
31+
return Solution()

0 commit comments

Comments
 (0)