#53. Maximum Subarray
Given an integer array nums, find the subarray with the largest sum, and return its sum. Example 1: Input: nums = [-2,1,-3,4,-1,2,1,-5,4] Output: 6 Explanation: The subarray [4,-1,2,1] has the largest sum 6. Example 2: Input: nums = [1] Output: 1 Explanation: The subarray [1] has the largest sum 1. Example 3: Input: nums = [5,4,-1,7,8] Output: 23 Explanation: The subarray [5,4,-1,7,8] has the la..
#49. Group Anagrams
Given an array of strings strs, group the anagrams together. You can return the answer in any order. An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. Example 1: Input: strs = ["eat","tea","tan","ate","nat","bat"] Output: [["bat"],["nat","tan"],["ate","eat","tea"]] Example 2: Input: strs = [""] O..
#46. Permutations
Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order. Example 1: Input: nums = [1,2,3] Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] Example 2: Input: nums = [0,1] Output: [[0,1],[1,0]] Example 3: Input: nums = [1] Output: [[1]] class Solution: def permute(self, nums: List[int]) -> List[List[int]]: def DFS(visited, pat..
#19. Remove Nth Node From End of List
Given the head of a linked list, remove the nth node from the end of the list and return its head. Example 1: Input: head = [1,2,3,4,5], n = 2 Output: [1,2,3,5] Example 2: Input: head = [1], n = 1 Output: [] Example 3: Input: head = [1,2], n = 1 Output: [1] # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Sol..
#15. 3Sum
Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0. Notice that the solution set must not contain duplicate triplets. Example 1: Input: nums = [-1,0,1,2,-1,-4] Output: [[-1,-1,2],[-1,0,1]] Explanation: nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0. nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) ..
#11. Container With Most Water [Two Ptr]
You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]). Find two lines that together with the x-axis form a container, such that the container contains the most water. Return the maximum amount of water a container can store. Example 1: Input: height = [1,8,6,2,5,4,8,3,7] Output: 49 Explanation..
# 17. Letter Combinations of a Phone Number [BACKTRACK, DFS]
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Return the answer in any order. A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters. Example 1: Input: digits = "23" Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"] Example 2: Input: digits = "..
#19. Remove Nth Node From End of List [Sliding window]
Given the head of a linked list, remove the nth node from the end of the list and return its head. Example 1: Input: head = [1,2,3,4,5], n = 2 Output: [1,2,3,5] Example 2: Input: head = [1], n = 1 Output: [] Example 3: Input: head = [1,2], n = 1 Output: [1] # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Sol..
Data Analysis
STATISTICS 1. REGRESSION: 연속된 독립변수와 종속변수의 관계 및 경향성을 찾는 방법 --> Least Squares (오차를 최소화하는 모델 찾기) X: [1, 2, 3, 4, 5] Y: [2, 4, 5, 4, 5] 1. FIND DEVIATION OF X and Y deviation of x = X - x̄ deviation of y = Y - ȳ deviation of x: [-2, -1, 0, 1, 2] deviation of y: [-2, 0, 1, 0, 1] 2. CALCULATE PRODUCT OF DEV deviation of x * deviation of y product of deviations: [4, 0, 0, 0, 2] 3. CALCULATE SQUARED DE..
LINEAR ALGEBRA
1. COVARIANCE MATRIX STEP 1: X: [2, 3, 5, 7, 10] Y: [6, 9, 12, 15, 18] Step 2: Calculate the Means (μx and μy): μx = (2 + 3 + 5 + 7 + 10) / 5 = 5.4 μy = (6 + 9 + 12 + 15 + 18) / 5 = 12 Step 3: Calculate the Covariance: Cov(X, Y) = Σ((xi - μx) * (yi - μy)) / (n - 1) = (20.4 + 7.2 + 0 + 4.8 + 27.6) / (5 - 1) = 59 / 4 ≈ 14.75 Cov(X, X) = Σ((xi - μx)^2) / (n - 1) = ((-3.4)^2 + (-2.4)^2 + (-0.4)^2 + ..
Algorithms
1. BINARY SEARCH def binary_search(arr, target): low = 0 high = len(arr) - 1 while low max_sum: max_sum = window_sum return max_sum # Example usage: my_list = [4, 2, 1, 7, 8, 1, 2, 8, 1, 0] window_size = 3 max_sum = sliding_window(my_list, window_size) print("Maximum sum within a window of size", window_size, ":", max_sum) 4. BRUTE FORCE def brute_force(arr, target): for i in range(len(arr)): fo..
#2 ArrayChallenge
# Have the function ArrayChallenge(strArr) read the array of strings stored in strArr, # which will contain 2 elements: the first element will be a sequence of characters, # and the second element will be a long string of comma-separated words, in alphabetical order, # that represents a dictionary of some arbitrary length. # For example: strArr can be: ["hellocat", "apple,bat,cat,goodbye,hello,y..