プログラミング能力を評価するオンラインテスト、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; } } |