-
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
a76dca1
commit 2ab7181
Showing
4 changed files
with
93 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,51 @@ | ||
//{ Driver Code Starts | ||
import java.io.*; | ||
import java.lang.*; | ||
import java.util.*; | ||
|
||
class GFG { | ||
public static void main(String[] args) throws IOException { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
int T = Integer.parseInt(br.readLine().trim()); | ||
while (T-- > 0) { | ||
String s = br.readLine().trim(); | ||
int n = Integer.parseInt(s); | ||
String S = br.readLine(); | ||
String[] s1 = S.split(" "); | ||
List<Integer> a = new ArrayList<Integer>(); | ||
for (int i = 0; i < n; i++) { | ||
a.add(Integer.parseInt(s1[i])); | ||
} | ||
Solution ob = new Solution(); | ||
int ans = ob.findPeakElement(a); | ||
System.out.println(ans); | ||
} | ||
} | ||
} | ||
|
||
// } Driver Code Ends | ||
|
||
|
||
class Solution { | ||
public int findPeakElement(List<Integer> a) { | ||
// Code here | ||
|
||
// Collections.sort(a); | ||
// return a.get(a.size()-1); | ||
|
||
int left = 0; | ||
int right = a.size() - 1; | ||
|
||
while (left < right) | ||
{ | ||
int mid = left + (right - left) / 2; | ||
|
||
if (a.get(mid) > a.get(right)) | ||
right = mid; | ||
else | ||
left = mid + 1; | ||
} | ||
|
||
return a.get(left); | ||
} | ||
} |
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(logn) | ||
Space complexity - O(1) |
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(h) |
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 @@ | ||
/** | ||
* 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 int distributeCoins(TreeNode root) | ||
{ | ||
dfs(root); | ||
return ans; | ||
} | ||
|
||
private int ans = 0; | ||
|
||
// Returns the number of coins I can give (positive) / take (negative). | ||
private int dfs(TreeNode root) | ||
{ | ||
if (root == null) | ||
return 0; | ||
|
||
int l = dfs(root.left); | ||
int r = dfs(root.right); | ||
ans += Math.abs(l) + Math.abs(r); | ||
|
||
return (root.val - 1) + l + r; | ||
} | ||
} |