This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
NewerOlder