diff --git a/#442/java/hashmap/.DS_Store b/#442/java/hashmap/.DS_Store new file mode 100644 index 0000000..fd11fde Binary files /dev/null and b/#442/java/hashmap/.DS_Store differ diff --git a/#442/java/hashmap/Solution.java b/#442/java/hashmap/Solution.java new file mode 100644 index 0000000..f65ae06 --- /dev/null +++ b/#442/java/hashmap/Solution.java @@ -0,0 +1,69 @@ +package com.jagjit.Hactoberfest; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + + +/** + * https://leetcode.com/problems/find-all-duplicates-in-an-array/ + * runtime beats 9.32% of Java submissions + * memory usage beats 36.11% of Java submissions + */ + + +public class FindDuplicates { + + // function that return the value + private static List findDuplicates(int[] nums) { + + List result = new ArrayList(); + + // initialising the hashmap + int count = 0; + Map map = new HashMap(); + + //iterates through every element + for (int i = 0; i < nums.length; i++) { + + //if map contains duplicate value then add to result list + if (map.containsKey(nums[i])) { + result.add(nums[i]); + } + //putting the value in the map + map.put(nums[i], count++); + } + + //return the final list + return result; + + } + + public static void main(String[] args) { + + // allocating memory for 5 integers. + int[] nums = new int[8]; + + // initialize the first elements of the array + nums[0] = 4; + nums[1] = 3; + nums[2] = 2; + nums[3] = 7; + nums[4] = 8; + nums[5] = 2; + nums[6] = 3; + nums[7] = 1; + + List result = findDuplicates(nums); + + for (Integer i : result) { + + //print the value of duplicates value + System.out.print(i + " "); + + } + + } + +} diff --git a/#70/java/dynamic_programming/Solution.java b/#70/java/dynamic_programming/Solution.java new file mode 100644 index 0000000..044d512 --- /dev/null +++ b/#70/java/dynamic_programming/Solution.java @@ -0,0 +1,24 @@ +package com.jagjit.Hactoberfest; + +/** + * https://leetcode.com/problems/climbing-stairs/ Runtime: 0 ms, faster than + * 100.00% of Java online submissions for Climbing Stairs. Memory Usage: 33.2 + * MB, less than 5.26% of Java online submissions for Climbing Stairs. + */ +public class Solution { + + // below the function which returns count + public int climbStairs(int n) { + int[] dp = new int[n]; + if (n < 2) { + return 1; + } + dp[0] = 1; + dp[1] = 2; + for (int i = 2; i < n; i++) { + dp[i] = dp[i - 1] + dp[i - 2]; + } + return dp[n - 1]; + } + +}