Skip to content

Commit 4b821af

Browse files
committed
Algorithms: Sorting Problems
1 parent c4b1fff commit 4b821af

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Insertion Sort Problem
2+
#
3+
# Monk's best friend Micro's birthday is coming up. Micro likes Nice Strings very much, so Monk decided to gift
4+
# him one. Monk is having N nice strings, so he'll choose one from those. But before he selects one, he need
5+
# to know the Niceness value of all of those. Strings are arranged in an array A, and the Niceness value of
6+
# string at position i is defined as the number of strings having position less than i which are lexicographicaly smaller than
7+
# A[i]. Since nowadays, Monk is very busy with the Code Monk Series, he asked for your help.
8+
# Note: Array's index starts from 1.
9+
#
10+
# Input:
11+
# First line consists of a single integer denoting N. N lines follow each containing a string made of lower case English alphabets.
12+
#
13+
# Output:
14+
# Print N lines, each containing an integer, where the integer in ith line denotes Niceness value of string A[i].
15+
#
16+
# Constraints:
17+
# 1≤N≤1000
18+
# 1≤|A[i]|≤10 ∀ i where 1≤i≤N, where
19+
# |A[i]| denotes the length of
20+
# ith string.
21+
#
22+
# SAMPLE INPUT
23+
# 4
24+
# a
25+
# c
26+
# d
27+
# b
28+
#
29+
# SAMPLE OUTPUT
30+
# 0
31+
# 1
32+
# 2
33+
# 1
34+
#
35+
# Explanation
36+
# Number of strings having index less than 1 which are less than "a" = 0
37+
# Number of strings having index less than 2 which are less than "c": ("a") = 1
38+
# Number of strings having index less than 3 which are less than "d": ("a", "c") = 2
39+
# Number of strings having index less than 4 which are less than "b": ("a") = 1
40+
41+
# My solution
42+
array = []
43+
for i in range(int(input())):
44+
array.append(input())
45+
46+
for i in range(len(array)):
47+
count = 0
48+
for j in range(0, i + 1):
49+
if array[j] < array[i]:
50+
count += 1
51+
print(count)
52+
53+
# Better solution
54+
n = int(input())
55+
a = []
56+
for i in range(n):
57+
a.append(input())
58+
ans = 0
59+
for j in a:
60+
if j < a[i]:
61+
ans += 1
62+
print(ans)

0 commit comments

Comments
 (0)