|
| 1 | +packagecom.fishercoder.solutions; |
| 2 | + |
| 3 | +importcom.fishercoder.common.utils.CommonUtils; |
| 4 | + |
| 5 | +importjava.util.ArrayList; |
| 6 | +importjava.util.Arrays; |
| 7 | +importjava.util.HashMap; |
| 8 | +importjava.util.HashSet; |
| 9 | +importjava.util.List; |
| 10 | +importjava.util.Map; |
| 11 | +importjava.util.Set; |
| 12 | + |
| 13 | +publicclassContest { |
| 14 | + |
| 15 | +publicList<Integer>findSmallestSetOfVertices(intn,List<List<Integer>>edges) { |
| 16 | +List<Integer>starts =newArrayList<>(); |
| 17 | +Map<Integer,Set<Integer>>indegree =newHashMap<>(); |
| 18 | +for (inti =0;i <edges.size();i++) { |
| 19 | +intend =edges.get(i).get(1); |
| 20 | +if (!indegree.containsKey(end)) { |
| 21 | +indegree.put(end,newHashSet<>()); |
| 22 | + } |
| 23 | +indegree.get(end).add(edges.get(i).get(0)); |
| 24 | + } |
| 25 | +for (inti =0;i <n;i++) { |
| 26 | +if (!indegree.containsKey(i)) { |
| 27 | +starts.add(i); |
| 28 | + } |
| 29 | + } |
| 30 | +returnstarts; |
| 31 | + } |
| 32 | + |
| 33 | +publicintminOperations(int[]nums) { |
| 34 | +Arrays.sort(nums); |
| 35 | +intops =0; |
| 36 | +while (!allZero(nums)) { |
| 37 | +if (allEvenAndNonZeroes(nums)) { |
| 38 | +nums =half(nums); |
| 39 | +ops++; |
| 40 | + }elseif (hasOdds(nums)) { |
| 41 | +int[]result =newint[nums.length]; |
| 42 | +for (inti =0;i <nums.length;i++) { |
| 43 | +if (nums[i] %2 !=0) { |
| 44 | +result[i] =nums[i] -1; |
| 45 | +ops++; |
| 46 | + }else { |
| 47 | +result[i] =nums[i]; |
| 48 | + } |
| 49 | + } |
| 50 | +nums =result; |
| 51 | + }else { |
| 52 | +int[]result =newint[nums.length]; |
| 53 | +for (inti =0;i <nums.length;i++) { |
| 54 | +if (nums[i] !=0) { |
| 55 | +result[i] =nums[i] /2; |
| 56 | + }else { |
| 57 | +result[i] =nums[i]; |
| 58 | + } |
| 59 | + } |
| 60 | +nums =result; |
| 61 | +ops++; |
| 62 | + } |
| 63 | + } |
| 64 | +returnops; |
| 65 | + } |
| 66 | + |
| 67 | +privatebooleanhasOdds(int[]nums) { |
| 68 | +for (inti :nums) { |
| 69 | +if (i %2 !=0) { |
| 70 | +returntrue; |
| 71 | + } |
| 72 | + } |
| 73 | +returnfalse; |
| 74 | + } |
| 75 | + |
| 76 | +privateint[]half(int[]nums) { |
| 77 | +int[]result =newint[nums.length]; |
| 78 | +for (inti =0;i <nums.length;i++) { |
| 79 | +result[i] =nums[i] /2; |
| 80 | + } |
| 81 | +returnresult; |
| 82 | + } |
| 83 | + |
| 84 | +privatebooleanallEvenAndNonZeroes(int[]nums) { |
| 85 | +for (inti :nums) { |
| 86 | +if (i %2 !=0 ||i ==0) { |
| 87 | +returnfalse; |
| 88 | + } |
| 89 | + } |
| 90 | +returntrue; |
| 91 | + } |
| 92 | + |
| 93 | +privatebooleanallZero(int[]nums) { |
| 94 | +for (inti :nums) { |
| 95 | +if (i !=0) { |
| 96 | +returnfalse; |
| 97 | + } |
| 98 | + } |
| 99 | +returntrue; |
| 100 | + } |
| 101 | + |
| 102 | +publicbooleancontainsCycle(char[][]grid) { |
| 103 | +intm =grid.length; |
| 104 | +intn =grid[0].length; |
| 105 | +for (inti =0;i <m;i++) { |
| 106 | +for (intj =0;j <n;j++) { |
| 107 | +boolean[][]visited =newboolean[m][n]; |
| 108 | +visited[i][j] =true; |
| 109 | +if (dfs(i,j,grid,grid[i][j],visited,i,j)) { |
| 110 | +returntrue; |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | +returnfalse; |
| 115 | + } |
| 116 | + |
| 117 | +int[]directions =newint[]{0,1,0, -1,0}; |
| 118 | +privatebooleandfs(inti,intj,char[][]grid,charc,boolean[][]visited,intstartI,intstartJ) { |
| 119 | +for (introw =0;row <directions.length -1;row++) { |
| 120 | +intnextI =i +directions[row]; |
| 121 | +intnextJ =j +directions[row +1]; |
| 122 | +if (nextI >=0 &&nextI <grid.length &&nextJ >=0 &&nextJ <grid[0].length &&grid[nextI][nextJ] ==c) { |
| 123 | +if (nextI ==startI &&nextJ ==startJ) { |
| 124 | +returntrue; |
| 125 | + }elseif (visited[nextI][nextJ]) { |
| 126 | +continue; |
| 127 | + } |
| 128 | +visited[nextI][nextJ] =true; |
| 129 | +dfs(nextI,nextJ,grid,c,visited,startI,startJ); |
| 130 | +visited[nextI][nextJ] =false; |
| 131 | + } |
| 132 | + } |
| 133 | +returnfalse; |
| 134 | + } |
| 135 | + |
| 136 | +publicstaticvoidmain(String...args) { |
| 137 | +System.out.println("hello world!"); |
| 138 | +Contestcontest =newContest(); |
| 139 | +// System.out.println(contest.thousandSeparator(1234)); |
| 140 | +// System.out.println(contest.thousandSeparator(0)); |
| 141 | +// System.out.println(contest.thousandSeparator(123456789)); |
| 142 | +// System.out.println(contest.thousandSeparator(987)); |
| 143 | + |
| 144 | +// System.out.println(contest.findSmallestSetOfVertices(6, Arrays.asList(Arrays.asList(0, 1), Arrays.asList(0, 2), Arrays.asList(2, 5), Arrays.asList(3, 4), Arrays.asList(4, 2)))); |
| 145 | +// System.out.println(contest.findSmallestSetOfVertices(5, Arrays.asList(Arrays.asList(0, 1), Arrays.asList(2, 1), Arrays.asList(3, 1), Arrays.asList(1, 4), Arrays.asList(2, 4)))); |
| 146 | + |
| 147 | +// System.out.println(contest.minOperations(new int[]{1, 5}));//5 |
| 148 | +// System.out.println(contest.minOperations(new int[]{2, 2}));//3 |
| 149 | +// System.out.println(contest.minOperations(new int[]{4, 2, 5}));//6 |
| 150 | +// System.out.println(contest.minOperations(new int[]{3, 2, 2, 4}));//7 |
| 151 | +// System.out.println(contest.minOperations(new int[]{2, 4, 8, 16}));//8 |
| 152 | + |
| 153 | +System.out.println(contest.containsCycle(newchar[][]{{'a','a','a','a'}, {'a','b','b','a'}, {'a','b','b','a'}, {'a','a','a','a'}})); |
| 154 | +System.out.println("finished."); |
| 155 | + } |
| 156 | +} |