Skip to content

Commit 534829c

Browse files
committed
1498. Number of Subsequences That Satisfy the Given Sum Condition
1 parent 4c94420 commit 534829c

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@
166166
| 1480 | Running Sum of 1d Array | [Ruby](./algorithms/ruby/1480-running-sum-of-1d-array.rb) | Easy |
167167
| 1491 | Average Salary Excluding the Minimum and Maximum Salary | [Ruby](./algorithms/ruby/1491-average-salary-excluding-the-minimum-and-maximum-salary.rb) | Easy |
168168
| 1493 | Longest Subarray of 1's After Deleting One Element | [Ruby](./algorithms/ruby/1493-longest-subarray-of-1s-after-deleting-one-element.rb) | Medium |
169+
| 1498 | Number of Subsequences That Satisfy the Given Sum Condition | [Ruby](./algorithms/ruby/1498-number-of-subsequences-that-satisfy-the-given-sum-condition.rb) | Medium |
169170
| 1523 | Count Odd Numbers in an Interval Range | [Ruby](./algorithms/ruby/1523-count-odd-numbers-in-an-interval-range.rb) | Easy |
170171
| 1539 | Kth Missing Positive Number | [Ruby](./algorithms/ruby/1539-kth-missing-positive-number.rb) | Easy |
171172
| 1579 | Remove Max Number of Edges to Keep Graph Fully Traversable | [Ruby](./algorithms/ruby/1579-remove-max-number-of-edges-to-keep-graph-fully-traversable.rb) | Hard |
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# frozen_string_literal: true
2+
3+
# 1498. Number of Subsequences That Satisfy the Given Sum Condition
4+
# https://leetcode.com/problems/number-of-subsequences-that-satisfy-the-given-sum-condition
5+
# Medium
6+
7+
=begin
8+
You are given an array of integers nums and an integer target.
9+
10+
Return the number of non-empty subsequences of nums such that the sum of the minimum and maximum element on it is less or equal to target. Since the answer may be too large, return it modulo 109 + 7.
11+
12+
Example 1:
13+
Input: nums = [3,5,6,7], target = 9
14+
Output: 4
15+
Explanation: There are 4 subsequences that satisfy the condition.
16+
[3] -> Min value + max value <= target (3 + 3 <= 9)
17+
[3,5] -> (3 + 5 <= 9)
18+
[3,5,6] -> (3 + 6 <= 9)
19+
[3,6] -> (3 + 6 <= 9)
20+
21+
Example 2:
22+
Input: nums = [3,3,6,8], target = 10
23+
Output: 6
24+
Explanation: There are 6 subsequences that satisfy the condition. (nums can have repeated numbers).
25+
[3] , [3] , [3,3], [3,6] , [3,6] , [3,3,6]
26+
27+
Example 3:
28+
Input: nums = [2,3,3,4,6,7], target = 12
29+
Output: 61
30+
Explanation: There are 63 non-empty subsequences, two of them do not satisfy the condition ([6,7], [7]).
31+
Number of valid subsequences (63 - 2 = 61).
32+
33+
Constraints:
34+
1 <= nums.length <= 105
35+
1 <= nums[i] <= 106
36+
1 <= target <= 106
37+
=end
38+
39+
# @param {Integer[]} nums
40+
# @param {Integer} target
41+
# @return {Integer}
42+
def num_subseq(nums, target)
43+
nums.sort!
44+
45+
result = 0
46+
47+
nums.each_with_index do |num, index|
48+
break if (num * 2) > target
49+
50+
max_index = (nums.bsearch_index { |num_searched| num_searched > (target - num) } || nums.size) - 1
51+
result += 2**(max_index - index)
52+
end
53+
54+
result % (10**9 + 7)
55+
end
56+
57+
# **************** #
58+
# TEST #
59+
# **************** #
60+
61+
require "test/unit"
62+
class Test_num_subseq < Test::Unit::TestCase
63+
def test_
64+
assert_equal 4, num_subseq([3, 5, 6, 7], 9)
65+
assert_equal 6, num_subseq([3, 3, 6, 8], 10)
66+
assert_equal 61, num_subseq([2, 3, 3, 4, 6, 7], 12)
67+
end
68+
end

0 commit comments

Comments
 (0)