Skip to content
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
38 changes: 38 additions & 0 deletions container-with-most-water/dev-jonghoonpark.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
- 문제: https://leetcode.com/problems/container-with-most-water/
- time complexity : O(n)
- space complexity : O(1)
- 블로그 주소 : https://algorithm.jonghoonpark.com/2024/06/02/leetcode-11

```java
class Solution {
public int maxArea(int[] height) {
int start = 0;
int end = height.length - 1;

int max = getArea(height, start, end);

while(start < end) {
if(height[start] >= height[end]) {
end--;
max = Math.max(max, getArea(height, start, end));
} else {
start++;
max = Math.max(max, getArea(height, start, end));
}
}

return max;
}

public int getArea(int[] height, int start, int end) {
if (start == end) {
return 0;
}
return getMinHeight(height[end], height[start]) * (end - start);
}

public int getMinHeight(int height1, int height2) {
return Math.min(height1, height2);
}
}
```
21 changes: 21 additions & 0 deletions find-minimum-in-rotated-sorted-array/dev-jonghoonpark.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
- 문제: https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/
- time complexity : O(log n)
- space complexity : O(1)
- 블로그 주소 : https://algorithm.jonghoonpark.com/2024/06/03/leetcode-153

```java
public int findMin(int[] nums) {
int left = 0, right = nums.length - 1;
while(left < right) {
int mid = left + (right - left) / 2;

if(nums[mid] < nums[right]) {
right = mid;
} else {
left = mid + 1;
}
}

return nums[left];
}
```
36 changes: 36 additions & 0 deletions longest-repeating-character-replacement/dev-jonghoonpark.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
- 문제: https://leetcode.com/problems/longest-repeating-character-replacement/
- time complexity : O(n)
- space complexity : O(1)
- 블로그 주소 : https://algorithm.jonghoonpark.com/2024/04/29/leetcode-424

```java
class Solution {
public int characterReplacement(String s, int k) {
int[] counter = new int[26];
int countOfMostFrequent = -1;

int headPointer = 0;
int tailPointer = 0;

int maxLength = 0;

while (headPointer < s.length()) {
char head = s.charAt(headPointer);
char tail = s.charAt(tailPointer);
counter[head - 'A']++;
headPointer++;

countOfMostFrequent = Math.max(counter[head - 'A'], countOfMostFrequent);
while (headPointer - tailPointer > countOfMostFrequent + k) {
counter[tail - 'A']--;
tailPointer++;
tail = s.charAt(tailPointer);
}

maxLength = Math.max(maxLength, headPointer - tailPointer);
}

return maxLength;
}
}
```
34 changes: 34 additions & 0 deletions longest-substring-without-repeating-characters/dev-jonghoonpark.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
- 문제: https://leetcode.com/problems/longest-substring-without-repeating-characters/
- time complexity : O(n)
- space complexity : O(n)
- 블로그 주소 : https://algorithm.jonghoonpark.com/2024/02/18/leetcode-3

```java
class Solution {
public int lengthOfLongestSubstring(String s) {
if (s.isEmpty()) {
return 0;
}
Comment on lines +9 to +11
Copy link
Member

Choose a reason for hiding this comment

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

이런 류의 체크는 큰 의미없이 코드의 길이만 늘릴 수 있지 않을까요? 얼마큼의 연산량이 아껴지는지 생각해보시면 좋을 것 같습니다. 특히 자바 같이 코드가 긴 편에 속하는 언어에서는 오히려 득보다 해가 될 수 있다고 생각합니다.


Set<Character> set = new HashSet<>();
int pointer = 0;
int longest = 0;

while (pointer < s.length()) {
char _char = s.charAt(pointer);
while (set.contains(_char)) {
set.remove(s.charAt(pointer - set.size()));
}
set.add(_char);
longest = Math.max(longest, set.size());
pointer++;
}
Comment on lines +17 to +25
Copy link
Member

Choose a reason for hiding this comment

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

오, 집합을 요렇게 활용하시다니 상당히 신선한데요?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

중복이 되면 안된다는 조건이 있어서 Set을 사용해보았습니다 : )


return longest;
}
}
```

## TC 추가 설명

내부 while이 있지만 O(n)으로 적을 수 있는 이유는 hashset의 경우 search 하는데 드는 비용이 O(1) 이기 때문이다.
33 changes: 33 additions & 0 deletions search-in-rotated-sorted-array/dev-jonghoonpark.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
- 문제: https://leetcode.com/problems/search-in-rotated-sorted-array/
- time complexity : O(log n)
- space complexity : O(1)
- 블로그 주소 : https://algorithm.jonghoonpark.com/2024/06/03/leetcode-33

```java
public int search(int[] nums, int target) {
int left = 0, right = nums.length - 1;
while(left <= right) {
int mid = left + (right - left) / 2;

if (nums[mid] == target) {
return mid;
}

if(nums[mid] < nums[right]) {
if (target < nums[mid] || target > nums[right]) {
right = mid - 1;
} else {
left = mid + 1;
}
} else {
if (target < nums[left] || target > nums[mid]) {
left = mid + 1;
} else {
right = mid - 1;
}
}
}

return -1;
}
```