Skip to content

Commit

Permalink
Added codes for 18 May
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed May 18, 2024
1 parent a76dca1 commit 2ab7181
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
51 changes: 51 additions & 0 deletions GeeksForGeeks/May/18-5-24/GFG.java
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);
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/May/18-5-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(logn)
Space complexity - O(1)
2 changes: 2 additions & 0 deletions LeetCode/May/18-5-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(h)
38 changes: 38 additions & 0 deletions LeetCode/May/18-5-24/Solution.java
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;
}
}

0 comments on commit 2ab7181

Please sign in to comment.