leetcode practice problem solutions

parents
# Default ignored files
/shelf/
/workspace.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="leetcode" />
</profile>
</annotationProcessing>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://repo.maven.apache.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="corretto-1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>leetcode</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
\ No newline at end of file
import java.util.*;
// LeetCode: 2248
public class ArrayIntersection {
public List<Integer> solution(int[][] nums) {
List<Integer> result = new ArrayList<>();
int lengthOfNums = nums.length;
Map<Integer, Integer> myMap = new HashMap<>();
for (int i = 0; i < lengthOfNums; i++) {
for (int j = 0; j < nums[i].length; j++) {
if(!myMap.containsKey(nums[i][j])){
myMap.put(nums[i][j], 1);
}
else {
myMap.put(nums[i][j], myMap.get(nums[i][j])+1);
}
}
}
for(int key: myMap.keySet()) {
int mapKeyValue = myMap.get(key);
if (mapKeyValue == lengthOfNums) {
result.add(key);
}
}
result.sort(Comparator.naturalOrder());
return result;
}
public static void main(String[] args) {
int[][] nums = {{7,34,45,10,12,27,13},{27,21,45,10,12,13}};
System.out.println(nums[1].length);
ArrayIntersection myArray = new ArrayIntersection();
System.out.println(myArray.solution(nums));
}
}
// LeetCode: 2239
public class ClosestToZero {
public int solution(int[] nums) {
int min = Integer.MAX_VALUE;
int number = 0;
for(int x: nums) {
if(x == 0) {
return 0;
}
else if (Math.abs(x) < Math.abs(min)){ // min = -4, x = 1
min = x;
}
else if(Math.abs(x) == Math.abs(min)) {
min = Math.max(x, min);
}
}
return min;
}
public static void main(String[] args) {
int[] nums = {-4,-2,2,4,8};
ClosestToZero zero = new ClosestToZero();
System.out.println(zero.solution(nums));
}
}
import java.util.HashMap;
import java.util.Map;
//LeetCode 2341
public class MaxPairs {
public int[] solution(int[] nums) {
int remainder = 0;
int pairs = 0;
Map<Integer, Integer> myMap = new HashMap<>();
for(int x: nums) {
if (myMap.get(x) == null) {
myMap.put(x,1);
}
else{
int currentValue = myMap.get(x);
myMap.put(x, currentValue + 1);
}
}
for(int key: myMap.keySet()) {
int p = (int) myMap.get(key) / 2;
int r = myMap.get(key) % 2;
remainder += r;
pairs += p;
}
int[] ans = {pairs, remainder};
return ans;
}
public static void main(String[] args) {
MaxPairs maxPairs = new MaxPairs();
int[] arr = {1,3,2,1,3,2,2};
int[] ans = maxPairs.solution(arr);
System.out.println(maxPairs.solution(arr));
}
}
// LeetCode: 2357
public class ZeroArray {
public int sums(int[] nums) {
int sum = 0;
for(int x: nums) {
sum = sum + x;
}
return sum;
}
public int findMin(int[] nums) {
int min = Integer.MAX_VALUE;
for(int x: nums) {
if (x < min && x != 0) {
min = x;
}
}
return min;
}
public int solution(int[] nums) {
int count = 0;
int sum = sums(nums);
while(sum > 0) {
int min = findMin(nums);
for (int i = 0; i < nums.length; i++) {
if(nums[i] != 0) {
nums[i] = nums[i] - min;
}
}
sum = sums(nums);
count ++;
}
return count;
}
public static void main(String[] args) {
ZeroArray zero = new ZeroArray();
int[] myNums = {1,5,0,3,5};
System.out.println(zero.solution(myNums));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment