Skip to content

[Jay-Mo-99] Week 7 #940

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 0 additions & 121 deletions design-add-and-search-words-data-structure/Jay-Mo-99.py

This file was deleted.

36 changes: 36 additions & 0 deletions longest-substring-without-repeating-characters/Jay-Mo-99.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
 #해석
#


#Big O
#- N: str s의 길이

#Time Complexity: O(N)
#- start,end: 각각 최대 N번 움직임 -> O(N)
#- set의 삽입, 삭제 -> O(1)

#Space Complexity: O(N)
#- chars: 최악의 경우 chars는 s의 모든 char을 저장한다 -> O(N)

class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
max_len = 0
chars = set() #현 윈도우 내 중복 없이 존재하는 문자들을 저장하는 set
start, end = 0, 0 # 슬라이딩 윈도우의 start 인덱스, end 인덱스
#end가 s의 마지막 인덱스에 도달할때까지 반복
while end < len(s):
#s[end]가 chars에 존재하면
if s[end] in chars:
#chars의 s[start]를 제거
chars.remove(s[start])
#start를 오른쪽으로 이동, 윈도우 축소
start += 1
else: #s[end] 가 chars에 존재하지 않으면
chars.add(s[end]) #해당 s[end]를 chars에 추가해준다
end += 1 #end를 오른쪽으로 옮겨 윈도우 확장
max_len = max(end - start, max_len) #start-end 계산하여 update
return max_len

mySolution = Solution()
mySolution.lengthOfLongestSubstring("pwwkew")

60 changes: 60 additions & 0 deletions number-of-islands/Jay-Mo-99.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#해석
# r,c nested loop로 grid의 모든 element를 검사한다
# 만약 1인 element를 만나면 sink함수를 호출한다
# -sink 함수는 해당 element를 0으로 만들고 해당 element의 좌,우,상,하가 1인지 체크한다
# -만약 1이 있다면 또 sink를 반복 호출하며 위의 검사를 반복한다.(1이 없을때까지)
# -만약 더이상 연결된 1이 없다면 재귀 호출 종료.
# sink함수 종료 시시 nested loop로 돌아와서 이후후 1인 element를 찾는다.
# grid의 1이 sink로 모두 없어지면 return cls한다.


#Big O
#- M: grid의 행의 갯수(r)
#- N: grid의 열의 갯수(c)

#Time Complexity: O(M*N)
#- for loop: 이중 루프로 grid의 모든 element에 도달 -> O(M*N)
#- sink(row,col): 최악의 경우 sink함수는 M*N번 호출 -> O(M*N)

#Space Complexity: O(M∗N)
#- sink 재귀호출:
# 최악의 경우 sink함수는 스택에 M*N번 재귀 호출 당한다.
# 스택에 해당 메모리 누적(재귀 호출 스택의 깊이가 M*N) -> O(M*N)
from typing import List
class Solution:
def numIslands(self, grid: List[List[str]]) -> int:
def sink(row, col):
grid[row][col] = "0"

for r, c in [
(row, col - 1), #Left
(row, col + 1), #Right
(row - 1, col), #Up
(row + 1, col), #Down
]:
# If the neighbor cell is within bounds and is land ("1"), sink it recursively.
if 0 <= r < len(grid) and 0 <= c < len(grid[r]):
if grid[r][c] == "1":
sink(r, c)

cnt = 0 # Count the number of islands.
# Loop through every cell in the grid.
for r in range(len(grid)):
for c in range(len(grid[r])):
if grid[r][c] == "1":
cnt += 1
sink(r, c) ## Sink the entire island by converting all connected "1"s to "0"s.
return cnt

mySolution = Solution()
mySolution.numIslands(
[
["1","1","1","1","0"],
["1","1","0","1","0"],
["1","1","0","0","0"],
["0","0","0","0","0"]
]
)



48 changes: 48 additions & 0 deletions reverse-linked-list/Jay-Mo-99.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 문제의 경우 LinkedList의 특성을 활용해서 푸는 문제라고 생각합니다.
해당 풀이처럼 temp에 모든 리스트들의 값을 저장 후 역으로 재구성할 수도 있지만
null 값을 가진 Node 하나를 사용해서 LinkedList가 가리키는 방향을 모두 역순으로 하는 풀이도 가능할거에요.
해당 문제의 특성을 살려 한번 더 풀이 해보시면 좋을것 같습니다!

Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
 #해석
# 매개변수 head (ListNode 클래스의 인스턴스)에서 값을 추출하여 temp 리스트에 저장한다.
# temp 리스트를 역순으로 정렬(reverse)하여 연결 리스트를 뒤집는다.
# temp 리스트의 각 값을 ListNode 클래스를 사용해 새 연결 리스트(myNode)를 생성하며 순서대로 추가한다.
# 최종적으로 myNode의 next를 반환한다(이것은 새 연결 리스트의 시작점을 가리킨다).


#Big O
#- N: 입력 연결 리스트(head)의 노드 갯수

