diff --git a/linked-list-cycle/dylan-jung.cpp b/linked-list-cycle/dylan-jung.cpp new file mode 100644 index 0000000000..1a468ba10e --- /dev/null +++ b/linked-list-cycle/dylan-jung.cpp @@ -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; + } +}; diff --git a/maximum-product-subarray/dylan-jung.cpp b/maximum-product-subarray/dylan-jung.cpp new file mode 100644 index 0000000000..4fd8eb29a3 --- /dev/null +++ b/maximum-product-subarray/dylan-jung.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + int maxProduct(vector& 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; + } +}; diff --git a/minimum-window-substring/dylan-jung.cpp b/minimum-window-substring/dylan-jung.cpp new file mode 100644 index 0000000000..8bbd481e7d --- /dev/null +++ b/minimum-window-substring/dylan-jung.cpp @@ -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); + } +}; diff --git a/pacific-atlantic-water-flow/dylan-jung.cpp b/pacific-atlantic-water-flow/dylan-jung.cpp new file mode 100644 index 0000000000..a41b5c9886 --- /dev/null +++ b/pacific-atlantic-water-flow/dylan-jung.cpp @@ -0,0 +1,61 @@ +class Solution { +public: + int m, n; + int pside[200][200]; + int aside[200][200]; + + void aflow(vector>& 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>& 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> pacificAtlantic(vector>& 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> 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; + } +}; diff --git a/sum-of-two-integers/dylan-jung.cpp b/sum-of-two-integers/dylan-jung.cpp new file mode 100644 index 0000000000..20f6ca9dc4 --- /dev/null +++ b/sum-of-two-integers/dylan-jung.cpp @@ -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; + } +};