Instantly share code, notes, and snippets.

🚩
Be better than yesterday

# Vamsi Tallapudi vamsitallapudi

🚩
Be better than yesterday
Last active April 22, 2024 13:59
3 Sum Problem Solution | 100 Days of Code Day 10
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 { public List> threeSum(int[] nums) { List> sol = new ArrayList<>(); // To sort the values as we will consider skipping them if same, to avoid duplicates Arrays.sort(nums); for(int i = 0; i< nums.length-2; i++) { if(nums[i] > 0) { break; } if(i>0 && nums[i] == nums[i-1]) {
Created April 1, 2024 13:06
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 { public int[] productExceptSelf(int[] nums) { int n = nums.length; int[] ans = new int[n]; Arrays.fill(ans, 1); for(int i = 1; i< n; i++) { ans[i] = ans[i-1] *nums[i-1]; // calculating prefix } int curr = 1; for(int j = n-1; j>=0; j--) {
Created April 1, 2024 12:34
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 { public int[] productExceptSelf(int[] nums) { int n = nums.length; int[] ans = new int[n]; int prod = 1; for(int i = 0; i< n;i++) { prod*=nums[i]; // product of all numbers in an array } for(int i = 0; i< n; i++) { ans[i] = prod/nums[i];
Last active April 1, 2024 12:15
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 { public int[] productExceptSelf(int[] nums) { int n = nums.length; int[] ans = new int[n]; for(int i = 0; i< n; i++) { int prod = 1; for(int j = 0; j
Created March 12, 2024 06:27
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 { public int maxSubArray(int[] nums) { int currMax = nums[0]; int overallMax = nums[0]; for(int i = 1; i< nums.length;i++) { currMax = Math.max(nums[i], currMax+nums[i]); overallMax = Math.max(currMax, overallMax); } return overallMax; }
Created January 2, 2024 18:10
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 { public int maxProfit(int[] prices) { int profit =0; int min = Integer.MAX_VALUE; for(int i = 0; i< prices.length;i++) { if(prices[i] < min) { min = prices[i]; } profit = Math.max(profit, prices[i] - min); }
Created January 2, 2024 10:02
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 { public boolean containsDuplicate(int[] nums) { // Using set because it can retreive values in O(1) time. Set set = new HashSet<>(); for(int i = 0; i< nums.length; i++) { if (set.contains(nums[i])) { return true; // found duplicate } else { set.add(nums[i]); }
Last active January 2, 2024 09:13
Solution to Two Sum problem using HashMap
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 { public int[] twoSum(int[] nums, int target) { // Hashmap to store remainder as key and current index as value Map map = new HashMap<>(); for(int i = 0; i < nums.length; i++) { if(map.containsKey(nums[i])) { // checks if map already contains value return new int[]{i, map.get(nums[i])}; } else { // putting remainder and current index in map
Last active October 3, 2023 09:16 — forked from krishnadey30/LeetCodeQuestions.md
Curated List of Top 75 LeetCode

## Array

Created August 26, 2023 12:47
Best Time to Buy and Sell Stock - Leetcode blind 75 #2
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 { public int maxProfit(int[] prices) { int maxProfit = 0; int minVal = Integer.MAX_VALUE; for(int i = 0; i< prices.length;i++) { if(prices[i] < minVal) { minVal = prices[i]; } maxProfit = Math.max(maxProfit, prices[i] - minVal);