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
25 changes: 25 additions & 0 deletions linked-list-cycle/dylan-jung.cpp
Copy link
Contributor

Choose a reason for hiding this comment

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

일치한다는 것을 발견했을 때 바로 true를 반환하는 것도 좋은 방법이군요! 배워갑니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool hasCycle(ListNode *head) {
if(head == nullptr || head->next == nullptr) return false;

ListNode* p1 = head;
ListNode* p2 = head;

while (p2 && p2->next) {
p1 = p1->next;
p2 = p2->next->next;

if (p1 == p2) return true;
}
return false;
}
};
21 changes: 21 additions & 0 deletions maximum-product-subarray/dylan-jung.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public:
int maxProduct(vector<int>& nums) {
int n = (int)nums.size();
int curMax = nums[0];
int curMin = nums[0];
int ans = nums[0];

for (int i = 1; i < n; i++) {
int x = nums[i];

if (x < 0) swap(curMax, curMin);

curMax = max(x, curMax * x);
curMin = min(x, curMin * x);

ans = max(ans, curMax);
}
return ans;
}
};
60 changes: 60 additions & 0 deletions minimum-window-substring/dylan-jung.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
class Solution {
public:
string minWindow(string s, string t) {
int m = (int)s.size();
if (t.empty() || s.empty()) return "";

int target[128] = {0};
int cnt[128] = {0};

int required = 0;
for (char c : t) {
unsigned char uc = (unsigned char)c;
if (target[uc] == 0) required++;
target[uc]++;
}

int formed = 0;
int l = 0, r = 0;
int ansl = 0, ansr = 0;
bool hasAns = false;

while (l <= r) {
bool isValid = (formed == required);

if (isValid) {
if (!hasAns || (r - l) < (ansr - ansl)) {
ansl = l;
ansr = r;
hasAns = true;
}

char cl = s[l];
cnt[cl]--;
if (target[cl] > 0 && cnt[cl] == target[cl] - 1) {
formed--;
}
l++;
}
else if (r >= m) {
char cl = s[l];
cnt[cl]--;
if (target[cl] > 0 && cnt[cl] == target[cl] - 1) {
formed--;
}
l++;
}
else {
char cr = s[r];
cnt[cr]++;
if (target[cr] > 0 && cnt[cr] == target[cr]) {
formed++;
}
r++;
}
}

if (!hasAns) return "";
return s.substr(ansl, ansr - ansl);
}
};
61 changes: 61 additions & 0 deletions pacific-atlantic-water-flow/dylan-jung.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
class Solution {
public:
int m, n;
int pside[200][200];
int aside[200][200];

void aflow(vector<vector<int>>& heights, int a, int b) {
int& val = aside[a][b];
if(val == 1) return;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
val = 1;
for(int i = 0; i < 4; i++) {
int na = a+dx[i];
int nb = b+dy[i];
if(!(0 <= na && na < m && 0 <= nb && nb < n)) continue;
if(heights[na][nb] >= heights[a][b])
aflow(heights, na, nb);
}
}

void pflow(vector<vector<int>>& heights, int a, int b) {
int& val = pside[a][b];
if(val == 1) return;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
val = 1;
int ans = 0;
for(int i = 0; i < 4; i++) {
int na = a+dx[i];
int nb = b+dy[i];
if(!(0 <= na && na < m && 0 <= nb && nb < n)) continue;
if(heights[na][nb] >= heights[a][b])
pflow(heights, na, nb);
}
}

vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {
m = heights.size();
n = heights[0].size();
fill(&pside[0][0], &pside[0][0]+200*200, 0);
fill(&aside[0][0], &aside[0][0]+200*200, 0);
vector<vector<int>> ans;

for(int i = 0; i < m; i++) aflow(heights, i, n-1);
for(int i = 0; i < n; i++) aflow(heights, m-1, i);
for(int i = 0; i < m; i++) pflow(heights, i, 0);
for(int i = 0; i < n; i++) pflow(heights, 0, i);

for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
// cout << pside[i][j] << " ";
if (pside[i][j] && aside[i][j]) {
ans.push_back({i, j});
}
}
// cout << "\n";
}
return ans;
}
};
13 changes: 13 additions & 0 deletions sum-of-two-integers/dylan-jung.cpp
Copy link
Contributor

Choose a reason for hiding this comment

The 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,13 @@
class Solution {
public:
int getSum(int a, int b) {
int ans = 0;
int carry = 0;
for(int i = 0; i < 32; i++) {
ans = ans | ((1 << i) & (a ^ b ^ (carry << i)));
carry = ((a >> i) & (b >> i) & 1) | (carry & ((a >> i) ^ (b >> i)));
// cout << carry;
}
return ans;
}
};