Sorting AtCoder ARC 086 C hashes to solve in Ruby, Perl, Java and Python

Introduction

This theme

AtCoder Regular Contest 086 C - Not so Diverse Difficulty: 431

This theme, hash sorting Ruby Speaking of duplicate lists, it's hashes.

ruby.rb


n, k = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
h = Hash.new(0)
a.each do |x|
  h[x] += 1
end
puts n - h.values.sort.reverse[0, k].inject(:+)

before.rb


h.sort_by{|key, v| -v}.each do |key, v|

after.rb


puts n - h.values.sort.reverse[0, k].inject(:+)

~~ * Ruby * doesn't have a destructive sort of hashes, but it seems to have few uses. ~~ ** Addition ** The original code was to sort the hashes, but in the comments I got the idea of taking the values side as an array, sorting, slicing and summing. Python

python.py


from collections import defaultdict

n, k = map(int, input().split())
a = list(map(int, input().split()))
h = defaultdict(int)
for x in a:
    h[x] += 1
ans = 0
i = 0
for key, v in sorted(h.items(), key=lambda x: -x[1]):
    i += 1
    if i > k:
        ans += v
print(ans)

sort.py


for key, v in sorted(h.items(), key=lambda x: -x[1]):

For * Python *, it's called a dictionary rather than a hash. Also, many players are using Counter instead of defaultdict. Perl

perl.pl


chomp (my ($n, $k) = split / /, <STDIN>);
chomp (my @a = split / /, <STDIN>);
my %h;
$h{$_}++ for @a;
my ($c, $ans) = (0, 0);
for my $v (sort {$b <=> $a} values %h) {
  $c++;
  $ans += $v if $c > $k;
}
print "$ans\n";

sort.pl


for my $v (sort {$b <=> $a} values %h) {

Sorting * Perl * is simple. Java

java.java


import java.util.*;
import java.util.stream.Collectors;

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.next());
        int k = Integer.parseInt(sc.next());
        HashMap<Integer, Integer> h = new HashMap<>();
        for (int i = 0; i < n; i++) {
            int a = Integer.parseInt(sc.next());
            h.merge(a, 1, Integer::sum);
        }
        sc.close();
        List<Integer> s = new ArrayList<>(h.values());
        Collections.sort(s);
        int ans = 0;
        for (int i = s.size() - k - 1; i >= 0; i--) {
            ans += s.get(i);
        }
        System.out.println(ans);
    }
}

sort.java


        List<Integer> s = new ArrayList<>(h.values());
        Collections.sort(s);

For * Java *, it is converted to a list and sorted. ** Addition ** I modified the code from the comments.

Ruby Python Perl Java
Code length 156 Byte 286 Byte 226 Byte 723 Byte
Execution time 187 ms 261 ms 214 ms 620 ms
memory 32880 KB 39352 KB 46668 KB 70772 KB

Summary

Referenced site

Recommended Posts

Sorting AtCoder ARC 086 C hashes to solve in Ruby, Perl, Java and Python
Solving in Ruby, Perl, Java, and Python AtCoder ARC 066 C Iterative Squares Hash
Solving with Ruby, Perl, Java, and Python AtCoder ARC 098 C Cumulative sum
Solve with Ruby, Perl, Java and Python AtCoder ABC 047 C Regular Expression
Solve with Ruby, Python and Java AtCoder ARC104 B Cumulative sum
Solving with Ruby, Perl, Java, and Python AtCoder ABC 065 C factorial
Solving with Ruby, Perl, Java and Python AtCoder CADDi 2018 C Prime Factorization
Solving with Ruby, Perl, Java and Python AtCoder ABC 131 D Array Sorting
AtCoder ARC080 D simulation solved in Ruby and Python
Solving with Ruby and Python AtCoder ARC 059 C Least Squares
Solving with Ruby, Perl, Java and Python AtCoder ATC 002 A
Solving with Ruby and Python AtCoder ARC067 C Prime Factorization
Solving with Ruby, Perl, Java and Python AtCoder ATC 002 B
Solving with Ruby, Perl, Java and Python AtCoder diverta 2019 Programming Contest C String Manipulation
Solving in Ruby, Python and Java AtCoder ABC141 D Priority Queuing
How to write the correct shebang in Perl, Python and Ruby scripts
Solving with Ruby, Perl, Java and Python AtCoder ABC 107 B String Manipulation
Solving with Ruby, Perl, Java, and Python AtCoder AGC 033 A Breadth-first search
Solving with Ruby, Perl, Java and Python AtCoder ABC 165 D Floor function
How to generate permutations in Python and C ++
Solve Atcoder ABC176 (A, B, C, E) in Python
Solve number sorting (equivalent to paiza rank D) in Python
Solve word counts (equivalent to paiza rank C) in Python
Solve with Ruby and Python AtCoder ABC133 D Cumulative sum
Solving with Ruby and Python AtCoder ABC011 C Dynamic programming
Object-oriented in C: Refactored "β—‹ βœ• game" and ported it to Python
Hello World in various languages [Python / PHP / Java / Perl / Ruby]
AtCoder ABC168 A case expression solved in Ruby and Python
Solve Fizz Buzz (equivalent to paiza rank C) in Python
How to log in to AtCoder with Python and submit automatically
AtCoder JSC2019 Qual B to solve with Ruby and Python Inverse element of arithmetic progression
Sorting algorithm and implementation in Python
Solve Atcoder ABC169 A-D in Python
Solve ABC036 A ~ C in Python
Ruby Python Java Case insensitive sorting
How to wrap C in Python
Solve ABC037 A ~ C in Python
I want to solve APG4b with Python (only 4.01 and 4.04 in Chapter 4)
How to use functions in separate files Perl and Python versions
Difference in how to write if statement between ruby ​​and python
How to display bytes in the same way in Java and Python
Overlapping regular expressions in Python and Java
Solving with Ruby and Python AtCoder ABC057 C Prime Factorization Bit Search
Solve ABC175 A, B, C in Python
Summary of how to write if statements (Scala, Java, Rust, C language, C ++, Go language, PHP, Perl, Python, Ruby)
Differences between Ruby and Python in scope
Implement FIR filters in Python and C
Differences in syntax between Python and Java
Summary of how to write increment / decrement (Scala, Java, Rust, C, C ++, Go, PHP, Perl, Python, Ruby, JavaScript)
Write O_SYNC file in C and Python
Solving with Ruby and Python AtCoder Tenka1 Programmer Contest C Cumulative sum
[With commentary] Solve Fizz Buzz (equivalent to paiza rank C) in Python
I wanted to solve ABC159 in Python
AtCoder ABC172 C Cumulative Sum Binary Search Solved by Ruby and Python
AtCoder Beginner Contest 170 B Problem "Crane and Turtle" Explanation (Python3, C ++, Java)
How to use is and == in Python
How to write Ruby to_s in Python
I compared the speed of regular expressions in Ruby, Python, and Perl (2013 version)
Solve with Ruby and Python AtCoder ABC084 D Cumulative sum of prime numbers
Solving with Ruby and Python AtCoder CODE FESTIVAL 2016 qual C B Priority queue
Difference in writing method to read external source code between Ruby and Python