Skip to content

Commit 5ee4798

Browse files
authored
Merge pull request #43 from remy727/20230323
724, 1480
2 parents 3d80857 + e92f051 commit 5ee4798

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
| 605 | Design Circular Queue | [Ruby](./algorithms/ruby/0605-can-place-flowers.rb) | Easy |
5959
| 622 | Design Circular Queue | [Ruby](./algorithms/ruby/0622-design-circular-queue.rb) | Medium |
6060
| 652 | Find Duplicate Subtrees | [Ruby](./algorithms/ruby/0652-find-duplicate-subtrees.rb) | Medium |
61+
| 724 | Find Pivot Index | [Ruby](./algorithms/ruby/0724-find-pivot-index.rb) | Easy |
6162
| 733 | Flood Fill | [Ruby](./algorithms/ruby/0733-flood-fill.rb) | Easy |
6263
| 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 |
6364
| 875 | Koko Eating Bananas | [Ruby](./algorithms/ruby/0875-koko-eating-bananas.rb) [Python3](./algorithms/python3/0875-koko-eating-bananas.py) | Medium |
@@ -76,6 +77,7 @@
7677
| 1345 | Jump Game IV | [Ruby](./algorithms/ruby/1345-jump-game-iv.rb) | Hard |
7778
| 1470 | Shuffle the Array | [Ruby](./algorithms/ruby/1470-shuffle-the-array.rb) | Easy |
7879
| 1472 | Design Browser History | [Ruby](./algorithms/ruby/1472-design-browser-history.rb) | Medium |
80+
| 1480 | Running Sum of 1d Array | [Ruby](./algorithms/ruby/1480-running-sum-of-1d-array.rb) | Easy |
7981
| 1523 | Count Odd Numbers in an Interval Range | [Ruby](./algorithms/ruby/1523-count-odd-numbers-in-an-interval-range.rb) | Easy |
8082
| 1539 | Kth Missing Positive Number | [Ruby](./algorithms/ruby/1539-kth-missing-positive-number.rb) | Easy |
8183
| 1675 | Minimize Deviation in Array | [Ruby](./algorithms/ruby/1675-minimize-deviation-in-array.rb) | Hard |
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# frozen_string_literal: true
2+
3+
# 724. Find Pivot Index
4+
# https://leetcode.com/problems/find-pivot-index
5+
6+
=begin
7+
8+
Given an array of integers nums, calculate the pivot index of this array.
9+
10+
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.
11+
12+
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.
13+
14+
Return the leftmost pivot index. If no such index exists, return -1
15+
16+
### Example 1:
17+
Input: nums = [1,7,3,6,5,6]
18+
Output: 3
19+
Explanation:
20+
The pivot index is 3.
21+
Left sum = nums[0] + nums[1] + nums[2] = 1 + 7 + 3 = 11
22+
Right sum = nums[4] + nums[5] = 5 + 6 = 11
23+
24+
### Example 2:
25+
Input: nums = [1,2,3]
26+
Output: -1
27+
Explanation:
28+
There is no index that satisfies the conditions in the problem statement.
29+
30+
### Example 3:
31+
Input: nums = [2,1,-1]
32+
Output: 0
33+
Explanation:
34+
The pivot index is 0.
35+
Left sum = 0 (no elements to the left of index 0)
36+
Right sum = nums[1] + nums[2] = 1 + -1 = 0
37+
38+
### Constraints:
39+
* 1 <= nums.length <= 104
40+
* -1000 <= nums[i] <= 1000
41+
42+
=end
43+
44+
# Runtime 109 ms
45+
# Memory 212.6 MB
46+
# @param {Integer[]} nums
47+
# @return {Integer}
48+
def pivot_index(nums)
49+
sum = nums.sum
50+
left_sum = 0
51+
52+
nums.each_with_index do |num, index|
53+
return index if left_sum == sum - left_sum - num
54+
55+
left_sum += num
56+
end
57+
58+
-1
59+
end
60+
61+
# **************** #
62+
# TEST #
63+
# **************** #
64+
65+
require "test/unit"
66+
class Test_pivot_index < Test::Unit::TestCase
67+
def test_
68+
assert_equal 3, pivot_index([1, 7, 3, 6, 5, 6])
69+
assert_equal(-1, pivot_index([1, 2, 3]))
70+
assert_equal 0, pivot_index([2, 1, -1])
71+
end
72+
end
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# frozen_string_literal: true
2+
3+
# 1480. Running Sum of 1d Array
4+
# https://leetcode.com/problems/running-sum-of-1d-array
5+
6+
=begin
7+
8+
Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]).
9+
10+
Return the running sum of nums.
11+
12+
### Example 1:
13+
Input: nums = [1,2,3,4]
14+
Output: [1,3,6,10]
15+
Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].
16+
17+
### Example 2:
18+
Input: nums = [1,1,1,1,1]
19+
Output: [1,2,3,4,5]
20+
Explanation: Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1].
21+
22+
### Example 3:
23+
Input: nums = [3,1,2,10,1]
24+
Output: [3,4,6,16,17]
25+
26+
### Constraints:
27+
28+
* 1 <= nums.length <= 1000
29+
* -10^6 <= nums[i] <= 10^6
30+
=end
31+
32+
# Runtime 96 ms
33+
# Memory 211.1 MB
34+
# @param {Integer[]} nums
35+
# @return {Integer[]}
36+
def running_sum(nums)
37+
nums.reduce([]) { _1 << _2 + (_1.last || 0) }
38+
end
39+
40+
# **************** #
41+
# TEST #
42+
# **************** #
43+
44+
require "test/unit"
45+
class Test_running_sum < Test::Unit::TestCase
46+
def test_
47+
assert_equal [1, 3, 6, 10], running_sum([1, 2, 3, 4])
48+
assert_equal [1, 2, 3, 4, 5], running_sum([1, 1, 1, 1, 1])
49+
assert_equal [3, 4, 6, 16, 17], running_sum([3, 1, 2, 10, 1])
50+
end
51+
end

0 commit comments

Comments
 (0)