Skip to content

Commit 71e7a57

Browse files
authored
2501. Longest Square Streak in an Array (#189)
1 parent 2f57423 commit 71e7a57

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,7 @@
494494
| 2477 | Minimum Fuel Cost to Report to the Capital | [Ruby](./algorithms/ruby/2477-minimum-fuel-cost-to-report-to-the-capital.rb) | Medium |
495495
| 2483 | Minimum Penalty for a Shop | [Ruby](./algorithms/ruby/2483-minimum-penalty-for-a-shop.rb) | Medium |
496496
| 2492 | Minimum Score of a Path Between Two Cities | [Ruby](./algorithms/ruby/2492-minimum-score-of-a-path-between-two-cities.rb) | Medium |
497+
| 2501 | Longest Square Streak in an Array | [Ruby](./algorithms/ruby/2501-longest-square-streak-in-an-array.rb) | Medium |
497498
| 2542 | Maximum Subsequence Score | [Ruby](./algorithms/ruby/2542-maximum-subsequence-score.rb) | Medium |
498499
| 2551 | Put Marbles in Bags | [Ruby](./algorithms/ruby/2551-put-marbles-in-bags.rb) | Hard |
499500
| 2583 | Kth Largest Sum in a Binary Tree | [Ruby](./algorithms/ruby/2583-kth-largest-sum-in-a-binary-tree.rb) | Medium |
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# frozen_string_literal: true
2+
3+
# 2501. Longest Square Streak in an Array
4+
# Medium
5+
# https://leetcode.com/problems/longest-square-streak-in-an-array
6+
7+
=begin
8+
You are given an integer array nums. A subsequence of nums is called a square streak if:
9+
* The length of the subsequence is at least 2, and
10+
* after sorting the subsequence, each element (except the first element) is the square of the previous number.
11+
Return the length of the longest square streak in nums, or return -1 if there is no square streak.
12+
A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.
13+
14+
Example 1:
15+
Input: nums = [4,3,6,16,8,2]
16+
Output: 3
17+
Explanation: Choose the subsequence [4,16,2]. After sorting it, it becomes [2,4,16].
18+
- 4 = 2 * 2.
19+
- 16 = 4 * 4.
20+
Therefore, [4,16,2] is a square streak.
21+
It can be shown that every subsequence of length 4 is not a square streak.
22+
23+
Example 2:
24+
Input: nums = [2,3,5,6,7]
25+
Output: -1
26+
Explanation: There is no square streak in nums so return -1.
27+
28+
Constraints:
29+
* 2 <= nums.length <= 105
30+
* 2 <= nums[i] <= 105
31+
=end
32+
33+
# @param {Integer[]} nums
34+
# @return {Integer}
35+
def longest_square_streak(nums)
36+
nums = nums.uniq.sort
37+
num_set = nums.to_set
38+
max_length = 0
39+
40+
nums.each do |num|
41+
length = 0
42+
current = num
43+
44+
while num_set.include?(current)
45+
length += 1
46+
current = current**2
47+
end
48+
49+
if length > 1
50+
max_length = [max_length, length].max
51+
end
52+
end
53+
54+
max_length > 1 ? max_length : -1
55+
end
56+
57+
# ********************#
58+
# TEST #
59+
# ********************#
60+
61+
require "test/unit"
62+
class Test_longest_square_streak < Test::Unit::TestCase
63+
def test_
64+
assert_equal 3, longest_square_streak([4, 3, 6, 16, 8, 2])
65+
assert_equal(-1, longest_square_streak([2, 3, 5, 6, 7]))
66+
end
67+
end

0 commit comments

Comments
 (0)