贪心
n皇后
This commit is contained in:
whai 2024-03-01 13:07:15 +08:00
parent c6bdb975ec
commit f14703c22a
12 changed files with 477 additions and 10 deletions

View File

@ -11,11 +11,26 @@ import org.junit.Test;
public class LeetCode134 {
@Test
public void test() {
System.out.println(new Solution().canCompleteCircuit(
new int[]{5,1,2,3,4},
new int[]{4,4,1,5,1}
));
public void test() throws InterruptedException {
System.out.println("初始化内存 -Xms");
System.out.println(Runtime.getRuntime().totalMemory() / 1024 / 1024 + "m");
System.out.println("最大可使用内存 ");
System.out.println(Runtime.getRuntime().freeMemory() / 1024 / 1024 + "m");
System.out.println("最大堆内存:-Xmx");
System.out.println(Runtime.getRuntime().maxMemory() / 1024 / 1024 + "m");
// -XX+PrintGCDetails
int[] ints = new int[10000];
// Thread.sleep(1000000);
// System.out.println(new Solution().canCompleteCircuit(
// new int[]{5,1,2,3,4},
// new int[]{4,4,1,5,1}
// ));
}

View File

@ -0,0 +1,66 @@
package cn.whaifree.leetCode.Greedy;
import org.junit.Test;
import java.util.*;
import java.util.function.IntFunction;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/2/29 11:52
* @注释
*/
public class LeetCode406 {
@Test
public void test() throws InterruptedException {
int[][] ints = new int[][]{
{7,0},{4,4},{7,1},{5,0},{6,1},{5,2}
};
String gc = new String("gc");
List<String> list = new LinkedList<>();
int i = 0;
while (true) {
list.add(gc + gc + i++);
}
// for (int[] i : new Solution().reconstructQueue(ints)) {
// System.out.println(Arrays.toString(i));
// }
}
class Solution {
public int[][] reconstructQueue(int[][] people) {
// {7,0},{4,4},{7,1},{5,0},{6,1},{5,2}
Arrays.sort(people, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
if (o1[0] == o2[0]) {
// 身高相同时,前面数量少的在前
return o1[1] - o2[1];
}else {
return o2[0] - o1[0];
}
}
});
for (int[] person : people) {
System.out.println(Arrays.toString(person));
}
System.out.println("====");
// 排序 [[7,0], [7,1], [6,1], [5,0], [5,2][4,4]]
List<Object> list = new LinkedList<>();
// 节点i的前面必然都比i高那么只要把i插入到i[1]对应的位置必然满足条件
for (int[] p : people) {
list.add(p[1],p);
}
return list.toArray(new int[people.length][]);
}
}
}

View File

