-
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.
- Loading branch information
1 parent
88962d0
commit a10e67e
Showing
4 changed files
with
161 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,99 @@ | ||
//{ Driver Code Starts | ||
import java.io.*; | ||
import java.util.*; | ||
|
||
|
||
class IntArray | ||
{ | ||
public static int[] input(BufferedReader br, int n) throws IOException | ||
{ | ||
String[] s = br.readLine().trim().split(" "); | ||
int[] a = new int[n]; | ||
for(int i = 0; i < n; i++) | ||
a[i] = Integer.parseInt(s[i]); | ||
|
||
return a; | ||
} | ||
|
||
public static void print(int[] a) | ||
{ | ||
for(int e : a) | ||
System.out.print(e + " "); | ||
System.out.println(); | ||
} | ||
|
||
public static void print(ArrayList<Integer> a) | ||
{ | ||
for(int e : a) | ||
System.out.print(e + " "); | ||
System.out.println(); | ||
} | ||
} | ||
|
||
class GFG { | ||
public static void main(String[] args) throws IOException { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
int t; | ||
t = Integer.parseInt(br.readLine()); | ||
while(t-- > 0){ | ||
|
||
int n; | ||
n = Integer.parseInt(br.readLine()); | ||
|
||
|
||
int k; | ||
k = Integer.parseInt(br.readLine()); | ||
|
||
|
||
int[] arr = IntArray.input(br, n); | ||
|
||
Solution obj = new Solution(); | ||
int res = obj.minimizeDifference(n, k, arr); | ||
|
||
System.out.println(res); | ||
|
||
} | ||
} | ||
} | ||
|
||
// } Driver Code Ends | ||
|
||
|
||
|
||
class Solution | ||
{ | ||
public static int minimizeDifference(int n, int k, int[] arr) | ||
{ | ||
// code here | ||
int[] postMax = new int[n]; | ||
int[] postMin = new int[n]; | ||
|
||
postMin[n - 1] = arr[n - 1]; | ||
postMax[n - 1] = arr[n - 1]; | ||
|
||
for (int i = n - 2; i >= 0; --i) | ||
{ | ||
postMax[i] = Math.max(arr[i], postMax[i + 1]); | ||
postMin[i] = Math.min(arr[i], postMin[i + 1]); | ||
} | ||
|
||
int minDiff = postMax[k] - postMin[k]; | ||
|
||
int pMin = arr[0]; | ||
int pMax = arr[0]; | ||
|
||
for (int i = 1; i < n - k; ++i) | ||
{ | ||
int currMin = Math.max(pMax, postMax[i + k]) - Math.min(pMin, postMin[i + k]); | ||
|
||
minDiff = Math.min(minDiff, currMin); | ||
pMax = Math.max(arr[i], pMax); | ||
pMin = Math.min(arr[i], pMin); | ||
} | ||
|
||
minDiff = Math.min(minDiff, pMax - pMin); | ||
|
||
return minDiff; | ||
} | ||
} | ||
|
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 @@ | ||
Time complexity - O(n) | ||
Space complexity - O(n) |
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 @@ | ||
Time complexity - O(n) | ||
Space complexity - O(n) |
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 @@ | ||
/** | ||
* Definition for a binary tree node. | ||
* public class TreeNode { | ||
* int val; | ||
* TreeNode left; | ||
* TreeNode right; | ||
* TreeNode() {} | ||
* TreeNode(int val) { this.val = val; } | ||
* TreeNode(int val, TreeNode left, TreeNode right) { | ||
* this.val = val; | ||
* this.left = left; | ||
* this.right = right; | ||
* } | ||
* } | ||
*/ | ||
class Solution | ||
{ | ||
public TreeNode addOneRow(TreeNode root, int val, int depth) | ||
{ | ||
if (depth == 1) | ||
{ | ||
TreeNode newRoot = new TreeNode(val); | ||
newRoot.left = root; | ||
return newRoot; | ||
} | ||
|
||
int d = 0; | ||
Queue<TreeNode> q = new ArrayDeque<>(Arrays.asList(root)); | ||
|
||
while (!q.isEmpty()) | ||
{ | ||
++d; | ||
for (int sz = q.size(); sz > 0; --sz) | ||
{ | ||
TreeNode node = q.poll(); | ||
if (node.left != null) | ||
q.offer(node.left); | ||
|
||
if (node.right != null) | ||
q.offer(node.right); | ||
|
||
if (d == depth - 1) | ||
{ | ||
TreeNode cachedLeft = node.left; | ||
TreeNode cachedRight = node.right; | ||
node.left = new TreeNode(val); | ||
node.right = new TreeNode(val); | ||
node.left.left = cachedLeft; | ||
node.right.right = cachedRight; | ||
} | ||
} | ||
if (d == depth - 1) | ||
break; | ||
} | ||
|
||
return root; | ||
} | ||
} |