-
Notifications
You must be signed in to change notification settings - Fork 53
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
longest-substring-without-repeating-characters/TonyKim9401.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// TC: O(n^2) | ||
// -> 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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// TC: O(n^2) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @DaleSeo 앗... 정정하겠습니다 |
||
// 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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
얼핏 보기에는
O(n)
솔루션 같은데, 시간 복잡도 설명 좀 부탁드리겠습니다.There was a problem hiding this comment.
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)
이 될거라 생각합니다.There was a problem hiding this comment.
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이 뜨거나하는 경우도 있는 것으로 알고 있습니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lymchgmk 의견 감사합니다!! 다음부터 적용해보도록 하겠습니다 :)