プログラミング能力を評価するオンラインテスト、Codilityで、Lesson 4 Time Complexity – FrogRiverOneに回答しました。
問題と結果画面
63%の評価。
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.Collections.Generic; class Solution { public int solution(int X, int[] A) { List<bool> checkList = new List<bool>(); for(int i = 0; i < X; i++) { checkList.Add(false); } for(int i = 0; i < A.Length; i++) { if(A[i] <= X) { checkList[(A[i] - 1)] = true; } bool allTrue = true; for(int j = 0; j < checkList.Count; j++) { if(checkList[j] == false) allTrue = false; } if(allTrue) return i; } // couldn't get to the other side of the river. return -1; } } |
問題と結果画面
72%の評価。
for文の数を3つから1つに。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
using System; using System.Linq; class Solution { public int solution(int X, int[] A) { bool[] checkList = new bool[X]; for(int i = 0; i < A.Length; i++) { if(A[i] <= X) { checkList[(A[i] - 1)] = true; } if(checkList.All (x => x == true)) return i; } // couldn't get to the other side of the river. return -1; } } |
問題と結果画面
81%の評価。
LINQより、ArrayのTrueForAllメソッドの方が若干パフォーマンスは良いみたいだ。
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 X, int[] A) { bool[] checkList = new bool[X]; for(int i = 0; i < A.Length; i++) { if(A[i] <= X) { checkList[(A[i] - 1)] = true; } if(System.Array.TrueForAll (checkList, x => x == true)) return i; } // couldn't get to the other side of the river. return -1; } } |
問題と結果画面
100%の評価。
シンプルにカウンタをデクリメントすることで100%を達成。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
using System; class Solution { public int solution(int X, int[] A) { bool[] checkList = new bool[X]; int cnt = X; for(int i = 0; i < A.Length; i++) { if(!checkList[(A[i] - 1)] && A[i] <= X) { checkList[(A[i] - 1)] = true; cnt--; } if(cnt == 0) return i; } // couldn't get to the other side of the river. return -1; } } |
プログラミング能力を評価するオンラインテスト、Codilityで、Lesson 3 Time Complexity – PermMissingElemに回答しました。
問題と結果画面
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
|
using System; class Solution { public int solution(int[] A) { System.Array.Sort (A); int count = 0; for(int i = 0; i < A.Length; i++) { bool isFound = false; count = i + 1; if(A[i] == count) { isFound = true; continue; } if(!isFound) return count; } return count + 1; } } |
プログラミング能力を評価するオンラインテスト、Codilityで、Lesson 3 Time Complexity – TapeEquilibriumに回答しました。
問題と結果画面
66%の評価。
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
|
using System; using System.Collections.Generic; class Solution { public int solution(int[] A) { List<int> difList = new List<int>(); for(int i = 1; i < A.Length; i++) { int totalFormer = 0; int totalAfter = 0; int dif = 0; for(int j = 0; j < i; j++) { totalFormer += A[j]; } for(int j = i; j < A.Length; j++) { totalAfter += A[j]; } dif = System.Math.Abs (totalFormer - totalAfter); difList.Add(dif); } int min = difList[0]; for(int i = 1; i < difList.Count; i ++) { if(difList[i] < min) { min = difList[i]; } } return min; } } |
66%の評価。
問題と結果画面
for文の数を4つから3つに。
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
|
using System; using System.Collections.Generic; class Solution { public int solution(int[] A) { int min = -1; for(int i = 1; i < A.Length; i++) { int totalFormer = 0; int totalAfter = 0; int dif = 0; for(int j = 0; j < i; j++) { totalFormer += A[j]; } for(int j = i; j < A.Length; j++) { totalAfter += A[j]; } dif = System.Math.Abs (totalFormer - totalAfter); if(min < 0 || dif < min) min = dif; } return min; } } |
問題と結果画面
66%の評価。
for文の数を3つから2つに。
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
|
using System; class Solution { public int solution(int[] A) { int sum = 0; foreach (int a in A) { sum += a; } int min = -1; for(int i = 1; i < A.Length; i++) { int sumFormer = 0; int sumAfter = 0; int dif = 0; for(int j = 0; j < i; j++) { sumFormer += A[j]; } sumAfter = (sum - sumFormer); dif = System.Math.Abs (sumFormer - sumAfter); if(min < 0 || dif < min) min = dif; } return min; } } |
問題と結果画面
100%の評価。
for文の数を2つから1つに。
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
|
using System; class Solution { public int solution(int[] A) { int sum = 0; foreach (int a in A) { sum += a; } int min = -1; int sumFormer = 0; for(int i = 0; i < A.Length - 1; i++) { sumFormer += A[i]; int dif = System.Math.Abs (sumFormer - (sum - sumFormer)); if(min < 0 || dif < min) min = dif; } return min; } } |
プログラミング能力を評価するオンラインテスト、Codilityで、Lesson 3 Time Complexity – FrogJmpに回答しました。
問題と結果画面
100%の評価。
|
using System; class Solution { public int solution(int X, int Y, int D) { int numerator = Y - X; int result = numerator / D; if(0 < (numerator % D)) result += 1; return result; } } |
プログラミング能力を評価するオンラインテスト、Codilityで、Lesson 2 Arrays – OddOccurrencesInArrayに回答しました。
問題と結果画面
66%の評価。
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
|
using System; class Solution { public int solution(int[] A) { bool isUnpaired; for (int i = 0; i < A.Length; i++) { if(A [i] == 0) continue; isUnpaired = true; for(int j = i + 1; j < A.Length; j++) { if(A [i] == A [j]) { isUnpaired = false; A [j] = 0; break; } } if (isUnpaired) { return A [i]; } } return 0; } } |
問題と結果画面
100%の評価。
for文を2つから1つに。
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) { System.Array.Sort(A); for (int i = 0; i < A.Length; i++) { if(A [i] < 1 || 1000000000 < A [i]) throw new System.InvalidOperationException(); if((i % 2) == 1) continue; if(A.Length <= i + 1) return A [i]; if(A [i] != A [i + 1]) return A [i]; } return 0; } } |
問題と結果画面
100%の評価。
XOR演算子を使えばこんなに簡潔にも書ける。
ビット演算っておもしろい。
|
using System; class Solution { public int solution(int[] A) { int unpairedInt = 0; foreach (int val in A) { unpairedInt ^= val; } return unpairedInt; } } |