-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsliding_window.rb
More file actions
154 lines (120 loc) · 4.59 KB
/
sliding_window.rb
File metadata and controls
154 lines (120 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#Problems 1, 2 taken from Brennan Fradelis on Youtube
#Problem 1: Max sum of any contiguous subarray of size K
#--------------------------------------------------------
def max_sum_subarray(arr, k)
max_sum = -1.0/0
start = 0
curr_sum = 0
arr.each_with_index do |val, idx|
curr_sum += val
if ((idx-start+1)==k)
max_sum = [max_sum, curr_sum].max
curr_sum -= arr[start]
start+=1
end
end
return max_sum
end
arr = [2,3,4,1,5]
k = 3
puts max_sum_subarray(arr, k)
#--------------------------------------------------------
#Problem 2: Given an array of positive numbers find the size of the smallest contiguous subarray with a sum >= s(given)
#--------------------------------------------------------
def smallest_gte_s(arr, s)
sum = s
smallest_size = 1.0/0
start = 0
curr_sum = 0
arr.each_with_index do |val, idx|
curr_sum += val
while curr_sum >= sum
smallest_size = [smallest_size, (idx-start+1)].min
curr_sum -= arr[start]
start += 1
end
end
return smallest_size
end
arr = [2,4,2,5,1]
s = 7
puts smallest_gte_s(arr, s)
#--------------------------------------------------------
#Question 3(taken from Michael Muinos, Youtube)
#Given a string, find longest substring without repeating characters
#--------------------------------------------------------
def longest_substring(str)
start, current_max = 0, 0
curr_substring = []
str.chars.each_with_index do |char, ending|
#ending => index
unless (char==' ')
while curr_substring.include?(char)
#since we are dealing with substrings and NOT subsequences,
#we can simply remove the first char,
#as substrings are never in this format => sub=str[0]+str[2..-1] (they basically cannot trim off chars in the middle)
curr_substring = curr_substring[1..-1]
#move the front window up
start+=1
end
curr_substring << char
current_max = [current_max, ending-start+1].max
end
#iterate to the next end window
end
return current_max
end
# def longest_substring(str)
# i,j, current_max = 0, 0, 0
# curr_substring = []
# while i<str.length
# char = str[i]
# while curr_substring.include?(char)
# #since we are dealing with substrings and NOT subsequences,
# #we can simply remove the first char,
# #as substrings are never in this format => sub=str[0]+str[2..-1] (they basically cannot trim off chars in the middle)
# curr_substring = curr_substring[1..-1]
# #move the front window up
# j+=1
# end
# curr_substring << char
# current_max = [current_max, i-j+1].max
# #iterate the end window
# i+=1
# end
# return current_max
# end
str = "pwwkew"
puts longest_substring(str)
# str = " "
# puts longest_substring(str)
# str = "abcabcbb"
# puts longest_substring(str)
# str = "bbbbb"
# puts longest_substring(str)
# puts "expect return value of 3"
#--------------------------------------------------------
#these problems have a specific format:
#firstly, we declare necessary variables:
#the returned min/max value which is defaulted to inf/-inf respectively (when dealing with summations)
#min --> default to inf
#max --> default to -inf
#if we aren't summing, default it to size = 0
#initialize our starting window to 0
#curr_sum initialized to 0;
#create our enumerable with indexes, to act as our ending windows
#iterate through the array,
#and add each value to the current sum
#once we reach a specific point, we enter an if/while conditional:
#within this conditional, we have to take the maximum/minimum relative to what
#we already have, versus the current sum
#now that we have entered this conditional and found the current max/min
#we will bump up the "start"-ing window to the right
#which means subtract the value from the arr[start]
#and then increment the value of "start"
#depending on the problem, we want to keep looping through this conditional,
#which is common for dynamic size related problems;
#else exit conditional,
#well move on to the next iteration of the enum where the "end" window
#now is also moved down one index, repeat the above process from line 69
#once weve gone through all indexes, we have found the max/min, which we return;