-
-
Notifications
You must be signed in to change notification settings - Fork 245
[njngwn] WEEK 07 solutions #1885
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Time Complexity: O(n), n: s.length() | ||
// Space Complexity: O(n), n: s.length() | ||
class Solution { | ||
public int lengthOfLongestSubstring(String s) { | ||
int begin = 0; | ||
int end = 0; | ||
int maxLength = 0; | ||
|
||
Set<Character> letterSet = new HashSet<>(); | ||
while (end < s.length()) { | ||
// if the letter is not in the hashset, then add it into hashset and move end pointer | ||
if (!letterSet.contains(s.charAt(end))) { | ||
letterSet.add(s.charAt(end)); | ||
++end; | ||
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. 아, 그냥 제 코딩습관인데 ++i가 i++보다 조금 더 빠르다는 말을 들어서요..!! 근데 자바라서 상관없을 것 같기도 하고, 요즘 컴파일러는 상관없다구 하네요. https://ssocoit.tistory.com/51 |
||
maxLength = Math.max(maxLength, end - begin); // update maxlength | ||
} else { // if the letter is in the hashset, then remove it and move begin pointer | ||
letterSet.remove(s.charAt(begin)); | ||
++begin; | ||
} | ||
} | ||
|
||
return maxLength; | ||
} | ||
} |
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. 앗! DFS로 푸는 문제들이 나왔군요. 좀 더 시간을 들여서 저도 풀었다면 좋았을텐데... 쪼끔 아쉬운 마음이 남네요. 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. 꼼꼼하게 봐주셔서 감사해요!! 다음 기수에 한 번 더 도전하셔서 풀어보셔도 좋을 것 같아요!! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Time Complexity: O(m*n), m: number of row, n: number of column | ||
// Space Complexity: O(m*n), m: number of row, n: number of column, because of call stack | ||
class Solution { | ||
void findLand(char[][] grid, int row, int col) { | ||
if (row < 0 || row > grid.length-1 || col < 0 || col > grid[0].length-1) return; | ||
if (grid[row][col] == '0') return; | ||
|
||
grid[row][col] = '0'; // make element '0'(water) | ||
|
||
findLand(grid, row-1, col); | ||
findLand(grid, row+1, col); | ||
findLand(grid, row, col-1); | ||
findLand(grid, row, col+1); | ||
} | ||
|
||
public int numIslands(char[][] grid) { | ||
int num = 0; | ||
|
||
for (int i = 0; i < grid.length; ++i) { | ||
for (int j = 0; j < grid[0].length; ++j) { | ||
if (grid[i][j] == '1') { | ||
findLand(grid, i, j); | ||
num++; | ||
} | ||
} | ||
} | ||
|
||
return num; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Time Complexity: O(m*n) | ||
// Space Complexity: O(m+n) | ||
class Solution { | ||
public void setZeroes(int[][] matrix) { | ||
ArrayList<Integer> rowList = new ArrayList<>(); | ||
ArrayList<Integer> colList = new ArrayList<>(); | ||
|
||
for (int m = 0; m < matrix.length; ++m) { | ||
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. 여기서도 전위 표기를 하셨네요! 선택한 이유가 있으신지 궁금합니다~ |
||
for (int n = 0; n < matrix[0].length; ++n) { | ||
if (matrix[m][n] != 0) continue; | ||
if (!rowList.contains(m)) rowList.add(m); | ||
if (!colList.contains(n)) colList.add(n); | ||
} | ||
} | ||
|
||
for (Integer row : rowList) { | ||
for (int n = 0; n < matrix[0].length; ++n) { | ||
matrix[row][n] = 0; | ||
} | ||
} | ||
|
||
for (Integer col : colList) { | ||
for (int n = 0; n < matrix.length; ++n) { | ||
matrix[n][col] = 0; | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Time Complexity: O(m*n), m: number of row, n: number of column | ||
// Space Complexity: O(m*n), m: number of row, n: number of column | ||
class Solution { | ||
public int uniquePaths(int m, int n) { | ||
int[][] pathMap = new int[m][n]; | ||
|
||
for (int i = 0; i < m; ++i) { | ||
for (int j = 0; j < n; ++j) { | ||
if (i == 0 || j == 0) { | ||
pathMap[i][j] = 1; | ||
} else { | ||
pathMap[i][j] = pathMap[i-1][j] + pathMap[i][j-1]; | ||
} | ||
} | ||
} | ||
|
||
return pathMap[m-1][n-1]; | ||
} | ||
} |
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.
문제를 안 읽었지만 요구하는 게 뭔지 알 거 같네요! 주석의 설명이 짧지만 명확하고 코드 역시 깔끔해서 가독성이 좋다고 생각합니다.
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.
감사합니다!!!