@ -16,13 +16,14 @@ public class LeetCode860 {
@Test
public void test() {
System.out.println(new Solution().lemonadeChange(new int[]{5,5,10,10,20}));
System.out.println(new Solution().lemonadeChange(new int[]{5, 5, 10, 10, 20}));
}
class Solution {
/**
* bills[i] 不是 5 就是 10 或是 20
*
* @param bills
* @return
*/
@ -55,4 +56,6 @@ public class LeetCode860 {
}
}

View File

@ -1,5 +1,8 @@
package cn.whaifree.leetCode;
import java.util.ArrayList;
import java.util.Random;
/**
* @version 1.0
* @Author whai文海
@ -100,3 +103,28 @@ class VirtualMethodTest {
fa.show("a");
}
}
class Picture{
int number = 0;
public Picture() {
}
public Picture(int number) {
this.number = number;
}
public static void main(String[]args){
ArrayList<Picture> list = new ArrayList<>();
while(true){
try {
Thread.sleep(10);
} catch (InterruptedException e){
e.printStackTrace();
}
list.add(new Picture(new Random().nextInt(1024*1024)));
}
}
}

View File

@ -2,10 +2,7 @@ package cn.whaifree.leetCode;
import cn.whaifree.leetCode.Tree.LeetCode94;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
@ -38,6 +35,11 @@ public class Test {
private int getNumber;
public int minus() {
localVarl();
int i = 1;

View File

@ -0,0 +1,31 @@
package cn.whaifree.redo.redo_24_3_1;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/3/1 11:16
* @注释
*/
public class LeetCode122 {
@Test
public void test() {
System.out.println(new Solution().maxProfit(new int[]{7, 1, 5, 3, 6, 4}));
}
class Solution {
public int maxProfit(int[] prices) {
int profit = 0;
for (int i = 1; i < prices.length; i++) {
int sub = prices[i] - prices[i - 1];
if (sub > 0) {
profit += sub;
}
}
return profit;
}
}
}

View File

@ -0,0 +1,56 @@
package cn.whaifree.redo.redo_24_3_1;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/3/1 12:07
* @注释
*/
public class LeetCode134 {
@Test
public void test() {
System.out.println(new Solution().canCompleteCircuit(new int[]{1, 2, 3, 4, 5}, new int[]{3, 4, 5, 1, 2}));
System.out.println(new Solution().canCompleteCircuit(new int[]{5,1,2,3,4}, new int[]{4,4,1,5,1}));
}
class Solution {
/**
* 1. 判断能否循环
* 2. 如果一定可以循环找到第一个能剩下油量的
* @param gas
* @param cost
* @return
*/
public int canCompleteCircuit(int[] gas, int[] cost) {
// 计算差值
int length = gas.length;
int[] rent = new int[length];
int rentGas = 0;
for (int i = 0; i < length; i++) {
rent[i] = gas[i] - cost[i];
rentGas += rent[i];
}
if (rentGas<0){
return -1;
}
int have = 0;
int resIndex = 0;
for (int i = 0; i < length; i++) {
have += rent[i];
if (have < 0) {
// 油量不够,从下一个点开始
resIndex = (i + 1) % length;
have = 0;
}
}
// [a,b] a内有不够用的时候b内也有可能有不够用的时候用index标记
return resIndex;
}
}
}

View File

@ -0,0 +1,50 @@
package cn.whaifree.redo.redo_24_3_1;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/3/1 10:37
* @注释
*/
public class LeetCode376 {
@Test
public void test() {
System.out.println(new Solution().wiggleMaxLength(new int[]{1, 7, 4, 9, 2, 5}));
}
class Solution {
/**
* 摆动序列
*
* @param nums
* @return
*/
public int wiggleMaxLength(int[] nums) {
if (nums.length == 1) {
return 1;
}
// 计算两数差值
int curSub = 0;
int preSub = 0;
int res = 1;
for (int i = 1; i < nums.length; i++) {
curSub = nums[i] - nums[i - 1];
// preSub = 0 主要让第一次进入循环
if ((curSub < 0 && preSub >= 0) || (curSub > 0 && preSub <= 0)) {
res++;
preSub = curSub;
}
}
return res;
}
}
}

View File

@ -0,0 +1,51 @@
package cn.whaifree.redo.redo_24_3_1;
import org.junit.Test;
import javax.annotation.PreDestroy;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/3/1 11:36
* @注释
*/
public class LeetCode45 {
@Test
public void test() {
System.out.println(new Solution().jump(new int[]{2,3,1,1,4}));
}
class Solution {
/**
* 每个index都有一个覆盖区间每个覆盖区间之间只jump一次
* @param nums
* @return
*/
public int jump(int[] nums) {
if (nums.length == 1) {
return 0;
}
int maxCover = 0; // 最大覆盖区间
int curCover = 0; // 当前覆盖区间
int jumpCount = 0; // 跳跃数
for (int i = 0; i < nums.length; i++) {
maxCover = Math.max(maxCover, i + nums[i]);
// 每次jump count++
if (maxCover >= nums.length - 1) {
jumpCount++;
break;
}
if (i == curCover) {
jumpCount++;
curCover = maxCover; // 到了最后一个[a,b] a已经执行完了下次就是b区间中jump一次
}
}
return jumpCount;
}
}
}

View File

@ -0,0 +1,92 @@
package cn.whaifree.redo.redo_24_3_1;
import cn.whaifree.leetCode.T;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/3/1 12:24
* @注释
*/
public class LeetCode51 {
@Test
public void test() {
new Solution().solveNQueens(4);
}
class Solution {
char[][] map = null;
List<List<String>> res = new ArrayList<>();
/**
* 判断是否符合条件
* @param n
* @return
*/
public List<List<String>> solveNQueens(int n) {
map = new char[n][n];
for (char[] chars : map) {
Arrays.fill(chars, '.');
}
backTracking(n, 0);
return res;
}
public void backTracking(int n,int row) {
if (n == row) {
List<String> e = new ArrayList<>();
for (char[] chars : map) {
StringBuilder s = new StringBuilder();
for (char aChar : chars) {
s.append(aChar);
}
e.add(s.toString());
}
res.add(e);
return;
}
for (int i = 0; i < n; i++) {
if (isValid(i, row, n)) {
map[row][i] = 'Q';
backTracking(n, row + 1);
map[row][i] = '.';
}
}
}
boolean isValid(int col, int row,int n) {
// 往上查找
for (int i = row; i >= 0; i--) {
if (map[i][col] == 'Q') {
return false;
}
}
// 左上45 这里不检查本节点可以提升很大的效率
for (int i = col-1, j = row-1; i >= 0 && j >= 0; i--, j--) {
if (map[j][i] == 'Q') {
return false;
}
}
// 右上45
for (int i = col + 1, j = row - 1; i < n && j >= 0; i++, j--) {
if (map[j][i] == 'Q') {
return false;
}
}
return true;
}
}
}

View File

@ -0,0 +1,39 @@
package cn.whaifree.redo.redo_24_3_1;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/3/1 11:01
* @注释
*/
public class LeetCode53 {
@Test
public void test(
) {
System.out.println(new Solution().maxSubArray(new int[]{-2,-1}));
}
class Solution {
/**
* 一旦加的那个区间为负只会拖累后面的串从下一个重加
*/
public int maxSubArray(int[] nums) {
if (nums.length == 1) {
return nums[0];
}
int maxSum = Integer.MIN_VALUE;
int curSum = 0;
for (int i = 0; i < nums.length; i++) {
curSum += nums[i];
maxSum = Math.max(curSum, maxSum);
if (curSum < 0) {
curSum = 0;
}
}
return maxSum;
}
}
}

View File

@ -0,0 +1,34 @@
package cn.whaifree.redo.redo_24_3_1;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/3/1 11:21
* @注释
*/
public class LeetCode55 {
@Test
public void test() {
System.out.println(new Solution().canJump(new int[]{2,3,1,1,4}));
}
class Solution {
public boolean canJump(int[] nums) {
if (nums.length == 1) {
return true;
}
int maxCover = 0;
for (int i = 0; i <= maxCover; i++) {
maxCover = Math.max(maxCover, i + nums[i]);
if (maxCover >= nums.length - 1) {
return true;
}
}
return false;
}
}
}