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<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]) { |
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--) { |
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]; |
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<n; j++) { | |
if(i==j) continue; // skipping in case if the indices are same | |
prod *=nums[j]; | |
} |
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; | |
} |
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); | |
} |
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<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]); | |
} |
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<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 |
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); |
NewerOlder