-
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.
Merge pull request #6 from terodea/pepcoding
Array problems java and scala
- Loading branch information
Showing
10 changed files
with
273 additions
and
0 deletions.
There are no files selected for viewing
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,2 @@ | ||
*.metals | ||
*.vscode |
34 changes: 34 additions & 0 deletions
34
pepcoding/java/L2/ArrayStrings/maxChunksToMakeArraySortedII.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,34 @@ | ||
import java.util.*; | ||
public class maxChunksToMakeArraySortedII { | ||
|
||
// ~~~~~~~~~~~~~~~User Section~~~~~~~~~~~~~~~~~ | ||
public static int maxChunksToSorted2(int[] arr) { | ||
int count = 0; | ||
int[] rightMin = new int[arr.length+1]; | ||
int n = arr.length; | ||
rightMin[n] = Integer.MAX_VALUE; | ||
|
||
for(int i= n-1; i>=0; i--){ | ||
rightMin[i] = Math.min(rightMin[i+1], arr[i]); | ||
} | ||
|
||
int leftMax = Integer.MIN_VALUE; | ||
|
||
for(int i=0; i<n; i++){ | ||
|
||
leftMax = Integer.max(arr[i], leftMax); | ||
|
||
if(leftMax <= rightMin[i+1]) { | ||
count++; | ||
} | ||
} | ||
return count; | ||
} | ||
|
||
// ~~~~~~~~~~~~~~~Input Management~~~~~~~~~~~~~~~~~ | ||
public static void main(String[] args) { | ||
int[] arr = {4,3,6,5,7,12,11,10}; | ||
int res = maxChunksToSorted2(arr); | ||
System.out.println(res); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
pepcoding/java/L2/ArrayStrings/noOfSubArraysWithBoundedMax.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,33 @@ | ||
public class noOfSubArraysWithBoundedMax{ | ||
public static int numSubarrayBoundedMax(int[] arr, int left, int right) { | ||
int prev_count = 0; | ||
int overall_count = 0; | ||
int i=0; | ||
int j=0; | ||
while(j < arr.length){ | ||
|
||
if(left <= arr[j] && arr[j] <= right){ | ||
|
||
prev_count = j - i + 1; | ||
overall_count += prev_count; | ||
|
||
}else if(arr[j] < left){ | ||
|
||
overall_count += prev_count; | ||
|
||
}else{ | ||
prev_count = 0; | ||
i = j + 1; | ||
} | ||
j++; | ||
} | ||
return overall_count; | ||
} | ||
|
||
public static void main(String[] args){ | ||
int[] arr = {2,1,4,3}; | ||
int left = 2; | ||
int right= 3; | ||
System.out.println(numSubarrayBoundedMax(arr, left, right)); | ||
} | ||
} |
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,38 @@ | ||
public class reverseVowelsOfString{ | ||
private static boolean isVowel(char ch){ | ||
String vowels = "aeiouAEIOU"; | ||
return vowels.contains(ch + ""); | ||
} | ||
|
||
public static String reverseVowels(String s){ | ||
|
||
char[] arr = s.toCharArray(); | ||
int left = 0; | ||
int right = arr.length - 1; | ||
|
||
while(left < right){ | ||
|
||
while(left < right && isVowel(arr[left]) == false){ | ||
left++; | ||
} | ||
|
||
while(left < right && isVowel(arr[right]) == false){ | ||
right--; | ||
} | ||
|
||
char temp = arr[left]; | ||
arr[left] = arr[right]; | ||
arr[right] = temp; | ||
|
||
left++; | ||
right--; | ||
} | ||
|
||
return String.valueOf(arr); | ||
} | ||
|
||
public static void main(String[] args){ | ||
String str = "hello"; | ||
System.out.println(reverseVowels(str)); | ||
} | ||
} |
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,27 @@ | ||
public class wiggleSort{ | ||
public static void wiggleSort(int[] arr) { | ||
for(int i=0; i<arr.length - 1; i++){ | ||
if(i % 2 == 0){ | ||
if(arr[i] > arr[i+1]){ | ||
swap(arr, i, i+1); | ||
} | ||
} | ||
else{ | ||
if(arr[i] < arr[i+1]){ | ||
swap(arr, i, i+1); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private static void swap(int[] arr, int i, int j){ | ||
int temp = arr[i]; | ||
arr[i] = arr[j]; | ||
arr[j] = temp; | ||
} | ||
|
||
public static void main(String[] args){ | ||
int[] arr = {3,5,2,1,6,4}; | ||
wiggleSort(arr).toString() | ||
} | ||
} |
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
26 changes: 26 additions & 0 deletions
26
pepcoding/scala/L2/ArrayStrings/maxChunksToMakeArraySortedII.scala
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,26 @@ | ||
object Solution { | ||
def maxChunksToSorted(arr: Array[Int]): Int = { | ||
var count=0; | ||
var rightMin:Array[Int] = new Array[Int](arr.size+1) | ||
var n = arr.size | ||
rightMin(n) = Int.MaxValue | ||
for (i <- (1 to n-1).reverse){ | ||
rightMin(i) = Math.min(rightMin(i+1), arr(i)) | ||
} | ||
|
||
var leftMax = -1 | ||
|
||
for(i <- 0 to n-1){ | ||
leftMax = (arr(i)).max(leftMax) | ||
if(leftMax <= rightMin(i+1)){ | ||
count = count + 1; | ||
} | ||
} | ||
count | ||
} | ||
|
||
def main(args: Array[String]): Int{ | ||
var data: Array[Int] = Array(4, 3, 2, 1, 0) | ||
maxChunksToSorted(arr=data) | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
pepcoding/scala/L2/ArrayStrings/noOfSubArraysWithBoundedMax.scala
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,27 @@ | ||
object noOfSubArraysWithBoundedMax{ | ||
def numSubarrayBoundedMax(arr: Array[Int], left: Int, right: Int): Int = { | ||
var prev_count=0 | ||
var overall_count=0 | ||
var i=0 | ||
var j=0 | ||
|
||
while(j < arr.size){ | ||
if(left <= arr(j) && arr(j) <= right){ | ||
prev_count = j-i+1 | ||
overall_count += prev_count | ||
}else if(arr(j) < left){ | ||
overall_count += prev_count | ||
}else{ | ||
prev_count=0 | ||
i=j+1 | ||
} | ||
j+=1 | ||
} | ||
overall_count | ||
} | ||
|
||
def main(args: Array[String]): Unit = { | ||
var arr = Array(2,1,4,3) | ||
print(numSubarrayBoundedMax(arr, 2, 3)) | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
pepcoding/scala/L2/ArrayStrings/reverseVowelsOfString.scala
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,58 @@ | ||
object reverseVowelsOfString{ | ||
|
||
val isVowel2 = Set('a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U') | ||
|
||
def reverseVowels2(s: String): String = { | ||
val a = s.toArray | ||
@tailrec | ||
def reverse(i: Int = 0, j: Int = a.length - 1): Unit = { | ||
if (i < j) { | ||
if (!isVowel2(a(i))) reverse(i+1, j) | ||
else if (!isVowel2(a(j))) reverse(i, j-1) | ||
else { | ||
val tmp = a(i) | ||
a(i) = a(j) | ||
a(j) = tmp | ||
reverse(i+1, j-1) | ||
} | ||
} | ||
} | ||
reverse() | ||
a.mkString | ||
} | ||
|
||
|
||
def isVowel(ch: Char): Boolean = { | ||
String vowels = "aeiouAEIOU" | ||
return vowels.contains(ch + "") | ||
} | ||
|
||
def reverseVowels(s: String): String = { | ||
var arr = s.toCharArray() | ||
var left = 0 | ||
var right = arr.size - 1 | ||
|
||
while(left < right){ | ||
|
||
while(left < right && isVowel(arr(left) == false)){ | ||
left += 1 | ||
} | ||
while(left < right && isVowel(arr(right) == false)){ | ||
right -= 1 | ||
} | ||
|
||
var temp = arr(left) | ||
arr(left) = arr(right) | ||
arr(right) = temp | ||
|
||
left += 1 | ||
right -=1 | ||
} | ||
String.valueOf(arr) | ||
} | ||
|
||
def main(args: Array[String]): Unit = { | ||
var s = "hello" | ||
print(reverseVowels(s)) | ||
} | ||
} |
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,27 @@ | ||
object wiggleSort{ | ||
def wiggleSort(arr: Array[Int]): Unit = { | ||
for(i <- 0 to arr.size - 1){ | ||
if(i % 2 == 0){ | ||
if(arr(i) > arr(i+1)){ | ||
swap(arr, i, i+1) | ||
} | ||
} | ||
else{ | ||
if(arr(i) < arr(i+1)){ | ||
swap(arr, i, i+1) | ||
} | ||
} | ||
} | ||
} | ||
|
||
def swap(arr: Array[Int], i:Int, j:Int): Unit = { | ||
var temp = arr(i) | ||
arr(i) = arr(j) | ||
arr(j) = temp | ||
} | ||
|
||
def main(args: Array[String]): Unit={ | ||
var arr = Array(3,5,2,1,6,4) | ||
print(wiggleSort(arr)) | ||
} | ||
} |