[LeetCode] 2441. Largest Positive Integer That Exists With Its Negative
Posted CNoodle
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[LeetCode] 2441. Largest Positive Integer That Exists With Its Negative相关的知识,希望对你有一定的参考价值。
Given an integer array nums
that does not contain any zeros, find the largest positive integer k
such that -k
also exists in the array.
Return the positive integer k
. If there is no such integer, return -1
.
Example 1:
Input: nums = [-1,2,-3,3] Output: 3 Explanation: 3 is the only valid k we can find in the array.
Example 2:
Input: nums = [-1,10,6,7,-7,1] Output: 7 Explanation: Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
Example 3:
Input: nums = [-10,8,6,7,-2,-3] Output: -1 Explanation: There is no a single valid k, we return -1.
Constraints:
1 <= nums.length <= 1000
-1000 <= nums[i] <= 1000
nums[i] != 0
与对应负数同时存在的最大正整数。
给你一个 不包含 任何零的整数数组 nums ,找出自身与对应的负数都在数组中存在的最大正整数 k 。
返回正整数 k ,如果不存在这样的整数,返回 -1 。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/largest-positive-integer-that-exists-with-its-negative
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路是 hashset。我们需要遍历 input 数组两遍,第一遍我们将所有元素都放入 hashset;第二遍遍历的时候,对于每个遇到的数字 num(有可能是负数),我们需要检查 hashset是否同时存在 -num,如果存在,则 Math.abs(num) 有可能是要找的目标值。注意最后有可能是找不到的,我们要返回 -1。
时间O(n)
空间O(n)
Java实现
class Solution public int findMaxK(int[] nums) Set<Integer> set = new HashSet<>(); for (int num : nums) set.add(num); int res = -1; for (int num : nums) if (set.contains(num) && set.contains(-num)) res = Math.max(res, Math.abs(num)); return res;
LeetCode Largest BST Subtree
原题链接在这里:https://leetcode.com/problems/largest-bst-subtree/
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest means subtree with largest number of nodes in it.
Note:
A subtree must include all of its descendants.
Here‘s an example:
10
/ 5 15
/ \ \
1 8 7
The Largest BST Subtree in this case is the highlighted one.
The return value is the subtree‘s size, which is 3.
Follow up:
Can you figure out ways to solve it with O(n) time complexity?
采用bottom-up的方法,简历新的class, 用来存储
- 当前节点为root的subtree是否是BST
- 若是,最小val 和最大val.
- size是当前subtree的大小。
然后从下到上更新,若是中间过程中size 比 res大,就更新res.
Time Complexity: O(n). 每个点不会访问超过两遍. Space: O(logn). Recursion stack space.
AC Java:
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */ 10 public class Solution { 11 public int largestBSTSubtree(TreeNode root) { 12 int [] res = {0}; 13 helper(root, res); 14 return res[0]; 15 } 16 17 private Node helper(TreeNode root, int [] res){ 18 Node cur = new Node(); 19 if(root == null){ 20 cur.isBST = true; 21 return cur; 22 } 23 Node left = helper(root.left, res); 24 Node right = helper(root.right, res); 25 if(left.isBST && root.val > left.max && right.isBST && root.val < right.min){ 26 cur.isBST = true; 27 cur.min = Math.min(root.val, left.min); 28 cur.max = Math.max(root.val, right.max); 29 cur.size = left.size + right.size + 1; 30 if(cur.size > res[0]){ 31 res[0] = cur.size; 32 } 33 } 34 return cur; 35 } 36 } 37 38 class Node{ 39 boolean isBST; 40 int min; 41 int max; 42 int size; 43 public Node(){ 44 isBST = false; 45 min = Integer.MAX_VALUE; 46 max = Integer.MIN_VALUE; 47 size = 0; 48 } 49 }
以上是关于[LeetCode] 2441. Largest Positive Integer That Exists With Its Negative的主要内容,如果未能解决你的问题,请参考以下文章
[Lintcode]184. Largest Number/[Leetcode]179. Largest Number
#Leetcode# 179. Largest Number
LeetCode Largest Divisible Subset