-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add leetcode problem on sorting. Level easy
- Loading branch information
1 parent
b0682c1
commit 424af87
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
JavaDsaWithTest/src/main/java/org/practice/dsa/leet_code/easy/sorting/MaxProduct.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.practice.dsa.leet_code.easy.sorting; | ||
|
||
// https://leetcode.com/problems/maximum-product-of-three-numbers/description/ | ||
public class MaxProduct { | ||
public static void main(String[] args) { | ||
int[] nums = {1,2,4}; | ||
System.out.println(maximumProduct(nums)); | ||
} | ||
|
||
public static int maximumProduct(int[] nums) { | ||
sorting(nums); | ||
int product1 = 1; | ||
for (int i = nums.length-1; i >= nums.length-3; i--) { | ||
product1 = product1 * nums[i]; | ||
} | ||
int n = nums.length; | ||
int product2 = nums[0] * nums[1] * nums[n-1]; | ||
return Math.max(product1, product2); | ||
} | ||
|
||
public static void sorting(int[] nums) { | ||
boolean isSwapped; | ||
|
||
for (int i = 0; i < nums.length-1; i++) { | ||
isSwapped = false; | ||
for (int j = 1; j < nums.length - i; j++) { | ||
if (nums[j] < nums[j-1]) { | ||
swap(nums, j, j-1); | ||
isSwapped = true; | ||
} | ||
} | ||
if(!isSwapped) { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
private static void swap(int[] nums, int i, int j) { | ||
int temp = nums[i]; | ||
nums[i] = nums[j]; | ||
nums[j] = temp; | ||
} | ||
} |