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
24 changes: 24 additions & 0 deletions maximum-subarray/YuuuuuuYu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Runtime: 1ms
* Time Complexity: O(n)
*
* Memory: 76.88MB
* Space Complexity: O(1)
*
* Approach: 카데인 알고리즘
* - 부분 배열의 합이 최대가 되는 값을 찾는 알고리즘
* 1) 이전 합보다 현재 숫자가 더 크면 현재 숫자로 sum을 초기화
*/
class Solution {
public int maxSubArray(int[] nums) {
int sum = nums[0];
int max = nums[0];

for (int i=1; i<nums.length; i++) {
sum = Math.max(nums[i], sum+nums[i]);
max = Math.max(max, sum);
}

return max;
}
}
23 changes: 23 additions & 0 deletions number-of-1-bits/YuuuuuuYu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Runtime: 0ms
* Time Complexity: O(log n)
* - n을 2로 나누는 과정을 반복하므로 log n에 비례
*
* Memory: 42.32MB
* Space Complexity: O(1)
*
* Approach: 비트 연산 (나눗셈 방식)
* 1) n을 2로 나눈 나머지(n%2)를 확인하여 1의 개수를 카운트
* 2) 마지막에 1은 무조건 남기 때문에 while문 종료 후 1을 더해줌
*/
class Solution {
public int hammingWeight(int n) {
int count = 0;
while (n > 1) {
count += n%2;
n /= 2;
}

return count+1;
}
}
37 changes: 37 additions & 0 deletions valid-palindrome/YuuuuuuYu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Runtime: 2ms
* Time Complexity: O(n)
*
* Memory: 44.29MB
* Space Complexity: O(1)
*
* Approach: 투 포인터
* 1) 문자열의 양 끝에서부터 시작하는 두 포인터를 설정
* 2) 포인터가 가리키는 문자가 영숫자가 아닌 경우, 해당 포인터를 이동
*/
class Solution {
public boolean isPalindrome(String s) {
int start = 0;
int end = s.length()-1;

while (start < end) {
char currLeft = s.charAt(start);
char currRight = s.charAt(end);

if (!Character.isLetterOrDigit(currLeft)) {
start++;
} else if (!Character.isLetterOrDigit(currRight)) {
end--;
} else {
if (Character.toLowerCase(currLeft) != Character.toLowerCase(currRight)) {
return false;
}

start++;
end--;
}
}

return true;
}
}