Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View vamsitallapudi's full-sized avatar
🚩
Be better than yesterday

Vamsi Tallapudi vamsitallapudi

🚩
Be better than yesterday
View GitHub Profile
@vamsitallapudi
vamsitallapudi / 3Sum.java
Last active April 22, 2024 13:59
3 Sum Problem Solution | 100 Days of Code Day 10
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> 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]) {
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--) {
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];
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<n; j++) {
if(i==j) continue; // skipping in case if the indices are same
prod *=nums[j];
}
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;
}
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);
}
class Solution {
public boolean containsDuplicate(int[] nums) {
// Using set because it can retreive values in O(1) time.
Set<Integer> 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]);
}
@vamsitallapudi
vamsitallapudi / TwoSum.java
Last active January 2, 2024 09:13
Solution to Two Sum problem using HashMap
class Solution {
public int[] twoSum(int[] nums, int target) {
// Hashmap to store remainder as key and current index as value
Map<Integer, Integer> 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
@vamsitallapudi
vamsitallapudi / BestTimeToBuySellStock.java
Created August 26, 2023 12:47
Best Time to Buy and Sell Stock - Leetcode blind 75 #2
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);