FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

commit week3 work by aibuilder-leo · Pull Request #621 · algorithm001/algorithm · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension No extension  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
19 changes: 19 additions & 0 deletions Week_03/id_100/LeetCode_104_100
This file contains hidden or 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,19 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int maxDepth(TreeNode root) {
if(root == null){
return 0;
}
int left = maxDepth(root.left)+1;
int right = maxDepth(root.right)+1;
return Math.max(left,right);
}
}
53 changes: 53 additions & 0 deletions Week_03/id_100/LeetCode_703_100
This file contains hidden or 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,53 @@
public class KthLargest {
private int k;
private int[] nums;

public KthLargest(int k, int[] nums) {
this.nums = new int[k + 1];
this.k = k;
this.count = 0;
for (int i = 0; i < nums.length; i++) {
this.add(nums[i]);
}
}

private int count;

public int add(int val) {
if (count == k && val < nums[1]) {
return nums[1];
}

if (count >= k) {
nums[1] = val;
int i = 1;
while (2 * i <= count) {
int j = 2;
if (j < count && nums[j] > nums[j + 1]) j++;
if (nums[1] <= nums[j]) break;
swap(nums, i, j);
i = j;
}
} else {
nums[++count] = val;
while (count > 1 && nums[count] < nums[count / 2]) {
swap(nums, count, count / 2);
count = count / 2;
}
}

return nums[1];
}

private void swap(int[] a, int i, int j) {
int origin = a[j];
a[j] = a[i];
a[i] = origin;
}
}

/**
* Your KthLargest object will be instantiated and called as such:
* KthLargest obj = new KthLargest(k, nums);
* int param_1 = obj.add(val);
*/

Back | FazBrowse Home | New Git URL