Skip to content

2024-05-10 v. 5.6.5: added "2395. Find Subarrays With Equal Sum" #622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -425,3 +425,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 2379. Minimum Recolors to Get K Consecutive Black Blocks | [Link](https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/) | [Link](./lib/easy/2379_minimum_recolors_to_get_k_consecutive_black_blocks.rb) |
| 2383. Minimum Hours of Training to Win a Competition | [Link](https://leetcode.com/problems/minimum-hours-of-training-to-win-a-competition/) | [Link](./lib/easy/2383_minimum_hours_of_training_to_win_a_competition.rb) |
| 2389. Longest Subsequence With Limited Sum | [Link](https://leetcode.com/problems/longest-subsequence-with-limited-sum/) | [Link](./lib/easy/2389_longest_subsequence_with_limited_sum.rb) |
| 2395. Find Subarrays With Equal Sum | [Link](https://leetcode.com/problems/find-subarrays-with-equal-sum/) | [Link](./lib/easy/2395_find_subarrays_with_equal_sum.rb) |
2 changes: 1 addition & 1 deletion leetcode-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require 'English'
::Gem::Specification.new do |s|
s.required_ruby_version = '>= 3.0'
s.name = 'leetcode-ruby'
s.version = '5.6.4'
s.version = '5.6.5'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[bin/leetcode-ruby README.md LICENSE]
s.executable = 'leetcode-ruby'
Expand Down
20 changes: 20 additions & 0 deletions lib/easy/2395_find_subarrays_with_equal_sum.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

# https://leetcode.com/problems/find-subarrays-with-equal-sum/
# @param {Integer[]} nums
# @return {Boolean}
def find_subarrays(nums)
sums = ::Set.new
prev = nums.first
(1...nums.length).each do |i|
num = nums[i]
sum = num + prev

return true if sums.include?(sum)

sums << sum
prev = num
end

false
end
13 changes: 13 additions & 0 deletions test/easy/test_2395_find_subarrays_with_equal_sum.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

require_relative '../test_helper'
require_relative '../../lib/easy/2395_find_subarrays_with_equal_sum'
require 'minitest/autorun'

class FindSubarraysWithEqualSumTest < ::Minitest::Test
def test_default
assert(find_subarrays([4, 2, 4]))
assert(!find_subarrays([1, 2, 3, 4, 5]))
assert(find_subarrays([0, 0, 0]))
end
end
Loading