プログラミング能力を評価するオンラインテスト、Codilityで、Lesson 7 Stacks and Queues – Bracketsに回答しました。
問題と結果画面
100%の評価。
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
|
using System; using System.Linq; class Solution { public int solution(string S) { // Console.WriteLine("S : " + S); S = S.Replace('(','2'); S = S.Replace(')','3'); S = S.Replace('[','4'); S = S.Replace(']','5'); S = S.Replace('{','6'); S = S.Replace('}','7'); int[] s = S.Select(a => a - '0').ToArray(); int N = s.Length; int result = 0; for(int i = 0; i < N; i++) { if(s[i] % 2 == 0 && i + 1 < N && s[i + 1] % 2 == 1 && s[i + 1] != (s[i] + 1)) return 0; } for(int i = 1; i < 4; i++) { int tgt = i * 2; // Console.WriteLine("tgt : " + tgt); for(int j = 0; j < N; j++) { // Console.WriteLine("s[j] : " + s[j]); // Console.WriteLine("result : " + result); if(s[j] == 0) continue; if(s[j] == tgt) { result += 1; s[j] = 0; } else if(s[j] == (tgt + 1)) { result -= 1; if(result < 0) return 0; s[j] = 0; } } if(result != 0) return 0; } return 1; } } |
プログラミング能力を評価するオンラインテスト、Codilityで、Lesson 6 Sorting – MaxProductOfThreeに回答しました。
問題と結果画面
100%の評価。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
using System; using System.Linq; class Solution { public int solution(int[] A) { int N = A.Length; // Array.Sort(A); // Array.Reverse(A); A = A.OrderByDescending(c => c).ToArray(); // for(int i = 0; i < A.Length; i++) // { // Console.WriteLine("A[" + i + "] : " + A[i]); // } // -1 * -1 = 1; return Math.Max(A[0] * A[1] * A[2], A[0] * A[N-1] * A[N-2]); } } |
プログラミング能力を評価するオンラインテスト、Codilityで、Lesson 6 Sorting – Distinctに回答しました。
問題と結果画面
75%の評価。
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
|
using System; using System.Collections.Generic; class Solution { public int solution(int[] A) { List<int> list = new List<int>(); for(int i = 0; i < A.Length; i++) { bool isFound = false; for(int j = 0; j < list.Count; j++) { if(A[i] == list[j]) { isFound = true; break; } } if(!isFound) list.Add(A[i]); } return list.Count; } } |
問題と結果画面
100%の評価。
LINQを使用して書き換え。
|
using System; using System.Linq; class Solution { public int solution(int[] A) { int [] array = A.Distinct().ToArray(); return array.Length; } } |
プログラミング能力を評価するオンラインテスト、Codilityで、Lesson 6 Sorting – Triangleに回答しました。
問題と結果画面
93%の評価。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
using System; class Solution { public int solution(int[] A) { Array.Sort(A); for(int i = 0; i < (A.Length - 2); i++) { if((A[i] + A[i + 1]) > A[i + 2] && (A[i + 1] + A[i + 2]) > A[i] && (A[i + 2] + A[i]) > A[i + 1]) { return 1; } } return 0; } } |
問題と結果画面
100%の評価。
int型の最大値が3つ渡ってきた時の処理を追加。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
using System; class Solution { public int solution(int[] A) { int N = A.Length; Array.Sort(A); for(int i = 0; i < (N - 2); i++) { if((A[i] + A[i + 1]) > A[i + 2] || (A[i] == Int32.MaxValue && A[i] == A[i+1] && A[i] == A[i+2])) { return 1; } } return 0; } } |
問題と結果画面
100%の評価。
上記コードで100%になることはなったが、根本的な原因であるint型最大値のオーバーフローの問題が解決されていなかったので、それに対応。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
using System; class Solution { public int solution(int[] A) { Array.Sort(A); int N = A.Length; long l, m; for(int i = 0; i < (N - 2); i++) { l = A[i]; m = A[i + 1]; // Console.WriteLine("l + m : " + (l + m)); if(l + m > A[i + 2]) { return 1; } } return 0; } } |
プログラミング能力を評価するオンラインテスト、Codilityで、Lesson 5 Prefix Sums – GenomicRangeQueryに回答しました。
問題と結果画面
62%の評価。
almost_all_same_letters : NG
large_random : NG
extreme_large : NG
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
|
using System; class Solution { public int[] solution(string S, int[] P, int[] Q) { if(S.Length < 1 || 100000 < S.Length || P.Length == 0 || 50000 < P.Length || Q.Length == 0 || 50000 < Q.Length || P.Length != Q.Length) throw new InvalidOperationException(); int[] result = new int[P.Length]; char[] dna = S.ToCharArray(); int[] impact = new int[dna.Length]; for(int i = 0; i < dna.Length; i++) { switch(dna[i]) { case 'A': impact[i] = 1; break; case 'C': impact[i] = 2; break; case 'G': impact[i] = 3; break; case 'T': impact[i] = 4; break; default: impact[i] = 0; break; } } for(int i = 0; i < P.Length; i++) { int value = 10; for(int K = P[i]; K <= Q[i]; K++) { if(impact[K] < value) value = impact[K]; } result[i] = value; } return result; } } |
問題と結果画面
75%の評価。
almost_all_same_letters : NG
large_random : OK
extreme_large : NG
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
|
using System; using System.Linq; class Solution { public int[] solution(string S, int[] P, int[] Q) { int[] result = new int[P.Length]; S = S.Replace('A','1'); S = S.Replace('C','2'); S = S.Replace('G','3'); // S = S.Replace('T','4'); int[] impact = S.Select(a => a - '0').ToArray(); for(int i = 0; i < P.Length; i++) { int value = 4; for(int K = P[i]; K <= Q[i]; K++) { // Console.WriteLine("impact[K] : " + impact[K]); if(impact[K] == 36) continue; if(impact[K] < value) value = impact[K]; if(impact[K] == 1) break; } result[i] = value; } return result; } } |
問題と結果画面
75%の評価。
almost_all_same_letters : NG
large_random : OK
extreme_large : NG
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
|
using System; using System.Linq; class Solution { public int[] solution(string S, int[] P, int[] Q) { int[] result = new int[P.Length]; S = S.Replace('A','1'); S = S.Replace('C','2'); S = S.Replace('G','3'); int[] impact = S.Select(a => a - '0').ToArray(); for(int i = 0; i < P.Length; i++) { int value = 4; bool isFound = false; for (int j = 1; j < 4; j++) { for(int K = P[i]; K <= Q[i]; K++) { if(impact[K] == j) { value = j; isFound = true; break; } } if(isFound) break; } result[i] = value; } return result; } } |
問題と結果画面
75%の評価。
almost_all_same_letters : NG
large_random : OK
extreme_large : NG
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
|
using System; using System.Linq; class Solution { public int[] solution(string S, int[] P, int[] Q) { int[] result = new int[P.Length]; S = S.Replace('A','1'); S = S.Replace('C','2'); S = S.Replace('G','3'); int[] impact = S.Select(a => a - '0').ToArray(); int[] count = new int[3]; count[0] = impact.Count(x => x == 1); count[1] = impact.Count(x => x == 2); count[2] = impact.Count(x => x == 3); for(int i = 0; i < P.Length; i++) { int value = 4; bool isFound = false; for (int j = 1; j < 4; j++) { if(count[j-1] == 0) continue; for(int K = P[i]; K <= Q[i]; K++) { if(impact[K] == j) { value = j; isFound = true; break; } } if(isFound) break; } result[i] = value; } return result; } } |
問題と結果画面
75%の評価。
パフォーマンスが0.4-5秒及ばず。
almost_all_same_letters : NG
large_random : NG
extreme_large : OK
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
|
using System; using System.Collections.Generic; using System.Linq; class Solution { public int[] solution(string S, int[] P, int[] Q) { int[] result = new int[P.Length]; char[] arr = S.ToCharArray(); List<List<int>> multi = new List<List<int>>(); multi.Add(new List<int>()); multi.Add(new List<int>()); multi.Add(new List<int>()); int len = arr.Length; for(int i = 0; i < len; i++) { if(arr[i] == 'T') continue; else if(arr[i] == 'A') multi[0].Add(i); else if(arr[i] == 'C') multi[1].Add(i); else multi[2].Add(i); } len = P.Length; for(int i = 0; i < len; i++) { int val = 4; bool found = false; int p = P[i]; int q = Q[i]; for (int j = 0; j < 3; j++) { int cnt = multi[j].Count; for(int k = 0; k < cnt; k++) { int pos = multi[j][k]; if(p <= pos && pos <= q) { val = j + 1; found = true; break; } } if(found) break; } result[i] = val; } return result; } } |
100%になっていないけれど、いったん保留。あとでまた考える。