Skip to content

Instantly share code, notes, and snippets.

View superzjn's full-sized avatar

Jianan Zhang superzjn

  • Morningstar
  • Chicago
View GitHub Profile
@superzjn
superzjn / example.java
Created November 3, 2022 20:26
[Download Database to file (Postgresql)] #FileIO
private File downloadDataFromDatabase(String filename, String query) {
long start = System.currentTimeMillis();
File tmp = getTempFile(filename);
try (FileOutputStream out = new FileOutputStream(tmp);
Connection pooledConnection = ds.getConnection()) {
// according to dbcp documentation, you are not supposed to close this connection, only the pooled connection above, so that is why it is not in the try-with-resources
BaseConnection conn = (BaseConnection) ((DelegatingConnection<?>) pooledConnection).getInnermostDelegate();
CopyManager copy = new CopyManager(conn);
copy.copyOut("COPY (" + query + ") TO STDOUT (FORMAT csv, HEADER true)", out);
@superzjn
superzjn / pSolution1.py
Created November 1, 2022 01:08
[Squares of a Sorted Array] LeetCode 977 #TwoPointers
class Solution:
def sortedSquares(self, nums: List[int]) -> List[int]:
n = len(nums)
result = [0] * n
left = 0
right = n - 1
index = n - 1
while left <= right:
@superzjn
superzjn / pSolution1.py
Last active November 1, 2022 00:59
[Remove Duplicates from Sorted Array] LeetCode 26 #TwoPointers
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
insert_index = 1
# Two pointer to the same direction
# We can also use range(1, len(nums)) to have better performance by skip comparing nums[0] and nums[0]
for i in range(len(nums)):
# find different numbers
if nums[i] != nums[insert_index - 1]:
nums[insert_index] = nums[i]
insert_index += 1
@superzjn
superzjn / psolution1.py
Created October 30, 2022 23:49
[Two Sum] LeetCode 1 #HashMap
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
table = {}
for i, num in enumerate(nums):
another = target - num
if another in table:
return [i, table[another]]
table[num] = i
@superzjn
superzjn / pSolution1.py
Last active October 28, 2022 01:50
[Longest Duplicate Substring] LeetCode 1044 #BinarySearch #SlidingWindow #RollingHash
class Solution:
def longestDupSubstring(self, S):
# Modulus value for the rolling hash function to avoid overflow.
# this is very important 10*9 + 7 wont pass
mod = 10 ** 12 + 7 # 2**63 - 1 also works
# Select a base value for the rolling hash function.
base = 26
# Convert string to array of integers to implement constant time slice.
@superzjn
superzjn / pSolution1.py
Last active October 28, 2022 01:49
[Jump Game VI] LeetCode 1696 #SlidingWindow #DP
class Solution:
def maxResult(self, nums: List[int], k: int) -> int:
n = len(nums)
priority_queue = []
score = [0] * n
score[0] = nums[0]
# use negative number to represent max-heap
heapq.heappush(priority_queue, (-nums[0], 0))
@superzjn
superzjn / pSolution1.py
Last active October 16, 2022 15:37
[Maximum Erasure Value] LeetCode 1695 #SlidingWindow
class Solution:
def maximumUniqueSubarray(self, nums: List[int]) -> int:
win_start, curr_sum, result = 0, 0, 0
curr_set = set()
for win_end in range(len(nums)):
right_num = nums[win_end]
while right_num in curr_set:
curr_set.remove(nums[win_start])
@superzjn
superzjn / jSolution1.java
Last active October 11, 2022 14:28
[Maximum Points You Can Obtain from Cards] LeetCode 1423 #SlidingWindow
T O(n) S O(n)
class Solution {
public int maxScore(int[] cardPoints, int k) {
int n = cardPoints.length;
int[] frontSetOfCards = new int[k + 1];
int[] rearSetOfCards = new int[k + 1];
for (int i = 0; i < k; i++) {
frontSetOfCards[i + 1] = cardPoints[i] + frontSetOfCards[i];
@superzjn
superzjn / pSolution1.py
Last active October 10, 2022 21:01
[Find K Closest Elements] LeetCode 658 #SlidingWindow #Queue
class Solution:
def findClosestElements(self, arr: List[int], k: int, x: int) -> List[int]:
win_start = 0
queue = collections.deque()
for win_end in range(len(arr)):
right_num = arr[win_end]
queue.append(right_num)
while len(queue) > k and win_end >= win_start:
@superzjn
superzjn / pSolution1.py
Last active October 9, 2022 16:43
[Minimum Operations to Reduce X to Zero] LeetCode 1658 #SlidingWindow
# Instead of find the minimum operations, we are trying to find the maximum length of subarrays which is equal to (sum(nums) - x)
class Solution:
def minOperations(self, nums: List[int], x: int) -> int:
result, win_start = 0, 0
target = sum(nums) - x
curr_sum = 0
if target==0:
return len(nums)