Skip to content
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

[TONY] WEEK 07 Solutions #485

Merged
merged 5 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
25 changes: 25 additions & 0 deletions longest-substring-without-repeating-characters/TonyKim9401.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// TC: O(n^2)
Copy link
Contributor

Choose a reason for hiding this comment

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

얼핏 보기에는 O(n) 솔루션 같은데, 시간 복잡도 설명 좀 부탁드리겠습니다.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@DaleSeo for문이 하나만 사용되었고 주어진 문자열 s만큼 반복을 하기에 얼핏 보면 O(n)으로 보일 수 있는데요, 내부 if문이 true 일 경우 i -= count 가 되면서 최대 O(n^2) 까지 나올 수 있다고 생각합니다! 따라서 최악의 경우 O(n^2) 이 될거라 생각합니다.

Copy link
Contributor

Choose a reason for hiding this comment

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

for문의 index를 for문 내부에서 수정하는데, 이런 용도에는 별도의 변수를 사용해서 for문을 중첩하거나, 첫 for문을 while문으로 수정하면 더 좋을 것 같습니다. 언어에 따라서는 for문의 index가 변경안되거나(capture되어서 별도의 변수로 여겨지는 경우), warning이 뜨거나하는 경우도 있는 것으로 알고 있습니다.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@lymchgmk 의견 감사합니다!! 다음부터 적용해보도록 하겠습니다 :)

// -> all elements can be retrived multiple times in the worst case
// SC: O(1)
// -> since declare, no more increase or decrease
class Solution {
public int lengthOfLongestSubstring(String s) {
int max = 0;
int count = 0;
boolean[] checkList = new boolean[128];

for (int i = 0; i < s.length(); i++) {
int idx = s.charAt(i);
if (checkList[idx]) {
max = Math.max(max, count);
i -= count;
count = 0;
checkList = new boolean[128];
} else {
count += 1;
checkList[idx] = true;
}
}
return max = Math.max(max, count);
}
}
28 changes: 28 additions & 0 deletions number-of-islands/TonyKim9401.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// TC: O(n * m)
// retrieve all elemetns, grid.length * grid[0].length
// SC: O(n * m(
// need to change all elements from 1 to 0 in the worst case
class Solution {
int output = 0;
public int numIslands(char[][] grid) {
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j] == '1') {
output += 1;
countIslands(i, j, grid);
}
}
}
return output;
}

private void countIslands(int i, int j, char[][] grid) {
if (i < 0 || i >= grid.length || j < 0 || j >= grid[0].length) return;
if (grid[i][j] == '0') return;
grid[i][j] = '0';
countIslands(i+1, j, grid);
countIslands(i-1, j, grid);
countIslands(i, j+1, grid);
countIslands(i, j-1, grid);
}
}
16 changes: 16 additions & 0 deletions reverse-linked-list/TonyKim9401.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// TC: O(n)
// -> visit all elements of head
// SC: O(1)
// -> constant space complexity
class Solution {
public ListNode reverseList(ListNode head) {
ListNode node = null;
while (head != null) {
ListNode temp = head.next;
head.next = node;
node = head;
head = temp;
}
return node;
}
}
32 changes: 32 additions & 0 deletions set-matrix-zeroes/TonyKim9401.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// TC: O(n^2)
Copy link
Contributor

Choose a reason for hiding this comment

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

n이 무엇을 의미하나요?

Copy link
Contributor Author

@TonyKim9401 TonyKim9401 Sep 27, 2024

Choose a reason for hiding this comment

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

@DaleSeo 앗... 정정하겠습니다 TC: O(n * m), n = the size of the matrix, m = the size of the matrix[n]

// SC: O(1)
class Solution {
public void setZeroes(int[][] matrix) {
boolean firstRow = false, firstCol = false;

for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
if (matrix[i][j] == 0) {
if (i == 0) firstRow = true;
if (j == 0) firstCol = true;
matrix[0][j] = 0;
matrix[i][0] = 0;
}
}
}

for (int i = 1; i < matrix.length; i++) {
for (int j = 1; j < matrix[0].length; j++) {
if (matrix[i][0] == 0 || matrix[0][j] == 0) matrix[i][j] = 0;
}
}

if (firstRow) {
for (int j = 0; j < matrix[0].length; j++) matrix[0][j] = 0;
}

if (firstCol) {
for (int i = 0; i < matrix.length; i++) matrix[i][0] = 0;
}
}
}
18 changes: 18 additions & 0 deletions unique-paths/TonyKim9401.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// TC: O (m * n)
// SC: O (m * n)
// -> need to retrieve all elements
class Solution {
public int uniquePaths(int m, int n) {
int[][] dp = new int[m][n];

for (int i = 0; i < m; i++) dp[i][0] = 1;
for (int j = 0; j < n; j++) dp[0][j] = 1;

for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
}
return dp[m-1][n-1];
}
}