Skip to content

Commit

Permalink
백준 12015번 가장 긴 증가하는 부분 수열 2 v2
Browse files Browse the repository at this point in the history
  • Loading branch information
skysign committed Mar 8, 2025
1 parent f4aab7c commit c65beb4
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
36 changes: 36 additions & 0 deletions 백준 12015번 가장 긴 증가하는 부분 수열 2 v2/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import sys


def solve():
input = sys.stdin.readline
N = int(input().strip())
dts = list(map(int, input().strip().split()))
LIS = [0]

for dt in dts:
if LIS[-1] < dt:
LIS.append(dt)
else:
idx = binary_search(LIS, dt)
LIS[idx] = dt

print(len(LIS) - 1)


def binary_search(LIS, dt):
bgn, end = 0, len(LIS) - 1

while bgn <= end:
mid = (bgn + end) // 2
if dt == LIS[mid]:
return mid
elif dt < LIS[mid]:
end = mid - 1
else: # dt > LIS[mid]
bgn = mid + 1

return bgn


if __name__ == '__main__':
solve()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
6
10 20 10 30 20 50
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import sys
from pathlib import Path
from unittest import TestCase
from main import solve


class Test(TestCase):
def my_solve(self, testcase_input):
sys.stdin = open(testcase_input, 'r')
stdout = sys.stdout
sys.stdout = open('stdout.txt', 'w')
solve()
sys.stdout.close()
sys.stdout = stdout

def test_solve(self, testcase_number: str):
self.my_solve('test' + testcase_number + '.txt')
self.assertEqual(
Path('test' + testcase_number + '_answer.txt').read_text().strip(),
Path('stdout.txt').read_text().strip())

def test1_solve(self):
self.test_solve('1')

0 comments on commit c65beb4

Please sign in to comment.