Skip to content
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
| 605 | Design Circular Queue | [Ruby](./algorithms/ruby/0605-can-place-flowers.rb) | Easy |
| 622 | Design Circular Queue | [Ruby](./algorithms/ruby/0622-design-circular-queue.rb) | Medium |
| 652 | Find Duplicate Subtrees | [Ruby](./algorithms/ruby/0652-find-duplicate-subtrees.rb) | Medium |
| 724 | Find Pivot Index | [Ruby](./algorithms/ruby/0724-find-pivot-index.rb) | Easy |
| 733 | Flood Fill | [Ruby](./algorithms/ruby/0733-flood-fill.rb) | Easy |
| 783 | Minimum Distance Between BST Nodes | [Ruby](./algorithms/ruby/0783-minimum-distance-between-bst-nodes.rb) [Python3](./algorithms/python3/0783-minimum-distance-between-bst-nodes.py) | Easy |
| 875 | Koko Eating Bananas | [Ruby](./algorithms/ruby/0875-koko-eating-bananas.rb) [Python3](./algorithms/python3/0875-koko-eating-bananas.py) | Medium |
Expand All @@ -76,6 +77,7 @@
| 1345 | Jump Game IV | [Ruby](./algorithms/ruby/1345-jump-game-iv.rb) | Hard |
| 1470 | Shuffle the Array | [Ruby](./algorithms/ruby/1470-shuffle-the-array.rb) | Easy |
| 1472 | Design Browser History | [Ruby](./algorithms/ruby/1472-design-browser-history.rb) | Medium |
| 1480 | Running Sum of 1d Array | [Ruby](./algorithms/ruby/1480-running-sum-of-1d-array.rb) | Easy |
| 1523 | Count Odd Numbers in an Interval Range | [Ruby](./algorithms/ruby/1523-count-odd-numbers-in-an-interval-range.rb) | Easy |
| 1539 | Kth Missing Positive Number | [Ruby](./algorithms/ruby/1539-kth-missing-positive-number.rb) | Easy |
| 1675 | Minimize Deviation in Array | [Ruby](./algorithms/ruby/1675-minimize-deviation-in-array.rb) | Hard |
Expand Down
72 changes: 72 additions & 0 deletions algorithms/ruby/0724-find-pivot-index.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# frozen_string_literal: true

# 724. Find Pivot Index
# https://leetcode.com/problems/find-pivot-index

=begin

Given an array of integers nums, calculate the pivot index of this array.

The pivot index is the index where the sum of all the numbers strictly to the left of the index is equal to the sum of all the numbers strictly to the index's right.

If the index is on the left edge of the array, then the left sum is 0 because there are no elements to the left. This also applies to the right edge of the array.

Return the leftmost pivot index. If no such index exists, return -1

### Example 1:
Input: nums = [1,7,3,6,5,6]
Output: 3
Explanation:
The pivot index is 3.
Left sum = nums[0] + nums[1] + nums[2] = 1 + 7 + 3 = 11
Right sum = nums[4] + nums[5] = 5 + 6 = 11

### Example 2:
Input: nums = [1,2,3]
Output: -1
Explanation:
There is no index that satisfies the conditions in the problem statement.

### Example 3:
Input: nums = [2,1,-1]
Output: 0
Explanation:
The pivot index is 0.
Left sum = 0 (no elements to the left of index 0)
Right sum = nums[1] + nums[2] = 1 + -1 = 0

### Constraints:
* 1 <= nums.length <= 104
* -1000 <= nums[i] <= 1000

=end

# Runtime 109 ms
# Memory 212.6 MB
# @param {Integer[]} nums
# @return {Integer}
def pivot_index(nums)
sum = nums.sum
left_sum = 0

nums.each_with_index do |num, index|
return index if left_sum == sum - left_sum - num

left_sum += num
end

-1
end

# **************** #
# TEST #
# **************** #

require "test/unit"
class Test_pivot_index < Test::Unit::TestCase
def test_
assert_equal 3, pivot_index([1, 7, 3, 6, 5, 6])
assert_equal(-1, pivot_index([1, 2, 3]))
assert_equal 0, pivot_index([2, 1, -1])
end
end
51 changes: 51 additions & 0 deletions algorithms/ruby/1480-running-sum-of-1d-array.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# frozen_string_literal: true

# 1480. Running Sum of 1d Array
# https://leetcode.com/problems/running-sum-of-1d-array

=begin

Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]).

Return the running sum of nums.

### Example 1:
Input: nums = [1,2,3,4]
Output: [1,3,6,10]
Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].

### Example 2:
Input: nums = [1,1,1,1,1]
Output: [1,2,3,4,5]
Explanation: Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1].

### Example 3:
Input: nums = [3,1,2,10,1]
Output: [3,4,6,16,17]

### Constraints:

* 1 <= nums.length <= 1000
* -10^6 <= nums[i] <= 10^6
=end

# Runtime 96 ms
# Memory 211.1 MB
# @param {Integer[]} nums
# @return {Integer[]}
def running_sum(nums)
nums.reduce([]) { _1 << _2 + (_1.last || 0) }
end

# **************** #
# TEST #
# **************** #

require "test/unit"
class Test_running_sum < Test::Unit::TestCase
def test_
assert_equal [1, 3, 6, 10], running_sum([1, 2, 3, 4])
assert_equal [1, 2, 3, 4, 5], running_sum([1, 1, 1, 1, 1])
assert_equal [3, 4, 6, 16, 17], running_sum([3, 1, 2, 10, 1])
end
end