#Time Complexity: O(N)
#- while head: 연결 리스트의 모든 노드를 순회하며 val을 temp에 저장하므로 O(N).
#- for value in temp: temp 리스트의 모든 값을 순회하며 새로운 노드를 생성하므로 O(N).

#Space Complexity: O(N)
#- temp : 연결 리스트의 모든 val을 저장하므로 O(N).
#- myNode 인스턴스: for loop 동안 current.next에 ListNode 인스턴스를 생성한다. 이 작업은 O(1) 작업이 N번 반복되므로 O(N).


# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def reverseList(self, head):
"""
:type head: Optional[ListNode]
:rtype: Optional[ListNode]
"""
temp =[]
while head:
temp.append(head.val)
head = head.next

temp = temp[::-1] #Reverse the temp list

myNode = ListNode() #Create the Listnode instance
current = myNode #Update current to myNode for Initialize

for value in temp:
current.next = ListNode(value) ## Create new ListNode Instance and assign it to current.next ,
current = current.next #Move to the current.next(new Instance base on ListNode )

return myNode.next ## Return the head of the newly created linked list



45 changes: 45 additions & 0 deletions set-matrix-zeroes/Jay-Mo-99.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#해석
# matrix의 모든 element를 검사한다, 만약 0을 발견하면 해당 element와 같은 row와 col을 가진 element를 "a" 로 바꾼다.
# 두번째로 matrix의 모든 element를 검사한다, 만약 a를 발견하면 이를 0으로 바꾼다.


#Big O
#- N: row의 크기(matrix 행의 갯수)
#- K: col의 크기(matrix 열의 갯수 )

#Time Complexity: O(N*K*(N+K))
#- for nested loop : 행의 갯수(N) 당 열의 갯수만큼(K) 루프 작동 -> O(N*K)
# - 최악의 경우, 첫번째 루프에서 for i in range(rows)가 M번 발동, for j in range(cols)가 N번 발동 -> O(N+K)

#Space Complexity: O(1)
#- rows, cols: 변수의 할당과 업데이트는 상수 취급한다 -> O(1)
from typing import List


class Solution:
def setZeroes(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
rows, cols = len(matrix), len(matrix[0]) #rows와 cols에 matrix의 좌표 부여

# 1차 matrix 순회: 0을 발견하면, 그 element의 같은 행과 열의 0이 아닌 수를 임시 값 "a"로 바꾸기
for r in range(rows):
for c in range(cols):
if matrix[r][c] == 0:
# 해당 행과 열의 0이 아닌 모든 값을 임시로 "a"로 변경
for i in range(rows): # 해당 열의 모든 값
if matrix[i][c] != 0:
matrix[i][c] = "a"
for j in range(cols): # 해당 행의 0이 아닌 모든 값을 "a"로 변경
if matrix[r][j] != 0:
matrix[r][j] = "a"

# 2차 matrix순회: "a"를 가진 수를 0으로 바꿔준다.
for r in range(rows):
for c in range(cols):
if matrix[r][c] == "a":
matrix[r][c] = 0



37 changes: 37 additions & 0 deletions unique-paths/Jay-Mo-99.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# 해석
# grid는 행렬(matrix)처럼 격자의 형태이다.
# 행이 m개라는 뜻은 좌표 상 (0,0), (1,0), ..., (m-1,0)까지 존재한다는 뜻이다.
# 열이 n개라는 뜻은 좌표 상 (0,0), (0,1), ..., (0,n-1)까지 존재한다는 뜻이다.
# 따라서 (0,0)에서 (m-1,n-1)까지의 모든 가능한 경로 수를 구해야 한다.
# - 아래로 m-1번 이동하고, 오른쪽으로 n-1번 이동해야 한다.
# - 총 이동 횟수는 m-1 + n-1 = m+n-2이다.
# - 총 이동 횟수 내부에서 아래로 m-1번 오른쪽으로 (n-1)번의 조합의 경우의 수를 구한다.
# - 예를 들어, 아래로 3번 오른쪽으로 다섯번으로 만들수 있는 모든 경우의 수를 구한다 ^^^>>>>> : ^와 > 가능한 조합을 찾아준다.
# 공식: (m+n-2)! / (m-1)! / (n-1)!


#Big O
#- N: int m의 크기
#- K: int n의 크기

#Time Complexity: O(M+K)
#- for i in range(1,m+1-1): m과 n의 크기의 합에 영향받아 계산 진행 -> O(N+K)

#Space Complexity: O(1)
#- up,down1,down2 - 변수와 변수를 업데이트하는 사칙연산은 상수로 여겨져 O(1),
class Solution:
def uniquePaths(self, m: int, n: int) -> int:
up = 1 #분자
down1 = 1 #분모 1
down2=1 #분모 2


for i in range(1,m+n-1):
up *= i
for i in range(1,m):
down1 *= i
for i in range(1,n):
down2 *= i

return int(up/(down1*down2))

Loading