Permutation
Permutation Sequence public String getPermutation(int n, int k) { List<Integer> num = new ArrayList<>(); int fact = 1; for (int i = 1; i <= n; i++) { num.add(i);...
Permutation Sequence public String getPermutation(int n, int k) { List<Integer> num = new ArrayList<>(); int fact = 1; for (int i = 1; i <= n; i++) { num.add(i);...
Tenth Line i=0 while (( i++ < 10 )) do read line done < file.txt echo $line tail -n +10 file.txt | head -1 sed -n 10p file.txt AWK Predefined Variables awk divides the input into r...
Update Swap Salary UPDATE salary SET sex = CASE sex WHEN 'm' THEN 'f' ELSE 'm' END; UPDATE salary SET sex = IF(sex='m', 'f', 'm'); UPDATE salary SET sex = CHAR(ASCII('...
Trie Index Pairs of a String public int[][] indexPairs(String text, String[] words) { TrieNode root = new TrieNode(); for (String w : words) { insert(root, w); } List<...
Best Time to Buy and Sell Stock with Transaction Fee stateDiagram-v2 S0 --> S1: buy S1 --> S0: sell S0 --> S0: rest S1 --> S1: rest public int maxProfit(int[] prices,...
Minimum Path Sum public int minPathSum(int[][] grid) { int m = grid.length, n = grid[0].length; int[][] dp = new int[m][n]; dp[m - 1][n - 1] = grid[m - 1][n - 1]; for (int j = n ...
Best Time to Buy and Sell Stock IV int maxProfit(int k, vector<int>& prices) { int n = prices.size(); // We can make maximum number of transactions if (k >= n / 2) { ...
K-SUM 2 Sum Two Sum One pass, Set (Map): O(n) Two Sum II - Input array is sorted Two pointers: O(n) Two Sum Less Than K Binary Search Count the Number of Fair Pairs public long countFairPa...
Best Time to Buy and Sell Stock II int maxProfit(vector<int>& prices) { int profit = 0; for (int i = 1; i < prices.size(); i++) { // Buys a stock at a valley and sells...
Kadane’s algorithm Maximum subarray problem Maximum Subarray public int maxSubArray(int[] nums) { int max = nums[0], currSum = nums[0]; for (int i = 1; i < nums.length; i++) { ...