leetcode_rb 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 44fe3d785272c207dc69395f86873ec2e127e813c222b5c59d44b01f4160869a
4
+ data.tar.gz: 5f8ab8c42f3179f37acb31a5658260dc2f8b63278e0fc1f43057cc5c958ee9a9
5
+ SHA512:
6
+ metadata.gz: cc51aadf04ee6dd63f83bce01e439028a870c51babda3086058ff0a3f2e30817e9035a566bea03d9503324d5332b455b3386a75dc4a30400f65ffdaf72b52b09
7
+ data.tar.gz: 5e34594116051230fac5abd3e7c0f128c80e61c28650de1a102a09f2d5e7c82be2db9e4b24063256be063a17a21adfab3f6e280e988910b771b583a6cdfe4bd4
@@ -0,0 +1,17 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ # RubyMine
14
+ /.idea/
15
+
16
+ # system files
17
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.3
7
+ before_install: gem install bundler -v 2.0.1
File without changes
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in leetcode_rb.gemspec
4
+ gemspec
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ leetcode_rb (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ rake (10.5.0)
11
+ rspec (3.8.0)
12
+ rspec-core (~> 3.8.0)
13
+ rspec-expectations (~> 3.8.0)
14
+ rspec-mocks (~> 3.8.0)
15
+ rspec-core (3.8.0)
16
+ rspec-support (~> 3.8.0)
17
+ rspec-expectations (3.8.2)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.8.0)
20
+ rspec-mocks (3.8.0)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.8.0)
23
+ rspec-support (3.8.0)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler (~> 2.0)
30
+ leetcode_rb!
31
+ rake (~> 10.0)
32
+ rspec (~> 3.0)
33
+
34
+ BUNDLED WITH
35
+ 2.0.1
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Kaitian Xie
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1 @@
1
+ # LeetCode in Ruby
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "leetcode_rb"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,42 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "leetcode_rb/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "leetcode_rb"
8
+ spec.version = LeetcodeRb::VERSION
9
+ spec.authors = ["Kaitian Xie"]
10
+ spec.email = ["xkaitian@gmail.com"]
11
+
12
+ spec.summary = %q{LeetCode Solutions in Ruby}
13
+ spec.description = %q{LeetCode Solutions in Ruby}
14
+ spec.homepage = "https://github.com/algobot76/leetcode_rb"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata["allowed_push_host"] = "https://rubygems.org/"
21
+
22
+ spec.metadata["homepage_uri"] = spec.homepage
23
+ spec.metadata["source_code_uri"] = "https://github.com/algobot76/leetcode_rb"
24
+ spec.metadata["changelog_uri"] = "https://github.com/algobot76/leetcode_rb/blob/master/CHANGELOG.md"
25
+ else
26
+ raise "RubyGems 2.0 or newer is required to protect against " \
27
+ "public gem pushes."
28
+ end
29
+
30
+ # Specify which files should be added to the gem when it is released.
31
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
32
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
33
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
34
+ end
35
+ spec.bindir = "exe"
36
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
37
+ spec.require_paths = ["lib"]
38
+
39
+ spec.add_development_dependency "bundler", "~> 2.0"
40
+ spec.add_development_dependency "rake", "~> 10.0"
41
+ spec.add_development_dependency "rspec", "~> 3.0"
42
+ end
@@ -0,0 +1,5 @@
1
+ require "leetcode_rb/version"
2
+ require "leetcode_rb/remove_duplicates_from_sorted_array"
3
+ require "leetcode_rb/merge_intervals"
4
+ require "leetcode_rb/jewels_and_stones"
5
+ require "leetcode_rb/first_unique_character_in_a_string"
@@ -0,0 +1,56 @@
1
+ class FirstUniqueCharacterInAString
2
+ # @param {String} s
3
+ # @return {Integer}
4
+
5
+ # Index and RIndex
6
+ # def first_uniq_char(s)
7
+ # s.each_char.with_index do |char, i|
8
+ # if s.index(char) == s.rindex(char)
9
+ # return i
10
+ # end
11
+ # end
12
+ #
13
+ # return -1
14
+ # end
15
+
16
+ # Two Arrays
17
+ # def first_uniq_char(s)
18
+ # counts = Array.new(26, 0)
19
+ # positions = Array.new(26, nil)
20
+ # offset = 'a'.ord
21
+ #
22
+ # (0...s.length).each do |index|
23
+ # char = s[index]
24
+ # pos = char.ord - offset
25
+ # counts[pos] +=1
26
+ # positions[pos] = index if positions[pos].nil?
27
+ # end
28
+ #
29
+ # s.each_char do |char|
30
+ # pos = char.ord - offset
31
+ # if counts[pos] == 1
32
+ # return positions[pos]
33
+ # end
34
+ # end
35
+ #
36
+ # return -1
37
+ # end
38
+
39
+ # Hash
40
+ # def first_uniq_char(s)
41
+ # return -1 if s.length == 0
42
+ #
43
+ # counts = Hash.new 0
44
+ # s.each_char do |char|
45
+ # counts[char] += 1
46
+ # end
47
+ #
48
+ # s.each_char.with_index do |char, i|
49
+ # if counts[char] == 1
50
+ # return i
51
+ # end
52
+ # end
53
+ #
54
+ # return -1
55
+ # end
56
+ end
@@ -0,0 +1,24 @@
1
+ class JewelsAndStones
2
+ # @param {String} j
3
+ # @param {String} s
4
+ # @return {Integer}
5
+ # 1 Line
6
+ def num_jewels_in_stones(j, s)
7
+ s.count(j)
8
+ end
9
+
10
+ # Hash
11
+ # def num_jewels_in_stones(j, s)
12
+ # stones = Hash.new 0
13
+ # s.each_char do |char|
14
+ # stones[char] += 1
15
+ # end
16
+ #
17
+ # sum = 0
18
+ # j.each_char do |char|
19
+ # sum += stones[char]
20
+ # end
21
+ #
22
+ # return sum
23
+ # end
24
+ end
@@ -0,0 +1,91 @@
1
+ require 'set'
2
+ require_relative 'utils/interval'
3
+
4
+ class MergeIntervals
5
+ # Definition for an interval.
6
+ # class Interval
7
+ # attr_accessor :start, :end
8
+ # def initialize(s=0, e=0)
9
+ # @start = s
10
+ # @end = e
11
+ # end
12
+ # end
13
+
14
+ # @param {Interval[]} intervals
15
+ # @return {Interval[]}
16
+ # Sorting
17
+ # def merge(intervals)
18
+ # merged = []
19
+ # intervals.sort_by! { |interval| interval.start }
20
+ #
21
+ # intervals.each do |interval|
22
+ # if merged.empty? || merged.last.end < interval.start
23
+ # merged << interval
24
+ # else
25
+ # merged.last.end = interval.end if interval.end > merged.last.end
26
+ # end
27
+ # end
28
+ #
29
+ # return merged
30
+ # end
31
+
32
+ # Connected Components
33
+ def merge(intervals)
34
+ graph = build_graph(intervals)
35
+ nodes_in_comp, comp_num =get_components(graph, intervals)
36
+
37
+ (0...comp_num).to_a.map { |comp| merge_nodes(nodes_in_comp[comp])}
38
+ end
39
+
40
+ private
41
+
42
+ def overlap(node1, node2)
43
+ return node1.start <= node2.end && node2.start <= node1.end
44
+ end
45
+
46
+ def build_graph(intervals)
47
+ graph = Hash.new []
48
+
49
+ intervals.each.with_index do |interval, i|
50
+ for j in (i + 1)...intervals.length
51
+ if overlap(interval, intervals[j])
52
+ graph[interval] << intervals[j]
53
+ graph[intervals[j]] << interval
54
+ end
55
+ end
56
+ end
57
+
58
+ return graph
59
+ end
60
+
61
+ def merge_nodes(nodes)
62
+ min_start = nodes.min_by { |node| node.start }
63
+ max_end = nodes.max_by { |node| node.end }
64
+ Interval.new(min_start, max_end)
65
+ end
66
+
67
+ def get_components(graph, intervals)
68
+ visited = Set.new
69
+ comp_num = 0
70
+ nodes_in_comp = Hash.new []
71
+
72
+ make_component_dfs = Proc.new do |start|
73
+ stack = [start]
74
+ while stack
75
+ node = stack.pop()
76
+ unless visited.include?(node)
77
+ visited.add(node)
78
+ nodes_in_comp[comp_num] << node
79
+ stack.concat(graph[node])
80
+ end
81
+ end
82
+ end
83
+
84
+ intervals.each do |interval|
85
+ unless visited.include?(interval)
86
+ make_component_dfs.call(interval)
87
+ return nodes_in_comp, comp_num
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,9 @@
1
+ class RemoveDuplicatesFromSortedArray
2
+
3
+ # @param {Integer[]} nums
4
+ # @return {Integer}
5
+ def remove_duplicates(nums)
6
+ nums.uniq!
7
+ return nums.length
8
+ end
9
+ end
@@ -0,0 +1,12 @@
1
+ class Interval
2
+ attr_accessor :start, :end
3
+
4
+ def initialize(s = 0, e = 0)
5
+ @start = s
6
+ @end = e
7
+ end
8
+
9
+ def ==(other)
10
+ return self.start == other.start && self.end == other.end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module LeetcodeRb
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: leetcode_rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kaitian Xie
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-02-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: LeetCode Solutions in Ruby
56
+ email:
57
+ - xkaitian@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - CHANGELOG.md
66
+ - Gemfile
67
+ - Gemfile.lock
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - bin/console
72
+ - bin/setup
73
+ - leetcode_rb.gemspec
74
+ - lib/leetcode_rb.rb
75
+ - lib/leetcode_rb/first_unique_character_in_a_string.rb
76
+ - lib/leetcode_rb/jewels_and_stones.rb
77
+ - lib/leetcode_rb/merge_intervals.rb
78
+ - lib/leetcode_rb/remove_duplicates_from_sorted_array.rb
79
+ - lib/leetcode_rb/utils/interval.rb
80
+ - lib/leetcode_rb/version.rb
81
+ homepage: https://github.com/algobot76/leetcode_rb
82
+ licenses:
83
+ - MIT
84
+ metadata:
85
+ allowed_push_host: https://rubygems.org/
86
+ homepage_uri: https://github.com/algobot76/leetcode_rb
87
+ source_code_uri: https://github.com/algobot76/leetcode_rb
88
+ changelog_uri: https://github.com/algobot76/leetcode_rb/blob/master/CHANGELOG.md
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubygems_version: 3.0.2
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: LeetCode Solutions in Ruby
108
+ test_files: []